Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

VideoLayout.js 80KB

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