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

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