Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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