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

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