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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  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. ContactList.setClickable(resourceJid, isShow);
  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(interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  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 = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  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. if(videoMutedSpan.length == 0) {
  599. videoMutedSpan = document.createElement('span');
  600. videoMutedSpan.className = 'videoMuted';
  601. $('#' + videoSpanId)[0].appendChild(videoMutedSpan);
  602. var mutedIndicator = document.createElement('i');
  603. mutedIndicator.className = 'icon-camera-disabled';
  604. Util.setTooltip(mutedIndicator,
  605. "Participant has<br/>stopped the camera.",
  606. "top");
  607. videoMutedSpan.appendChild(mutedIndicator);
  608. }
  609. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  610. videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  611. videoMutedSpan.css({right: ((audioMutedSpan.length > 0)?'30px':'0px')});
  612. }
  613. };
  614. /**
  615. * Shows audio muted indicator over small videos.
  616. */
  617. my.showAudioIndicator = function(videoSpanId, isMuted) {
  618. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  619. if (isMuted === 'false') {
  620. if (audioMutedSpan.length > 0) {
  621. audioMutedSpan.popover('hide');
  622. audioMutedSpan.remove();
  623. }
  624. }
  625. else {
  626. if(audioMutedSpan.length > 0 )
  627. return;
  628. audioMutedSpan = document.createElement('span');
  629. audioMutedSpan.className = 'audioMuted';
  630. Util.setTooltip(audioMutedSpan,
  631. "Participant is muted",
  632. "top");
  633. $('#' + videoSpanId)[0].appendChild(audioMutedSpan);
  634. var mutedIndicator = document.createElement('i');
  635. mutedIndicator.className = 'icon-mic-disabled';
  636. audioMutedSpan.appendChild(mutedIndicator);
  637. }
  638. };
  639. /**
  640. * Resizes the large video container.
  641. */
  642. my.resizeLargeVideoContainer = function () {
  643. Chat.resizeChat();
  644. var availableHeight = window.innerHeight;
  645. var availableWidth = Util.getAvailableVideoWidth();
  646. if (availableWidth < 0 || availableHeight < 0) return;
  647. $('#videospace').width(availableWidth);
  648. $('#videospace').height(availableHeight);
  649. $('#largeVideoContainer').width(availableWidth);
  650. $('#largeVideoContainer').height(availableHeight);
  651. VideoLayout.resizeThumbnails();
  652. };
  653. /**
  654. * Resizes thumbnails.
  655. */
  656. my.resizeThumbnails = function() {
  657. var videoSpaceWidth = $('#remoteVideos').width();
  658. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  659. var width = thumbnailSize[0];
  660. var height = thumbnailSize[1];
  661. // size videos so that while keeping AR and max height, we have a
  662. // nice fit
  663. $('#remoteVideos').height(height);
  664. $('#remoteVideos>span').width(width);
  665. $('#remoteVideos>span').height(height);
  666. $(document).trigger("remotevideo.resized", [width, height]);
  667. };
  668. /**
  669. * Enables the dominant speaker UI.
  670. *
  671. * @param resourceJid the jid indicating the video element to
  672. * activate/deactivate
  673. * @param isEnable indicates if the dominant speaker should be enabled or
  674. * disabled
  675. */
  676. my.enableDominantSpeaker = function(resourceJid, isEnable) {
  677. var displayName = resourceJid;
  678. var nameSpan = $('#participant_' + resourceJid + '>span.displayname');
  679. if (nameSpan.length > 0)
  680. displayName = nameSpan.text();
  681. console.log("UI enable dominant speaker",
  682. displayName,
  683. resourceJid,
  684. isEnable);
  685. var videoSpanId = null;
  686. var videoContainerId = null;
  687. if (resourceJid
  688. === Strophe.getResourceFromJid(connection.emuc.myroomjid)) {
  689. videoSpanId = 'localVideoWrapper';
  690. videoContainerId = 'localVideoContainer';
  691. }
  692. else {
  693. videoSpanId = 'participant_' + resourceJid;
  694. videoContainerId = videoSpanId;
  695. }
  696. videoSpan = document.getElementById(videoContainerId);
  697. if (!videoSpan) {
  698. console.error("No video element for jid", resourceJid);
  699. return;
  700. }
  701. var video = $('#' + videoSpanId + '>video');
  702. if (video && video.length > 0) {
  703. if (isEnable) {
  704. VideoLayout.showDisplayName(videoContainerId, true);
  705. if (!videoSpan.classList.contains("dominantspeaker"))
  706. videoSpan.classList.add("dominantspeaker");
  707. video.css({visibility: 'hidden'});
  708. }
  709. else {
  710. VideoLayout.showDisplayName(videoContainerId, false);
  711. if (videoSpan.classList.contains("dominantspeaker"))
  712. videoSpan.classList.remove("dominantspeaker");
  713. video.css({visibility: 'visible'});
  714. }
  715. }
  716. };
  717. /**
  718. * Gets the selector of video thumbnail container for the user identified by
  719. * given <tt>userJid</tt>
  720. * @param userJid user's Jid for whom we want to get the video container.
  721. */
  722. function getParticipantContainer(userJid)
  723. {
  724. if (!userJid)
  725. return null;
  726. if (userJid === connection.emuc.myroomjid)
  727. return $("#localVideoContainer");
  728. else
  729. return $("#participant_" + Strophe.getResourceFromJid(userJid));
  730. }
  731. /**
  732. * Sets the size and position of the given video element.
  733. *
  734. * @param video the video element to position
  735. * @param width the desired video width
  736. * @param height the desired video height
  737. * @param horizontalIndent the left and right indent
  738. * @param verticalIndent the top and bottom indent
  739. */
  740. function positionVideo(video,
  741. width,
  742. height,
  743. horizontalIndent,
  744. verticalIndent) {
  745. video.width(width);
  746. video.height(height);
  747. video.css({ top: verticalIndent + 'px',
  748. bottom: verticalIndent + 'px',
  749. left: horizontalIndent + 'px',
  750. right: horizontalIndent + 'px'});
  751. }
  752. /**
  753. * Calculates the thumbnail size.
  754. *
  755. * @param videoSpaceWidth the width of the video space
  756. */
  757. my.calculateThumbnailSize = function (videoSpaceWidth) {
  758. // Calculate the available height, which is the inner window height minus
  759. // 39px for the header minus 2px for the delimiter lines on the top and
  760. // bottom of the large video, minus the 36px space inside the remoteVideos
  761. // container used for highlighting shadow.
  762. var availableHeight = 100;
  763. var numvids = $('#remoteVideos>span:visible').length;
  764. if (lastNCount && lastNCount > 0) {
  765. numvids = Math.min(lastNCount + 1, numvids);
  766. }
  767. // Remove the 3px borders arround videos and border around the remote
  768. // videos area and the 4 pixels between the local video and the others
  769. //TODO: Find out where the 4 pixels come from and remove them
  770. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70 - 4;
  771. var availableWidth = availableWinWidth / numvids;
  772. var aspectRatio = 16.0 / 9.0;
  773. var maxHeight = Math.min(160, availableHeight);
  774. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  775. if (availableHeight < availableWidth / aspectRatio) {
  776. availableWidth = Math.floor(availableHeight * aspectRatio);
  777. }
  778. return [availableWidth, availableHeight];
  779. };
  780. /**
  781. * Returns an array of the video dimensions, so that it keeps it's aspect
  782. * ratio and fits available area with it's larger dimension. This method
  783. * ensures that whole video will be visible and can leave empty areas.
  784. *
  785. * @return an array with 2 elements, the video width and the video height
  786. */
  787. function getDesktopVideoSize(videoWidth,
  788. videoHeight,
  789. videoSpaceWidth,
  790. videoSpaceHeight) {
  791. if (!videoWidth)
  792. videoWidth = currentVideoWidth;
  793. if (!videoHeight)
  794. videoHeight = currentVideoHeight;
  795. var aspectRatio = videoWidth / videoHeight;
  796. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  797. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  798. videoSpaceHeight -= $('#remoteVideos').outerHeight();
  799. if (availableWidth / aspectRatio >= videoSpaceHeight)
  800. {
  801. availableHeight = videoSpaceHeight;
  802. availableWidth = availableHeight * aspectRatio;
  803. }
  804. if (availableHeight * aspectRatio >= videoSpaceWidth)
  805. {
  806. availableWidth = videoSpaceWidth;
  807. availableHeight = availableWidth / aspectRatio;
  808. }
  809. return [availableWidth, availableHeight];
  810. }
  811. /**
  812. * Creates the edit display name button.
  813. *
  814. * @returns the edit button
  815. */
  816. function createEditDisplayNameButton() {
  817. var editButton = document.createElement('a');
  818. editButton.className = 'displayname';
  819. Util.setTooltip(editButton,
  820. 'Click to edit your<br/>display name',
  821. "top");
  822. editButton.innerHTML = '<i class="fa fa-pencil"></i>';
  823. return editButton;
  824. }
  825. /**
  826. * Creates the element indicating the focus of the conference.
  827. *
  828. * @param parentElement the parent element where the focus indicator will
  829. * be added
  830. */
  831. function createFocusIndicatorElement(parentElement) {
  832. var focusIndicator = document.createElement('i');
  833. focusIndicator.className = 'fa fa-star';
  834. parentElement.appendChild(focusIndicator);
  835. Util.setTooltip(parentElement,
  836. "The owner of<br/>this conference",
  837. "top");
  838. }
  839. /**
  840. * Updates the remote video menu.
  841. *
  842. * @param jid the jid indicating the video for which we're adding a menu.
  843. * @param isMuted indicates the current mute state
  844. */
  845. my.updateRemoteVideoMenu = function(jid, isMuted) {
  846. var muteMenuItem
  847. = $('#remote_popupmenu_'
  848. + Strophe.getResourceFromJid(jid)
  849. + '>li>a.mutelink');
  850. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  851. if (muteMenuItem.length) {
  852. var muteLink = muteMenuItem.get(0);
  853. if (isMuted === 'true') {
  854. muteLink.innerHTML = mutedIndicator + ' Muted';
  855. muteLink.className = 'mutelink disabled';
  856. }
  857. else {
  858. muteLink.innerHTML = mutedIndicator + ' Mute';
  859. muteLink.className = 'mutelink';
  860. }
  861. }
  862. };
  863. /**
  864. * Returns the current dominant speaker resource jid.
  865. */
  866. my.getDominantSpeakerResourceJid = function () {
  867. return currentDominantSpeaker;
  868. };
  869. /**
  870. * Returns the corresponding resource jid to the given peer container
  871. * DOM element.
  872. *
  873. * @return the corresponding resource jid to the given peer container
  874. * DOM element
  875. */
  876. my.getPeerContainerResourceJid = function (containerElement) {
  877. var i = containerElement.id.indexOf('participant_');
  878. if (i >= 0)
  879. return containerElement.id.substring(i + 12);
  880. };
  881. /**
  882. * Adds the remote video menu element for the given <tt>jid</tt> in the
  883. * given <tt>parentElement</tt>.
  884. *
  885. * @param jid the jid indicating the video for which we're adding a menu.
  886. * @param parentElement the parent element where this menu will be added
  887. */
  888. function addRemoteVideoMenu(jid, parentElement) {
  889. var spanElement = document.createElement('span');
  890. spanElement.className = 'remotevideomenu';
  891. parentElement.appendChild(spanElement);
  892. var menuElement = document.createElement('i');
  893. menuElement.className = 'fa fa-angle-down';
  894. menuElement.title = 'Remote user controls';
  895. spanElement.appendChild(menuElement);
  896. // <ul class="popupmenu">
  897. // <li><a href="#">Mute</a></li>
  898. // <li><a href="#">Eject</a></li>
  899. // </ul>
  900. var popupmenuElement = document.createElement('ul');
  901. popupmenuElement.className = 'popupmenu';
  902. popupmenuElement.id
  903. = 'remote_popupmenu_' + Strophe.getResourceFromJid(jid);
  904. spanElement.appendChild(popupmenuElement);
  905. var muteMenuItem = document.createElement('li');
  906. var muteLinkItem = document.createElement('a');
  907. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  908. if (!mutedAudios[jid]) {
  909. muteLinkItem.innerHTML = mutedIndicator + 'Mute';
  910. muteLinkItem.className = 'mutelink';
  911. }
  912. else {
  913. muteLinkItem.innerHTML = mutedIndicator + ' Muted';
  914. muteLinkItem.className = 'mutelink disabled';
  915. }
  916. muteLinkItem.onclick = function(){
  917. if ($(this).attr('disabled') != undefined) {
  918. event.preventDefault();
  919. }
  920. var isMute = !mutedAudios[jid];
  921. connection.moderate.setMute(jid, isMute);
  922. popupmenuElement.setAttribute('style', 'display:none;');
  923. if (isMute) {
  924. this.innerHTML = mutedIndicator + ' Muted';
  925. this.className = 'mutelink disabled';
  926. }
  927. else {
  928. this.innerHTML = mutedIndicator + ' Mute';
  929. this.className = 'mutelink';
  930. }
  931. };
  932. muteMenuItem.appendChild(muteLinkItem);
  933. popupmenuElement.appendChild(muteMenuItem);
  934. var ejectIndicator = "<i class='fa fa-eject'></i>";
  935. var ejectMenuItem = document.createElement('li');
  936. var ejectLinkItem = document.createElement('a');
  937. ejectLinkItem.innerHTML = ejectIndicator + ' Kick out';
  938. ejectLinkItem.onclick = function(){
  939. connection.moderate.eject(jid);
  940. popupmenuElement.setAttribute('style', 'display:none;');
  941. };
  942. ejectMenuItem.appendChild(ejectLinkItem);
  943. popupmenuElement.appendChild(ejectMenuItem);
  944. var paddingSpan = document.createElement('span');
  945. paddingSpan.className = 'popupmenuPadding';
  946. popupmenuElement.appendChild(paddingSpan);
  947. }
  948. /**
  949. * On audio muted event.
  950. */
  951. $(document).bind('audiomuted.muc', function (event, jid, isMuted) {
  952. var videoSpanId = null;
  953. if (jid === connection.emuc.myroomjid) {
  954. videoSpanId = 'localVideoContainer';
  955. } else {
  956. VideoLayout.ensurePeerContainerExists(jid);
  957. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  958. }
  959. if (focus) {
  960. mutedAudios[jid] = isMuted;
  961. VideoLayout.updateRemoteVideoMenu(jid, isMuted);
  962. }
  963. if (videoSpanId)
  964. VideoLayout.showAudioIndicator(videoSpanId, isMuted);
  965. });
  966. /**
  967. * On video muted event.
  968. */
  969. $(document).bind('videomuted.muc', function (event, jid, isMuted) {
  970. var videoSpanId = null;
  971. if (jid === connection.emuc.myroomjid) {
  972. videoSpanId = 'localVideoContainer';
  973. } else {
  974. VideoLayout.ensurePeerContainerExists(jid);
  975. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  976. }
  977. if (videoSpanId)
  978. VideoLayout.showVideoIndicator(videoSpanId, isMuted);
  979. });
  980. /**
  981. * Display name changed.
  982. */
  983. $(document).bind('displaynamechanged',
  984. function (event, jid, displayName, status) {
  985. if (jid === 'localVideoContainer'
  986. || jid === connection.emuc.myroomjid) {
  987. setDisplayName('localVideoContainer',
  988. displayName);
  989. } else {
  990. VideoLayout.ensurePeerContainerExists(jid);
  991. setDisplayName(
  992. 'participant_' + Strophe.getResourceFromJid(jid),
  993. displayName,
  994. status);
  995. }
  996. });
  997. /**
  998. * On dominant speaker changed event.
  999. */
  1000. $(document).bind('dominantspeakerchanged', function (event, resourceJid) {
  1001. // We ignore local user events.
  1002. if (resourceJid
  1003. === Strophe.getResourceFromJid(connection.emuc.myroomjid))
  1004. return;
  1005. // Update the current dominant speaker.
  1006. if (resourceJid !== currentDominantSpeaker) {
  1007. var oldSpeakerVideoSpanId = "participant_" + currentDominantSpeaker,
  1008. newSpeakerVideoSpanId = "participant_" + resourceJid;
  1009. if($("#" + oldSpeakerVideoSpanId + ">span.displayname").text() ===
  1010. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME) {
  1011. setDisplayName(oldSpeakerVideoSpanId, null);
  1012. }
  1013. if($("#" + newSpeakerVideoSpanId + ">span.displayname").text() ===
  1014. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME) {
  1015. setDisplayName(newSpeakerVideoSpanId,
  1016. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  1017. }
  1018. currentDominantSpeaker = resourceJid;
  1019. } else {
  1020. return;
  1021. }
  1022. // Obtain container for new dominant speaker.
  1023. var container = document.getElementById(
  1024. 'participant_' + resourceJid);
  1025. // Local video will not have container found, but that's ok
  1026. // since we don't want to switch to local video.
  1027. if (container && !focusedVideoSrc)
  1028. {
  1029. var video = container.getElementsByTagName("video");
  1030. // Update the large video if the video source is already available,
  1031. // otherwise wait for the "videoactive.jingle" event.
  1032. if (video.length && video[0].currentTime > 0)
  1033. VideoLayout.updateLargeVideo(video[0].src);
  1034. }
  1035. });
  1036. /**
  1037. * On last N change event.
  1038. *
  1039. * @param event the event that notified us
  1040. * @param lastNEndpoints the list of last N endpoints
  1041. * @param endpointsEnteringLastN the list currently entering last N
  1042. * endpoints
  1043. */
  1044. $(document).bind('lastnchanged', function ( event,
  1045. lastNEndpoints,
  1046. endpointsEnteringLastN,
  1047. stream) {
  1048. if (lastNCount !== lastNEndpoints.length)
  1049. lastNCount = lastNEndpoints.length;
  1050. lastNEndpointsCache = lastNEndpoints;
  1051. $('#remoteVideos>span').each(function( index, element ) {
  1052. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  1053. if (resourceJid
  1054. && lastNEndpoints.indexOf(resourceJid) < 0) {
  1055. console.log("Remove from last N", resourceJid);
  1056. showPeerContainer(resourceJid, false);
  1057. }
  1058. });
  1059. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  1060. endpointsEnteringLastN = lastNEndpoints;
  1061. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  1062. endpointsEnteringLastN.forEach(function (resourceJid) {
  1063. if (!$('#participant_' + resourceJid).is(':visible')) {
  1064. console.log("Add to last N", resourceJid);
  1065. showPeerContainer(resourceJid, true);
  1066. mediaStreams.some(function (mediaStream) {
  1067. if (mediaStream.peerjid
  1068. && Strophe.getResourceFromJid(mediaStream.peerjid)
  1069. === resourceJid
  1070. && mediaStream.type === mediaStream.VIDEO_TYPE) {
  1071. var sel = $('#participant_' + resourceJid + '>video');
  1072. var simulcast = new Simulcast();
  1073. var videoStream = simulcast.getReceivingVideoStream(mediaStream.stream);
  1074. RTC.attachMediaStream(sel, videoStream);
  1075. waitForRemoteVideo(
  1076. sel,
  1077. mediaStream.ssrc,
  1078. mediaStream.stream);
  1079. return true;
  1080. }
  1081. });
  1082. }
  1083. });
  1084. }
  1085. });
  1086. $(document).bind('videoactive.jingle', function (event, videoelem) {
  1087. if (videoelem.attr('id').indexOf('mixedmslabel') === -1) {
  1088. // ignore mixedmslabela0 and v0
  1089. videoelem.show();
  1090. VideoLayout.resizeThumbnails();
  1091. var videoParent = videoelem.parent();
  1092. var parentResourceJid = null;
  1093. if (videoParent)
  1094. parentResourceJid
  1095. = VideoLayout.getPeerContainerResourceJid(videoParent[0]);
  1096. // Update the large video to the last added video only if there's no
  1097. // current dominant or focused speaker or update it to the current
  1098. // dominant speaker.
  1099. if ((!focusedVideoSrc && !VideoLayout.getDominantSpeakerResourceJid())
  1100. || (parentResourceJid
  1101. && VideoLayout.getDominantSpeakerResourceJid()
  1102. === parentResourceJid)) {
  1103. VideoLayout.updateLargeVideo(videoelem.attr('src'), 1);
  1104. }
  1105. VideoLayout.showFocusIndicator();
  1106. }
  1107. });
  1108. $(document).bind('simulcastlayerstarted simulcastlayerstopped', function(event) {
  1109. var localVideoSelector = $('#' + 'localVideo_' + connection.jingle.localVideo.id);
  1110. var simulcast = new Simulcast();
  1111. var stream = simulcast.getLocalVideoStream();
  1112. var updateLargeVideo = (connection.emuc.myroomjid
  1113. == getJidFromVideoSrc(largeVideoNewSrc));
  1114. var updateFocusedVideoSrc = (localVideoSrc == focusedVideoSrc);
  1115. // Attach WebRTC stream
  1116. RTC.attachMediaStream(localVideoSelector, stream);
  1117. localVideoSrc = $(localVideoSelector).attr('src');
  1118. if (updateLargeVideo) {
  1119. VideoLayout.updateLargeVideo(localVideoSrc);
  1120. }
  1121. if (updateFocusedVideoSrc) {
  1122. focusedVideoSrc = localVideoSrc;
  1123. }
  1124. });
  1125. /**
  1126. * On simulcast layers changed event.
  1127. */
  1128. $(document).bind('simulcastlayerschanged', function (event, endpointSimulcastLayers) {
  1129. var simulcast = new Simulcast();
  1130. endpointSimulcastLayers.forEach(function (esl) {
  1131. var primarySSRC = esl.simulcastLayer.primarySSRC;
  1132. var msid = simulcast.getRemoteVideoStreamIdBySSRC(primarySSRC);
  1133. // Get session and stream from msid.
  1134. var session, electedStream;
  1135. var i, j, k;
  1136. if (connection.jingle) {
  1137. var keys = Object.keys(connection.jingle.sessions);
  1138. for (i = 0; i < keys.length; i++) {
  1139. var sid = keys[i];
  1140. if (electedStream) {
  1141. // stream found, stop.
  1142. break;
  1143. }
  1144. session = connection.jingle.sessions[sid];
  1145. if (session.remoteStreams) {
  1146. for (j = 0; j < session.remoteStreams.length; j++) {
  1147. var remoteStream = session.remoteStreams[j];
  1148. if (electedStream) {
  1149. // stream found, stop.
  1150. break;
  1151. }
  1152. var tracks = remoteStream.getVideoTracks();
  1153. if (tracks) {
  1154. for (k = 0; k < tracks.length; k++) {
  1155. var track = tracks[k];
  1156. if (msid === [remoteStream.id, track.id].join(' ')) {
  1157. electedStream = new webkitMediaStream([track]);
  1158. // stream found, stop.
  1159. break;
  1160. }
  1161. }
  1162. }
  1163. }
  1164. }
  1165. }
  1166. }
  1167. if (session && electedStream) {
  1168. console.info('Switching simulcast substream.');
  1169. console.info([esl, primarySSRC, msid, session, electedStream]);
  1170. var msidParts = msid.split(' ');
  1171. var selRemoteVideo = $(['#', 'remoteVideo_', session.sid, '_', msidParts[0]].join(''));
  1172. var updateLargeVideo = (ssrc2jid[videoSrcToSsrc[selRemoteVideo.attr('src')]]
  1173. == ssrc2jid[videoSrcToSsrc[largeVideoNewSrc]]);
  1174. var updateFocusedVideoSrc = (selRemoteVideo.attr('src') == focusedVideoSrc);
  1175. var electedStreamUrl = webkitURL.createObjectURL(electedStream);
  1176. selRemoteVideo.attr('src', electedStreamUrl);
  1177. videoSrcToSsrc[selRemoteVideo.attr('src')] = primarySSRC;
  1178. if (updateLargeVideo) {
  1179. VideoLayout.updateLargeVideo(electedStreamUrl);
  1180. }
  1181. if (updateFocusedVideoSrc) {
  1182. focusedVideoSrc = electedStreamUrl;
  1183. }
  1184. } else {
  1185. console.error('Could not find a stream or a session.', session, electedStream);
  1186. }
  1187. });
  1188. });
  1189. return my;
  1190. }(VideoLayout || {}));