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

videolayout.js 51KB

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