Advertisement
Guest User

Untitled

a guest
May 12th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.72 KB | None | 0 0
  1. $(function() {
  2. $("#logArea").html('');
  3. var ui = {
  4. $usersTitle: $('.j-users__title'),
  5. $usersList: $('.j-users__list'),
  6.  
  7. $panel: $('.j-pl'),
  8. $callees: $('.j-callees'),
  9.  
  10. $btnCall: $('.j-call'),
  11. $btnHangup: $('.j-hangup'),
  12.  
  13. $ctrlBtn: $('.j-caller__ctrl'),
  14. filterClassName: '.j-filter',
  15.  
  16. modal: {
  17. 'income_call': '#income_call'
  18. },
  19.  
  20. sounds: {
  21. 'call': 'callingSignal',
  22. 'end': 'endCallSignal',
  23. 'rington': 'ringtoneSignal'
  24. },
  25.  
  26. setPositionFooter: function() {
  27. var $footer = $('.j-footer'),
  28. invisibleClassName = 'invisible',
  29. footerFixedClassName = 'footer-fixed';
  30.  
  31. if( $(window).outerHeight() > $('.j-wrapper').outerHeight() ) {
  32. $footer.addClass(footerFixedClassName);
  33. }
  34. else {
  35. $footer.removeClass(footerFixedClassName);
  36. }
  37. $footer.removeClass(invisibleClassName);
  38. },
  39. togglePreloadMain: function(action) {
  40. var $main = $('.j-main'),
  41. preloadClassName = 'main-preload';
  42.  
  43. if(action === 'show') {
  44. $main.addClass( preloadClassName );
  45. }
  46. else {
  47. $main.removeClass( preloadClassName );
  48. }
  49. },
  50.  
  51. createUsers: function(users, $node) {
  52. var tpl = _.template( $('#user_tpl').html() ),
  53. usersHTML = '';
  54. $node.empty();
  55. _.each(users, function(user, i, list) {
  56. console.log(user);
  57. console.log(list);
  58. usersHTML += tpl(user);
  59. });
  60. $node.append(usersHTML);
  61. },
  62.  
  63. showCallBtn: function() {
  64. this.$btnHangup.addClass('hidden');
  65. this.$btnCall.removeClass('hidden');
  66. },
  67. hideCallBtn: function() {
  68. this.$btnHangup.removeClass('hidden');
  69. this.$btnCall.addClass('hidden');
  70. },
  71. toggleRemoteVideoView: function(userID, action) {
  72. var $video = $('#remote_video_' + userID);
  73.  
  74. if(!_.isEmpty(app.currentSession) && $video.length){
  75. if(action === 'show') {
  76. $video.parents('.j-callee').removeClass('callees__callee-wait');
  77. }
  78. else if(action === 'hide') {
  79. $video.parents('.j-callee').addClass('callees__callee-wait');
  80. }
  81. else if(action === 'clear') {
  82. app.currentSession.detachMediaStream('remote_video_' + userID);
  83. $video.removeClass('fw-video-wait');
  84. }
  85. }
  86. },
  87. classesNameFilter: 'no aden reyes perpetua inkwell toaster walden hudson gingham mayfair lofi xpro2 _1977 brooklyn',
  88. changeFilter: function(selector, filterName) {
  89. $(selector)
  90. .removeClass(this.classesNameFilter)
  91. .addClass( filterName );
  92. },
  93. callTime: 0,
  94. updTimer: function() {
  95. this.callTime += 1000;
  96. $('#timer').removeClass('hidden').text( new Date(this.callTime).toUTCString().split(/ /)[4] );
  97. }
  98. },
  99. app = {
  100. caller: {},
  101. callees: {},
  102. currentSession: {},
  103. mainVideo: 0
  104. },
  105. isDeviceAccess = true,
  106. takedCallCallee = [],
  107. remoteStreamCounter = 0,
  108. authorizationing = false,
  109. callTimer,
  110. network = {
  111. users: {}
  112. },
  113. is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  114.  
  115. window.app = app;
  116.  
  117.  
  118. function initializeUI(arg) {
  119. var params = arg || {};
  120.  
  121. ui.createUsers(QBUsers, ui.$usersList);
  122. ui.$usersTitle.text(MESSAGES.title_login);
  123.  
  124. if(!params.withoutUpdMsg || params.msg) {
  125. //qbApp.MsgBoard.update(params.msg);
  126. $("#logArea").append(params.msg+'n');
  127. }
  128. }
  129.  
  130. function showErrorAccessPermission(error) {
  131. var errorTitle = 'Error: ',
  132. errorMsg = 'Failed to get access to your camera or microphone. Please check your hardware configuration.';
  133.  
  134. if(error && error.message) {
  135. alert(errorTitle + error.message);
  136. }
  137. else {
  138. alert(errorTitle + errorMsg);
  139. }
  140. }
  141.  
  142. function isBytesReceivedChanges(userId, inboundrtp) {
  143. var res = true,
  144. inbBytesRec = inboundrtp.bytesReceived;
  145.  
  146. if(network[userId] === undefined) {
  147. network[userId] = {
  148. 'bytesReceived': inbBytesRec
  149. };
  150. }
  151. else {
  152. if(network[userId].bytesReceived === inbBytesRec) {
  153. res = false;
  154. }
  155. else {
  156. network[userId] = {
  157. 'bytesReceived': inbBytesRec
  158. };
  159. }
  160. }
  161. return res;
  162. }
  163.  
  164. function notifyIfUserLeaveCall(session, userId, reason, title) {
  165. var userInfo = _.findWhere(QBUsers, {id: +userId}),
  166. currentUserInfo = _.findWhere(QBUsers, {id: session.currentUserID});
  167.  
  168. if(session.opponentsIDs.length === 1) {
  169. $("#logArea").append('Call Update : n p2p_call_stopn name : '+userInfo.full_name+'n currentName:'+currentUserInfo.full_name+'n reason : '+reason+'n');
  170. console.log('Call Update : n p2p_call_stopn name : '+userInfo.full_name+'n currentName:'+currentUserInfo.full_name+'n reason : '+reason+'n');
  171. }
  172. $('.j-callee_status_' + userId).text(title);
  173. }
  174.  
  175. /**
  176. * INITIALIZE
  177. */
  178. ui.setPositionFooter();
  179. if (!QB.webrtc) {
  180.  
  181. $("logArera").append('webrtc_not_avaible n');
  182. $("logArera").append('Error: ' + window.MESSAGES.webrtc_not_avaible+'n');
  183. return;
  184. }
  185.  
  186. //*** Events ***
  187. $(document).on('click', '.btnVideoCal', function(e) {
  188. QBUsers = [];
  189. $('input[name="list_user"]:checked').each(function(){
  190. alert('checkbox checked'+$(this).val());
  191. QBUsers.push({
  192. "id" : $(this).val(),
  193. "email" : $(this).attr('data-email'),
  194. "password" : $(this).attr('data-password'),
  195. "fullname" : $(this).attr('data-fullname')
  196. });
  197. });
  198. console.log('QBUsers Build');
  199. console.log(QBUsers);
  200. var $el = $(this),
  201. usersWithoutCaller = [],
  202. user = {},
  203. classNameCheckedUser = 'users__user-active';
  204.  
  205. if(!window.navigator.onLine) {
  206. $("logArera").append('No internet n');
  207. }
  208. else
  209. {
  210. if(_.isEmpty(app.caller)) {
  211. authorizationing = true;
  212. ui.togglePreloadMain('show');
  213. app.caller = {
  214. id: +$.trim( $("#loggedUserInfo").attr('data-id') ),
  215. email: $.trim( $("#loggedUserInfo").attr('data-email') ),
  216. password: $.trim( $("#loggedUserInfo").attr('data-pass') ),
  217. full_name: $.trim( $("#loggedUserInfo").attr('data-name') )
  218. };
  219. console.log('app.caller Build');
  220. console.log(app.caller);
  221. usersWithoutCaller = _.filter(QBUsers, function(i) { return i.id !== app.caller.id; });
  222. console.log('Users with out caller ');
  223. console.log(usersWithoutCaller);
  224. ui.$usersList.empty();
  225. $("logArera").append('Connect n');
  226. QB.chat.connect({
  227. jid: QB.chat.helpers.getUserJid( app.caller.id, QBApp.appId ),
  228. password: app.caller.password
  229. }, function(err, res) {
  230.  
  231. if(err !== null) {
  232. $("#logArera").append('QB.chat.connect Error Part n');
  233. app.caller = {};
  234.  
  235. ui.setPositionFooter();
  236. ui.togglePreloadMain('hide');
  237. QB.chat.disconnect();
  238. }
  239. else {
  240. console.log('QB.chat.connect Else Part');
  241. ui.createUsers(usersWithoutCaller, ui.$usersList);
  242. ui.$usersTitle.text(MESSAGES.title_callee);
  243.  
  244. $("logArera").append('Set logout to message board n');
  245. ui.$panel.removeClass('hidden');
  246. ui.setPositionFooter();
  247. ui.togglePreloadMain('hide');
  248. }
  249.  
  250. authorizationing = false;
  251. });
  252. }
  253. $('input[name="list_user"]:checked').each(function(){
  254. user.id = +$.trim( $(this).val() );
  255. user.name = $.trim( $(this).attr('data-fullname') );
  256. //if want to remove user form call in demo it is checked with class.
  257. // Remove code will go here check above delete app.callees[user.id];
  258. $("#logArea").append('Adding Calleesn');
  259. app.callees[user.id] = user.name;
  260. });
  261. }
  262. return false;
  263. });
  264.  
  265. //## Logout
  266. $(document).on('click', '.j-logout', function() {
  267. QB.chat.disconnect();
  268. });
  269.  
  270. $(document).on('click', '.j-call', function(e) {
  271. $("#logArea").append('Start call Clicked n');
  272. var videoElems = '',
  273. mediaParams = {
  274. audio: true,
  275. video: true,
  276. options: {
  277. muted: true,
  278. mirror: true
  279. },
  280. elemId: 'localVideo'
  281. };
  282.  
  283. if(!window.navigator.onLine) {
  284. $("#logArea").append('No internet n');
  285. }
  286. else {
  287. if ( _.isEmpty(app.callees) ) {
  288. $("#logArea").append('Please select user to call n');
  289. }
  290. else {
  291. app.currentSession = QB.webrtc.createNewSession(Object.keys(app.callees), QB.webrtc.CallType.VIDEO);
  292. app.currentSession.getUserMedia(mediaParams, function(err, stream) {
  293. if (err || !stream.getAudioTracks().length || !stream.getVideoTracks().length) {
  294. var errorMsg = '';
  295.  
  296. if(err && err.message) {
  297. errorMsg += 'Error: ' + err.message;
  298. $("#logArea").append('Media Error : '+errorMsg+' n');
  299. }
  300. else {
  301. errorMsg += 'device_not_found';
  302. $("#logArea").append('Media Error : '+errorMsg+' n');
  303. }
  304. app.currentSession.stop({});
  305. showErrorAccessPermission(err);
  306.  
  307. }
  308. else {
  309. app.currentSession.call({}, function(error) {
  310. if(error) {
  311. console.warn(error.detail);
  312. $("#logArea").append('Call Error : '+error.detail+' n');
  313. }
  314. else {
  315. var compiled = _.template( $('#callee_video').html() );
  316. console.log('Calling......');
  317. $("#logArea").append('Calling....n');
  318. document.getElementById(ui.sounds.call).play();
  319.  
  320.  
  321. Object.keys(app.callees).forEach(function(userID, i, arr) {
  322. videoElems += compiled({userID: userID, name: app.callees[userID] });
  323. });
  324. $("#logArea").append('Append Callees Video Elementn');
  325. ui.$callees.append(videoElems);
  326.  
  327. ui.hideCallBtn();
  328. //ui.setPositionFooter();
  329. $("#logArea").append('Leaving start call clickn');
  330. }
  331. });
  332. }
  333. });
  334. }
  335. }
  336. });
  337.  
  338. // ## Hangup
  339. $(document).on('click', '.j-hangup', function() {
  340. if(!_.isEmpty(app.currentSession)) {
  341. app.currentSession.stop({});
  342. app.currentSession = {};
  343. }
  344. });
  345.  
  346. //## Accept
  347. $(document).on('click', '.j-accept', function() {
  348. var mediaParams = {
  349. audio: true,
  350. video: true,
  351. elemId: 'localVideo',
  352. options: {
  353. muted: true,
  354. mirror: true
  355. }
  356. },
  357. videoElems = '';
  358.  
  359. $(ui.modal.income_call).modal('hide');
  360. document.getElementById(ui.sounds.rington).pause();
  361.  
  362. app.currentSession.getUserMedia(mediaParams, function(err, stream) {
  363. if (err || !stream.getAudioTracks().length || !stream.getVideoTracks().length) {
  364. var errorMsg = '';
  365.  
  366. if(err && err.message) {
  367. errorMsg += 'Error: ' + err.message;
  368. }
  369. else {
  370. errorMsg += 'device_not_found';
  371. }
  372. showErrorAccessPermission(err);
  373. isDeviceAccess = false;
  374. app.currentSession.stop({});
  375. }
  376. else {
  377. var opponents = [app.currentSession.initiatorID],
  378. compiled = _.template( $('#callee_video').html() );
  379. ui.hideCallBtn();
  380.  
  381. app.currentSession.opponentsIDs.forEach( function(userID, i, arr) {
  382. if(userID != app.currentSession.currentUserID){
  383. opponents.push(userID);
  384. }
  385. });
  386. opponents.forEach(function(userID, i, arr) {
  387. var peerState = app.currentSession.connectionStateForUser(userID),
  388. userInfo = _.findWhere(QBUsers, {id: userID});
  389.  
  390. if( (document.getElementById('remote_video_' + userID) === null) ) {
  391. videoElems += compiled({userID: userID, name: userInfo.full_name});
  392.  
  393. if(peerState === QB.webrtc.PeerConnectionState.CLOSED){
  394. ui.toggleRemoteVideoView(userID, 'clear');
  395. }
  396. }
  397. });
  398.  
  399. ui.$callees.append(videoElems);
  400. ui.setPositionFooter();
  401. app.currentSession.accept({});
  402. }
  403. });
  404. });
  405.  
  406. //## Reject
  407. $(document).on('click', '.j-decline', function() {
  408. if (!_.isEmpty(app.currentSession)) {
  409. app.currentSession.reject({});
  410.  
  411. $(ui.modal.income_call).modal('hide');
  412. document.getElementById(ui.sounds.rington).pause();
  413. }
  414. });
  415. /** Mute / Unmute cam / mic */
  416. $(document).on('click', '.j-caller__ctrl', function() {
  417. var $btn = $(this),
  418. isActive = $btn.hasClass('active');
  419.  
  420. if( _.isEmpty( app.currentSession)) {
  421. return false;
  422. } else {
  423. if(isActive) {
  424. $btn.removeClass('active');
  425. app.currentSession.unmute( $btn.data('target') );
  426. } else {
  427. $btn.addClass('active');
  428. app.currentSession.mute( $btn.data('target') );
  429. }
  430. }
  431. });
  432. /** set main video */
  433. $(document).on('click', '.j-callees__callee_video', function() {
  434. var $that = $(this),
  435. userID = +($(this).data('user')),
  436. classesName = [],
  437. activeClass = [];
  438.  
  439. if( app.currentSession.peerConnections[userID].stream && !_.isEmpty( $that.attr('src')) ) {
  440. if( $that.hasClass('active') ) {
  441. $that.removeClass('active');
  442.  
  443. app.currentSession.detachMediaStream('main_video');
  444. ui.changeFilter('#main_video', 'no');
  445. app.mainVideo = 0;
  446. } else {
  447. $('.j-callees__callee_video').removeClass('active');
  448. $that.addClass('active');
  449.  
  450. ui.changeFilter('#main_video', 'no');
  451.  
  452. activeClass = _.intersection($that.attr('class').split(/s+/), ui.classesNameFilter.split(/s+/) );
  453.  
  454. if(activeClass.length) {
  455. ui.changeFilter('#main_video', activeClass[0]);
  456. }
  457. app.currentSession.attachMediaStream('main_video', app.currentSession.peerConnections[userID].stream);
  458. app.mainVideo = userID;
  459. }
  460. }
  461. });
  462.  
  463. /** Change filter for filter */
  464. $(document).on('change', ui.filterClassName, function() {
  465. var val = $.trim( $(this).val() );
  466.  
  467. ui.changeFilter('#localVideo', val);
  468.  
  469. if(!_.isEmpty( app.currentSession)) {
  470. app.currentSession.update({filter: val});
  471. }
  472. });
  473.  
  474. $(window).on('resize', function() {
  475. ui.setPositionFooter();
  476. });
  477.  
  478. /**
  479. * QB Event listener.
  480. */
  481.  
  482. QB.chat.onDisconnectedListener = function() {
  483. console.log('onDisconnectedListener.');
  484. var initUIParams = authorizationing ? {withoutUpdMsg: false, msg: 'no_internet'} : {withoutUpdMsg: false, msg: 'login'};
  485.  
  486. app.caller = {};
  487. app.callees = [];
  488. app.mainVideo = 0;
  489. remoteStreamCounter = 0;
  490.  
  491. ui.togglePreloadMain('hide');
  492. initializeUI(initUIParams);
  493. ui.$panel.addClass('hidden');
  494.  
  495. /** delete callee video elements */
  496. $('.j-callee').remove();
  497.  
  498. ui.setPositionFooter();
  499. authorizationing = false;
  500. };
  501.  
  502. QB.webrtc.onCallStatsReport = function onCallStatsReport(session, userId, stats) {
  503. $("#logArea").append('onCallStatsReport n');
  504. console.group('onCallStatsReport');
  505. console.log('userId: ' + userId);
  506. // console.log('Stats: ', stats);
  507. console.groupEnd();
  508.  
  509. /**
  510. * Hack for Firefox
  511. * (https://bugzilla.mozilla.org/show_bug.cgi?id=852665)
  512. */
  513. if(is_firefox) {
  514. var inboundrtp = _.findWhere(stats, {type: 'inboundrtp'});
  515.  
  516. if(!inboundrtp || !isBytesReceivedChanges(userId, inboundrtp)) {
  517. console.warn("This is Firefox and user " + userId + " has lost his connection.");
  518.  
  519. if(!_.isEmpty(app.currentSession)) {
  520. app.currentSession.closeConnection(userId);
  521.  
  522. notifyIfUserLeaveCall(session, userId, 'disconnected', 'Disconnected');
  523. }
  524. }
  525. }
  526. };
  527.  
  528. QB.webrtc.onSessionCloseListener = function onSessionCloseListener(session){
  529. console.log('onSessionCloseListener: ' + session);
  530.  
  531. /** pause play call sound */
  532. document.getElementById(ui.sounds.call).pause();
  533. document.getElementById(ui.sounds.end).play();
  534.  
  535. ui.showCallBtn();
  536.  
  537. if(!isDeviceAccess) {
  538. isDeviceAccess = true;
  539. } else {
  540. if(session.opponentsIDs.length > 1) {
  541. //qbApp.MsgBoard.update('call_stop', {name: app.caller.full_name});
  542. $("#logArea").append('call_stop : name : '+app.caller.full_name+' n ');
  543. }
  544. }
  545.  
  546. /** delete blob from myself video */
  547. document.getElementById('localVideo').src = '';
  548.  
  549. /** disable controls (mute cam/min) */
  550. ui.$ctrlBtn.removeClass('active');
  551.  
  552. /** delete callee video elements */
  553. $('.j-callee').remove();
  554. /** clear main video */
  555. app.currentSession.detachMediaStream('main_video');
  556. app.mainVideo = 0;
  557. remoteStreamCounter = 0;
  558. };
  559.  
  560. QB.webrtc.onUserNotAnswerListener = function onUserNotAnswerListener(session, userId) {
  561. console.group('onUserNotAnswerListener.');
  562. console.log('UserId: ' + userId);
  563. console.log('Session: ' + session);
  564. console.groupEnd();
  565.  
  566. var userInfo = _.findWhere(QBUsers, {id: +userId}),
  567. currentUserInfo = _.findWhere(QBUsers, {id: app.currentSession.currentUserID});
  568.  
  569. /** It's for p2p call */
  570. if(session.opponentsIDs.length === 1) {
  571. /*qbApp.MsgBoard.update(
  572. 'p2p_call_stop',
  573. {
  574. name: userInfo.full_name,
  575. currentName: currentUserInfo.full_name,
  576. reason: 'not answered'
  577. }
  578. );*/
  579. //$("#logArea").append('onUserNotAnswerListener : p2p_call_stop=> name : '+userInfo.full_name+'n currentName : '+currentUserInfo.full_name+'n reason : '+not answered+'n');
  580. }
  581.  
  582. $('.j-callee_status_' + userId).text('No Answer');
  583. };
  584.  
  585. QB.webrtc.onUpdateCallListener = function onUpdateCallListener(session, userId, extension) {
  586. console.group('onUpdateCallListener.');
  587. console.log('UserId: ' + userId);
  588. console.log('Session: ' + session);
  589. console.log('Extension: ' + JSON.stringify(extension));
  590. console.groupEnd();
  591.  
  592. ui.changeFilter('#remote_video_' + userId, extension.filter);
  593. if (+(app.mainVideo) === userId) {
  594. ui.changeFilter('#main_video', extension.filter);
  595. }
  596. };
  597.  
  598. QB.webrtc.onCallListener = function onCallListener(session, extension) {
  599. alert('Now in onCallListener');
  600. $("#logArea").append('Start : onCallListener n');
  601. console.group('onCallListener.');
  602. console.log('Session: ' + session);
  603. console.log('Extension: ' + JSON.stringify(extension));
  604. console.groupEnd();
  605.  
  606. /** close previous modal if his is exist */
  607. $(ui.modal.income_call).modal('hide');
  608.  
  609. var userInfo = _.findWhere(QBUsers, {id: session.initiatorID});
  610.  
  611. app.currentSession = session;
  612.  
  613. /** set name of caller */
  614. $('.j-ic_initiator').text( 'Call from caller' );//
  615. console.log('Open accept call window');
  616. $(ui.modal.income_call).modal('show');
  617. //document.getElementById(ui.sounds.rington).play();
  618. };
  619.  
  620. QB.webrtc.onAcceptCallListener = function onAcceptCallListener(session, userId, extension) {
  621. console.group('onAcceptCallListener.');
  622. console.log('UserId: ' + userId);
  623. console.log('Session: ' + session);
  624. console.log('Extension: ' + JSON.stringify(extension));
  625. console.groupEnd();
  626.  
  627. var userInfo = _.findWhere(QBUsers, {id: userId}),
  628. filterName = $.trim( $(ui.filterClassName).val() );
  629.  
  630. document.getElementById(ui.sounds.call).pause();
  631.  
  632. app.currentSession.update({filter: filterName});
  633.  
  634. /** update list of callee who take call */
  635. takedCallCallee.push(userInfo);
  636.  
  637. if(app.currentSession.currentUserID === app.currentSession.initiatorID) {
  638. //qbApp.MsgBoard.update('accept_call', {users: takedCallCallee});
  639. $("#logArea").append('onAcceptCallListener => accept_call : users : '+takedCallCallee+' n');
  640. }
  641. };
  642.  
  643. QB.webrtc.onRejectCallListener = function onRejectCallListener(session, userId, extension) {
  644. console.group('onRejectCallListener.');
  645. console.log('UserId: ' + userId);
  646. console.log('Session: ' + session);
  647. console.log('Extension: ' + JSON.stringify(extension));
  648. console.groupEnd();
  649.  
  650. var userInfo = _.findWhere(QBUsers, {id: userId}),
  651. currentUserInfo = _.findWhere(QBUsers, {id: app.currentSession.currentUserID});
  652.  
  653. /** It's for p2p call */
  654. if(session.opponentsIDs.length === 1) {
  655. qbApp.MsgBoard.update(
  656. 'p2p_call_stop',
  657. {
  658. name: userInfo.full_name,
  659. currentName: currentUserInfo.full_name,
  660. reason: 'rejected the call'
  661. }
  662. );
  663. }
  664.  
  665. /** It's for groups call */
  666. $('.j-callee_status_' + userId).text('Rejected');
  667. };
  668.  
  669. QB.webrtc.onStopCallListener = function onStopCallListener(session, userId, extension) {
  670. console.group('onStopCallListener.');
  671. console.log('UserId: ' + userId);
  672. console.log('Session: ' + session);
  673. console.log('Extension: ' + JSON.stringify(extension));
  674. console.groupEnd();
  675.  
  676. notifyIfUserLeaveCall(session, userId, 'hung up the call', 'Hung Up');
  677. };
  678.  
  679. QB.webrtc.onRemoteStreamListener = function onRemoteStreamListener(session, userID, stream) {
  680. console.group('onRemoteStreamListener.');
  681. console.log('userID: ' + userID);
  682. console.log('Session: ' + session);
  683. console.groupEnd();
  684. app.currentSession.peerConnections[userID].stream = stream;
  685.  
  686. app.currentSession.attachMediaStream('remote_video_' + userID, stream);
  687.  
  688. if( remoteStreamCounter === 0) {
  689. $('#remote_video_' + userID).click();
  690.  
  691. app.mainVideo = userID;
  692. ++remoteStreamCounter;
  693. }
  694.  
  695. if(!callTimer) {
  696. callTimer = setInterval( function(){ ui.updTimer.call(ui); }, 1000);
  697. }
  698. };
  699.  
  700. QB.webrtc.onSessionConnectionStateChangedListener = function onSessionConnectionStateChangedListener(session, userID, connectionState) {
  701. console.group('onSessionConnectionStateChangedListener.');
  702. console.log('UserID: ' + userID);
  703. console.log('Session: ' + session);
  704. console.log('Сonnection state: ' + connectionState);
  705. console.groupEnd();
  706.  
  707. var connectionStateName = _.invert(QB.webrtc.SessionConnectionState)[connectionState],
  708. $calleeStatus = $('.j-callee_status_' + userID),
  709. isCallEnded = false;
  710.  
  711. if(connectionState === QB.webrtc.SessionConnectionState.CONNECTING) {
  712. $calleeStatus.text(connectionStateName);
  713. }
  714.  
  715. if(connectionState === QB.webrtc.SessionConnectionState.CONNECTED) {
  716. ui.toggleRemoteVideoView(userID, 'show');
  717. $calleeStatus.text(connectionStateName);
  718. }
  719.  
  720. if(connectionState === QB.webrtc.SessionConnectionState.COMPLETED) {
  721. ui.toggleRemoteVideoView(userID, 'show');
  722. $calleeStatus.text('connected');
  723. }
  724.  
  725. if(connectionState === QB.webrtc.SessionConnectionState.DISCONNECTED){
  726. ui.toggleRemoteVideoView(userID, 'hide');
  727. $calleeStatus.text('disconnected');
  728. }
  729.  
  730. if(connectionState === QB.webrtc.SessionConnectionState.CLOSED){
  731. ui.toggleRemoteVideoView(userID, 'clear');
  732. document.getElementById(ui.sounds.rington).pause();
  733.  
  734. if(app.mainVideo === userID) {
  735. $('#remote_video_' + userID).removeClass('active');
  736.  
  737. ui.changeFilter('#main_video', 'no');
  738. app.currentSession.detachMediaStream('main_video');
  739. app.mainVideo = 0;
  740. }
  741.  
  742. if( !_.isEmpty(app.currentSession) ) {
  743. if ( Object.keys(app.currentSession.peerConnections).length === 1 || userID === app.currentSession.initiatorID) {
  744. $(ui.modal.income_call).modal('hide');
  745. }
  746. }
  747.  
  748. isCallEnded = _.every(app.currentSession.peerConnections, function(i) {
  749. return i.iceConnectionState === 'closed';
  750. });
  751.  
  752. /** remove filters */
  753. if( isCallEnded ) {
  754. ui.changeFilter('#localVideo', 'no');
  755. ui.changeFilter('#main_video', 'no');
  756. $(ui.filterClassName).val('no');
  757.  
  758. takedCallCallee = [];
  759. }
  760.  
  761. if (app.currentSession.currentUserID === app.currentSession.initiatorID && !isCallEnded) {
  762. /** get array if users without user who ends call */
  763. takedCallCallee = _.reject(takedCallCallee, function(num){ return num.id === +userID; });
  764.  
  765. //qbApp.MsgBoard.update('accept_call', {users: takedCallCallee});
  766. $("#logArea").append('onSessionConnectionStateChangedListener => accept_call users: '+takedCallCallee+'n ');
  767. }
  768.  
  769. if( _.isEmpty(app.currentSession) || isCallEnded ) {
  770. if(callTimer) {
  771. $('#timer').addClass('hidden');
  772. clearInterval(callTimer);
  773. callTimer = null;
  774. ui.callTime = 0;
  775.  
  776. network = {};
  777. }
  778. }
  779. }
  780. };
  781. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement