You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

videolayout.js 50KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. var VideoLayout = (function (my) {
  2. var preMuted = false;
  3. var currentDominantSpeaker = null;
  4. var lastNCount = config.channelLastN;
  5. var lastNEndpointsCache = [];
  6. var largeVideoNewSrc = '';
  7. my.changeLocalAudio = function(stream) {
  8. connection.jingle.localAudio = stream;
  9. RTC.attachMediaStream($('#localAudio'), stream);
  10. document.getElementById('localAudio').autoplay = true;
  11. document.getElementById('localAudio').volume = 0;
  12. if (preMuted) {
  13. toggleAudio();
  14. preMuted = false;
  15. }
  16. };
  17. my.changeLocalVideo = function(stream, flipX) {
  18. connection.jingle.localVideo = stream;
  19. var localVideo = document.createElement('video');
  20. localVideo.id = 'localVideo_' + stream.id;
  21. localVideo.autoplay = true;
  22. localVideo.volume = 0; // is it required if audio is separated ?
  23. localVideo.oncontextmenu = function () { return false; };
  24. var localVideoContainer = document.getElementById('localVideoWrapper');
  25. localVideoContainer.appendChild(localVideo);
  26. // Set default display name.
  27. setDisplayName('localVideoContainer');
  28. AudioLevels.updateAudioLevelCanvas();
  29. var localVideoSelector = $('#' + localVideo.id);
  30. // Add click handler to both video and video wrapper elements in case
  31. // there's no video.
  32. localVideoSelector.click(function () {
  33. VideoLayout.handleVideoThumbClicked(localVideo.src);
  34. });
  35. $('#localVideoContainer').click(function () {
  36. VideoLayout.handleVideoThumbClicked(localVideo.src);
  37. });
  38. // Add hover handler
  39. $('#localVideoContainer').hover(
  40. function() {
  41. VideoLayout.showDisplayName('localVideoContainer', true);
  42. },
  43. function() {
  44. if (!VideoLayout.isLargeVideoVisible()
  45. || localVideo.src !== $('#largeVideo').attr('src'))
  46. VideoLayout.showDisplayName('localVideoContainer', false);
  47. }
  48. );
  49. // Add stream ended handler
  50. stream.onended = function () {
  51. localVideoContainer.removeChild(localVideo);
  52. VideoLayout.updateRemovedVideo(localVideo.src);
  53. };
  54. // Flip video x axis if needed
  55. flipXLocalVideo = flipX;
  56. if (flipX) {
  57. localVideoSelector.addClass("flipVideoX");
  58. }
  59. // Attach WebRTC stream
  60. RTC.attachMediaStream(localVideoSelector, stream);
  61. localVideoSrc = localVideo.src;
  62. VideoLayout.updateLargeVideo(localVideoSrc, 0);
  63. };
  64. /**
  65. * Checks if removed video is currently displayed and tries to display
  66. * another one instead.
  67. * @param removedVideoSrc src stream identifier of the video.
  68. */
  69. my.updateRemovedVideo = function(removedVideoSrc) {
  70. if (removedVideoSrc === $('#largeVideo').attr('src')) {
  71. // this is currently displayed as large
  72. // pick the last visible video in the row
  73. // if nobody else is left, this picks the local video
  74. var pick
  75. = $('#remoteVideos>span[id!="mixedstream"]:visible:last>video')
  76. .get(0);
  77. if (!pick) {
  78. console.info("Last visible video no longer exists");
  79. pick = $('#remoteVideos>span[id!="mixedstream"]>video').get(0);
  80. if (!pick || !pick.src) {
  81. // Try local video
  82. console.info("Fallback to local video...");
  83. pick = $('#remoteVideos>span>span>video').get(0);
  84. }
  85. }
  86. // mute if localvideo
  87. if (pick) {
  88. VideoLayout.updateLargeVideo(pick.src, pick.volume);
  89. } else {
  90. console.warn("Failed to elect large video");
  91. }
  92. }
  93. };
  94. /**
  95. * Updates the large video with the given new video source.
  96. */
  97. my.updateLargeVideo = function(newSrc, vol) {
  98. console.log('hover in', newSrc);
  99. if ($('#largeVideo').attr('src') != newSrc) {
  100. largeVideoNewSrc = newSrc;
  101. var isVisible = $('#largeVideo').is(':visible');
  102. // we need this here because after the fade the videoSrc may have
  103. // changed.
  104. var isDesktop = isVideoSrcDesktop(newSrc);
  105. var userJid = getJidFromVideoSrc(newSrc);
  106. // we want the notification to trigger even if userJid is undefined,
  107. // or null.
  108. $(document).trigger("selectedendpointchanged", [userJid]);
  109. $('#largeVideo').fadeOut(300, function () {
  110. var oldSrc = $(this).attr('src');
  111. $(this).attr('src', newSrc);
  112. // Screen stream is already rotated
  113. var flipX = (newSrc === localVideoSrc) && flipXLocalVideo;
  114. var videoTransform = document.getElementById('largeVideo')
  115. .style.webkitTransform;
  116. if (flipX && videoTransform !== 'scaleX(-1)') {
  117. document.getElementById('largeVideo').style.webkitTransform
  118. = "scaleX(-1)";
  119. }
  120. else if (!flipX && videoTransform === 'scaleX(-1)') {
  121. document.getElementById('largeVideo').style.webkitTransform
  122. = "none";
  123. }
  124. // Change the way we'll be measuring and positioning large video
  125. getVideoSize = isDesktop
  126. ? getDesktopVideoSize
  127. : getCameraVideoSize;
  128. getVideoPosition = isDesktop
  129. ? getDesktopVideoPosition
  130. : getCameraVideoPosition;
  131. if (isVisible) {
  132. // Only if the large video is currently visible.
  133. // Disable previous dominant speaker video.
  134. var oldJid = getJidFromVideoSrc(oldSrc);
  135. if (oldJid) {
  136. var oldResourceJid = Strophe.getResourceFromJid(oldJid);
  137. VideoLayout.enableDominantSpeaker(oldResourceJid, false);
  138. }
  139. // Enable new dominant speaker in the remote videos section.
  140. var userJid = getJidFromVideoSrc(newSrc);
  141. if (userJid)
  142. {
  143. var resourceJid = Strophe.getResourceFromJid(userJid);
  144. VideoLayout.enableDominantSpeaker(resourceJid, true);
  145. }
  146. $(this).fadeIn(300);
  147. }
  148. });
  149. }
  150. };
  151. my.handleVideoThumbClicked = function(videoSrc) {
  152. // Restore style for previously focused video
  153. var focusJid = getJidFromVideoSrc(focusedVideoSrc);
  154. var oldContainer = getParticipantContainer(focusJid);
  155. if (oldContainer) {
  156. oldContainer.removeClass("videoContainerFocused");
  157. }
  158. // Unlock current focused.
  159. if (focusedVideoSrc === videoSrc)
  160. {
  161. focusedVideoSrc = null;
  162. var dominantSpeakerVideo = null;
  163. // Enable the currently set dominant speaker.
  164. if (currentDominantSpeaker) {
  165. dominantSpeakerVideo
  166. = $('#participant_' + currentDominantSpeaker + '>video')
  167. .get(0);
  168. if (dominantSpeakerVideo) {
  169. VideoLayout.updateLargeVideo(dominantSpeakerVideo.src, 1);
  170. }
  171. }
  172. return;
  173. }
  174. // Lock new video
  175. focusedVideoSrc = videoSrc;
  176. // Update focused/pinned interface.
  177. var userJid = getJidFromVideoSrc(videoSrc);
  178. if (userJid)
  179. {
  180. var container = getParticipantContainer(userJid);
  181. container.addClass("videoContainerFocused");
  182. }
  183. // Triggers a "video.selected" event. The "false" parameter indicates
  184. // this isn't a prezi.
  185. $(document).trigger("video.selected", [false]);
  186. VideoLayout.updateLargeVideo(videoSrc, 1);
  187. $('audio').each(function (idx, el) {
  188. if (el.id.indexOf('mixedmslabel') !== -1) {
  189. el.volume = 0;
  190. el.volume = 1;
  191. }
  192. });
  193. };
  194. /**
  195. * Positions the large video.
  196. *
  197. * @param videoWidth the stream video width
  198. * @param videoHeight the stream video height
  199. */
  200. my.positionLarge = function (videoWidth, videoHeight) {
  201. var videoSpaceWidth = $('#videospace').width();
  202. var videoSpaceHeight = window.innerHeight;
  203. var videoSize = getVideoSize(videoWidth,
  204. videoHeight,
  205. videoSpaceWidth,
  206. videoSpaceHeight);
  207. var largeVideoWidth = videoSize[0];
  208. var largeVideoHeight = videoSize[1];
  209. var videoPosition = getVideoPosition(largeVideoWidth,
  210. largeVideoHeight,
  211. videoSpaceWidth,
  212. videoSpaceHeight);
  213. var horizontalIndent = videoPosition[0];
  214. var verticalIndent = videoPosition[1];
  215. positionVideo($('#largeVideo'),
  216. largeVideoWidth,
  217. largeVideoHeight,
  218. horizontalIndent, verticalIndent);
  219. };
  220. /**
  221. * Shows/hides the large video.
  222. */
  223. my.setLargeVideoVisible = function(isVisible) {
  224. var largeVideoJid = getJidFromVideoSrc($('#largeVideo').attr('src'));
  225. var resourceJid = Strophe.getResourceFromJid(largeVideoJid);
  226. if (isVisible) {
  227. $('#largeVideo').css({visibility: 'visible'});
  228. $('.watermark').css({visibility: 'visible'});
  229. VideoLayout.enableDominantSpeaker(resourceJid, true);
  230. }
  231. else {
  232. $('#largeVideo').css({visibility: 'hidden'});
  233. $('.watermark').css({visibility: 'hidden'});
  234. VideoLayout.enableDominantSpeaker(resourceJid, false);
  235. }
  236. };
  237. /**
  238. * Indicates if the large video is currently visible.
  239. *
  240. * @return <tt>true</tt> if visible, <tt>false</tt> - otherwise
  241. */
  242. my.isLargeVideoVisible = function() {
  243. return $('#largeVideo').is(':visible');
  244. };
  245. /**
  246. * Checks if container for participant identified by given peerJid exists
  247. * in the document and creates it eventually.
  248. *
  249. * @param peerJid peer Jid to check.
  250. *
  251. * @return Returns <tt>true</tt> if the peer container exists,
  252. * <tt>false</tt> - otherwise
  253. */
  254. my.ensurePeerContainerExists = function(peerJid) {
  255. ContactList.ensureAddContact(peerJid);
  256. var resourceJid = Strophe.getResourceFromJid(peerJid);
  257. var videoSpanId = 'participant_' + resourceJid;
  258. if ($('#' + videoSpanId).length > 0) {
  259. // If there's been a focus change, make sure we add focus related
  260. // interface!!
  261. if (focus && $('#remote_popupmenu_' + resourceJid).length <= 0)
  262. addRemoteVideoMenu( peerJid,
  263. document.getElementById(videoSpanId));
  264. }
  265. else {
  266. var container
  267. = VideoLayout.addRemoteVideoContainer(peerJid, videoSpanId);
  268. // Set default display name.
  269. setDisplayName(videoSpanId);
  270. var nickfield = document.createElement('span');
  271. nickfield.className = "nick";
  272. nickfield.appendChild(document.createTextNode(resourceJid));
  273. container.appendChild(nickfield);
  274. // In case this is not currently in the last n we don't show it.
  275. if (lastNCount
  276. && lastNCount > 0
  277. && $('#remoteVideos>span').length >= lastNCount + 2) {
  278. showPeerContainer(resourceJid, false);
  279. }
  280. else
  281. VideoLayout.resizeThumbnails();
  282. }
  283. };
  284. my.addRemoteVideoContainer = function(peerJid, spanId) {
  285. var container = document.createElement('span');
  286. container.id = spanId;
  287. container.className = 'videocontainer';
  288. var remotes = document.getElementById('remoteVideos');
  289. // If the peerJid is null then this video span couldn't be directly
  290. // associated with a participant (this could happen in the case of prezi).
  291. if (focus && peerJid != null)
  292. addRemoteVideoMenu(peerJid, container);
  293. remotes.appendChild(container);
  294. AudioLevels.updateAudioLevelCanvas(peerJid);
  295. return container;
  296. };
  297. /**
  298. * Creates an audio or video stream element.
  299. */
  300. my.createStreamElement = function (sid, stream) {
  301. var isVideo = stream.getVideoTracks().length > 0;
  302. var element = isVideo
  303. ? document.createElement('video')
  304. : document.createElement('audio');
  305. var id = (isVideo ? 'remoteVideo_' : 'remoteAudio_')
  306. + sid + '_' + stream.id;
  307. element.id = id;
  308. element.autoplay = true;
  309. element.oncontextmenu = function () { return false; };
  310. return element;
  311. };
  312. my.addRemoteStreamElement
  313. = function (container, sid, stream, peerJid, thessrc) {
  314. var newElementId = null;
  315. var isVideo = stream.getVideoTracks().length > 0;
  316. if (container) {
  317. var streamElement = VideoLayout.createStreamElement(sid, stream);
  318. newElementId = streamElement.id;
  319. container.appendChild(streamElement);
  320. var sel = $('#' + newElementId);
  321. sel.hide();
  322. // If the container is currently visible we attach the stream.
  323. if (!isVideo
  324. || (container.offsetParent !== null && isVideo)) {
  325. var simulcast = new Simulcast();
  326. var videoStream = simulcast.getReceivingVideoStream(stream);
  327. RTC.attachMediaStream(sel, videoStream);
  328. if (isVideo)
  329. waitForRemoteVideo(sel, thessrc, stream);
  330. }
  331. stream.onended = function () {
  332. console.log('stream ended', this);
  333. VideoLayout.removeRemoteStreamElement(stream, container);
  334. if (peerJid)
  335. ContactList.removeContact(peerJid);
  336. };
  337. // Add click handler.
  338. container.onclick = function (event) {
  339. /*
  340. * FIXME It turns out that videoThumb may not exist (if there is
  341. * no actual video).
  342. */
  343. var videoThumb = $('#' + container.id + '>video').get(0);
  344. if (videoThumb)
  345. VideoLayout.handleVideoThumbClicked(videoThumb.src);
  346. event.preventDefault();
  347. return false;
  348. };
  349. // Add hover handler
  350. $(container).hover(
  351. function() {
  352. VideoLayout.showDisplayName(container.id, true);
  353. },
  354. function() {
  355. var videoSrc = null;
  356. if ($('#' + container.id + '>video')
  357. && $('#' + container.id + '>video').length > 0) {
  358. videoSrc = $('#' + container.id + '>video').get(0).src;
  359. }
  360. // If the video has been "pinned" by the user we want to
  361. // keep the display name on place.
  362. if (!VideoLayout.isLargeVideoVisible()
  363. || videoSrc !== $('#largeVideo').attr('src'))
  364. VideoLayout.showDisplayName(container.id, false);
  365. }
  366. );
  367. }
  368. return newElementId;
  369. };
  370. /**
  371. * Removes the remote stream element corresponding to the given stream and
  372. * parent container.
  373. *
  374. * @param stream the stream
  375. * @param container
  376. */
  377. my.removeRemoteStreamElement = function (stream, container) {
  378. if (!container)
  379. return;
  380. var select = null;
  381. var removedVideoSrc = null;
  382. if (stream.getVideoTracks().length > 0) {
  383. select = $('#' + container.id + '>video');
  384. removedVideoSrc = select.get(0).src;
  385. }
  386. else
  387. select = $('#' + container.id + '>audio');
  388. // Remove video source from the mapping.
  389. delete videoSrcToSsrc[removedVideoSrc];
  390. // Mark video as removed to cancel waiting loop(if video is removed
  391. // before has started)
  392. select.removed = true;
  393. select.remove();
  394. var audioCount = $('#' + container.id + '>audio').length;
  395. var videoCount = $('#' + container.id + '>video').length;
  396. if (!audioCount && !videoCount) {
  397. console.log("Remove whole user", container.id);
  398. // Remove whole container
  399. container.remove();
  400. Util.playSoundNotification('userLeft');
  401. VideoLayout.resizeThumbnails();
  402. }
  403. if (removedVideoSrc)
  404. VideoLayout.updateRemovedVideo(removedVideoSrc);
  405. };
  406. /**
  407. * Show/hide peer container for the given resourceJid.
  408. */
  409. function showPeerContainer(resourceJid, isShow) {
  410. var peerContainer = $('#participant_' + resourceJid);
  411. if (!peerContainer)
  412. return;
  413. if (!peerContainer.is(':visible') && isShow)
  414. peerContainer.show();
  415. else if (peerContainer.is(':visible') && !isShow)
  416. peerContainer.hide();
  417. };
  418. /**
  419. * Sets the display name for the given video span id.
  420. */
  421. function setDisplayName(videoSpanId, displayName) {
  422. var nameSpan = $('#' + videoSpanId + '>span.displayname');
  423. var defaultLocalDisplayName = "Me";
  424. // If we already have a display name for this video.
  425. if (nameSpan.length > 0) {
  426. var nameSpanElement = nameSpan.get(0);
  427. if (nameSpanElement.id === 'localDisplayName' &&
  428. $('#localDisplayName').text() !== displayName) {
  429. if (displayName && displayName.length > 0)
  430. $('#localDisplayName').text(displayName + ' (me)');
  431. else
  432. $('#localDisplayName').text(defaultLocalDisplayName);
  433. } else {
  434. if (displayName && displayName.length > 0)
  435. $('#' + videoSpanId + '_name').text(displayName);
  436. else
  437. $('#' + videoSpanId + '_name').text(interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  438. }
  439. } else {
  440. var editButton = null;
  441. nameSpan = document.createElement('span');
  442. nameSpan.className = 'displayname';
  443. $('#' + videoSpanId)[0].appendChild(nameSpan);
  444. if (videoSpanId === 'localVideoContainer') {
  445. editButton = createEditDisplayNameButton();
  446. nameSpan.innerText = defaultLocalDisplayName;
  447. }
  448. else {
  449. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  450. }
  451. if (displayName && displayName.length > 0) {
  452. nameSpan.innerText = displayName;
  453. }
  454. if (!editButton) {
  455. nameSpan.id = videoSpanId + '_name';
  456. } else {
  457. nameSpan.id = 'localDisplayName';
  458. $('#' + videoSpanId)[0].appendChild(editButton);
  459. var editableText = document.createElement('input');
  460. editableText.className = 'displayname';
  461. editableText.type = 'text';
  462. editableText.id = 'editDisplayName';
  463. if (displayName && displayName.length) {
  464. editableText.value
  465. = displayName.substring(0, displayName.indexOf(' (me)'));
  466. }
  467. editableText.setAttribute('style', 'display:none;');
  468. editableText.setAttribute('placeholder', 'ex. Jane Pink');
  469. $('#' + videoSpanId)[0].appendChild(editableText);
  470. $('#localVideoContainer .displayname')
  471. .bind("click", function (e) {
  472. e.preventDefault();
  473. $('#localDisplayName').hide();
  474. $('#editDisplayName').show();
  475. $('#editDisplayName').focus();
  476. $('#editDisplayName').select();
  477. var inputDisplayNameHandler = function (name) {
  478. if (nickname !== name) {
  479. nickname = name;
  480. window.localStorage.displayname = nickname;
  481. connection.emuc.addDisplayNameToPresence(nickname);
  482. connection.emuc.sendPresence();
  483. Chat.setChatConversationMode(true);
  484. }
  485. if (!$('#localDisplayName').is(":visible")) {
  486. if (nickname)
  487. $('#localDisplayName').text(nickname + " (me)");
  488. else
  489. $('#localDisplayName')
  490. .text(defaultLocalDisplayName);
  491. $('#localDisplayName').show();
  492. }
  493. $('#editDisplayName').hide();
  494. };
  495. $('#editDisplayName').one("focusout", function (e) {
  496. inputDisplayNameHandler(this.value);
  497. });
  498. $('#editDisplayName').on('keydown', function (e) {
  499. if (e.keyCode === 13) {
  500. e.preventDefault();
  501. inputDisplayNameHandler(this.value);
  502. }
  503. });
  504. });
  505. }
  506. }
  507. };
  508. /**
  509. * Shows/hides the display name on the remote video.
  510. * @param videoSpanId the identifier of the video span element
  511. * @param isShow indicates if the display name should be shown or hidden
  512. */
  513. my.showDisplayName = function(videoSpanId, isShow) {
  514. var nameSpan = $('#' + videoSpanId + '>span.displayname').get(0);
  515. if (isShow) {
  516. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  517. nameSpan.setAttribute("style", "display:inline-block;");
  518. }
  519. else {
  520. if (nameSpan)
  521. nameSpan.setAttribute("style", "display:none;");
  522. }
  523. };
  524. /**
  525. * Shows the presence status message for the given video.
  526. */
  527. my.setPresenceStatus = function (videoSpanId, statusMsg) {
  528. if (!$('#' + videoSpanId).length) {
  529. // No container
  530. return;
  531. }
  532. var statusSpan = $('#' + videoSpanId + '>span.status');
  533. if (!statusSpan.length) {
  534. //Add status span
  535. statusSpan = document.createElement('span');
  536. statusSpan.className = 'status';
  537. statusSpan.id = videoSpanId + '_status';
  538. $('#' + videoSpanId)[0].appendChild(statusSpan);
  539. statusSpan = $('#' + videoSpanId + '>span.status');
  540. }
  541. // Display status
  542. if (statusMsg && statusMsg.length) {
  543. $('#' + videoSpanId + '_status').text(statusMsg);
  544. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  545. }
  546. else {
  547. // Hide
  548. statusSpan.get(0).setAttribute("style", "display:none;");
  549. }
  550. };
  551. /**
  552. * Shows a visual indicator for the focus of the conference.
  553. * Currently if we're not the owner of the conference we obtain the focus
  554. * from the connection.jingle.sessions.
  555. */
  556. my.showFocusIndicator = function() {
  557. if (focus !== null) {
  558. var indicatorSpan = $('#localVideoContainer .focusindicator');
  559. if (indicatorSpan.children().length === 0)
  560. {
  561. createFocusIndicatorElement(indicatorSpan[0]);
  562. }
  563. }
  564. else if (Object.keys(connection.jingle.sessions).length > 0) {
  565. // If we're only a participant the focus will be the only session we have.
  566. var session
  567. = connection.jingle.sessions
  568. [Object.keys(connection.jingle.sessions)[0]];
  569. var focusId
  570. = 'participant_' + Strophe.getResourceFromJid(session.peerjid);
  571. var focusContainer = document.getElementById(focusId);
  572. if (!focusContainer) {
  573. console.error("No focus container!");
  574. return;
  575. }
  576. var indicatorSpan = $('#' + focusId + ' .focusindicator');
  577. if (!indicatorSpan || indicatorSpan.length === 0) {
  578. indicatorSpan = document.createElement('span');
  579. indicatorSpan.className = 'focusindicator';
  580. focusContainer.appendChild(indicatorSpan);
  581. createFocusIndicatorElement(indicatorSpan);
  582. }
  583. }
  584. };
  585. /**
  586. * Shows video muted indicator over small videos.
  587. */
  588. my.showVideoIndicator = function(videoSpanId, isMuted) {
  589. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  590. if (isMuted === 'false') {
  591. if (videoMutedSpan.length > 0) {
  592. videoMutedSpan.remove();
  593. }
  594. }
  595. else {
  596. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  597. videoMutedSpan = document.createElement('span');
  598. videoMutedSpan.className = 'videoMuted';
  599. if (audioMutedSpan) {
  600. videoMutedSpan.right = '30px';
  601. }
  602. $('#' + videoSpanId)[0].appendChild(videoMutedSpan);
  603. var mutedIndicator = document.createElement('i');
  604. mutedIndicator.className = 'icon-camera-disabled';
  605. Util.setTooltip(mutedIndicator,
  606. "Participant has<br/>stopped the camera.",
  607. "top");
  608. videoMutedSpan.appendChild(mutedIndicator);
  609. }
  610. };
  611. /**
  612. * Shows audio muted indicator over small videos.
  613. */
  614. my.showAudioIndicator = function(videoSpanId, isMuted) {
  615. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  616. if (isMuted === 'false') {
  617. if (audioMutedSpan.length > 0) {
  618. audioMutedSpan.popover('hide');
  619. audioMutedSpan.remove();
  620. }
  621. }
  622. else {
  623. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  624. audioMutedSpan = document.createElement('span');
  625. audioMutedSpan.className = 'audioMuted';
  626. Util.setTooltip(audioMutedSpan,
  627. "Participant is muted",
  628. "top");
  629. if (videoMutedSpan) {
  630. audioMutedSpan.right = '30px';
  631. }
  632. $('#' + videoSpanId)[0].appendChild(audioMutedSpan);
  633. var mutedIndicator = document.createElement('i');
  634. mutedIndicator.className = 'icon-mic-disabled';
  635. audioMutedSpan.appendChild(mutedIndicator);
  636. }
  637. };
  638. /**
  639. * Resizes the large video container.
  640. */
  641. my.resizeLargeVideoContainer = function () {
  642. Chat.resizeChat();
  643. var availableHeight = window.innerHeight;
  644. var availableWidth = Util.getAvailableVideoWidth();
  645. if (availableWidth < 0 || availableHeight < 0) return;
  646. $('#videospace').width(availableWidth);
  647. $('#videospace').height(availableHeight);
  648. $('#largeVideoContainer').width(availableWidth);
  649. $('#largeVideoContainer').height(availableHeight);
  650. VideoLayout.resizeThumbnails();
  651. };
  652. /**
  653. * Resizes thumbnails.
  654. */
  655. my.resizeThumbnails = function() {
  656. var videoSpaceWidth = $('#remoteVideos').width();
  657. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  658. var width = thumbnailSize[0];
  659. var height = thumbnailSize[1];
  660. // size videos so that while keeping AR and max height, we have a
  661. // nice fit
  662. $('#remoteVideos').height(height);
  663. $('#remoteVideos>span').width(width);
  664. $('#remoteVideos>span').height(height);
  665. $(document).trigger("remotevideo.resized", [width, height]);
  666. };
  667. /**
  668. * Enables the dominant speaker UI.
  669. *
  670. * @param resourceJid the jid indicating the video element to
  671. * activate/deactivate
  672. * @param isEnable indicates if the dominant speaker should be enabled or
  673. * disabled
  674. */
  675. my.enableDominantSpeaker = function(resourceJid, isEnable) {
  676. var displayName = resourceJid;
  677. var nameSpan = $('#participant_' + resourceJid + '>span.displayname');
  678. if (nameSpan.length > 0)
  679. displayName = nameSpan.text();
  680. console.log("UI enable dominant speaker",
  681. displayName,
  682. resourceJid,
  683. isEnable);
  684. var videoSpanId = null;
  685. var videoContainerId = null;
  686. if (resourceJid
  687. === Strophe.getResourceFromJid(connection.emuc.myroomjid)) {
  688. videoSpanId = 'localVideoWrapper';
  689. videoContainerId = 'localVideoContainer';
  690. }
  691. else {
  692. videoSpanId = 'participant_' + resourceJid;
  693. videoContainerId = videoSpanId;
  694. }
  695. videoSpan = document.getElementById(videoContainerId);
  696. if (!videoSpan) {
  697. console.error("No video element for jid", resourceJid);
  698. return;
  699. }
  700. var video = $('#' + videoSpanId + '>video');
  701. if (video && video.length > 0) {
  702. if (isEnable) {
  703. VideoLayout.showDisplayName(videoContainerId, true);
  704. if (!videoSpan.classList.contains("dominantspeaker"))
  705. videoSpan.classList.add("dominantspeaker");
  706. video.css({visibility: 'hidden'});
  707. }
  708. else {
  709. VideoLayout.showDisplayName(videoContainerId, false);
  710. if (videoSpan.classList.contains("dominantspeaker"))
  711. videoSpan.classList.remove("dominantspeaker");
  712. video.css({visibility: 'visible'});
  713. }
  714. }
  715. };
  716. /**
  717. * Gets the selector of video thumbnail container for the user identified by
  718. * given <tt>userJid</tt>
  719. * @param userJid user's Jid for whom we want to get the video container.
  720. */
  721. function getParticipantContainer(userJid)
  722. {
  723. if (!userJid)
  724. return null;
  725. if (userJid === connection.emuc.myroomjid)
  726. return $("#localVideoContainer");
  727. else
  728. return $("#participant_" + Strophe.getResourceFromJid(userJid));
  729. }
  730. /**
  731. * Sets the size and position of the given video element.
  732. *
  733. * @param video the video element to position
  734. * @param width the desired video width
  735. * @param height the desired video height
  736. * @param horizontalIndent the left and right indent
  737. * @param verticalIndent the top and bottom indent
  738. */
  739. function positionVideo(video,
  740. width,
  741. height,
  742. horizontalIndent,
  743. verticalIndent) {
  744. video.width(width);
  745. video.height(height);
  746. video.css({ top: verticalIndent + 'px',
  747. bottom: verticalIndent + 'px',
  748. left: horizontalIndent + 'px',
  749. right: horizontalIndent + 'px'});
  750. }
  751. /**
  752. * Calculates the thumbnail size.
  753. *
  754. * @param videoSpaceWidth the width of the video space
  755. */
  756. my.calculateThumbnailSize = function (videoSpaceWidth) {
  757. // Calculate the available height, which is the inner window height minus
  758. // 39px for the header minus 2px for the delimiter lines on the top and
  759. // bottom of the large video, minus the 36px space inside the remoteVideos
  760. // container used for highlighting shadow.
  761. var availableHeight = 100;
  762. var numvids = 0;
  763. if (lastNCount && lastNCount > 0)
  764. numvids = lastNCount + 1;
  765. else
  766. numvids = $('#remoteVideos>span:visible').length;
  767. // Remove the 3px borders arround videos and border around the remote
  768. // videos area
  769. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70;
  770. var availableWidth = availableWinWidth / numvids;
  771. var aspectRatio = 16.0 / 9.0;
  772. var maxHeight = Math.min(160, availableHeight);
  773. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  774. if (availableHeight < availableWidth / aspectRatio) {
  775. availableWidth = Math.floor(availableHeight * aspectRatio);
  776. }
  777. return [availableWidth, availableHeight];
  778. };
  779. /**
  780. * Returns an array of the video dimensions, so that it keeps it's aspect
  781. * ratio and fits available area with it's larger dimension. This method
  782. * ensures that whole video will be visible and can leave empty areas.
  783. *
  784. * @return an array with 2 elements, the video width and the video height
  785. */
  786. function getDesktopVideoSize(videoWidth,
  787. videoHeight,
  788. videoSpaceWidth,
  789. videoSpaceHeight) {
  790. if (!videoWidth)
  791. videoWidth = currentVideoWidth;
  792. if (!videoHeight)
  793. videoHeight = currentVideoHeight;
  794. var aspectRatio = videoWidth / videoHeight;
  795. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  796. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  797. videoSpaceHeight -= $('#remoteVideos').outerHeight();
  798. if (availableWidth / aspectRatio >= videoSpaceHeight)
  799. {
  800. availableHeight = videoSpaceHeight;
  801. availableWidth = availableHeight * aspectRatio;
  802. }
  803. if (availableHeight * aspectRatio >= videoSpaceWidth)
  804. {
  805. availableWidth = videoSpaceWidth;
  806. availableHeight = availableWidth / aspectRatio;
  807. }
  808. return [availableWidth, availableHeight];
  809. }
  810. /**
  811. * Creates the edit display name button.
  812. *
  813. * @returns the edit button
  814. */
  815. function createEditDisplayNameButton() {
  816. var editButton = document.createElement('a');
  817. editButton.className = 'displayname';
  818. Util.setTooltip(editButton,
  819. 'Click to edit your<br/>display name',
  820. "top");
  821. editButton.innerHTML = '<i class="fa fa-pencil"></i>';
  822. return editButton;
  823. }
  824. /**
  825. * Creates the element indicating the focus of the conference.
  826. *
  827. * @param parentElement the parent element where the focus indicator will
  828. * be added
  829. */
  830. function createFocusIndicatorElement(parentElement) {
  831. var focusIndicator = document.createElement('i');
  832. focusIndicator.className = 'fa fa-star';
  833. parentElement.appendChild(focusIndicator);
  834. Util.setTooltip(parentElement,
  835. "The owner of<br/>this conference",
  836. "top");
  837. }
  838. /**
  839. * Updates the remote video menu.
  840. *
  841. * @param jid the jid indicating the video for which we're adding a menu.
  842. * @param isMuted indicates the current mute state
  843. */
  844. my.updateRemoteVideoMenu = function(jid, isMuted) {
  845. var muteMenuItem
  846. = $('#remote_popupmenu_'
  847. + Strophe.getResourceFromJid(jid)
  848. + '>li>a.mutelink');
  849. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  850. if (muteMenuItem.length) {
  851. var muteLink = muteMenuItem.get(0);
  852. if (isMuted === 'true') {
  853. muteLink.innerHTML = mutedIndicator + ' Muted';
  854. muteLink.className = 'mutelink disabled';
  855. }
  856. else {
  857. muteLink.innerHTML = mutedIndicator + ' Mute';
  858. muteLink.className = 'mutelink';
  859. }
  860. }
  861. };
  862. /**
  863. * Returns the current dominant speaker resource jid.
  864. */
  865. my.getDominantSpeakerResourceJid = function () {
  866. return currentDominantSpeaker;
  867. };
  868. /**
  869. * Returns the corresponding resource jid to the given peer container
  870. * DOM element.
  871. *
  872. * @return the corresponding resource jid to the given peer container
  873. * DOM element
  874. */
  875. my.getPeerContainerResourceJid = function (containerElement) {
  876. var i = containerElement.id.indexOf('participant_');
  877. if (i >= 0)
  878. return containerElement.id.substring(i + 12);
  879. };
  880. /**
  881. * Adds the remote video menu element for the given <tt>jid</tt> in the
  882. * given <tt>parentElement</tt>.
  883. *
  884. * @param jid the jid indicating the video for which we're adding a menu.
  885. * @param parentElement the parent element where this menu will be added
  886. */
  887. function addRemoteVideoMenu(jid, parentElement) {
  888. var spanElement = document.createElement('span');
  889. spanElement.className = 'remotevideomenu';
  890. parentElement.appendChild(spanElement);
  891. var menuElement = document.createElement('i');
  892. menuElement.className = 'fa fa-angle-down';
  893. menuElement.title = 'Remote user controls';
  894. spanElement.appendChild(menuElement);
  895. // <ul class="popupmenu">
  896. // <li><a href="#">Mute</a></li>
  897. // <li><a href="#">Eject</a></li>
  898. // </ul>
  899. var popupmenuElement = document.createElement('ul');
  900. popupmenuElement.className = 'popupmenu';
  901. popupmenuElement.id
  902. = 'remote_popupmenu_' + Strophe.getResourceFromJid(jid);
  903. spanElement.appendChild(popupmenuElement);
  904. var muteMenuItem = document.createElement('li');
  905. var muteLinkItem = document.createElement('a');
  906. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  907. if (!mutedAudios[jid]) {
  908. muteLinkItem.innerHTML = mutedIndicator + 'Mute';
  909. muteLinkItem.className = 'mutelink';
  910. }
  911. else {
  912. muteLinkItem.innerHTML = mutedIndicator + ' Muted';
  913. muteLinkItem.className = 'mutelink disabled';
  914. }
  915. muteLinkItem.onclick = function(){
  916. if ($(this).attr('disabled') != undefined) {
  917. event.preventDefault();
  918. }
  919. var isMute = !mutedAudios[jid];
  920. connection.moderate.setMute(jid, isMute);
  921. popupmenuElement.setAttribute('style', 'display:none;');
  922. if (isMute) {
  923. this.innerHTML = mutedIndicator + ' Muted';
  924. this.className = 'mutelink disabled';
  925. }
  926. else {
  927. this.innerHTML = mutedIndicator + ' Mute';
  928. this.className = 'mutelink';
  929. }
  930. };
  931. muteMenuItem.appendChild(muteLinkItem);
  932. popupmenuElement.appendChild(muteMenuItem);
  933. var ejectIndicator = "<i class='fa fa-eject'></i>";
  934. var ejectMenuItem = document.createElement('li');
  935. var ejectLinkItem = document.createElement('a');
  936. ejectLinkItem.innerHTML = ejectIndicator + ' Kick out';
  937. ejectLinkItem.onclick = function(){
  938. connection.moderate.eject(jid);
  939. popupmenuElement.setAttribute('style', 'display:none;');
  940. };
  941. ejectMenuItem.appendChild(ejectLinkItem);
  942. popupmenuElement.appendChild(ejectMenuItem);
  943. var paddingSpan = document.createElement('span');
  944. paddingSpan.className = 'popupmenuPadding';
  945. popupmenuElement.appendChild(paddingSpan);
  946. }
  947. /**
  948. * On audio muted event.
  949. */
  950. $(document).bind('audiomuted.muc', function (event, jid, isMuted) {
  951. var videoSpanId = null;
  952. if (jid === connection.emuc.myroomjid) {
  953. videoSpanId = 'localVideoContainer';
  954. } else {
  955. VideoLayout.ensurePeerContainerExists(jid);
  956. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  957. }
  958. if (focus) {
  959. mutedAudios[jid] = isMuted;
  960. VideoLayout.updateRemoteVideoMenu(jid, isMuted);
  961. }
  962. if (videoSpanId)
  963. VideoLayout.showAudioIndicator(videoSpanId, isMuted);
  964. });
  965. /**
  966. * On video muted event.
  967. */
  968. $(document).bind('videomuted.muc', function (event, jid, isMuted) {
  969. var videoSpanId = null;
  970. if (jid === connection.emuc.myroomjid) {
  971. videoSpanId = 'localVideoContainer';
  972. } else {
  973. VideoLayout.ensurePeerContainerExists(jid);
  974. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  975. }
  976. if (videoSpanId)
  977. VideoLayout.showVideoIndicator(videoSpanId, isMuted);
  978. });
  979. /**
  980. * Display name changed.
  981. */
  982. $(document).bind('displaynamechanged',
  983. function (event, jid, displayName, status) {
  984. if (jid === 'localVideoContainer'
  985. || jid === connection.emuc.myroomjid) {
  986. setDisplayName('localVideoContainer',
  987. displayName);
  988. } else {
  989. VideoLayout.ensurePeerContainerExists(jid);
  990. setDisplayName(
  991. 'participant_' + Strophe.getResourceFromJid(jid),
  992. displayName,
  993. status);
  994. }
  995. });
  996. /**
  997. * On dominant speaker changed event.
  998. */
  999. $(document).bind('dominantspeakerchanged', function (event, resourceJid) {
  1000. // We ignore local user events.
  1001. if (resourceJid
  1002. === Strophe.getResourceFromJid(connection.emuc.myroomjid))
  1003. return;
  1004. // Update the current dominant speaker.
  1005. if (resourceJid !== currentDominantSpeaker) {
  1006. var oldSpeakerVideoSpanId = "participant_" + currentDominantSpeaker,
  1007. newSpeakerVideoSpanId = "participant_" + resourceJid;
  1008. if($("#" + oldSpeakerVideoSpanId + ">span.displayname").text() ===
  1009. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME) {
  1010. setDisplayName(oldSpeakerVideoSpanId, null);
  1011. }
  1012. if($("#" + newSpeakerVideoSpanId + ">span.displayname").text() ===
  1013. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME) {
  1014. setDisplayName(newSpeakerVideoSpanId,
  1015. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  1016. }
  1017. currentDominantSpeaker = resourceJid;
  1018. } else {
  1019. return;
  1020. }
  1021. // Obtain container for new dominant speaker.
  1022. var container = document.getElementById(
  1023. 'participant_' + resourceJid);
  1024. // Local video will not have container found, but that's ok
  1025. // since we don't want to switch to local video.
  1026. if (container && !focusedVideoSrc)
  1027. {
  1028. var video = container.getElementsByTagName("video");
  1029. // Update the large video if the video source is already available,
  1030. // otherwise wait for the "videoactive.jingle" event.
  1031. if (video.length && video[0].currentTime > 0)
  1032. VideoLayout.updateLargeVideo(video[0].src);
  1033. }
  1034. });
  1035. /**
  1036. * On last N change event.
  1037. *
  1038. * @param event the event that notified us
  1039. * @param lastNEndpoints the list of last N endpoints
  1040. * @param endpointsEnteringLastN the list currently entering last N
  1041. * endpoints
  1042. */
  1043. $(document).bind('lastnchanged', function ( event,
  1044. lastNEndpoints,
  1045. endpointsEnteringLastN,
  1046. stream) {
  1047. if (lastNCount !== lastNEndpoints.length)
  1048. lastNCount = lastNEndpoints.length;
  1049. lastNEndpointsCache = lastNEndpoints;
  1050. $('#remoteVideos>span').each(function( index, element ) {
  1051. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  1052. if (resourceJid
  1053. && lastNEndpoints.indexOf(resourceJid) < 0) {
  1054. console.log("Remove from last N", resourceJid);
  1055. showPeerContainer(resourceJid, false);
  1056. }
  1057. });
  1058. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  1059. endpointsEnteringLastN = lastNEndpoints;
  1060. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  1061. endpointsEnteringLastN.forEach(function (resourceJid) {
  1062. if (!$('#participant_' + resourceJid).is(':visible')) {
  1063. console.log("Add to last N", resourceJid);
  1064. showPeerContainer(resourceJid, true);
  1065. mediaStreams.some(function (mediaStream) {
  1066. if (mediaStream.peerjid
  1067. && Strophe.getResourceFromJid(mediaStream.peerjid)
  1068. === resourceJid
  1069. && mediaStream.type === mediaStream.VIDEO_TYPE) {
  1070. var sel = $('#participant_' + resourceJid + '>video');
  1071. var simulcast = new Simulcast();
  1072. var videoStream = simulcast.getReceivingVideoStream(mediaStream.stream);
  1073. RTC.attachMediaStream(sel, videoStream);
  1074. waitForRemoteVideo(
  1075. sel,
  1076. mediaStream.ssrc,
  1077. mediaStream.stream);
  1078. return true;
  1079. }
  1080. });
  1081. }
  1082. });
  1083. }
  1084. });
  1085. $(document).bind('videoactive.jingle', function (event, videoelem) {
  1086. if (videoelem.attr('id').indexOf('mixedmslabel') === -1) {
  1087. // ignore mixedmslabela0 and v0
  1088. videoelem.show();
  1089. VideoLayout.resizeThumbnails();
  1090. var videoParent = videoelem.parent();
  1091. var parentResourceJid = null;
  1092. if (videoParent)
  1093. parentResourceJid
  1094. = VideoLayout.getPeerContainerResourceJid(videoParent[0]);
  1095. // Update the large video to the last added video only if there's no
  1096. // current dominant or focused speaker or update it to the current
  1097. // dominant speaker.
  1098. if ((!focusedVideoSrc && !VideoLayout.getDominantSpeakerResourceJid())
  1099. || (parentResourceJid
  1100. && VideoLayout.getDominantSpeakerResourceJid()
  1101. === parentResourceJid)) {
  1102. VideoLayout.updateLargeVideo(videoelem.attr('src'), 1);
  1103. }
  1104. VideoLayout.showFocusIndicator();
  1105. }
  1106. });
  1107. $(document).bind('simulcastlayerstarted', function(event) {
  1108. var localVideoSelector = $('#' + 'localVideo_' + connection.jingle.localVideo.id);
  1109. var simulcast = new Simulcast();
  1110. var stream = simulcast.getLocalVideoStream();
  1111. // Attach WebRTC stream
  1112. RTC.attachMediaStream(localVideoSelector, stream);
  1113. localVideoSrc = $(localVideoSelector).attr('src');
  1114. });
  1115. $(document).bind('simulcastlayerstopped', function(event) {
  1116. var localVideoSelector = $('#' + 'localVideo_' + connection.jingle.localVideo.id);
  1117. var simulcast = new Simulcast();
  1118. var stream = simulcast.getLocalVideoStream();
  1119. // Attach WebRTC stream
  1120. RTC.attachMediaStream(localVideoSelector, stream);
  1121. localVideoSrc = $(localVideoSelector).attr('src');
  1122. });
  1123. /**
  1124. * On simulcast layers changed event.
  1125. */
  1126. $(document).bind('simulcastlayerschanged', function (event, endpointSimulcastLayers) {
  1127. var simulcast = new Simulcast();
  1128. endpointSimulcastLayers.forEach(function (esl) {
  1129. var primarySSRC = esl.simulcastLayer.primarySSRC;
  1130. var msid = simulcast.getRemoteVideoStreamIdBySSRC(primarySSRC);
  1131. // Get session and stream from msid.
  1132. var session, electedStream;
  1133. var i, j, k;
  1134. if (connection.jingle) {
  1135. var keys = Object.keys(connection.jingle.sessions);
  1136. for (i = 0; i < keys.length; i++) {
  1137. var sid = keys[i];
  1138. if (electedStream) {
  1139. // stream found, stop.
  1140. break;
  1141. }
  1142. session = connection.jingle.sessions[sid];
  1143. if (session.remoteStreams) {
  1144. for (j = 0; j < session.remoteStreams.length; j++) {
  1145. var remoteStream = session.remoteStreams[j];
  1146. if (electedStream) {
  1147. // stream found, stop.
  1148. break;
  1149. }
  1150. var tracks = remoteStream.getVideoTracks();
  1151. if (tracks) {
  1152. for (k = 0; k < tracks.length; k++) {
  1153. var track = tracks[k];
  1154. if (msid === [remoteStream.id, track.id].join(' ')) {
  1155. electedStream = new webkitMediaStream([track]);
  1156. // stream found, stop.
  1157. break;
  1158. }
  1159. }
  1160. }
  1161. }
  1162. }
  1163. }
  1164. }
  1165. if (session && electedStream) {
  1166. console.info('Switching simulcast substream.');
  1167. console.info([esl, primarySSRC, msid, session, electedStream]);
  1168. var msidParts = msid.split(' ');
  1169. var selRemoteVideo = $(['#', 'remoteVideo_', session.sid, '_', msidParts[0]].join(''));
  1170. var updateLargeVideo = (ssrc2jid[videoSrcToSsrc[selRemoteVideo.attr('src')]]
  1171. == ssrc2jid[videoSrcToSsrc[largeVideoNewSrc]]);
  1172. var updateFocusedVideoSrc = (selRemoteVideo.attr('src') == focusedVideoSrc);
  1173. var electedStreamUrl = webkitURL.createObjectURL(electedStream);
  1174. selRemoteVideo.attr('src', electedStreamUrl);
  1175. videoSrcToSsrc[selRemoteVideo.attr('src')] = primarySSRC;
  1176. if (updateLargeVideo) {
  1177. VideoLayout.updateLargeVideo(electedStreamUrl);
  1178. }
  1179. if (updateFocusedVideoSrc) {
  1180. focusedVideoSrc = electedStreamUrl;
  1181. }
  1182. } else {
  1183. console.error('Could not find a stream or a session.');
  1184. }
  1185. });
  1186. });
  1187. return my;
  1188. }(VideoLayout || {}));