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 49KB

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