Advertisement
Guest User

Untitled

a guest
Jan 20th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.76 KB | None | 0 0
  1. /*
  2. Filename:sipController.js
  3. Date:5 Aug 2015
  4. Auther:Murugan Pandian
  5. Copyright:IPCOM
  6. */
  7.  
  8. var ipcom = angular.module('ipcom', ['ngMaterial']);
  9.  
  10. ipcom.directive('schrollBottom', function() {
  11. return {
  12. scope: {
  13. schrollBottom: "="
  14. },
  15. link: function(scope, element) {
  16. scope.$watchCollection('schrollBottom', function(newValue) {
  17. if (newValue) {
  18. $(element).scrollTop($(element)[0].scrollHeight);
  19. }
  20. });
  21. }
  22. }
  23. });
  24.  
  25.  
  26. ipcom.controller('sipController', function($scope, $mdToast, $rootScope,$timeout) {
  27.  
  28. /*
  29. Register to proxy server
  30. {uri}:proxy server uri
  31. {wsServers}:websocket server url
  32. {authorizationUser}:sip username
  33. {password}: sip password
  34. */
  35.  
  36. var session;
  37. var callsession;
  38. var currentcall = [];
  39.  
  40.  
  41. //Load Audio file
  42. var ring = new Howl({
  43. urls: ['tone/ring.mp3'],
  44. loop: true,
  45. volume: 0.5
  46. });
  47.  
  48. var dialer_ring = new Howl({
  49. urls: ['tone/ring.wav'],
  50. loop: true,
  51. volume: 0.5
  52. });
  53.  
  54. var hangup = new Howl({
  55. urls: ['tone/hangup.wav'],
  56. loop: false,
  57. volume: 0.5
  58. });
  59.  
  60. var config = {
  61. userAgentString: 'SIP.js/0.7.0 BB',
  62. traceSip: true,
  63. register: false
  64. };
  65.  
  66.  
  67.  
  68. $scope.reset = function() {
  69. $scope.user = {};
  70.  
  71.  
  72. }
  73.  
  74. //Assign localStorage value
  75. try {
  76. $scope.user = {
  77. uri: localStorage.url,
  78. wsserver: localStorage.wsserver,
  79. userid: localStorage.userid,
  80. name: localStorage.name
  81. };
  82. } catch (e) {
  83. console.log(e);
  84. }
  85.  
  86. //Disable
  87. $scope.textboxenable=true;
  88.  
  89. //register action
  90. $scope.register = function() {
  91.  
  92. //store register details in localStorage
  93. localStorage.url = $scope.user.uri
  94. localStorage.wsserver = $scope.user.wsserver
  95. localStorage.userid = $scope.user.userid
  96. localStorage.name = $scope.user.name
  97.  
  98. uri = $scope.agent + '';
  99. //send register request
  100. session = new SIP.UA({
  101. userAgentString: 'SIP.js/0.7.0 BB',
  102. uri: $scope.user.uri,
  103. wsServers: [$scope.user.wsserver],
  104. stunServers: ['stun:stun.l.google.com:19302'],
  105. turnServers: [{
  106. urls: 'turn:turn.bistri.com:80',
  107. username: 'homeo',
  108. password: 'homeo'
  109. }],
  110. authorizationUser: $scope.user.userid,
  111. password: $scope.user.password,
  112. displayName: $scope.user.name,
  113. traceSip: true,
  114. register: false
  115. });
  116.  
  117.  
  118.  
  119. //Registraion failed callback
  120. session.on('registrationFailed', function(data) {
  121.  
  122. $scope.$apply(function() {
  123. $scope.registerstatus = data.status_code + ' ' + data.reason_phrase;
  124. });
  125.  
  126. });
  127.  
  128. //websocket disconnected callback
  129. session.on('disconnected', function(data) {
  130.  
  131. console.log(data);
  132. });
  133.  
  134. //websocket disconnected callback
  135. session.on('connecting', function(data) {
  136. $scope.$apply(function() {
  137. $scope.registerstatus = 'Connecting...';
  138. });
  139.  
  140. });
  141.  
  142. //websocket connected callback
  143. session.on('connected', function() {
  144. $scope.$apply(function() {
  145.  
  146. $scope.registerstatus = 'Connecting...';
  147. });
  148. if(!session.isRegistered())
  149. {
  150. session.register();
  151. }
  152.  
  153. })
  154.  
  155. //sip unregister callback
  156. session.on('unregistered', function(cause) {
  157. if(cause)
  158. {
  159. $scope.registerstatus = 'Unregistered';
  160. $scope.textboxenable=true;
  161. $scope.$apply(function() {
  162. $scope.homediv = false;
  163. });
  164. }
  165.  
  166. });
  167.  
  168. //sip registered successfully
  169. session.on('registered', function() {
  170. $timeout.cancel($scope.registertimer)
  171. $scope.registerstatus = 'Registered Successfully';
  172. $scope.textboxenable=false;
  173. $scope.$apply(function() {
  174. $scope.homediv = true;
  175. });
  176. });
  177.  
  178. //receive message callback
  179. session.on('message', function(data) {
  180. console.log(data);
  181. $scope.$apply(function() {});
  182. });
  183.  
  184.  
  185. //receive invite
  186. session.on('invite', function(incomesession) {
  187. $mdToast.show({
  188. controller: 'sipController',
  189. templateUrl: 'app/views/incoming.html',
  190. hideDelay: 6000000000,
  191. position: 'top right'
  192. });
  193. ring.play();
  194. //bye event
  195. incomesession.on('bye', function(data) {
  196. window.curcall = null
  197. //pause audio
  198. ring.pause();
  199. $mdToast.hide();
  200. $scope.$apply(function() {
  201. $scope.callbtn = false;
  202. });
  203. //hangup
  204. hangup.play();
  205. });
  206.  
  207. //call failure message from proxy
  208. incomesession.on('failed', function(data) {
  209. window.curcall = null
  210. //pause audio
  211. console.log(data);
  212. $mdToast.hide();
  213. //hangup
  214. hangup.play();
  215. $scope.$apply(function() {
  216. $scope.callbtn = false;
  217. });
  218.  
  219. });
  220.  
  221. //Receject Call
  222. incomesession.on('rejected', function(incoming, cause) {
  223. //hangup
  224. hangup.play();
  225. window.curcall = null;
  226. //pause audio
  227. ring.pause();
  228. $mdToast.hide();
  229. });
  230.  
  231. //Accept Call
  232. incomesession.on('accepted', function(incoming, cause) {
  233. //pause audio
  234. ring.pause();
  235. $mdToast.hide();
  236. $scope.$apply(function() {
  237. $scope.callbtn = true;
  238. });
  239.  
  240. });
  241.  
  242. //store to global value
  243. window.incom = incomesession;
  244. window.curcall = incomesession;
  245.  
  246.  
  247. });
  248.  
  249.  
  250. //Receject Call
  251. session.on('rejected', function(incoming, cause) {
  252. ring.pause();
  253. });
  254.  
  255.  
  256. }
  257.  
  258.  
  259. //make call
  260. $scope.dial = function() {
  261.  
  262. //SIPJS Option to make call
  263. var options = {
  264. media: {
  265. constraints: {
  266. audio: true,
  267. video: $scope.enablevideo
  268. },
  269. render: {
  270. remote: document.getElementById('remotevideo'),
  271. local: document.getElementById('sourcevideo')
  272. }
  273. }
  274. };
  275.  
  276. if (session.isRegistered()) {
  277.  
  278. //makes the call
  279. window.callsession = session.invite('sip:' + $scope.dialuri, options);
  280. $scope.callbtn = true;
  281.  
  282.  
  283. //call failure message from proxy
  284. window.callsession.on('failed', function(data) {
  285. //hangup
  286. hangup.play();
  287. window.callsession = null;
  288. if (!$scope.$$phase) {
  289. $scope.$apply(function() {
  290. $scope.callbtn = false;
  291. });
  292. } else {
  293. $scope.callbtn = false;
  294. }
  295. dialer_ring.pause();
  296. });
  297.  
  298.  
  299. //bye event
  300. window.callsession.on('bye', function(data) {
  301. //hangup
  302. hangup.play();
  303. window.callsession = null;
  304. if (!$scope.$$phase) {
  305. $scope.$apply(function() {
  306. $scope.callbtn = false;
  307. });
  308. } else {
  309. $scope.callbtn = false;
  310. }
  311. });
  312.  
  313.  
  314.  
  315. //Receject Call
  316. window.callsession.on('rejected', function(incoming, cause) {
  317. dialer_ring.pause();
  318. });
  319.  
  320. //Accepted Call
  321. window.callsession.on('accepted', function(incoming, cause) {
  322. dialer_ring.pause();
  323.  
  324. });
  325.  
  326. dialer_ring.play();
  327.  
  328.  
  329. } else {
  330. $scope.homediv = false;
  331. }
  332.  
  333. }
  334.  
  335. //Disconnect call
  336. $scope.disconnect = function() {
  337. if (window.callsession) {
  338. window.callsession.terminate();
  339. window.callsession = null;
  340. } else if (window.curcall) {
  341. window.curcall.terminate();
  342. window.curcall = null;
  343. }
  344. $scope.callbtn = false;
  345. }
  346.  
  347.  
  348. //Send DTMF
  349. $scope.dtmf = function(id) {
  350. if (window.callsession) {
  351. window.callsession.dtmf(id);
  352. } else if (window.curcall) {
  353. window.curcall.dtmf(id);
  354. }
  355.  
  356. }
  357.  
  358.  
  359.  
  360. //Toast for incoming call
  361. //Position for Toasting
  362. $scope.toastPosition = {
  363. bottom: false,
  364. top: true,
  365. left: false,
  366. right: true
  367. };
  368.  
  369.  
  370.  
  371.  
  372. //accept incomming call
  373. $rootScope.accept = function() {
  374. $scope.callbtn = true;
  375. $mdToast.hide();
  376. var options = {
  377. media: {
  378. constraints: {
  379. audio: true,
  380. video:$scope.videoenable
  381. },
  382. render: {
  383. remote: document.getElementById('remotevideo'),
  384. local: document.getElementById('sourcevideo')
  385. }
  386. }
  387. };
  388. window.incom.accept(options);
  389. }
  390. //reject incomming call
  391. $scope.reject = function() {
  392. window.incom.reject();
  393. $mdToast.hide();
  394. }
  395.  
  396. //Unregister
  397. $scope.unregister = function() {
  398. session.stop();
  399. }
  400.  
  401.  
  402. $scope.messages = [];
  403. //Send message
  404. $scope.sendMessage = function(form) {
  405. $scope.messages.push({
  406. message: $scope.newMessageText,
  407. username:$scope.to
  408. });
  409. //send message to other user
  410. if (session) {
  411. if (session.isRegistered()) {
  412. session.message($scope.to, {
  413. message: $scope.newMessageText,
  414. username: localStorage.userid
  415. })
  416.  
  417. }
  418. }
  419. $scope.newMessageText = '';
  420. form.$setUntouched();
  421. };
  422.  
  423. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement