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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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. VideoLayout.resizeThumbnails();
  418. };
  419. /**
  420. * Sets the display name for the given video span id.
  421. */
  422. function setDisplayName(videoSpanId, displayName) {
  423. var nameSpan = $('#' + videoSpanId + '>span.displayname');
  424. var defaultLocalDisplayName = "Me";
  425. // If we already have a display name for this video.
  426. if (nameSpan.length > 0) {
  427. var nameSpanElement = nameSpan.get(0);
  428. if (nameSpanElement.id === 'localDisplayName' &&
  429. $('#localDisplayName').text() !== displayName) {
  430. if (displayName && displayName.length > 0)
  431. $('#localDisplayName').text(displayName + ' (me)');
  432. else
  433. $('#localDisplayName').text(defaultLocalDisplayName);
  434. } else {
  435. if (displayName && displayName.length > 0)
  436. $('#' + videoSpanId + '_name').text(displayName);
  437. else
  438. $('#' + videoSpanId + '_name').text(interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  439. }
  440. } else {
  441. var editButton = null;
  442. nameSpan = document.createElement('span');
  443. nameSpan.className = 'displayname';
  444. $('#' + videoSpanId)[0].appendChild(nameSpan);
  445. if (videoSpanId === 'localVideoContainer') {
  446. editButton = createEditDisplayNameButton();
  447. nameSpan.innerText = defaultLocalDisplayName;
  448. }
  449. else {
  450. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  451. }
  452. if (displayName && displayName.length > 0) {
  453. nameSpan.innerText = displayName;
  454. }
  455. if (!editButton) {
  456. nameSpan.id = videoSpanId + '_name';
  457. } else {
  458. nameSpan.id = 'localDisplayName';
  459. $('#' + videoSpanId)[0].appendChild(editButton);
  460. var editableText = document.createElement('input');
  461. editableText.className = 'displayname';
  462. editableText.type = 'text';
  463. editableText.id = 'editDisplayName';
  464. if (displayName && displayName.length) {
  465. editableText.value
  466. = displayName.substring(0, displayName.indexOf(' (me)'));
  467. }
  468. editableText.setAttribute('style', 'display:none;');
  469. editableText.setAttribute('placeholder', 'ex. Jane Pink');
  470. $('#' + videoSpanId)[0].appendChild(editableText);
  471. $('#localVideoContainer .displayname')
  472. .bind("click", function (e) {
  473. e.preventDefault();
  474. $('#localDisplayName').hide();
  475. $('#editDisplayName').show();
  476. $('#editDisplayName').focus();
  477. $('#editDisplayName').select();
  478. var inputDisplayNameHandler = function (name) {
  479. if (nickname !== name) {
  480. nickname = name;
  481. window.localStorage.displayname = nickname;
  482. connection.emuc.addDisplayNameToPresence(nickname);
  483. connection.emuc.sendPresence();
  484. Chat.setChatConversationMode(true);
  485. }
  486. if (!$('#localDisplayName').is(":visible")) {
  487. if (nickname)
  488. $('#localDisplayName').text(nickname + " (me)");
  489. else
  490. $('#localDisplayName')
  491. .text(defaultLocalDisplayName);
  492. $('#localDisplayName').show();
  493. }
  494. $('#editDisplayName').hide();
  495. };
  496. $('#editDisplayName').one("focusout", function (e) {
  497. inputDisplayNameHandler(this.value);
  498. });
  499. $('#editDisplayName').on('keydown', function (e) {
  500. if (e.keyCode === 13) {
  501. e.preventDefault();
  502. inputDisplayNameHandler(this.value);
  503. }
  504. });
  505. });
  506. }
  507. }
  508. };
  509. /**
  510. * Shows/hides the display name on the remote video.
  511. * @param videoSpanId the identifier of the video span element
  512. * @param isShow indicates if the display name should be shown or hidden
  513. */
  514. my.showDisplayName = function(videoSpanId, isShow) {
  515. var nameSpan = $('#' + videoSpanId + '>span.displayname').get(0);
  516. if (isShow) {
  517. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  518. nameSpan.setAttribute("style", "display:inline-block;");
  519. }
  520. else {
  521. if (nameSpan)
  522. nameSpan.setAttribute("style", "display:none;");
  523. }
  524. };
  525. /**
  526. * Shows the presence status message for the given video.
  527. */
  528. my.setPresenceStatus = function (videoSpanId, statusMsg) {
  529. if (!$('#' + videoSpanId).length) {
  530. // No container
  531. return;
  532. }
  533. var statusSpan = $('#' + videoSpanId + '>span.status');
  534. if (!statusSpan.length) {
  535. //Add status span
  536. statusSpan = document.createElement('span');
  537. statusSpan.className = 'status';
  538. statusSpan.id = videoSpanId + '_status';
  539. $('#' + videoSpanId)[0].appendChild(statusSpan);
  540. statusSpan = $('#' + videoSpanId + '>span.status');
  541. }
  542. // Display status
  543. if (statusMsg && statusMsg.length) {
  544. $('#' + videoSpanId + '_status').text(statusMsg);
  545. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  546. }
  547. else {
  548. // Hide
  549. statusSpan.get(0).setAttribute("style", "display:none;");
  550. }
  551. };
  552. /**
  553. * Shows a visual indicator for the focus of the conference.
  554. * Currently if we're not the owner of the conference we obtain the focus
  555. * from the connection.jingle.sessions.
  556. */
  557. my.showFocusIndicator = function() {
  558. if (focus !== null) {
  559. var indicatorSpan = $('#localVideoContainer .focusindicator');
  560. if (indicatorSpan.children().length === 0)
  561. {
  562. createFocusIndicatorElement(indicatorSpan[0]);
  563. }
  564. }
  565. else if (Object.keys(connection.jingle.sessions).length > 0) {
  566. // If we're only a participant the focus will be the only session we have.
  567. var session
  568. = connection.jingle.sessions
  569. [Object.keys(connection.jingle.sessions)[0]];
  570. var focusId
  571. = 'participant_' + Strophe.getResourceFromJid(session.peerjid);
  572. var focusContainer = document.getElementById(focusId);
  573. if (!focusContainer) {
  574. console.error("No focus container!");
  575. return;
  576. }
  577. var indicatorSpan = $('#' + focusId + ' .focusindicator');
  578. if (!indicatorSpan || indicatorSpan.length === 0) {
  579. indicatorSpan = document.createElement('span');
  580. indicatorSpan.className = 'focusindicator';
  581. focusContainer.appendChild(indicatorSpan);
  582. createFocusIndicatorElement(indicatorSpan);
  583. }
  584. }
  585. };
  586. /**
  587. * Shows video muted indicator over small videos.
  588. */
  589. my.showVideoIndicator = function(videoSpanId, isMuted) {
  590. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  591. if (isMuted === 'false') {
  592. if (videoMutedSpan.length > 0) {
  593. videoMutedSpan.remove();
  594. }
  595. }
  596. else {
  597. if(videoMutedSpan.length == 0) {
  598. videoMutedSpan = document.createElement('span');
  599. videoMutedSpan.className = 'videoMuted';
  600. $('#' + videoSpanId)[0].appendChild(videoMutedSpan);
  601. var mutedIndicator = document.createElement('i');
  602. mutedIndicator.className = 'icon-camera-disabled';
  603. Util.setTooltip(mutedIndicator,
  604. "Participant has<br/>stopped the camera.",
  605. "top");
  606. videoMutedSpan.appendChild(mutedIndicator);
  607. }
  608. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  609. videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  610. videoMutedSpan.css({right: ((audioMutedSpan.length > 0)?'30px':'0px')});
  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. if(audioMutedSpan.length > 0 )
  626. return;
  627. audioMutedSpan = document.createElement('span');
  628. audioMutedSpan.className = 'audioMuted';
  629. Util.setTooltip(audioMutedSpan,
  630. "Participant is muted",
  631. "top");
  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 = $('#remoteVideos>span:visible').length;
  763. if (lastNCount && lastNCount > 0) {
  764. numvids = Math.min(lastNCount + 1, numvids);
  765. }
  766. // Remove the 3px borders arround videos and border around the remote
  767. // videos area and the 4 pixels between the local video and the others
  768. //TODO: Find out where the 4 pixels come from and remove them
  769. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70 - 4;
  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. var paddingSpan = document.createElement('span');
  944. paddingSpan.className = 'popupmenuPadding';
  945. popupmenuElement.appendChild(paddingSpan);
  946. }
  947. /**
  948. * On audio muted event.
  949. */
  950. $(document).bind('audiomuted.muc', function (event, jid, isMuted) {
  951. var videoSpanId = null;
  952. if (jid === connection.emuc.myroomjid) {
  953. videoSpanId = 'localVideoContainer';
  954. } else {
  955. VideoLayout.ensurePeerContainerExists(jid);
  956. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  957. }
  958. if (focus) {
  959. mutedAudios[jid] = isMuted;
  960. VideoLayout.updateRemoteVideoMenu(jid, isMuted);
  961. }
  962. if (videoSpanId)
  963. VideoLayout.showAudioIndicator(videoSpanId, isMuted);
  964. });
  965. /**
  966. * On video muted event.
  967. */
  968. $(document).bind('videomuted.muc', function (event, jid, isMuted) {
  969. var videoSpanId = null;
  970. if (jid === connection.emuc.myroomjid) {
  971. videoSpanId = 'localVideoContainer';
  972. } else {
  973. VideoLayout.ensurePeerContainerExists(jid);
  974. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  975. }
  976. if (videoSpanId)
  977. VideoLayout.showVideoIndicator(videoSpanId, isMuted);
  978. });
  979. /**
  980. * Display name changed.
  981. */
  982. $(document).bind('displaynamechanged',
  983. function (event, jid, displayName, status) {
  984. if (jid === 'localVideoContainer'
  985. || jid === connection.emuc.myroomjid) {
  986. setDisplayName('localVideoContainer',
  987. displayName);
  988. } else {
  989. VideoLayout.ensurePeerContainerExists(jid);
  990. setDisplayName(
  991. 'participant_' + Strophe.getResourceFromJid(jid),
  992. displayName,
  993. status);
  994. }
  995. });
  996. /**
  997. * On dominant speaker changed event.
  998. */
  999. $(document).bind('dominantspeakerchanged', function (event, resourceJid) {
  1000. // We ignore local user events.
  1001. if (resourceJid
  1002. === Strophe.getResourceFromJid(connection.emuc.myroomjid))
  1003. return;
  1004. // Update the current dominant speaker.
  1005. if (resourceJid !== currentDominantSpeaker) {
  1006. var oldSpeakerVideoSpanId = "participant_" + currentDominantSpeaker,
  1007. newSpeakerVideoSpanId = "participant_" + resourceJid;
  1008. if($("#" + oldSpeakerVideoSpanId + ">span.displayname").text() ===
  1009. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME) {
  1010. setDisplayName(oldSpeakerVideoSpanId, null);
  1011. }
  1012. if($("#" + newSpeakerVideoSpanId + ">span.displayname").text() ===
  1013. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME) {
  1014. setDisplayName(newSpeakerVideoSpanId,
  1015. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  1016. }
  1017. currentDominantSpeaker = resourceJid;
  1018. } else {
  1019. return;
  1020. }
  1021. // Obtain container for new dominant speaker.
  1022. var container = document.getElementById(
  1023. 'participant_' + resourceJid);
  1024. // Local video will not have container found, but that's ok
  1025. // since we don't want to switch to local video.
  1026. if (container && !focusedVideoSrc)
  1027. {
  1028. var video = container.getElementsByTagName("video");
  1029. // Update the large video if the video source is already available,
  1030. // otherwise wait for the "videoactive.jingle" event.
  1031. if (video.length && video[0].currentTime > 0)
  1032. VideoLayout.updateLargeVideo(video[0].src);
  1033. }
  1034. });
  1035. /**
  1036. * On last N change event.
  1037. *
  1038. * @param event the event that notified us
  1039. * @param lastNEndpoints the list of last N endpoints
  1040. * @param endpointsEnteringLastN the list currently entering last N
  1041. * endpoints
  1042. */
  1043. $(document).bind('lastnchanged', function ( event,
  1044. lastNEndpoints,
  1045. endpointsEnteringLastN,
  1046. stream) {
  1047. if (lastNCount !== lastNEndpoints.length)
  1048. lastNCount = lastNEndpoints.length;
  1049. lastNEndpointsCache = lastNEndpoints;
  1050. $('#remoteVideos>span').each(function( index, element ) {
  1051. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  1052. if (resourceJid
  1053. && lastNEndpoints.indexOf(resourceJid) < 0) {
  1054. console.log("Remove from last N", resourceJid);
  1055. showPeerContainer(resourceJid, false);
  1056. }
  1057. });
  1058. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  1059. endpointsEnteringLastN = lastNEndpoints;
  1060. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  1061. endpointsEnteringLastN.forEach(function (resourceJid) {
  1062. if (!$('#participant_' + resourceJid).is(':visible')) {
  1063. console.log("Add to last N", resourceJid);
  1064. showPeerContainer(resourceJid, true);
  1065. mediaStreams.some(function (mediaStream) {
  1066. if (mediaStream.peerjid
  1067. && Strophe.getResourceFromJid(mediaStream.peerjid)
  1068. === resourceJid
  1069. && mediaStream.type === mediaStream.VIDEO_TYPE) {
  1070. var sel = $('#participant_' + resourceJid + '>video');
  1071. var simulcast = new Simulcast();
  1072. var videoStream = simulcast.getReceivingVideoStream(mediaStream.stream);
  1073. RTC.attachMediaStream(sel, videoStream);
  1074. waitForRemoteVideo(
  1075. sel,
  1076. mediaStream.ssrc,
  1077. mediaStream.stream);
  1078. return true;
  1079. }
  1080. });
  1081. }
  1082. });
  1083. }
  1084. });
  1085. $(document).bind('videoactive.jingle', function (event, videoelem) {
  1086. if (videoelem.attr('id').indexOf('mixedmslabel') === -1) {
  1087. // ignore mixedmslabela0 and v0
  1088. videoelem.show();
  1089. VideoLayout.resizeThumbnails();
  1090. var videoParent = videoelem.parent();
  1091. var parentResourceJid = null;
  1092. if (videoParent)
  1093. parentResourceJid
  1094. = VideoLayout.getPeerContainerResourceJid(videoParent[0]);
  1095. // Update the large video to the last added video only if there's no
  1096. // current dominant or focused speaker or update it to the current
  1097. // dominant speaker.
  1098. if ((!focusedVideoSrc && !VideoLayout.getDominantSpeakerResourceJid())
  1099. || (parentResourceJid
  1100. && VideoLayout.getDominantSpeakerResourceJid()
  1101. === parentResourceJid)) {
  1102. VideoLayout.updateLargeVideo(videoelem.attr('src'), 1);
  1103. }
  1104. VideoLayout.showFocusIndicator();
  1105. }
  1106. });
  1107. $(document).bind('simulcastlayerstarted simulcastlayerstopped', function(event) {
  1108. var localVideoSelector = $('#' + 'localVideo_' + connection.jingle.localVideo.id);
  1109. var simulcast = new Simulcast();
  1110. var stream = simulcast.getLocalVideoStream();
  1111. var updateLargeVideo = (connection.emuc.myroomjid
  1112. == getJidFromVideoSrc(largeVideoNewSrc));
  1113. var updateFocusedVideoSrc = (localVideoSrc == focusedVideoSrc);
  1114. // Attach WebRTC stream
  1115. RTC.attachMediaStream(localVideoSelector, stream);
  1116. localVideoSrc = $(localVideoSelector).attr('src');
  1117. if (updateLargeVideo) {
  1118. VideoLayout.updateLargeVideo(localVideoSrc);
  1119. }
  1120. if (updateFocusedVideoSrc) {
  1121. focusedVideoSrc = localVideoSrc;
  1122. }
  1123. });
  1124. /**
  1125. * On simulcast layers changed event.
  1126. */
  1127. $(document).bind('simulcastlayerschanged', function (event, endpointSimulcastLayers) {
  1128. var simulcast = new Simulcast();
  1129. endpointSimulcastLayers.forEach(function (esl) {
  1130. var primarySSRC = esl.simulcastLayer.primarySSRC;
  1131. var msid = simulcast.getRemoteVideoStreamIdBySSRC(primarySSRC);
  1132. // Get session and stream from msid.
  1133. var session, electedStream;
  1134. var i, j, k;
  1135. if (connection.jingle) {
  1136. var keys = Object.keys(connection.jingle.sessions);
  1137. for (i = 0; i < keys.length; i++) {
  1138. var sid = keys[i];
  1139. if (electedStream) {
  1140. // stream found, stop.
  1141. break;
  1142. }
  1143. session = connection.jingle.sessions[sid];
  1144. if (session.remoteStreams) {
  1145. for (j = 0; j < session.remoteStreams.length; j++) {
  1146. var remoteStream = session.remoteStreams[j];
  1147. if (electedStream) {
  1148. // stream found, stop.
  1149. break;
  1150. }
  1151. var tracks = remoteStream.getVideoTracks();
  1152. if (tracks) {
  1153. for (k = 0; k < tracks.length; k++) {
  1154. var track = tracks[k];
  1155. if (msid === [remoteStream.id, track.id].join(' ')) {
  1156. electedStream = new webkitMediaStream([track]);
  1157. // stream found, stop.
  1158. break;
  1159. }
  1160. }
  1161. }
  1162. }
  1163. }
  1164. }
  1165. }
  1166. if (session && electedStream) {
  1167. console.info('Switching simulcast substream.');
  1168. console.info([esl, primarySSRC, msid, session, electedStream]);
  1169. var msidParts = msid.split(' ');
  1170. var selRemoteVideo = $(['#', 'remoteVideo_', session.sid, '_', msidParts[0]].join(''));
  1171. var updateLargeVideo = (ssrc2jid[videoSrcToSsrc[selRemoteVideo.attr('src')]]
  1172. == ssrc2jid[videoSrcToSsrc[largeVideoNewSrc]]);
  1173. var updateFocusedVideoSrc = (selRemoteVideo.attr('src') == focusedVideoSrc);
  1174. var electedStreamUrl = webkitURL.createObjectURL(electedStream);
  1175. selRemoteVideo.attr('src', electedStreamUrl);
  1176. videoSrcToSsrc[selRemoteVideo.attr('src')] = primarySSRC;
  1177. if (updateLargeVideo) {
  1178. VideoLayout.updateLargeVideo(electedStreamUrl);
  1179. }
  1180. if (updateFocusedVideoSrc) {
  1181. focusedVideoSrc = electedStreamUrl;
  1182. }
  1183. } else {
  1184. console.error('Could not find a stream or a session.', session, electedStream);
  1185. }
  1186. });
  1187. });
  1188. return my;
  1189. }(VideoLayout || {}));