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

videolayout.js 50KB

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