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