Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Filename:sipController.js
- Date:5 Aug 2015
- Auther:Murugan Pandian
- Copyright:IPCOM
- */
- var ipcom = angular.module('ipcom', ['ngMaterial']);
- ipcom.directive('schrollBottom', function() {
- return {
- scope: {
- schrollBottom: "="
- },
- link: function(scope, element) {
- scope.$watchCollection('schrollBottom', function(newValue) {
- if (newValue) {
- $(element).scrollTop($(element)[0].scrollHeight);
- }
- });
- }
- }
- });
- ipcom.controller('sipController', function($scope, $mdToast, $rootScope,$timeout) {
- /*
- Register to proxy server
- {uri}:proxy server uri
- {wsServers}:websocket server url
- {authorizationUser}:sip username
- {password}: sip password
- */
- var session;
- var callsession;
- var currentcall = [];
- //Load Audio file
- var ring = new Howl({
- urls: ['tone/ring.mp3'],
- loop: true,
- volume: 0.5
- });
- var dialer_ring = new Howl({
- urls: ['tone/ring.wav'],
- loop: true,
- volume: 0.5
- });
- var hangup = new Howl({
- urls: ['tone/hangup.wav'],
- loop: false,
- volume: 0.5
- });
- var config = {
- userAgentString: 'SIP.js/0.7.0 BB',
- traceSip: true,
- register: false
- };
- $scope.reset = function() {
- $scope.user = {};
- }
- //Assign localStorage value
- try {
- $scope.user = {
- uri: localStorage.url,
- wsserver: localStorage.wsserver,
- userid: localStorage.userid,
- name: localStorage.name
- };
- } catch (e) {
- console.log(e);
- }
- //Disable
- $scope.textboxenable=true;
- //register action
- $scope.register = function() {
- //store register details in localStorage
- localStorage.url = $scope.user.uri
- localStorage.wsserver = $scope.user.wsserver
- localStorage.userid = $scope.user.userid
- localStorage.name = $scope.user.name
- uri = $scope.agent + '';
- //send register request
- session = new SIP.UA({
- userAgentString: 'SIP.js/0.7.0 BB',
- uri: $scope.user.uri,
- wsServers: [$scope.user.wsserver],
- stunServers: ['stun:stun.l.google.com:19302'],
- turnServers: [{
- urls: 'turn:turn.bistri.com:80',
- username: 'homeo',
- password: 'homeo'
- }],
- authorizationUser: $scope.user.userid,
- password: $scope.user.password,
- displayName: $scope.user.name,
- traceSip: true,
- register: false
- });
- //Registraion failed callback
- session.on('registrationFailed', function(data) {
- $scope.$apply(function() {
- $scope.registerstatus = data.status_code + ' ' + data.reason_phrase;
- });
- });
- //websocket disconnected callback
- session.on('disconnected', function(data) {
- console.log(data);
- });
- //websocket disconnected callback
- session.on('connecting', function(data) {
- $scope.$apply(function() {
- $scope.registerstatus = 'Connecting...';
- });
- });
- //websocket connected callback
- session.on('connected', function() {
- $scope.$apply(function() {
- $scope.registerstatus = 'Connecting...';
- });
- if(!session.isRegistered())
- {
- session.register();
- }
- })
- //sip unregister callback
- session.on('unregistered', function(cause) {
- if(cause)
- {
- $scope.registerstatus = 'Unregistered';
- $scope.textboxenable=true;
- $scope.$apply(function() {
- $scope.homediv = false;
- });
- }
- });
- //sip registered successfully
- session.on('registered', function() {
- $timeout.cancel($scope.registertimer)
- $scope.registerstatus = 'Registered Successfully';
- $scope.textboxenable=false;
- $scope.$apply(function() {
- $scope.homediv = true;
- });
- });
- //receive message callback
- session.on('message', function(data) {
- console.log(data);
- $scope.$apply(function() {});
- });
- //receive invite
- session.on('invite', function(incomesession) {
- $mdToast.show({
- controller: 'sipController',
- templateUrl: 'app/views/incoming.html',
- hideDelay: 6000000000,
- position: 'top right'
- });
- ring.play();
- //bye event
- incomesession.on('bye', function(data) {
- window.curcall = null
- //pause audio
- ring.pause();
- $mdToast.hide();
- $scope.$apply(function() {
- $scope.callbtn = false;
- });
- //hangup
- hangup.play();
- });
- //call failure message from proxy
- incomesession.on('failed', function(data) {
- window.curcall = null
- //pause audio
- console.log(data);
- $mdToast.hide();
- //hangup
- hangup.play();
- $scope.$apply(function() {
- $scope.callbtn = false;
- });
- });
- //Receject Call
- incomesession.on('rejected', function(incoming, cause) {
- //hangup
- hangup.play();
- window.curcall = null;
- //pause audio
- ring.pause();
- $mdToast.hide();
- });
- //Accept Call
- incomesession.on('accepted', function(incoming, cause) {
- //pause audio
- ring.pause();
- $mdToast.hide();
- $scope.$apply(function() {
- $scope.callbtn = true;
- });
- });
- //store to global value
- window.incom = incomesession;
- window.curcall = incomesession;
- });
- //Receject Call
- session.on('rejected', function(incoming, cause) {
- ring.pause();
- });
- }
- //make call
- $scope.dial = function() {
- //SIPJS Option to make call
- var options = {
- media: {
- constraints: {
- audio: true,
- video: $scope.enablevideo
- },
- render: {
- remote: document.getElementById('remotevideo'),
- local: document.getElementById('sourcevideo')
- }
- }
- };
- if (session.isRegistered()) {
- //makes the call
- window.callsession = session.invite('sip:' + $scope.dialuri, options);
- $scope.callbtn = true;
- //call failure message from proxy
- window.callsession.on('failed', function(data) {
- //hangup
- hangup.play();
- window.callsession = null;
- if (!$scope.$$phase) {
- $scope.$apply(function() {
- $scope.callbtn = false;
- });
- } else {
- $scope.callbtn = false;
- }
- dialer_ring.pause();
- });
- //bye event
- window.callsession.on('bye', function(data) {
- //hangup
- hangup.play();
- window.callsession = null;
- if (!$scope.$$phase) {
- $scope.$apply(function() {
- $scope.callbtn = false;
- });
- } else {
- $scope.callbtn = false;
- }
- });
- //Receject Call
- window.callsession.on('rejected', function(incoming, cause) {
- dialer_ring.pause();
- });
- //Accepted Call
- window.callsession.on('accepted', function(incoming, cause) {
- dialer_ring.pause();
- });
- dialer_ring.play();
- } else {
- $scope.homediv = false;
- }
- }
- //Disconnect call
- $scope.disconnect = function() {
- if (window.callsession) {
- window.callsession.terminate();
- window.callsession = null;
- } else if (window.curcall) {
- window.curcall.terminate();
- window.curcall = null;
- }
- $scope.callbtn = false;
- }
- //Send DTMF
- $scope.dtmf = function(id) {
- if (window.callsession) {
- window.callsession.dtmf(id);
- } else if (window.curcall) {
- window.curcall.dtmf(id);
- }
- }
- //Toast for incoming call
- //Position for Toasting
- $scope.toastPosition = {
- bottom: false,
- top: true,
- left: false,
- right: true
- };
- //accept incomming call
- $rootScope.accept = function() {
- $scope.callbtn = true;
- $mdToast.hide();
- var options = {
- media: {
- constraints: {
- audio: true,
- video:$scope.videoenable
- },
- render: {
- remote: document.getElementById('remotevideo'),
- local: document.getElementById('sourcevideo')
- }
- }
- };
- window.incom.accept(options);
- }
- //reject incomming call
- $scope.reject = function() {
- window.incom.reject();
- $mdToast.hide();
- }
- //Unregister
- $scope.unregister = function() {
- session.stop();
- }
- $scope.messages = [];
- //Send message
- $scope.sendMessage = function(form) {
- $scope.messages.push({
- message: $scope.newMessageText,
- username:$scope.to
- });
- //send message to other user
- if (session) {
- if (session.isRegistered()) {
- session.message($scope.to, {
- message: $scope.newMessageText,
- username: localStorage.userid
- })
- }
- }
- $scope.newMessageText = '';
- form.$setUntouched();
- };
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement