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

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