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 76KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  1. var AudioLevels = require("../audio_levels/AudioLevels");
  2. var Avatar = require("../avatar/Avatar");
  3. var Chat = require("../side_pannels/chat/Chat");
  4. var ContactList = require("../side_pannels/contactlist/ContactList");
  5. var UIUtil = require("../util/UIUtil");
  6. var ConnectionIndicator = require("./ConnectionIndicator");
  7. var currentDominantSpeaker = null;
  8. var lastNCount = config.channelLastN;
  9. var localLastNCount = config.channelLastN;
  10. var localLastNSet = [];
  11. var lastNEndpointsCache = [];
  12. var lastNPickupJid = null;
  13. var largeVideoState = {
  14. updateInProgress: false,
  15. newSrc: ''
  16. };
  17. var defaultLocalDisplayName = "Me";
  18. /**
  19. * Returns an array of the video horizontal and vertical indents,
  20. * so that if fits its parent.
  21. *
  22. * @return an array with 2 elements, the horizontal indent and the vertical
  23. * indent
  24. */
  25. function getCameraVideoPosition(videoWidth,
  26. videoHeight,
  27. videoSpaceWidth,
  28. videoSpaceHeight) {
  29. // Parent height isn't completely calculated when we position the video in
  30. // full screen mode and this is why we use the screen height in this case.
  31. // Need to think it further at some point and implement it properly.
  32. var isFullScreen = document.fullScreen ||
  33. document.mozFullScreen ||
  34. document.webkitIsFullScreen;
  35. if (isFullScreen)
  36. videoSpaceHeight = window.innerHeight;
  37. var horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  38. var verticalIndent = (videoSpaceHeight - videoHeight) / 2;
  39. return [horizontalIndent, verticalIndent];
  40. }
  41. /**
  42. * Returns an array of the video horizontal and vertical indents.
  43. * Centers horizontally and top aligns vertically.
  44. *
  45. * @return an array with 2 elements, the horizontal indent and the vertical
  46. * indent
  47. */
  48. function getDesktopVideoPosition(videoWidth,
  49. videoHeight,
  50. videoSpaceWidth,
  51. videoSpaceHeight) {
  52. var horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  53. var verticalIndent = 0;// Top aligned
  54. return [horizontalIndent, verticalIndent];
  55. }
  56. /**
  57. * Returns an array of the video dimensions, so that it covers the screen.
  58. * It leaves no empty areas, but some parts of the video might not be visible.
  59. *
  60. * @return an array with 2 elements, the video width and the video height
  61. */
  62. function getCameraVideoSize(videoWidth,
  63. videoHeight,
  64. videoSpaceWidth,
  65. videoSpaceHeight) {
  66. if (!videoWidth)
  67. videoWidth = currentVideoWidth;
  68. if (!videoHeight)
  69. videoHeight = currentVideoHeight;
  70. var aspectRatio = videoWidth / videoHeight;
  71. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  72. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  73. if (availableWidth / aspectRatio < videoSpaceHeight) {
  74. availableHeight = videoSpaceHeight;
  75. availableWidth = availableHeight * aspectRatio;
  76. }
  77. if (availableHeight * aspectRatio < videoSpaceWidth) {
  78. availableWidth = videoSpaceWidth;
  79. availableHeight = availableWidth / aspectRatio;
  80. }
  81. return [availableWidth, availableHeight];
  82. }
  83. /**
  84. * Sets the display name for the given video span id.
  85. */
  86. function setDisplayName(videoSpanId, displayName) {
  87. var nameSpan = $('#' + videoSpanId + '>span.displayname');
  88. var defaultLocalDisplayName = interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME;
  89. // If we already have a display name for this video.
  90. if (nameSpan.length > 0) {
  91. var nameSpanElement = nameSpan.get(0);
  92. if (nameSpanElement.id === 'localDisplayName' &&
  93. $('#localDisplayName').text() !== displayName) {
  94. if (displayName && displayName.length > 0)
  95. $('#localDisplayName').html(displayName + ' (me)');
  96. else
  97. $('#localDisplayName').text(defaultLocalDisplayName);
  98. } else {
  99. if (displayName && displayName.length > 0)
  100. $('#' + videoSpanId + '_name').html(displayName);
  101. else
  102. $('#' + videoSpanId + '_name').text(interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  103. }
  104. } else {
  105. var editButton = null;
  106. nameSpan = document.createElement('span');
  107. nameSpan.className = 'displayname';
  108. $('#' + videoSpanId)[0].appendChild(nameSpan);
  109. if (videoSpanId === 'localVideoContainer') {
  110. editButton = createEditDisplayNameButton();
  111. nameSpan.innerText = defaultLocalDisplayName;
  112. }
  113. else {
  114. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  115. }
  116. if (displayName && displayName.length > 0) {
  117. nameSpan.innerText = displayName;
  118. }
  119. if (!editButton) {
  120. nameSpan.id = videoSpanId + '_name';
  121. } else {
  122. nameSpan.id = 'localDisplayName';
  123. $('#' + videoSpanId)[0].appendChild(editButton);
  124. var editableText = document.createElement('input');
  125. editableText.className = 'displayname';
  126. editableText.type = 'text';
  127. editableText.id = 'editDisplayName';
  128. if (displayName && displayName.length) {
  129. editableText.value
  130. = displayName.substring(0, displayName.indexOf(' (me)'));
  131. }
  132. editableText.setAttribute('style', 'display:none;');
  133. editableText.setAttribute('placeholder', 'ex. Jane Pink');
  134. $('#' + videoSpanId)[0].appendChild(editableText);
  135. $('#localVideoContainer .displayname')
  136. .bind("click", function (e) {
  137. e.preventDefault();
  138. e.stopPropagation();
  139. $('#localDisplayName').hide();
  140. $('#editDisplayName').show();
  141. $('#editDisplayName').focus();
  142. $('#editDisplayName').select();
  143. $('#editDisplayName').one("focusout", function (e) {
  144. VideoLayout.inputDisplayNameHandler(this.value);
  145. });
  146. $('#editDisplayName').on('keydown', function (e) {
  147. if (e.keyCode === 13) {
  148. e.preventDefault();
  149. VideoLayout.inputDisplayNameHandler(this.value);
  150. }
  151. });
  152. });
  153. }
  154. }
  155. }
  156. /**
  157. * Gets the selector of video thumbnail container for the user identified by
  158. * given <tt>userJid</tt>
  159. * @param resourceJid user's Jid for whom we want to get the video container.
  160. */
  161. function getParticipantContainer(resourceJid)
  162. {
  163. if (!resourceJid)
  164. return null;
  165. if (resourceJid === Strophe.getResourceFromJid(connection.emuc.myroomjid))
  166. return $("#localVideoContainer");
  167. else
  168. return $("#participant_" + resourceJid);
  169. }
  170. /**
  171. * Sets the size and position of the given video element.
  172. *
  173. * @param video the video element to position
  174. * @param width the desired video width
  175. * @param height the desired video height
  176. * @param horizontalIndent the left and right indent
  177. * @param verticalIndent the top and bottom indent
  178. */
  179. function positionVideo(video,
  180. width,
  181. height,
  182. horizontalIndent,
  183. verticalIndent) {
  184. video.width(width);
  185. video.height(height);
  186. video.css({ top: verticalIndent + 'px',
  187. bottom: verticalIndent + 'px',
  188. left: horizontalIndent + 'px',
  189. right: horizontalIndent + 'px'});
  190. }
  191. /**
  192. * Adds the remote video menu element for the given <tt>jid</tt> in the
  193. * given <tt>parentElement</tt>.
  194. *
  195. * @param jid the jid indicating the video for which we're adding a menu.
  196. * @param parentElement the parent element where this menu will be added
  197. */
  198. function addRemoteVideoMenu(jid, parentElement) {
  199. var spanElement = document.createElement('span');
  200. spanElement.className = 'remotevideomenu';
  201. parentElement.appendChild(spanElement);
  202. var menuElement = document.createElement('i');
  203. menuElement.className = 'fa fa-angle-down';
  204. menuElement.title = 'Remote user controls';
  205. spanElement.appendChild(menuElement);
  206. // <ul class="popupmenu">
  207. // <li><a href="#">Mute</a></li>
  208. // <li><a href="#">Eject</a></li>
  209. // </ul>
  210. var popupmenuElement = document.createElement('ul');
  211. popupmenuElement.className = 'popupmenu';
  212. popupmenuElement.id
  213. = 'remote_popupmenu_' + Strophe.getResourceFromJid(jid);
  214. spanElement.appendChild(popupmenuElement);
  215. var muteMenuItem = document.createElement('li');
  216. var muteLinkItem = document.createElement('a');
  217. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  218. if (!mutedAudios[jid]) {
  219. muteLinkItem.innerHTML = mutedIndicator + 'Mute';
  220. muteLinkItem.className = 'mutelink';
  221. }
  222. else {
  223. muteLinkItem.innerHTML = mutedIndicator + ' Muted';
  224. muteLinkItem.className = 'mutelink disabled';
  225. }
  226. muteLinkItem.onclick = function(){
  227. if ($(this).attr('disabled') != undefined) {
  228. event.preventDefault();
  229. }
  230. var isMute = mutedAudios[jid] == true;
  231. connection.moderate.setMute(jid, !isMute);
  232. popupmenuElement.setAttribute('style', 'display:none;');
  233. if (isMute) {
  234. this.innerHTML = mutedIndicator + ' Muted';
  235. this.className = 'mutelink disabled';
  236. }
  237. else {
  238. this.innerHTML = mutedIndicator + ' Mute';
  239. this.className = 'mutelink';
  240. }
  241. };
  242. muteMenuItem.appendChild(muteLinkItem);
  243. popupmenuElement.appendChild(muteMenuItem);
  244. var ejectIndicator = "<i class='fa fa-eject'></i>";
  245. var ejectMenuItem = document.createElement('li');
  246. var ejectLinkItem = document.createElement('a');
  247. ejectLinkItem.innerHTML = ejectIndicator + ' Kick out';
  248. ejectLinkItem.onclick = function(){
  249. connection.moderate.eject(jid);
  250. popupmenuElement.setAttribute('style', 'display:none;');
  251. };
  252. ejectMenuItem.appendChild(ejectLinkItem);
  253. popupmenuElement.appendChild(ejectMenuItem);
  254. var paddingSpan = document.createElement('span');
  255. paddingSpan.className = 'popupmenuPadding';
  256. popupmenuElement.appendChild(paddingSpan);
  257. }
  258. /**
  259. * Removes remote video menu element from video element identified by
  260. * given <tt>videoElementId</tt>.
  261. *
  262. * @param videoElementId the id of local or remote video element.
  263. */
  264. function removeRemoteVideoMenu(videoElementId) {
  265. var menuSpan = $('#' + videoElementId + '>span.remotevideomenu');
  266. if (menuSpan.length) {
  267. menuSpan.remove();
  268. }
  269. }
  270. /**
  271. * Updates the data for the indicator
  272. * @param id the id of the indicator
  273. * @param percent the percent for connection quality
  274. * @param object the data
  275. */
  276. function updateStatsIndicator(id, percent, object) {
  277. if(VideoLayout.connectionIndicators[id])
  278. VideoLayout.connectionIndicators[id].updateConnectionQuality(percent, object);
  279. }
  280. /**
  281. * Returns an array of the video dimensions, so that it keeps it's aspect
  282. * ratio and fits available area with it's larger dimension. This method
  283. * ensures that whole video will be visible and can leave empty areas.
  284. *
  285. * @return an array with 2 elements, the video width and the video height
  286. */
  287. function getDesktopVideoSize(videoWidth,
  288. videoHeight,
  289. videoSpaceWidth,
  290. videoSpaceHeight) {
  291. if (!videoWidth)
  292. videoWidth = currentVideoWidth;
  293. if (!videoHeight)
  294. videoHeight = currentVideoHeight;
  295. var aspectRatio = videoWidth / videoHeight;
  296. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  297. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  298. videoSpaceHeight -= $('#remoteVideos').outerHeight();
  299. if (availableWidth / aspectRatio >= videoSpaceHeight)
  300. {
  301. availableHeight = videoSpaceHeight;
  302. availableWidth = availableHeight * aspectRatio;
  303. }
  304. if (availableHeight * aspectRatio >= videoSpaceWidth)
  305. {
  306. availableWidth = videoSpaceWidth;
  307. availableHeight = availableWidth / aspectRatio;
  308. }
  309. return [availableWidth, availableHeight];
  310. }
  311. /**
  312. * Creates the edit display name button.
  313. *
  314. * @returns the edit button
  315. */
  316. function createEditDisplayNameButton() {
  317. var editButton = document.createElement('a');
  318. editButton.className = 'displayname';
  319. Util.setTooltip(editButton,
  320. 'Click to edit your<br/>display name',
  321. "top");
  322. editButton.innerHTML = '<i class="fa fa-pencil"></i>';
  323. return editButton;
  324. }
  325. /**
  326. * Creates the element indicating the moderator(owner) of the conference.
  327. *
  328. * @param parentElement the parent element where the owner indicator will
  329. * be added
  330. */
  331. function createModeratorIndicatorElement(parentElement) {
  332. var moderatorIndicator = document.createElement('i');
  333. moderatorIndicator.className = 'fa fa-star';
  334. parentElement.appendChild(moderatorIndicator);
  335. Util.setTooltip(parentElement,
  336. "The owner of<br/>this conference",
  337. "top");
  338. }
  339. var VideoLayout = (function (my) {
  340. my.connectionIndicators = {};
  341. // By default we use camera
  342. my.getVideoSize = getCameraVideoSize;
  343. my.getVideoPosition = getCameraVideoPosition;
  344. my.isInLastN = function(resource) {
  345. return lastNCount < 0 // lastN is disabled, return true
  346. || (lastNCount > 0 && lastNEndpointsCache.length == 0) // lastNEndpoints cache not built yet, return true
  347. || (lastNEndpointsCache && lastNEndpointsCache.indexOf(resource) !== -1);
  348. };
  349. my.changeLocalStream = function (stream) {
  350. connection.jingle.localAudio = stream;
  351. VideoLayout.changeLocalVideo(stream, true);
  352. };
  353. my.changeLocalAudio = function(stream) {
  354. connection.jingle.localAudio = stream;
  355. RTC.attachMediaStream($('#localAudio'), stream);
  356. document.getElementById('localAudio').autoplay = true;
  357. document.getElementById('localAudio').volume = 0;
  358. if (preMuted) {
  359. setAudioMuted(true);
  360. preMuted = false;
  361. }
  362. };
  363. my.changeLocalVideo = function(stream, flipX) {
  364. connection.jingle.localVideo = stream;
  365. var localVideo = document.createElement('video');
  366. localVideo.id = 'localVideo_' + RTC.getStreamID(stream);
  367. localVideo.autoplay = true;
  368. localVideo.volume = 0; // is it required if audio is separated ?
  369. localVideo.oncontextmenu = function () { return false; };
  370. var localVideoContainer = document.getElementById('localVideoWrapper');
  371. localVideoContainer.appendChild(localVideo);
  372. // Set default display name.
  373. setDisplayName('localVideoContainer');
  374. if(!VideoLayout.connectionIndicators["localVideoContainer"]) {
  375. VideoLayout.connectionIndicators["localVideoContainer"]
  376. = new ConnectionIndicator($("#localVideoContainer")[0], null, VideoLayout);
  377. }
  378. AudioLevels.updateAudioLevelCanvas(null, VideoLayout);
  379. var localVideoSelector = $('#' + localVideo.id);
  380. // Add click handler to both video and video wrapper elements in case
  381. // there's no video.
  382. localVideoSelector.click(function (event) {
  383. event.stopPropagation();
  384. VideoLayout.handleVideoThumbClicked(
  385. RTC.getVideoSrc(localVideo),
  386. false,
  387. Strophe.getResourceFromJid(connection.emuc.myroomjid));
  388. });
  389. $('#localVideoContainer').click(function (event) {
  390. event.stopPropagation();
  391. VideoLayout.handleVideoThumbClicked(
  392. RTC.getVideoSrc(localVideo),
  393. false,
  394. Strophe.getResourceFromJid(connection.emuc.myroomjid));
  395. });
  396. // Add hover handler
  397. $('#localVideoContainer').hover(
  398. function() {
  399. VideoLayout.showDisplayName('localVideoContainer', true);
  400. },
  401. function() {
  402. if (!VideoLayout.isLargeVideoVisible()
  403. || RTC.getVideoSrc(localVideo) !== RTC.getVideoSrc($('#largeVideo')[0]))
  404. VideoLayout.showDisplayName('localVideoContainer', false);
  405. }
  406. );
  407. // Add stream ended handler
  408. stream.onended = function () {
  409. localVideoContainer.removeChild(localVideo);
  410. VideoLayout.updateRemovedVideo(RTC.getVideoSrc(localVideo));
  411. };
  412. // Flip video x axis if needed
  413. flipXLocalVideo = flipX;
  414. if (flipX) {
  415. localVideoSelector.addClass("flipVideoX");
  416. }
  417. // Attach WebRTC stream
  418. var videoStream = simulcast.getLocalVideoStream();
  419. RTC.attachMediaStream(localVideoSelector, videoStream);
  420. localVideoSrc = RTC.getVideoSrc(localVideo);
  421. var myResourceJid = null;
  422. if(connection.emuc.myroomjid)
  423. {
  424. myResourceJid = Strophe.getResourceFromJid(connection.emuc.myroomjid);
  425. }
  426. VideoLayout.updateLargeVideo(localVideoSrc, 0,
  427. myResourceJid);
  428. };
  429. /**
  430. * Checks if removed video is currently displayed and tries to display
  431. * another one instead.
  432. * @param removedVideoSrc src stream identifier of the video.
  433. */
  434. my.updateRemovedVideo = function(removedVideoSrc) {
  435. if (removedVideoSrc === RTC.getVideoSrc($('#largeVideo')[0])) {
  436. // this is currently displayed as large
  437. // pick the last visible video in the row
  438. // if nobody else is left, this picks the local video
  439. var pick
  440. = $('#remoteVideos>span[id!="mixedstream"]:visible:last>video')
  441. .get(0);
  442. if (!pick) {
  443. console.info("Last visible video no longer exists");
  444. pick = $('#remoteVideos>span[id!="mixedstream"]>video').get(0);
  445. if (!pick || !RTC.getVideoSrc(pick)) {
  446. // Try local video
  447. console.info("Fallback to local video...");
  448. pick = $('#remoteVideos>span>span>video').get(0);
  449. }
  450. }
  451. // mute if localvideo
  452. if (pick) {
  453. var container = pick.parentNode;
  454. var jid = null;
  455. if(container)
  456. {
  457. if(container.id == "localVideoWrapper")
  458. {
  459. jid = Strophe.getResourceFromJid(connection.emuc.myroomjid);
  460. }
  461. else
  462. {
  463. jid = VideoLayout.getPeerContainerResourceJid(container);
  464. }
  465. }
  466. VideoLayout.updateLargeVideo(RTC.getVideoSrc(pick), pick.volume, jid);
  467. } else {
  468. console.warn("Failed to elect large video");
  469. }
  470. }
  471. };
  472. my.onRemoteStreamAdded = function (stream) {
  473. var container;
  474. var remotes = document.getElementById('remoteVideos');
  475. if (stream.peerjid) {
  476. VideoLayout.ensurePeerContainerExists(stream.peerjid);
  477. container = document.getElementById(
  478. 'participant_' + Strophe.getResourceFromJid(stream.peerjid));
  479. } else {
  480. var id = stream.getOriginalStream().id;
  481. if (id !== 'mixedmslabel'
  482. // FIXME: default stream is added always with new focus
  483. // (to be investigated)
  484. && id !== 'default') {
  485. console.error('can not associate stream',
  486. id,
  487. 'with a participant');
  488. // We don't want to add it here since it will cause troubles
  489. return;
  490. }
  491. // FIXME: for the mixed ms we dont need a video -- currently
  492. container = document.createElement('span');
  493. container.id = 'mixedstream';
  494. container.className = 'videocontainer';
  495. remotes.appendChild(container);
  496. Util.playSoundNotification('userJoined');
  497. }
  498. if (container) {
  499. VideoLayout.addRemoteStreamElement( container,
  500. stream.sid,
  501. stream.getOriginalStream(),
  502. stream.peerjid,
  503. stream.ssrc);
  504. }
  505. }
  506. my.getLargeVideoState = function () {
  507. return largeVideoState;
  508. };
  509. /**
  510. * Updates the large video with the given new video source.
  511. */
  512. my.updateLargeVideo = function(newSrc, vol, resourceJid) {
  513. console.log('hover in', newSrc);
  514. if (RTC.getVideoSrc($('#largeVideo')[0]) !== newSrc) {
  515. $('#activeSpeaker').css('visibility', 'hidden');
  516. // Due to the simulcast the localVideoSrc may have changed when the
  517. // fadeOut event triggers. In that case the getJidFromVideoSrc and
  518. // isVideoSrcDesktop methods will not function correctly.
  519. //
  520. // Also, again due to the simulcast, the updateLargeVideo method can
  521. // be called multiple times almost simultaneously. Therefore, we
  522. // store the state here and update only once.
  523. largeVideoState.newSrc = newSrc;
  524. largeVideoState.isVisible = $('#largeVideo').is(':visible');
  525. largeVideoState.isDesktop = isVideoSrcDesktop(resourceJid);
  526. if(jid2Ssrc[largeVideoState.userResourceJid] ||
  527. (connection && connection.emuc.myroomjid &&
  528. largeVideoState.userResourceJid ===
  529. Strophe.getResourceFromJid(connection.emuc.myroomjid))) {
  530. largeVideoState.oldResourceJid = largeVideoState.userResourceJid;
  531. } else {
  532. largeVideoState.oldResourceJid = null;
  533. }
  534. largeVideoState.userResourceJid = resourceJid;
  535. // Screen stream is already rotated
  536. largeVideoState.flipX = (newSrc === localVideoSrc) && flipXLocalVideo;
  537. var userChanged = false;
  538. if (largeVideoState.oldResourceJid !== largeVideoState.userResourceJid) {
  539. userChanged = true;
  540. // we want the notification to trigger even if userJid is undefined,
  541. // or null.
  542. $(document).trigger("selectedendpointchanged", [largeVideoState.userResourceJid]);
  543. }
  544. if (!largeVideoState.updateInProgress) {
  545. largeVideoState.updateInProgress = true;
  546. var doUpdate = function () {
  547. Avatar.updateActiveSpeakerAvatarSrc(
  548. connection.emuc.findJidFromResource(
  549. largeVideoState.userResourceJid));
  550. if (!userChanged && largeVideoState.preload &&
  551. largeVideoState.preload !== null &&
  552. RTC.getVideoSrc($(largeVideoState.preload)[0]) === newSrc)
  553. {
  554. console.info('Switching to preloaded video');
  555. var attributes = $('#largeVideo').prop("attributes");
  556. // loop through largeVideo attributes and apply them on
  557. // preload.
  558. $.each(attributes, function () {
  559. if (this.name !== 'id' && this.name !== 'src') {
  560. largeVideoState.preload.attr(this.name, this.value);
  561. }
  562. });
  563. largeVideoState.preload.appendTo($('#largeVideoContainer'));
  564. $('#largeVideo').attr('id', 'previousLargeVideo');
  565. largeVideoState.preload.attr('id', 'largeVideo');
  566. $('#previousLargeVideo').remove();
  567. largeVideoState.preload.on('loadedmetadata', function (e) {
  568. currentVideoWidth = this.videoWidth;
  569. currentVideoHeight = this.videoHeight;
  570. VideoLayout.positionLarge(currentVideoWidth, currentVideoHeight);
  571. });
  572. largeVideoState.preload = null;
  573. largeVideoState.preload_ssrc = 0;
  574. } else {
  575. RTC.setVideoSrc($('#largeVideo')[0], largeVideoState.newSrc);
  576. }
  577. var videoTransform = document.getElementById('largeVideo')
  578. .style.webkitTransform;
  579. if (largeVideoState.flipX && videoTransform !== 'scaleX(-1)') {
  580. document.getElementById('largeVideo').style.webkitTransform
  581. = "scaleX(-1)";
  582. }
  583. else if (!largeVideoState.flipX && videoTransform === 'scaleX(-1)') {
  584. document.getElementById('largeVideo').style.webkitTransform
  585. = "none";
  586. }
  587. // Change the way we'll be measuring and positioning large video
  588. VideoLayout.getVideoSize = largeVideoState.isDesktop
  589. ? getDesktopVideoSize
  590. : getCameraVideoSize;
  591. VideoLayout.getVideoPosition = largeVideoState.isDesktop
  592. ? getDesktopVideoPosition
  593. : getCameraVideoPosition;
  594. // Only if the large video is currently visible.
  595. // Disable previous dominant speaker video.
  596. if (largeVideoState.oldResourceJid) {
  597. VideoLayout.enableDominantSpeaker(
  598. largeVideoState.oldResourceJid,
  599. false);
  600. }
  601. // Enable new dominant speaker in the remote videos section.
  602. if (largeVideoState.userResourceJid) {
  603. VideoLayout.enableDominantSpeaker(
  604. largeVideoState.userResourceJid,
  605. true);
  606. }
  607. if (userChanged && largeVideoState.isVisible) {
  608. // using "this" should be ok because we're called
  609. // from within the fadeOut event.
  610. $(this).fadeIn(300);
  611. }
  612. if(userChanged) {
  613. Avatar.showUserAvatar(
  614. connection.emuc.findJidFromResource(
  615. largeVideoState.oldResourceJid));
  616. }
  617. largeVideoState.updateInProgress = false;
  618. };
  619. if (userChanged) {
  620. $('#largeVideo').fadeOut(300, doUpdate);
  621. } else {
  622. doUpdate();
  623. }
  624. }
  625. } else {
  626. Avatar.showUserAvatar(
  627. connection.emuc.findJidFromResource(
  628. largeVideoState.userResourceJid));
  629. }
  630. };
  631. my.handleVideoThumbClicked = function(videoSrc,
  632. noPinnedEndpointChangedEvent,
  633. resourceJid) {
  634. // Restore style for previously focused video
  635. var oldContainer = null;
  636. if(focusedVideoInfo) {
  637. var focusResourceJid = focusedVideoInfo.resourceJid;
  638. oldContainer = getParticipantContainer(focusResourceJid);
  639. }
  640. if (oldContainer) {
  641. oldContainer.removeClass("videoContainerFocused");
  642. }
  643. // Unlock current focused.
  644. if (focusedVideoInfo && focusedVideoInfo.src === videoSrc)
  645. {
  646. focusedVideoInfo = null;
  647. var dominantSpeakerVideo = null;
  648. // Enable the currently set dominant speaker.
  649. if (currentDominantSpeaker) {
  650. dominantSpeakerVideo
  651. = $('#participant_' + currentDominantSpeaker + '>video')
  652. .get(0);
  653. if (dominantSpeakerVideo) {
  654. VideoLayout.updateLargeVideo(
  655. RTC.getVideoSrc(dominantSpeakerVideo),
  656. 1,
  657. currentDominantSpeaker);
  658. }
  659. }
  660. if (!noPinnedEndpointChangedEvent) {
  661. $(document).trigger("pinnedendpointchanged");
  662. }
  663. return;
  664. }
  665. // Lock new video
  666. focusedVideoInfo = {
  667. src: videoSrc,
  668. resourceJid: resourceJid
  669. };
  670. // Update focused/pinned interface.
  671. if (resourceJid)
  672. {
  673. var container = getParticipantContainer(resourceJid);
  674. container.addClass("videoContainerFocused");
  675. if (!noPinnedEndpointChangedEvent) {
  676. $(document).trigger("pinnedendpointchanged", [resourceJid]);
  677. }
  678. }
  679. if ($('#largeVideo').attr('src') === videoSrc &&
  680. VideoLayout.isLargeVideoOnTop()) {
  681. return;
  682. }
  683. // Triggers a "video.selected" event. The "false" parameter indicates
  684. // this isn't a prezi.
  685. $(document).trigger("video.selected", [false]);
  686. VideoLayout.updateLargeVideo(videoSrc, 1, resourceJid);
  687. $('audio').each(function (idx, el) {
  688. if (el.id.indexOf('mixedmslabel') !== -1) {
  689. el.volume = 0;
  690. el.volume = 1;
  691. }
  692. });
  693. };
  694. /**
  695. * Positions the large video.
  696. *
  697. * @param videoWidth the stream video width
  698. * @param videoHeight the stream video height
  699. */
  700. my.positionLarge = function (videoWidth, videoHeight) {
  701. var videoSpaceWidth = $('#videospace').width();
  702. var videoSpaceHeight = window.innerHeight;
  703. var videoSize = VideoLayout.getVideoSize(videoWidth,
  704. videoHeight,
  705. videoSpaceWidth,
  706. videoSpaceHeight);
  707. var largeVideoWidth = videoSize[0];
  708. var largeVideoHeight = videoSize[1];
  709. var videoPosition = VideoLayout.getVideoPosition(largeVideoWidth,
  710. largeVideoHeight,
  711. videoSpaceWidth,
  712. videoSpaceHeight);
  713. var horizontalIndent = videoPosition[0];
  714. var verticalIndent = videoPosition[1];
  715. positionVideo($('#largeVideo'),
  716. largeVideoWidth,
  717. largeVideoHeight,
  718. horizontalIndent, verticalIndent);
  719. };
  720. /**
  721. * Shows/hides the large video.
  722. */
  723. my.setLargeVideoVisible = function(isVisible) {
  724. var resourceJid = largeVideoState.userResourceJid;
  725. if (isVisible) {
  726. $('#largeVideo').css({visibility: 'visible'});
  727. $('.watermark').css({visibility: 'visible'});
  728. VideoLayout.enableDominantSpeaker(resourceJid, true);
  729. }
  730. else {
  731. $('#largeVideo').css({visibility: 'hidden'});
  732. $('#activeSpeaker').css('visibility', 'hidden');
  733. $('.watermark').css({visibility: 'hidden'});
  734. VideoLayout.enableDominantSpeaker(resourceJid, false);
  735. if(focusedVideoInfo) {
  736. var focusResourceJid = focusedVideoInfo.resourceJid;
  737. var oldContainer = getParticipantContainer(focusResourceJid);
  738. if (oldContainer && oldContainer.length > 0) {
  739. oldContainer.removeClass("videoContainerFocused");
  740. }
  741. focusedVideoInfo = null;
  742. if(focusResourceJid) {
  743. Avatar.showUserAvatar(
  744. connection.emuc.findJidFromResource(focusResourceJid));
  745. }
  746. }
  747. }
  748. };
  749. /**
  750. * Indicates if the large video is currently visible.
  751. *
  752. * @return <tt>true</tt> if visible, <tt>false</tt> - otherwise
  753. */
  754. my.isLargeVideoVisible = function() {
  755. return $('#largeVideo').is(':visible');
  756. };
  757. my.isLargeVideoOnTop = function () {
  758. var Etherpad = require("../etherpad/Etherpad");
  759. var Prezi = require("../prezi/Prezi");
  760. return !Prezi.isPresentationVisible() && !Etherpad.isVisible();
  761. };
  762. /**
  763. * Checks if container for participant identified by given peerJid exists
  764. * in the document and creates it eventually.
  765. *
  766. * @param peerJid peer Jid to check.
  767. * @param userId user email or id for setting the avatar
  768. *
  769. * @return Returns <tt>true</tt> if the peer container exists,
  770. * <tt>false</tt> - otherwise
  771. */
  772. my.ensurePeerContainerExists = function(peerJid, userId) {
  773. ContactList.ensureAddContact(peerJid, userId);
  774. var resourceJid = Strophe.getResourceFromJid(peerJid);
  775. var videoSpanId = 'participant_' + resourceJid;
  776. if (!$('#' + videoSpanId).length) {
  777. var container =
  778. VideoLayout.addRemoteVideoContainer(peerJid, videoSpanId, userId);
  779. Avatar.setUserAvatar(peerJid, userId);
  780. // Set default display name.
  781. setDisplayName(videoSpanId);
  782. VideoLayout.connectionIndicators[videoSpanId] =
  783. new ConnectionIndicator(container, peerJid, VideoLayout);
  784. var nickfield = document.createElement('span');
  785. nickfield.className = "nick";
  786. nickfield.appendChild(document.createTextNode(resourceJid));
  787. container.appendChild(nickfield);
  788. // In case this is not currently in the last n we don't show it.
  789. if (localLastNCount
  790. && localLastNCount > 0
  791. && $('#remoteVideos>span').length >= localLastNCount + 2) {
  792. showPeerContainer(resourceJid, 'hide');
  793. }
  794. else
  795. VideoLayout.resizeThumbnails();
  796. }
  797. };
  798. my.addRemoteVideoContainer = function(peerJid, spanId) {
  799. var container = document.createElement('span');
  800. container.id = spanId;
  801. container.className = 'videocontainer';
  802. var remotes = document.getElementById('remoteVideos');
  803. // If the peerJid is null then this video span couldn't be directly
  804. // associated with a participant (this could happen in the case of prezi).
  805. if (Moderator.isModerator() && peerJid !== null)
  806. addRemoteVideoMenu(peerJid, container);
  807. remotes.appendChild(container);
  808. AudioLevels.updateAudioLevelCanvas(peerJid, VideoLayout);
  809. return container;
  810. };
  811. /**
  812. * Creates an audio or video stream element.
  813. */
  814. my.createStreamElement = function (sid, stream) {
  815. var isVideo = stream.getVideoTracks().length > 0;
  816. var element = isVideo
  817. ? document.createElement('video')
  818. : document.createElement('audio');
  819. var id = (isVideo ? 'remoteVideo_' : 'remoteAudio_')
  820. + sid + '_' + RTC.getStreamID(stream);
  821. element.id = id;
  822. element.autoplay = true;
  823. element.oncontextmenu = function () { return false; };
  824. return element;
  825. };
  826. my.addRemoteStreamElement
  827. = function (container, sid, stream, peerJid, thessrc) {
  828. var newElementId = null;
  829. var isVideo = stream.getVideoTracks().length > 0;
  830. if (container) {
  831. var streamElement = VideoLayout.createStreamElement(sid, stream);
  832. newElementId = streamElement.id;
  833. container.appendChild(streamElement);
  834. var sel = $('#' + newElementId);
  835. sel.hide();
  836. // If the container is currently visible we attach the stream.
  837. if (!isVideo
  838. || (container.offsetParent !== null && isVideo)) {
  839. var videoStream = simulcast.getReceivingVideoStream(stream);
  840. RTC.attachMediaStream(sel, videoStream);
  841. if (isVideo)
  842. waitForRemoteVideo(sel, thessrc, stream, peerJid);
  843. }
  844. stream.onended = function () {
  845. console.log('stream ended', this);
  846. VideoLayout.removeRemoteStreamElement(
  847. stream, isVideo, container);
  848. // NOTE(gp) it seems that under certain circumstances, the
  849. // onended event is not fired and thus the contact list is not
  850. // updated.
  851. //
  852. // The onended event of a stream should be fired when the SSRCs
  853. // corresponding to that stream are removed from the SDP; but
  854. // this doesn't seem to always be the case, resulting in ghost
  855. // contacts.
  856. //
  857. // In an attempt to fix the ghost contacts problem, I'm moving
  858. // the removeContact() method call in app.js, inside the
  859. // 'muc.left' event handler.
  860. //if (peerJid)
  861. // ContactList.removeContact(peerJid);
  862. };
  863. // Add click handler.
  864. container.onclick = function (event) {
  865. /*
  866. * FIXME It turns out that videoThumb may not exist (if there is
  867. * no actual video).
  868. */
  869. var videoThumb = $('#' + container.id + '>video').get(0);
  870. if (videoThumb) {
  871. VideoLayout.handleVideoThumbClicked(
  872. RTC.getVideoSrc(videoThumb),
  873. false,
  874. Strophe.getResourceFromJid(peerJid));
  875. }
  876. event.stopPropagation();
  877. event.preventDefault();
  878. return false;
  879. };
  880. // Add hover handler
  881. $(container).hover(
  882. function() {
  883. VideoLayout.showDisplayName(container.id, true);
  884. },
  885. function() {
  886. var videoSrc = null;
  887. if ($('#' + container.id + '>video')
  888. && $('#' + container.id + '>video').length > 0) {
  889. videoSrc = RTC.getVideoSrc($('#' + container.id + '>video').get(0));
  890. }
  891. // If the video has been "pinned" by the user we want to
  892. // keep the display name on place.
  893. if (!VideoLayout.isLargeVideoVisible()
  894. || videoSrc !== RTC.getVideoSrc($('#largeVideo')[0]))
  895. VideoLayout.showDisplayName(container.id, false);
  896. }
  897. );
  898. }
  899. return newElementId;
  900. };
  901. /**
  902. * Removes the remote stream element corresponding to the given stream and
  903. * parent container.
  904. *
  905. * @param stream the stream
  906. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  907. * @param container
  908. */
  909. my.removeRemoteStreamElement = function (stream, isVideo, container) {
  910. if (!container)
  911. return;
  912. var select = null;
  913. var removedVideoSrc = null;
  914. if (isVideo) {
  915. select = $('#' + container.id + '>video');
  916. removedVideoSrc = RTC.getVideoSrc(select.get(0));
  917. }
  918. else
  919. select = $('#' + container.id + '>audio');
  920. // Mark video as removed to cancel waiting loop(if video is removed
  921. // before has started)
  922. select.removed = true;
  923. select.remove();
  924. var audioCount = $('#' + container.id + '>audio').length;
  925. var videoCount = $('#' + container.id + '>video').length;
  926. if (!audioCount && !videoCount) {
  927. console.log("Remove whole user", container.id);
  928. if(VideoLayout.connectionIndicators[container.id])
  929. VideoLayout.connectionIndicators[container.id].remove();
  930. // Remove whole container
  931. container.remove();
  932. Util.playSoundNotification('userLeft');
  933. VideoLayout.resizeThumbnails();
  934. }
  935. if (removedVideoSrc)
  936. VideoLayout.updateRemovedVideo(removedVideoSrc);
  937. };
  938. /**
  939. * Show/hide peer container for the given resourceJid.
  940. */
  941. function showPeerContainer(resourceJid, state) {
  942. var peerContainer = $('#participant_' + resourceJid);
  943. if (!peerContainer)
  944. return;
  945. var isHide = state === 'hide';
  946. var resizeThumbnails = false;
  947. if (!isHide) {
  948. if (!peerContainer.is(':visible')) {
  949. resizeThumbnails = true;
  950. peerContainer.show();
  951. }
  952. if (state == 'show')
  953. {
  954. // peerContainer.css('-webkit-filter', '');
  955. var jid = connection.emuc.findJidFromResource(resourceJid);
  956. Avatar.showUserAvatar(jid, false);
  957. }
  958. else // if (state == 'avatar')
  959. {
  960. // peerContainer.css('-webkit-filter', 'grayscale(100%)');
  961. var jid = connection.emuc.findJidFromResource(resourceJid);
  962. Avatar.showUserAvatar(jid, true);
  963. }
  964. }
  965. else if (peerContainer.is(':visible') && isHide)
  966. {
  967. resizeThumbnails = true;
  968. peerContainer.hide();
  969. if(VideoLayout.connectionIndicators['participant_' + resourceJid])
  970. VideoLayout.connectionIndicators['participant_' + resourceJid].hide();
  971. }
  972. if (resizeThumbnails) {
  973. VideoLayout.resizeThumbnails();
  974. }
  975. // We want to be able to pin a participant from the contact list, even
  976. // if he's not in the lastN set!
  977. // ContactList.setClickable(resourceJid, !isHide);
  978. };
  979. my.inputDisplayNameHandler = function (name) {
  980. if (name && nickname !== name) {
  981. nickname = name;
  982. window.localStorage.displayname = nickname;
  983. connection.emuc.addDisplayNameToPresence(nickname);
  984. connection.emuc.sendPresence();
  985. Chat.setChatConversationMode(true);
  986. }
  987. if (!$('#localDisplayName').is(":visible")) {
  988. if (nickname)
  989. $('#localDisplayName').text(nickname + " (me)");
  990. else
  991. $('#localDisplayName')
  992. .text(interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME);
  993. $('#localDisplayName').show();
  994. }
  995. $('#editDisplayName').hide();
  996. };
  997. /**
  998. * Shows/hides the display name on the remote video.
  999. * @param videoSpanId the identifier of the video span element
  1000. * @param isShow indicates if the display name should be shown or hidden
  1001. */
  1002. my.showDisplayName = function(videoSpanId, isShow) {
  1003. var nameSpan = $('#' + videoSpanId + '>span.displayname').get(0);
  1004. if (isShow) {
  1005. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  1006. nameSpan.setAttribute("style", "display:inline-block;");
  1007. }
  1008. else {
  1009. if (nameSpan)
  1010. nameSpan.setAttribute("style", "display:none;");
  1011. }
  1012. };
  1013. /**
  1014. * Shows the presence status message for the given video.
  1015. */
  1016. my.setPresenceStatus = function (videoSpanId, statusMsg) {
  1017. if (!$('#' + videoSpanId).length) {
  1018. // No container
  1019. return;
  1020. }
  1021. var statusSpan = $('#' + videoSpanId + '>span.status');
  1022. if (!statusSpan.length) {
  1023. //Add status span
  1024. statusSpan = document.createElement('span');
  1025. statusSpan.className = 'status';
  1026. statusSpan.id = videoSpanId + '_status';
  1027. $('#' + videoSpanId)[0].appendChild(statusSpan);
  1028. statusSpan = $('#' + videoSpanId + '>span.status');
  1029. }
  1030. // Display status
  1031. if (statusMsg && statusMsg.length) {
  1032. $('#' + videoSpanId + '_status').text(statusMsg);
  1033. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  1034. }
  1035. else {
  1036. // Hide
  1037. statusSpan.get(0).setAttribute("style", "display:none;");
  1038. }
  1039. };
  1040. /**
  1041. * Shows a visual indicator for the moderator of the conference.
  1042. */
  1043. my.showModeratorIndicator = function () {
  1044. var isModerator = Moderator.isModerator();
  1045. if (isModerator) {
  1046. var indicatorSpan = $('#localVideoContainer .focusindicator');
  1047. if (indicatorSpan.children().length === 0)
  1048. {
  1049. createModeratorIndicatorElement(indicatorSpan[0]);
  1050. }
  1051. }
  1052. Object.keys(connection.emuc.members).forEach(function (jid) {
  1053. if (Strophe.getResourceFromJid(jid) === 'focus') {
  1054. // Skip server side focus
  1055. return;
  1056. }
  1057. var resourceJid = Strophe.getResourceFromJid(jid);
  1058. var videoSpanId = 'participant_' + resourceJid;
  1059. var videoContainer = document.getElementById(videoSpanId);
  1060. if (!videoContainer) {
  1061. console.error("No video container for " + jid);
  1062. return;
  1063. }
  1064. var member = connection.emuc.members[jid];
  1065. if (member.role === 'moderator') {
  1066. // Remove menu if peer is moderator
  1067. var menuSpan = $('#' + videoSpanId + '>span.remotevideomenu');
  1068. if (menuSpan.length) {
  1069. removeRemoteVideoMenu(videoSpanId);
  1070. }
  1071. // Show moderator indicator
  1072. var indicatorSpan
  1073. = $('#' + videoSpanId + ' .focusindicator');
  1074. if (!indicatorSpan || indicatorSpan.length === 0) {
  1075. indicatorSpan = document.createElement('span');
  1076. indicatorSpan.className = 'focusindicator';
  1077. videoContainer.appendChild(indicatorSpan);
  1078. createModeratorIndicatorElement(indicatorSpan);
  1079. }
  1080. } else if (isModerator) {
  1081. // We are moderator, but user is not - add menu
  1082. if ($('#remote_popupmenu_' + resourceJid).length <= 0) {
  1083. addRemoteVideoMenu(
  1084. jid,
  1085. document.getElementById('participant_' + resourceJid));
  1086. }
  1087. }
  1088. });
  1089. };
  1090. /**
  1091. * Shows video muted indicator over small videos.
  1092. */
  1093. my.showVideoIndicator = function(videoSpanId, isMuted) {
  1094. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  1095. if (isMuted === 'false') {
  1096. if (videoMutedSpan.length > 0) {
  1097. videoMutedSpan.remove();
  1098. }
  1099. }
  1100. else {
  1101. if(videoMutedSpan.length == 0) {
  1102. videoMutedSpan = document.createElement('span');
  1103. videoMutedSpan.className = 'videoMuted';
  1104. $('#' + videoSpanId)[0].appendChild(videoMutedSpan);
  1105. var mutedIndicator = document.createElement('i');
  1106. mutedIndicator.className = 'icon-camera-disabled';
  1107. Util.setTooltip(mutedIndicator,
  1108. "Participant has<br/>stopped the camera.",
  1109. "top");
  1110. videoMutedSpan.appendChild(mutedIndicator);
  1111. }
  1112. VideoLayout.updateMutePosition(videoSpanId);
  1113. }
  1114. };
  1115. my.updateMutePosition = function (videoSpanId) {
  1116. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  1117. var connectionIndicator = $('#' + videoSpanId + '>div.connectionindicator');
  1118. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  1119. if(connectionIndicator.length > 0
  1120. && connectionIndicator[0].style.display != "none") {
  1121. audioMutedSpan.css({right: "23px"});
  1122. videoMutedSpan.css({right: ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  1123. }
  1124. else
  1125. {
  1126. audioMutedSpan.css({right: "0px"});
  1127. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  1128. }
  1129. }
  1130. /**
  1131. * Shows audio muted indicator over small videos.
  1132. * @param {string} isMuted
  1133. */
  1134. my.showAudioIndicator = function(videoSpanId, isMuted) {
  1135. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  1136. if (isMuted === 'false') {
  1137. if (audioMutedSpan.length > 0) {
  1138. audioMutedSpan.popover('hide');
  1139. audioMutedSpan.remove();
  1140. }
  1141. }
  1142. else {
  1143. if(audioMutedSpan.length == 0 ) {
  1144. audioMutedSpan = document.createElement('span');
  1145. audioMutedSpan.className = 'audioMuted';
  1146. Util.setTooltip(audioMutedSpan,
  1147. "Participant is muted",
  1148. "top");
  1149. $('#' + videoSpanId)[0].appendChild(audioMutedSpan);
  1150. var mutedIndicator = document.createElement('i');
  1151. mutedIndicator.className = 'icon-mic-disabled';
  1152. audioMutedSpan.appendChild(mutedIndicator);
  1153. }
  1154. VideoLayout.updateMutePosition(videoSpanId);
  1155. }
  1156. };
  1157. /*
  1158. * Shows or hides the audio muted indicator over the local thumbnail video.
  1159. * @param {boolean} isMuted
  1160. */
  1161. my.showLocalAudioIndicator = function(isMuted) {
  1162. VideoLayout.showAudioIndicator('localVideoContainer', isMuted.toString());
  1163. };
  1164. /**
  1165. * Resizes the large video container.
  1166. */
  1167. my.resizeLargeVideoContainer = function () {
  1168. Chat.resizeChat();
  1169. var availableHeight = window.innerHeight;
  1170. var availableWidth = UIUtil.getAvailableVideoWidth();
  1171. if (availableWidth < 0 || availableHeight < 0) return;
  1172. $('#videospace').width(availableWidth);
  1173. $('#videospace').height(availableHeight);
  1174. $('#largeVideoContainer').width(availableWidth);
  1175. $('#largeVideoContainer').height(availableHeight);
  1176. var avatarSize = interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE;
  1177. var top = availableHeight / 2 - avatarSize / 4 * 3;
  1178. $('#activeSpeaker').css('top', top);
  1179. VideoLayout.resizeThumbnails();
  1180. };
  1181. /**
  1182. * Resizes thumbnails.
  1183. */
  1184. my.resizeThumbnails = function() {
  1185. var videoSpaceWidth = $('#remoteVideos').width();
  1186. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  1187. var width = thumbnailSize[0];
  1188. var height = thumbnailSize[1];
  1189. // size videos so that while keeping AR and max height, we have a
  1190. // nice fit
  1191. $('#remoteVideos').height(height);
  1192. $('#remoteVideos>span').width(width);
  1193. $('#remoteVideos>span').height(height);
  1194. $('.userAvatar').css('left', (width - height) / 2);
  1195. $(document).trigger("remotevideo.resized", [width, height]);
  1196. };
  1197. /**
  1198. * Enables the dominant speaker UI.
  1199. *
  1200. * @param resourceJid the jid indicating the video element to
  1201. * activate/deactivate
  1202. * @param isEnable indicates if the dominant speaker should be enabled or
  1203. * disabled
  1204. */
  1205. my.enableDominantSpeaker = function(resourceJid, isEnable) {
  1206. var videoSpanId = null;
  1207. var videoContainerId = null;
  1208. if (resourceJid
  1209. === Strophe.getResourceFromJid(connection.emuc.myroomjid)) {
  1210. videoSpanId = 'localVideoWrapper';
  1211. videoContainerId = 'localVideoContainer';
  1212. }
  1213. else {
  1214. videoSpanId = 'participant_' + resourceJid;
  1215. videoContainerId = videoSpanId;
  1216. }
  1217. var displayName = resourceJid;
  1218. var nameSpan = $('#' + videoContainerId + '>span.displayname');
  1219. if (nameSpan.length > 0)
  1220. displayName = nameSpan.html();
  1221. console.log("UI enable dominant speaker",
  1222. displayName,
  1223. resourceJid,
  1224. isEnable);
  1225. videoSpan = document.getElementById(videoContainerId);
  1226. if (!videoSpan) {
  1227. return;
  1228. }
  1229. var video = $('#' + videoSpanId + '>video');
  1230. if (video && video.length > 0) {
  1231. if (isEnable) {
  1232. var isLargeVideoVisible = VideoLayout.isLargeVideoOnTop();
  1233. VideoLayout.showDisplayName(videoContainerId, isLargeVideoVisible);
  1234. if (!videoSpan.classList.contains("dominantspeaker"))
  1235. videoSpan.classList.add("dominantspeaker");
  1236. }
  1237. else {
  1238. VideoLayout.showDisplayName(videoContainerId, false);
  1239. if (videoSpan.classList.contains("dominantspeaker"))
  1240. videoSpan.classList.remove("dominantspeaker");
  1241. }
  1242. Avatar.showUserAvatar(
  1243. connection.emuc.findJidFromResource(resourceJid));
  1244. }
  1245. };
  1246. /**
  1247. * Calculates the thumbnail size.
  1248. *
  1249. * @param videoSpaceWidth the width of the video space
  1250. */
  1251. my.calculateThumbnailSize = function (videoSpaceWidth) {
  1252. // Calculate the available height, which is the inner window height minus
  1253. // 39px for the header minus 2px for the delimiter lines on the top and
  1254. // bottom of the large video, minus the 36px space inside the remoteVideos
  1255. // container used for highlighting shadow.
  1256. var availableHeight = 100;
  1257. var numvids = $('#remoteVideos>span:visible').length;
  1258. if (localLastNCount && localLastNCount > 0) {
  1259. numvids = Math.min(localLastNCount + 1, numvids);
  1260. }
  1261. // Remove the 3px borders arround videos and border around the remote
  1262. // videos area and the 4 pixels between the local video and the others
  1263. //TODO: Find out where the 4 pixels come from and remove them
  1264. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70 - 4;
  1265. var availableWidth = availableWinWidth / numvids;
  1266. var aspectRatio = 16.0 / 9.0;
  1267. var maxHeight = Math.min(160, availableHeight);
  1268. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  1269. if (availableHeight < availableWidth / aspectRatio) {
  1270. availableWidth = Math.floor(availableHeight * aspectRatio);
  1271. }
  1272. return [availableWidth, availableHeight];
  1273. };
  1274. /**
  1275. * Updates the remote video menu.
  1276. *
  1277. * @param jid the jid indicating the video for which we're adding a menu.
  1278. * @param isMuted indicates the current mute state
  1279. */
  1280. my.updateRemoteVideoMenu = function(jid, isMuted) {
  1281. var muteMenuItem
  1282. = $('#remote_popupmenu_'
  1283. + Strophe.getResourceFromJid(jid)
  1284. + '>li>a.mutelink');
  1285. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  1286. if (muteMenuItem.length) {
  1287. var muteLink = muteMenuItem.get(0);
  1288. if (isMuted === 'true') {
  1289. muteLink.innerHTML = mutedIndicator + ' Muted';
  1290. muteLink.className = 'mutelink disabled';
  1291. }
  1292. else {
  1293. muteLink.innerHTML = mutedIndicator + ' Mute';
  1294. muteLink.className = 'mutelink';
  1295. }
  1296. }
  1297. };
  1298. /**
  1299. * Returns the current dominant speaker resource jid.
  1300. */
  1301. my.getDominantSpeakerResourceJid = function () {
  1302. return currentDominantSpeaker;
  1303. };
  1304. /**
  1305. * Returns the corresponding resource jid to the given peer container
  1306. * DOM element.
  1307. *
  1308. * @return the corresponding resource jid to the given peer container
  1309. * DOM element
  1310. */
  1311. my.getPeerContainerResourceJid = function (containerElement) {
  1312. var i = containerElement.id.indexOf('participant_');
  1313. if (i >= 0)
  1314. return containerElement.id.substring(i + 12);
  1315. };
  1316. /**
  1317. * On contact list item clicked.
  1318. */
  1319. $(ContactList).bind('contactclicked', function(event, jid) {
  1320. if (!jid) {
  1321. return;
  1322. }
  1323. var resource = Strophe.getResourceFromJid(jid);
  1324. var videoContainer = $("#participant_" + resource);
  1325. if (videoContainer.length > 0) {
  1326. var videoThumb = $('video', videoContainer).get(0);
  1327. // It is not always the case that a videoThumb exists (if there is
  1328. // no actual video).
  1329. if (videoThumb) {
  1330. if (videoThumb.src && videoThumb.src != '') {
  1331. // We have a video src, great! Let's update the large video
  1332. // now.
  1333. VideoLayout.handleVideoThumbClicked(
  1334. videoThumb.src,
  1335. false,
  1336. Strophe.getResourceFromJid(jid));
  1337. } else {
  1338. // If we don't have a video src for jid, there's absolutely
  1339. // no point in calling handleVideoThumbClicked; Quite
  1340. // simply, it won't work because it needs an src to attach
  1341. // to the large video.
  1342. //
  1343. // Instead, we trigger the pinned endpoint changed event to
  1344. // let the bridge adjust its lastN set for myjid and store
  1345. // the pinned user in the lastNPickupJid variable to be
  1346. // picked up later by the lastN changed event handler.
  1347. lastNPickupJid = jid;
  1348. $(document).trigger("pinnedendpointchanged", [jid]);
  1349. }
  1350. } else if (jid == connection.emuc.myroomjid) {
  1351. $("#localVideoContainer").click();
  1352. }
  1353. }
  1354. });
  1355. /**
  1356. * On audio muted event.
  1357. */
  1358. $(document).bind('audiomuted.muc', function (event, jid, isMuted) {
  1359. /*
  1360. // FIXME: but focus can not mute in this case ? - check
  1361. if (jid === connection.emuc.myroomjid) {
  1362. // The local mute indicator is controlled locally
  1363. return;
  1364. }*/
  1365. var videoSpanId = null;
  1366. if (jid === connection.emuc.myroomjid) {
  1367. videoSpanId = 'localVideoContainer';
  1368. } else {
  1369. VideoLayout.ensurePeerContainerExists(jid);
  1370. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  1371. }
  1372. mutedAudios[jid] = isMuted;
  1373. if (Moderator.isModerator()) {
  1374. VideoLayout.updateRemoteVideoMenu(jid, isMuted);
  1375. }
  1376. if (videoSpanId)
  1377. VideoLayout.showAudioIndicator(videoSpanId, isMuted);
  1378. });
  1379. /**
  1380. * On video muted event.
  1381. */
  1382. $(document).bind('videomuted.muc', function (event, jid, isMuted) {
  1383. if(!RTC.muteRemoteVideoStream(jid, isMuted))
  1384. return;
  1385. Avatar.showUserAvatar(jid, isMuted);
  1386. var videoSpanId = null;
  1387. if (jid === connection.emuc.myroomjid) {
  1388. videoSpanId = 'localVideoContainer';
  1389. } else {
  1390. VideoLayout.ensurePeerContainerExists(jid);
  1391. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  1392. }
  1393. if (videoSpanId)
  1394. VideoLayout.showVideoIndicator(videoSpanId, isMuted);
  1395. });
  1396. /**
  1397. * Display name changed.
  1398. */
  1399. $(document).bind('displaynamechanged',
  1400. function (event, jid, displayName, status) {
  1401. var name = null;
  1402. if (jid === 'localVideoContainer'
  1403. || jid === connection.emuc.myroomjid) {
  1404. name = nickname;
  1405. setDisplayName('localVideoContainer',
  1406. displayName);
  1407. } else {
  1408. VideoLayout.ensurePeerContainerExists(jid);
  1409. name = $('#participant_' + Strophe.getResourceFromJid(jid) + "_name").text();
  1410. setDisplayName(
  1411. 'participant_' + Strophe.getResourceFromJid(jid),
  1412. displayName,
  1413. status);
  1414. }
  1415. if(jid === 'localVideoContainer')
  1416. jid = connection.emuc.myroomjid;
  1417. if(!name || name != displayName)
  1418. API.triggerEvent("displayNameChange",{jid: jid, displayname: displayName});
  1419. });
  1420. /**
  1421. * On dominant speaker changed event.
  1422. */
  1423. $(document).bind('dominantspeakerchanged', function (event, resourceJid) {
  1424. // We ignore local user events.
  1425. if (resourceJid
  1426. === Strophe.getResourceFromJid(connection.emuc.myroomjid))
  1427. return;
  1428. // Update the current dominant speaker.
  1429. if (resourceJid !== currentDominantSpeaker) {
  1430. var oldSpeakerVideoSpanId = "participant_" + currentDominantSpeaker,
  1431. newSpeakerVideoSpanId = "participant_" + resourceJid;
  1432. if($("#" + oldSpeakerVideoSpanId + ">span.displayname").text() ===
  1433. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME) {
  1434. setDisplayName(oldSpeakerVideoSpanId, null);
  1435. }
  1436. if($("#" + newSpeakerVideoSpanId + ">span.displayname").text() ===
  1437. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME) {
  1438. setDisplayName(newSpeakerVideoSpanId,
  1439. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  1440. }
  1441. currentDominantSpeaker = resourceJid;
  1442. } else {
  1443. return;
  1444. }
  1445. // Obtain container for new dominant speaker.
  1446. var container = document.getElementById(
  1447. 'participant_' + resourceJid);
  1448. // Local video will not have container found, but that's ok
  1449. // since we don't want to switch to local video.
  1450. if (container && !focusedVideoInfo)
  1451. {
  1452. var video = container.getElementsByTagName("video");
  1453. // Update the large video if the video source is already available,
  1454. // otherwise wait for the "videoactive.jingle" event.
  1455. if (video.length && video[0].currentTime > 0)
  1456. VideoLayout.updateLargeVideo(RTC.getVideoSrc(video[0]), resourceJid);
  1457. }
  1458. });
  1459. /**
  1460. * On last N change event.
  1461. *
  1462. * @param event the event that notified us
  1463. * @param lastNEndpoints the list of last N endpoints
  1464. * @param endpointsEnteringLastN the list currently entering last N
  1465. * endpoints
  1466. */
  1467. $(document).bind('lastnchanged', function ( event,
  1468. lastNEndpoints,
  1469. endpointsEnteringLastN,
  1470. stream) {
  1471. if (lastNCount !== lastNEndpoints.length)
  1472. lastNCount = lastNEndpoints.length;
  1473. lastNEndpointsCache = lastNEndpoints;
  1474. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  1475. //
  1476. // If LastN drops to, say, 2, because of adaptivity, then E should see
  1477. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  1478. // so E sees them. C is only in E's local LastN set.
  1479. //
  1480. // If F starts talking and LastN = 3, then E should see thumbnails for
  1481. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  1482. // enters E's local LastN ejecting C.
  1483. // Increase the local LastN set size, if necessary.
  1484. if (lastNCount > localLastNCount) {
  1485. localLastNCount = lastNCount;
  1486. }
  1487. // Update the local LastN set preserving the order in which the
  1488. // endpoints appeared in the LastN/local LastN set.
  1489. var nextLocalLastNSet = lastNEndpoints.slice(0);
  1490. for (var i = 0; i < localLastNSet.length; i++) {
  1491. if (nextLocalLastNSet.length >= localLastNCount) {
  1492. break;
  1493. }
  1494. var resourceJid = localLastNSet[i];
  1495. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  1496. nextLocalLastNSet.push(resourceJid);
  1497. }
  1498. }
  1499. localLastNSet = nextLocalLastNSet;
  1500. var updateLargeVideo = false;
  1501. // Handle LastN/local LastN changes.
  1502. $('#remoteVideos>span').each(function( index, element ) {
  1503. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  1504. var isReceived = true;
  1505. if (resourceJid
  1506. && lastNEndpoints.indexOf(resourceJid) < 0
  1507. && localLastNSet.indexOf(resourceJid) < 0) {
  1508. console.log("Remove from last N", resourceJid);
  1509. showPeerContainer(resourceJid, 'hide');
  1510. isReceived = false;
  1511. } else if (resourceJid
  1512. && $('#participant_' + resourceJid).is(':visible')
  1513. && lastNEndpoints.indexOf(resourceJid) < 0
  1514. && localLastNSet.indexOf(resourceJid) >= 0) {
  1515. showPeerContainer(resourceJid, 'avatar');
  1516. isReceived = false;
  1517. }
  1518. if (!isReceived) {
  1519. // resourceJid has dropped out of the server side lastN set, so
  1520. // it is no longer being received. If resourceJid was being
  1521. // displayed in the large video we have to switch to another
  1522. // user.
  1523. var largeVideoResource = largeVideoState.userResourceJid;
  1524. if (!updateLargeVideo && resourceJid === largeVideoResource) {
  1525. updateLargeVideo = true;
  1526. }
  1527. }
  1528. });
  1529. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  1530. endpointsEnteringLastN = lastNEndpoints;
  1531. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  1532. endpointsEnteringLastN.forEach(function (resourceJid) {
  1533. var isVisible = $('#participant_' + resourceJid).is(':visible');
  1534. showPeerContainer(resourceJid, 'show');
  1535. if (!isVisible) {
  1536. console.log("Add to last N", resourceJid);
  1537. var jid = connection.emuc.findJidFromResource(resourceJid);
  1538. var mediaStream = RTC.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  1539. var sel = $('#participant_' + resourceJid + '>video');
  1540. var videoStream = simulcast.getReceivingVideoStream(
  1541. mediaStream.stream);
  1542. RTC.attachMediaStream(sel, videoStream);
  1543. if (lastNPickupJid == mediaStream.peerjid) {
  1544. // Clean up the lastN pickup jid.
  1545. lastNPickupJid = null;
  1546. // Don't fire the events again, they've already
  1547. // been fired in the contact list click handler.
  1548. VideoLayout.handleVideoThumbClicked(
  1549. $(sel).attr('src'),
  1550. false,
  1551. Strophe.getResourceFromJid(mediaStream.peerjid));
  1552. updateLargeVideo = false;
  1553. }
  1554. waitForRemoteVideo(sel, mediaStream.ssrc, mediaStream.stream, resourceJid);
  1555. }
  1556. })
  1557. }
  1558. // The endpoint that was being shown in the large video has dropped out
  1559. // of the lastN set and there was no lastN pickup jid. We need to update
  1560. // the large video now.
  1561. if (updateLargeVideo) {
  1562. var resource, container, src;
  1563. var myResource
  1564. = Strophe.getResourceFromJid(connection.emuc.myroomjid);
  1565. // Find out which endpoint to show in the large video.
  1566. for (var i = 0; i < lastNEndpoints.length; i++) {
  1567. resource = lastNEndpoints[i];
  1568. if (!resource || resource === myResource)
  1569. continue;
  1570. container = $("#participant_" + resource);
  1571. if (container.length == 0)
  1572. continue;
  1573. src = $('video', container).attr('src');
  1574. if (!src)
  1575. continue;
  1576. // videoSrcToSsrc needs to be update for this call to succeed.
  1577. VideoLayout.updateLargeVideo(src);
  1578. break;
  1579. }
  1580. }
  1581. });
  1582. $(document).bind('videoactive.jingle', function (event, videoelem) {
  1583. if (videoelem.attr('id').indexOf('mixedmslabel') === -1) {
  1584. // ignore mixedmslabela0 and v0
  1585. videoelem.show();
  1586. VideoLayout.resizeThumbnails();
  1587. var videoParent = videoelem.parent();
  1588. var parentResourceJid = null;
  1589. if (videoParent)
  1590. parentResourceJid
  1591. = VideoLayout.getPeerContainerResourceJid(videoParent[0]);
  1592. // Update the large video to the last added video only if there's no
  1593. // current dominant, focused speaker or prezi playing or update it to
  1594. // the current dominant speaker.
  1595. if ((!focusedVideoInfo &&
  1596. !VideoLayout.getDominantSpeakerResourceJid() &&
  1597. !require("../prezi/Prezi").isPresentationVisible()) ||
  1598. (parentResourceJid &&
  1599. VideoLayout.getDominantSpeakerResourceJid() === parentResourceJid)) {
  1600. VideoLayout.updateLargeVideo(
  1601. RTC.getVideoSrc(videoelem[0]),
  1602. 1,
  1603. parentResourceJid);
  1604. }
  1605. VideoLayout.showModeratorIndicator();
  1606. }
  1607. });
  1608. $(document).bind('simulcastlayerschanging', function (event, endpointSimulcastLayers) {
  1609. endpointSimulcastLayers.forEach(function (esl) {
  1610. var resource = esl.endpoint;
  1611. // if lastN is enabled *and* the endpoint is *not* in the lastN set,
  1612. // then ignore the event (= do not preload anything).
  1613. //
  1614. // The bridge could probably stop sending this message if it's for
  1615. // an endpoint that's not in lastN.
  1616. if (lastNCount != -1
  1617. && (lastNCount < 1 || lastNEndpointsCache.indexOf(resource) === -1)) {
  1618. return;
  1619. }
  1620. var primarySSRC = esl.simulcastLayer.primarySSRC;
  1621. // Get session and stream from primary ssrc.
  1622. var res = simulcast.getReceivingVideoStreamBySSRC(primarySSRC);
  1623. var session = res.session;
  1624. var electedStream = res.stream;
  1625. if (session && electedStream) {
  1626. var msid = simulcast.getRemoteVideoStreamIdBySSRC(primarySSRC);
  1627. console.info([esl, primarySSRC, msid, session, electedStream]);
  1628. var msidParts = msid.split(' ');
  1629. var preload = (Strophe.getResourceFromJid(ssrc2jid[primarySSRC]) == largeVideoState.userResourceJid);
  1630. if (preload) {
  1631. if (largeVideoState.preload)
  1632. {
  1633. $(largeVideoState.preload).remove();
  1634. }
  1635. console.info('Preloading remote video');
  1636. largeVideoState.preload = $('<video autoplay></video>');
  1637. // ssrcs are unique in an rtp session
  1638. largeVideoState.preload_ssrc = primarySSRC;
  1639. RTC.attachMediaStream(largeVideoState.preload, electedStream)
  1640. }
  1641. } else {
  1642. console.error('Could not find a stream or a session.', session, electedStream);
  1643. }
  1644. });
  1645. });
  1646. /**
  1647. * On simulcast layers changed event.
  1648. */
  1649. $(document).bind('simulcastlayerschanged', function (event, endpointSimulcastLayers) {
  1650. endpointSimulcastLayers.forEach(function (esl) {
  1651. var resource = esl.endpoint;
  1652. // if lastN is enabled *and* the endpoint is *not* in the lastN set,
  1653. // then ignore the event (= do not change large video/thumbnail
  1654. // SRCs).
  1655. //
  1656. // Note that even if we ignore the "changed" event in this event
  1657. // handler, the bridge must continue sending these events because
  1658. // the simulcast code in simulcast.js uses it to know what's going
  1659. // to be streamed by the bridge when/if the endpoint gets back into
  1660. // the lastN set.
  1661. if (lastNCount != -1
  1662. && (lastNCount < 1 || lastNEndpointsCache.indexOf(resource) === -1)) {
  1663. return;
  1664. }
  1665. var primarySSRC = esl.simulcastLayer.primarySSRC;
  1666. // Get session and stream from primary ssrc.
  1667. var res = simulcast.getReceivingVideoStreamBySSRC(primarySSRC);
  1668. var session = res.session;
  1669. var electedStream = res.stream;
  1670. if (session && electedStream) {
  1671. var msid = simulcast.getRemoteVideoStreamIdBySSRC(primarySSRC);
  1672. console.info('Switching simulcast substream.');
  1673. console.info([esl, primarySSRC, msid, session, electedStream]);
  1674. var msidParts = msid.split(' ');
  1675. var selRemoteVideo = $(['#', 'remoteVideo_', session.sid, '_', msidParts[0]].join(''));
  1676. var updateLargeVideo = (Strophe.getResourceFromJid(ssrc2jid[primarySSRC])
  1677. == largeVideoState.userResourceJid);
  1678. var updateFocusedVideoSrc = (focusedVideoInfo && focusedVideoInfo.src && focusedVideoInfo.src != '' &&
  1679. (RTC.getVideoSrc(selRemoteVideo[0]) == focusedVideoInfo.src));
  1680. var electedStreamUrl;
  1681. if (largeVideoState.preload_ssrc == primarySSRC)
  1682. {
  1683. RTC.setVideoSrc(selRemoteVideo[0], RTC.getVideoSrc(largeVideoState.preload[0]));
  1684. }
  1685. else
  1686. {
  1687. if (largeVideoState.preload
  1688. && largeVideoState.preload != null) {
  1689. $(largeVideoState.preload).remove();
  1690. }
  1691. largeVideoState.preload_ssrc = 0;
  1692. RTC.attachMediaStream(selRemoteVideo, electedStream);
  1693. }
  1694. var jid = ssrc2jid[primarySSRC];
  1695. jid2Ssrc[jid] = primarySSRC;
  1696. if (updateLargeVideo) {
  1697. VideoLayout.updateLargeVideo(RTC.getVideoSrc(selRemoteVideo[0]), null,
  1698. Strophe.getResourceFromJid(jid));
  1699. }
  1700. if (updateFocusedVideoSrc) {
  1701. focusedVideoInfo.src = RTC.getVideoSrc(selRemoteVideo[0]);
  1702. }
  1703. var videoId;
  1704. if(resource == Strophe.getResourceFromJid(connection.emuc.myroomjid))
  1705. {
  1706. videoId = "localVideoContainer";
  1707. }
  1708. else
  1709. {
  1710. videoId = "participant_" + resource;
  1711. }
  1712. var connectionIndicator = VideoLayout.connectionIndicators[videoId];
  1713. if(connectionIndicator)
  1714. connectionIndicator.updatePopoverData();
  1715. } else {
  1716. console.error('Could not find a stream or a session.', session, electedStream);
  1717. }
  1718. });
  1719. });
  1720. /**
  1721. * Updates local stats
  1722. * @param percent
  1723. * @param object
  1724. */
  1725. my.updateLocalConnectionStats = function (percent, object) {
  1726. var resolution = null;
  1727. if(object.resolution !== null)
  1728. {
  1729. resolution = object.resolution;
  1730. object.resolution = resolution[connection.emuc.myroomjid];
  1731. delete resolution[connection.emuc.myroomjid];
  1732. }
  1733. updateStatsIndicator("localVideoContainer", percent, object);
  1734. for(var jid in resolution)
  1735. {
  1736. if(resolution[jid] === null)
  1737. continue;
  1738. var id = 'participant_' + Strophe.getResourceFromJid(jid);
  1739. if(VideoLayout.connectionIndicators[id])
  1740. {
  1741. VideoLayout.connectionIndicators[id].updateResolution(resolution[jid]);
  1742. }
  1743. }
  1744. };
  1745. /**
  1746. * Updates remote stats.
  1747. * @param jid the jid associated with the stats
  1748. * @param percent the connection quality percent
  1749. * @param object the stats data
  1750. */
  1751. my.updateConnectionStats = function (jid, percent, object) {
  1752. var resourceJid = Strophe.getResourceFromJid(jid);
  1753. var videoSpanId = 'participant_' + resourceJid;
  1754. updateStatsIndicator(videoSpanId, percent, object);
  1755. };
  1756. /**
  1757. * Removes the connection
  1758. * @param jid
  1759. */
  1760. my.removeConnectionIndicator = function (jid) {
  1761. if(VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)])
  1762. VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)].remove();
  1763. };
  1764. /**
  1765. * Hides the connection indicator
  1766. * @param jid
  1767. */
  1768. my.hideConnectionIndicator = function (jid) {
  1769. if(VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)])
  1770. VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)].hide();
  1771. };
  1772. /**
  1773. * Hides all the indicators
  1774. */
  1775. my.onStatsStop = function () {
  1776. for(var indicator in VideoLayout.connectionIndicators)
  1777. {
  1778. VideoLayout.connectionIndicators[indicator].hideIndicator();
  1779. }
  1780. };
  1781. return my;
  1782. }(VideoLayout || {}));
  1783. module.exports = VideoLayout;