Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

videolayout.js 50KB

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