You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VideoLayout.js 81KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297
  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. * Adds or removes icons for not available camera and microphone.
  545. * @param resourceJid the jid of user
  546. * @param devices available devices
  547. */
  548. my.setDeviceAvailabilityIcons = function (resourceJid, devices) {
  549. if(!devices)
  550. return;
  551. var container = null
  552. if(!resourceJid)
  553. {
  554. container = $("#localVideoContainer")[0];
  555. }
  556. else
  557. {
  558. container = $("#participant_" + resourceJid)[0];
  559. }
  560. if(!container)
  561. return;
  562. $("#" + container.id + " > .noMic").remove();
  563. $("#" + container.id + " > .noVideo").remove();
  564. if(!devices.audio)
  565. {
  566. container.appendChild(document.createElement("div")).setAttribute("class","noMic");
  567. }
  568. if(!devices.video)
  569. {
  570. container.appendChild(document.createElement("div")).setAttribute("class","noVideo");
  571. }
  572. if(!devices.audio && !devices.video)
  573. {
  574. $("#" + container.id + " > .noMic").css("background-position", "75%");
  575. $("#" + container.id + " > .noVideo").css("background-position", "25%");
  576. $("#" + container.id + " > .noVideo").css("background-color", "transparent");
  577. }
  578. }
  579. /**
  580. * Checks if removed video is currently displayed and tries to display
  581. * another one instead.
  582. * @param removedVideoSrc src stream identifier of the video.
  583. */
  584. my.updateRemovedVideo = function(removedVideoSrc) {
  585. if (removedVideoSrc === APP.RTC.getVideoSrc($('#largeVideo')[0])) {
  586. // this is currently displayed as large
  587. // pick the last visible video in the row
  588. // if nobody else is left, this picks the local video
  589. var pick
  590. = $('#remoteVideos>span[id!="mixedstream"]:visible:last>video')
  591. .get(0);
  592. if (!pick) {
  593. console.info("Last visible video no longer exists");
  594. pick = $('#remoteVideos>span[id!="mixedstream"]>video').get(0);
  595. if (!pick || !APP.RTC.getVideoSrc(pick)) {
  596. // Try local video
  597. console.info("Fallback to local video...");
  598. pick = $('#remoteVideos>span>span>video').get(0);
  599. }
  600. }
  601. // mute if localvideo
  602. if (pick) {
  603. var container = pick.parentNode;
  604. var jid = null;
  605. if(container)
  606. {
  607. if(container.id == "localVideoWrapper")
  608. {
  609. jid = APP.xmpp.myResource();
  610. }
  611. else
  612. {
  613. jid = VideoLayout.getPeerContainerResourceJid(container);
  614. }
  615. }
  616. VideoLayout.updateLargeVideo(APP.RTC.getVideoSrc(pick), pick.volume, jid);
  617. } else {
  618. console.warn("Failed to elect large video");
  619. }
  620. }
  621. };
  622. my.onRemoteStreamAdded = function (stream) {
  623. var container;
  624. var remotes = document.getElementById('remoteVideos');
  625. if (stream.peerjid) {
  626. VideoLayout.ensurePeerContainerExists(stream.peerjid);
  627. container = document.getElementById(
  628. 'participant_' + Strophe.getResourceFromJid(stream.peerjid));
  629. } else {
  630. var id = stream.getOriginalStream().id;
  631. if (id !== 'mixedmslabel'
  632. // FIXME: default stream is added always with new focus
  633. // (to be investigated)
  634. && id !== 'default') {
  635. console.error('can not associate stream',
  636. id,
  637. 'with a participant');
  638. // We don't want to add it here since it will cause troubles
  639. return;
  640. }
  641. // FIXME: for the mixed ms we dont need a video -- currently
  642. container = document.createElement('span');
  643. container.id = 'mixedstream';
  644. container.className = 'videocontainer';
  645. remotes.appendChild(container);
  646. UIUtil.playSoundNotification('userJoined');
  647. }
  648. if (container) {
  649. VideoLayout.addRemoteStreamElement( container,
  650. stream.sid,
  651. stream.getOriginalStream(),
  652. stream.peerjid,
  653. stream.ssrc);
  654. }
  655. }
  656. my.getLargeVideoState = function () {
  657. return largeVideoState;
  658. };
  659. /**
  660. * Updates the large video with the given new video source.
  661. */
  662. my.updateLargeVideo = function(newSrc, vol, resourceJid) {
  663. console.log('hover in', newSrc);
  664. if (APP.RTC.getVideoSrc($('#largeVideo')[0]) !== newSrc) {
  665. $('#activeSpeaker').css('visibility', 'hidden');
  666. // Due to the simulcast the localVideoSrc may have changed when the
  667. // fadeOut event triggers. In that case the getJidFromVideoSrc and
  668. // isVideoSrcDesktop methods will not function correctly.
  669. //
  670. // Also, again due to the simulcast, the updateLargeVideo method can
  671. // be called multiple times almost simultaneously. Therefore, we
  672. // store the state here and update only once.
  673. largeVideoState.newSrc = newSrc;
  674. largeVideoState.isVisible = $('#largeVideo').is(':visible');
  675. largeVideoState.isDesktop = APP.RTC.isVideoSrcDesktop(
  676. APP.xmpp.findJidFromResource(resourceJid));
  677. if(largeVideoState.userResourceJid) {
  678. largeVideoState.oldResourceJid = largeVideoState.userResourceJid;
  679. } else {
  680. largeVideoState.oldResourceJid = null;
  681. }
  682. largeVideoState.userResourceJid = resourceJid;
  683. // Screen stream is already rotated
  684. largeVideoState.flipX = (newSrc === localVideoSrc) && flipXLocalVideo;
  685. var userChanged = false;
  686. if (largeVideoState.oldResourceJid !== largeVideoState.userResourceJid) {
  687. userChanged = true;
  688. // we want the notification to trigger even if userJid is undefined,
  689. // or null.
  690. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT,
  691. largeVideoState.userResourceJid);
  692. }
  693. if (!largeVideoState.updateInProgress) {
  694. largeVideoState.updateInProgress = true;
  695. var doUpdate = function () {
  696. Avatar.updateActiveSpeakerAvatarSrc(
  697. APP.xmpp.findJidFromResource(
  698. largeVideoState.userResourceJid));
  699. if (!userChanged && largeVideoState.preload &&
  700. largeVideoState.preload !== null &&
  701. APP.RTC.getVideoSrc($(largeVideoState.preload)[0]) === newSrc)
  702. {
  703. console.info('Switching to preloaded video');
  704. var attributes = $('#largeVideo').prop("attributes");
  705. // loop through largeVideo attributes and apply them on
  706. // preload.
  707. $.each(attributes, function () {
  708. if (this.name !== 'id' && this.name !== 'src') {
  709. largeVideoState.preload.attr(this.name, this.value);
  710. }
  711. });
  712. largeVideoState.preload.appendTo($('#largeVideoContainer'));
  713. $('#largeVideo').attr('id', 'previousLargeVideo');
  714. largeVideoState.preload.attr('id', 'largeVideo');
  715. $('#previousLargeVideo').remove();
  716. largeVideoState.preload.on('loadedmetadata', function (e) {
  717. currentVideoWidth = this.videoWidth;
  718. currentVideoHeight = this.videoHeight;
  719. VideoLayout.positionLarge(currentVideoWidth, currentVideoHeight);
  720. });
  721. largeVideoState.preload = null;
  722. largeVideoState.preload_ssrc = 0;
  723. } else {
  724. APP.RTC.setVideoSrc($('#largeVideo')[0], largeVideoState.newSrc);
  725. }
  726. var videoTransform = document.getElementById('largeVideo')
  727. .style.webkitTransform;
  728. if (largeVideoState.flipX && videoTransform !== 'scaleX(-1)') {
  729. document.getElementById('largeVideo').style.webkitTransform
  730. = "scaleX(-1)";
  731. }
  732. else if (!largeVideoState.flipX && videoTransform === 'scaleX(-1)') {
  733. document.getElementById('largeVideo').style.webkitTransform
  734. = "none";
  735. }
  736. // Change the way we'll be measuring and positioning large video
  737. VideoLayout.getVideoSize = largeVideoState.isDesktop
  738. ? getDesktopVideoSize
  739. : getCameraVideoSize;
  740. VideoLayout.getVideoPosition = largeVideoState.isDesktop
  741. ? getDesktopVideoPosition
  742. : getCameraVideoPosition;
  743. // Only if the large video is currently visible.
  744. // Disable previous dominant speaker video.
  745. if (largeVideoState.oldResourceJid) {
  746. VideoLayout.enableDominantSpeaker(
  747. largeVideoState.oldResourceJid,
  748. false);
  749. }
  750. // Enable new dominant speaker in the remote videos section.
  751. if (largeVideoState.userResourceJid) {
  752. VideoLayout.enableDominantSpeaker(
  753. largeVideoState.userResourceJid,
  754. true);
  755. }
  756. if (userChanged && largeVideoState.isVisible) {
  757. // using "this" should be ok because we're called
  758. // from within the fadeOut event.
  759. $(this).fadeIn(300);
  760. }
  761. if(userChanged) {
  762. Avatar.showUserAvatar(
  763. APP.xmpp.findJidFromResource(
  764. largeVideoState.oldResourceJid));
  765. }
  766. largeVideoState.updateInProgress = false;
  767. };
  768. if (userChanged) {
  769. $('#largeVideo').fadeOut(300, doUpdate);
  770. } else {
  771. doUpdate();
  772. }
  773. }
  774. } else {
  775. Avatar.showUserAvatar(
  776. APP.xmpp.findJidFromResource(
  777. largeVideoState.userResourceJid));
  778. }
  779. };
  780. my.handleVideoThumbClicked = function(videoSrc,
  781. noPinnedEndpointChangedEvent,
  782. resourceJid) {
  783. // Restore style for previously focused video
  784. var oldContainer = null;
  785. if(focusedVideoInfo) {
  786. var focusResourceJid = focusedVideoInfo.resourceJid;
  787. oldContainer = getParticipantContainer(focusResourceJid);
  788. }
  789. if (oldContainer) {
  790. oldContainer.removeClass("videoContainerFocused");
  791. }
  792. // Unlock current focused.
  793. if (focusedVideoInfo && focusedVideoInfo.src === videoSrc)
  794. {
  795. focusedVideoInfo = null;
  796. var dominantSpeakerVideo = null;
  797. // Enable the currently set dominant speaker.
  798. if (currentDominantSpeaker) {
  799. dominantSpeakerVideo
  800. = $('#participant_' + currentDominantSpeaker + '>video')
  801. .get(0);
  802. if (dominantSpeakerVideo) {
  803. VideoLayout.updateLargeVideo(
  804. APP.RTC.getVideoSrc(dominantSpeakerVideo),
  805. 1,
  806. currentDominantSpeaker);
  807. }
  808. }
  809. if (!noPinnedEndpointChangedEvent) {
  810. eventEmitter.emit(UIEvents.PINNED_ENDPOINT);
  811. }
  812. return;
  813. }
  814. // Lock new video
  815. focusedVideoInfo = {
  816. src: videoSrc,
  817. resourceJid: resourceJid
  818. };
  819. // Update focused/pinned interface.
  820. if (resourceJid)
  821. {
  822. var container = getParticipantContainer(resourceJid);
  823. container.addClass("videoContainerFocused");
  824. if (!noPinnedEndpointChangedEvent) {
  825. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, resourceJid);
  826. }
  827. }
  828. if ($('#largeVideo').attr('src') === videoSrc &&
  829. VideoLayout.isLargeVideoOnTop()) {
  830. return;
  831. }
  832. // Triggers a "video.selected" event. The "false" parameter indicates
  833. // this isn't a prezi.
  834. $(document).trigger("video.selected", [false]);
  835. VideoLayout.updateLargeVideo(videoSrc, 1, resourceJid);
  836. $('audio').each(function (idx, el) {
  837. if (el.id.indexOf('mixedmslabel') !== -1) {
  838. el.volume = 0;
  839. el.volume = 1;
  840. }
  841. });
  842. };
  843. /**
  844. * Positions the large video.
  845. *
  846. * @param videoWidth the stream video width
  847. * @param videoHeight the stream video height
  848. */
  849. my.positionLarge = function (videoWidth, videoHeight) {
  850. var videoSpaceWidth = $('#videospace').width();
  851. var videoSpaceHeight = window.innerHeight;
  852. var videoSize = VideoLayout.getVideoSize(videoWidth,
  853. videoHeight,
  854. videoSpaceWidth,
  855. videoSpaceHeight);
  856. var largeVideoWidth = videoSize[0];
  857. var largeVideoHeight = videoSize[1];
  858. var videoPosition = VideoLayout.getVideoPosition(largeVideoWidth,
  859. largeVideoHeight,
  860. videoSpaceWidth,
  861. videoSpaceHeight);
  862. var horizontalIndent = videoPosition[0];
  863. var verticalIndent = videoPosition[1];
  864. positionVideo($('#largeVideo'),
  865. largeVideoWidth,
  866. largeVideoHeight,
  867. horizontalIndent, verticalIndent);
  868. };
  869. /**
  870. * Shows/hides the large video.
  871. */
  872. my.setLargeVideoVisible = function(isVisible) {
  873. var resourceJid = largeVideoState.userResourceJid;
  874. if (isVisible) {
  875. $('#largeVideo').css({visibility: 'visible'});
  876. $('.watermark').css({visibility: 'visible'});
  877. VideoLayout.enableDominantSpeaker(resourceJid, true);
  878. }
  879. else {
  880. $('#largeVideo').css({visibility: 'hidden'});
  881. $('#activeSpeaker').css('visibility', 'hidden');
  882. $('.watermark').css({visibility: 'hidden'});
  883. VideoLayout.enableDominantSpeaker(resourceJid, false);
  884. if(focusedVideoInfo) {
  885. var focusResourceJid = focusedVideoInfo.resourceJid;
  886. var oldContainer = getParticipantContainer(focusResourceJid);
  887. if (oldContainer && oldContainer.length > 0) {
  888. oldContainer.removeClass("videoContainerFocused");
  889. }
  890. focusedVideoInfo = null;
  891. if(focusResourceJid) {
  892. Avatar.showUserAvatar(
  893. APP.xmpp.findJidFromResource(focusResourceJid));
  894. }
  895. }
  896. }
  897. };
  898. /**
  899. * Indicates if the large video is currently visible.
  900. *
  901. * @return <tt>true</tt> if visible, <tt>false</tt> - otherwise
  902. */
  903. my.isLargeVideoVisible = function() {
  904. return $('#largeVideo').is(':visible');
  905. };
  906. my.isLargeVideoOnTop = function () {
  907. var Etherpad = require("../etherpad/Etherpad");
  908. var Prezi = require("../prezi/Prezi");
  909. return !Prezi.isPresentationVisible() && !Etherpad.isVisible();
  910. };
  911. /**
  912. * Checks if container for participant identified by given peerJid exists
  913. * in the document and creates it eventually.
  914. *
  915. * @param peerJid peer Jid to check.
  916. * @param userId user email or id for setting the avatar
  917. *
  918. * @return Returns <tt>true</tt> if the peer container exists,
  919. * <tt>false</tt> - otherwise
  920. */
  921. my.ensurePeerContainerExists = function(peerJid, userId) {
  922. ContactList.ensureAddContact(peerJid, userId);
  923. var resourceJid = Strophe.getResourceFromJid(peerJid);
  924. var videoSpanId = 'participant_' + resourceJid;
  925. if (!$('#' + videoSpanId).length) {
  926. var container =
  927. VideoLayout.addRemoteVideoContainer(peerJid, videoSpanId, userId);
  928. Avatar.setUserAvatar(peerJid, userId);
  929. // Set default display name.
  930. setDisplayName(videoSpanId);
  931. VideoLayout.connectionIndicators[videoSpanId] =
  932. new ConnectionIndicator(container, peerJid, VideoLayout);
  933. var nickfield = document.createElement('span');
  934. nickfield.className = "nick";
  935. nickfield.appendChild(document.createTextNode(resourceJid));
  936. container.appendChild(nickfield);
  937. // In case this is not currently in the last n we don't show it.
  938. if (localLastNCount
  939. && localLastNCount > 0
  940. && $('#remoteVideos>span').length >= localLastNCount + 2) {
  941. showPeerContainer(resourceJid, 'hide');
  942. }
  943. else
  944. VideoLayout.resizeThumbnails();
  945. }
  946. };
  947. my.addRemoteVideoContainer = function(peerJid, spanId) {
  948. var container = document.createElement('span');
  949. container.id = spanId;
  950. container.className = 'videocontainer';
  951. var remotes = document.getElementById('remoteVideos');
  952. remotes.appendChild(container);
  953. // If the peerJid is null then this video span couldn't be directly
  954. // associated with a participant (this could happen in the case of prezi).
  955. if (APP.xmpp.isModerator() && peerJid !== null)
  956. addRemoteVideoMenu(peerJid, container);
  957. AudioLevels.updateAudioLevelCanvas(peerJid, VideoLayout);
  958. return container;
  959. };
  960. /**
  961. * Creates an audio or video stream element.
  962. */
  963. my.createStreamElement = function (sid, stream) {
  964. var isVideo = stream.getVideoTracks().length > 0;
  965. var element = isVideo
  966. ? document.createElement('video')
  967. : document.createElement('audio');
  968. var id = (isVideo ? 'remoteVideo_' : 'remoteAudio_')
  969. + sid + '_' + APP.RTC.getStreamID(stream);
  970. element.id = id;
  971. element.autoplay = true;
  972. element.oncontextmenu = function () { return false; };
  973. return element;
  974. };
  975. my.addRemoteStreamElement
  976. = function (container, sid, stream, peerJid, thessrc) {
  977. var newElementId = null;
  978. var isVideo = stream.getVideoTracks().length > 0;
  979. if (container) {
  980. var streamElement = VideoLayout.createStreamElement(sid, stream);
  981. newElementId = streamElement.id;
  982. container.appendChild(streamElement);
  983. var sel = $('#' + newElementId);
  984. sel.hide();
  985. // If the container is currently visible we attach the stream.
  986. if (!isVideo
  987. || (container.offsetParent !== null && isVideo)) {
  988. var videoStream = APP.simulcast.getReceivingVideoStream(stream);
  989. APP.RTC.attachMediaStream(sel, videoStream);
  990. if (isVideo)
  991. waitForRemoteVideo(sel, thessrc, stream, peerJid);
  992. }
  993. stream.onended = function () {
  994. console.log('stream ended', this);
  995. VideoLayout.removeRemoteStreamElement(
  996. stream, isVideo, container);
  997. // NOTE(gp) it seems that under certain circumstances, the
  998. // onended event is not fired and thus the contact list is not
  999. // updated.
  1000. //
  1001. // The onended event of a stream should be fired when the SSRCs
  1002. // corresponding to that stream are removed from the SDP; but
  1003. // this doesn't seem to always be the case, resulting in ghost
  1004. // contacts.
  1005. //
  1006. // In an attempt to fix the ghost contacts problem, I'm moving
  1007. // the removeContact() method call in app.js, inside the
  1008. // 'muc.left' event handler.
  1009. //if (peerJid)
  1010. // ContactList.removeContact(peerJid);
  1011. };
  1012. // Add click handler.
  1013. container.onclick = function (event) {
  1014. /*
  1015. * FIXME It turns out that videoThumb may not exist (if there is
  1016. * no actual video).
  1017. */
  1018. var videoThumb = $('#' + container.id + '>video').get(0);
  1019. if (videoThumb) {
  1020. VideoLayout.handleVideoThumbClicked(
  1021. APP.RTC.getVideoSrc(videoThumb),
  1022. false,
  1023. Strophe.getResourceFromJid(peerJid));
  1024. }
  1025. event.stopPropagation();
  1026. event.preventDefault();
  1027. return false;
  1028. };
  1029. // Add hover handler
  1030. $(container).hover(
  1031. function() {
  1032. VideoLayout.showDisplayName(container.id, true);
  1033. },
  1034. function() {
  1035. var videoSrc = null;
  1036. if ($('#' + container.id + '>video')
  1037. && $('#' + container.id + '>video').length > 0) {
  1038. videoSrc = APP.RTC.getVideoSrc($('#' + container.id + '>video').get(0));
  1039. }
  1040. // If the video has been "pinned" by the user we want to
  1041. // keep the display name on place.
  1042. if (!VideoLayout.isLargeVideoVisible()
  1043. || videoSrc !== APP.RTC.getVideoSrc($('#largeVideo')[0]))
  1044. VideoLayout.showDisplayName(container.id, false);
  1045. }
  1046. );
  1047. }
  1048. return newElementId;
  1049. };
  1050. /**
  1051. * Removes the remote stream element corresponding to the given stream and
  1052. * parent container.
  1053. *
  1054. * @param stream the stream
  1055. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  1056. * @param container
  1057. */
  1058. my.removeRemoteStreamElement = function (stream, isVideo, container) {
  1059. if (!container)
  1060. return;
  1061. var select = null;
  1062. var removedVideoSrc = null;
  1063. if (isVideo) {
  1064. select = $('#' + container.id + '>video');
  1065. removedVideoSrc = APP.RTC.getVideoSrc(select.get(0));
  1066. }
  1067. else
  1068. select = $('#' + container.id + '>audio');
  1069. // Mark video as removed to cancel waiting loop(if video is removed
  1070. // before has started)
  1071. select.removed = true;
  1072. select.remove();
  1073. var audioCount = $('#' + container.id + '>audio').length;
  1074. var videoCount = $('#' + container.id + '>video').length;
  1075. if (!audioCount && !videoCount) {
  1076. console.log("Remove whole user", container.id);
  1077. if(VideoLayout.connectionIndicators[container.id])
  1078. VideoLayout.connectionIndicators[container.id].remove();
  1079. // Remove whole container
  1080. container.remove();
  1081. UIUtil.playSoundNotification('userLeft');
  1082. VideoLayout.resizeThumbnails();
  1083. }
  1084. if (removedVideoSrc)
  1085. VideoLayout.updateRemovedVideo(removedVideoSrc);
  1086. };
  1087. /**
  1088. * Show/hide peer container for the given resourceJid.
  1089. */
  1090. function showPeerContainer(resourceJid, state) {
  1091. var peerContainer = $('#participant_' + resourceJid);
  1092. if (!peerContainer)
  1093. return;
  1094. var isHide = state === 'hide';
  1095. var resizeThumbnails = false;
  1096. if (!isHide) {
  1097. if (!peerContainer.is(':visible')) {
  1098. resizeThumbnails = true;
  1099. peerContainer.show();
  1100. }
  1101. var jid = APP.xmpp.findJidFromResource(resourceJid);
  1102. if (state == 'show')
  1103. {
  1104. // peerContainer.css('-webkit-filter', '');
  1105. Avatar.showUserAvatar(jid, false);
  1106. }
  1107. else // if (state == 'avatar')
  1108. {
  1109. // peerContainer.css('-webkit-filter', 'grayscale(100%)');
  1110. Avatar.showUserAvatar(jid, true);
  1111. }
  1112. }
  1113. else if (peerContainer.is(':visible') && isHide)
  1114. {
  1115. resizeThumbnails = true;
  1116. peerContainer.hide();
  1117. if(VideoLayout.connectionIndicators['participant_' + resourceJid])
  1118. VideoLayout.connectionIndicators['participant_' + resourceJid].hide();
  1119. }
  1120. if (resizeThumbnails) {
  1121. VideoLayout.resizeThumbnails();
  1122. }
  1123. // We want to be able to pin a participant from the contact list, even
  1124. // if he's not in the lastN set!
  1125. // ContactList.setClickable(resourceJid, !isHide);
  1126. };
  1127. my.inputDisplayNameHandler = function (name) {
  1128. NicknameHandler.setNickname(name);
  1129. if (!$('#localDisplayName').is(":visible")) {
  1130. if (NicknameHandler.getNickname())
  1131. {
  1132. var meHTML = APP.translation.generateTranslatonHTML("me");
  1133. $('#localDisplayName').html(NicknameHandler.getNickname() + " (" + meHTML + ")");
  1134. }
  1135. else
  1136. {
  1137. var defaultHTML = APP.translation.generateTranslatonHTML(
  1138. interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME);
  1139. $('#localDisplayName')
  1140. .html(defaultHTML);
  1141. }
  1142. $('#localDisplayName').show();
  1143. }
  1144. $('#editDisplayName').hide();
  1145. };
  1146. /**
  1147. * Shows/hides the display name on the remote video.
  1148. * @param videoSpanId the identifier of the video span element
  1149. * @param isShow indicates if the display name should be shown or hidden
  1150. */
  1151. my.showDisplayName = function(videoSpanId, isShow) {
  1152. var nameSpan = $('#' + videoSpanId + '>span.displayname').get(0);
  1153. if (isShow) {
  1154. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  1155. nameSpan.setAttribute("style", "display:inline-block;");
  1156. }
  1157. else {
  1158. if (nameSpan)
  1159. nameSpan.setAttribute("style", "display:none;");
  1160. }
  1161. };
  1162. /**
  1163. * Shows the presence status message for the given video.
  1164. */
  1165. my.setPresenceStatus = function (videoSpanId, statusMsg) {
  1166. if (!$('#' + videoSpanId).length) {
  1167. // No container
  1168. return;
  1169. }
  1170. var statusSpan = $('#' + videoSpanId + '>span.status');
  1171. if (!statusSpan.length) {
  1172. //Add status span
  1173. statusSpan = document.createElement('span');
  1174. statusSpan.className = 'status';
  1175. statusSpan.id = videoSpanId + '_status';
  1176. $('#' + videoSpanId)[0].appendChild(statusSpan);
  1177. statusSpan = $('#' + videoSpanId + '>span.status');
  1178. }
  1179. // Display status
  1180. if (statusMsg && statusMsg.length) {
  1181. $('#' + videoSpanId + '_status').text(statusMsg);
  1182. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  1183. }
  1184. else {
  1185. // Hide
  1186. statusSpan.get(0).setAttribute("style", "display:none;");
  1187. }
  1188. };
  1189. /**
  1190. * Shows a visual indicator for the moderator of the conference.
  1191. */
  1192. my.showModeratorIndicator = function () {
  1193. var isModerator = APP.xmpp.isModerator();
  1194. if (isModerator) {
  1195. var indicatorSpan = $('#localVideoContainer .focusindicator');
  1196. if (indicatorSpan.children().length === 0)
  1197. {
  1198. createModeratorIndicatorElement(indicatorSpan[0]);
  1199. //translates text in focus indicator
  1200. APP.translation.translateElement($('#localVideoContainer .focusindicator'));
  1201. }
  1202. }
  1203. var members = APP.xmpp.getMembers();
  1204. Object.keys(members).forEach(function (jid) {
  1205. if (Strophe.getResourceFromJid(jid) === 'focus') {
  1206. // Skip server side focus
  1207. return;
  1208. }
  1209. var resourceJid = Strophe.getResourceFromJid(jid);
  1210. var videoSpanId = 'participant_' + resourceJid;
  1211. var videoContainer = document.getElementById(videoSpanId);
  1212. if (!videoContainer) {
  1213. console.error("No video container for " + jid);
  1214. return;
  1215. }
  1216. var member = members[jid];
  1217. if (member.role === 'moderator') {
  1218. // Remove menu if peer is moderator
  1219. var menuSpan = $('#' + videoSpanId + '>span.remotevideomenu');
  1220. if (menuSpan.length) {
  1221. removeRemoteVideoMenu(videoSpanId);
  1222. }
  1223. // Show moderator indicator
  1224. var indicatorSpan
  1225. = $('#' + videoSpanId + ' .focusindicator');
  1226. if (!indicatorSpan || indicatorSpan.length === 0) {
  1227. indicatorSpan = document.createElement('span');
  1228. indicatorSpan.className = 'focusindicator';
  1229. videoContainer.appendChild(indicatorSpan);
  1230. createModeratorIndicatorElement(indicatorSpan);
  1231. //translates text in focus indicators
  1232. APP.translation.translateElement($('#' + videoSpanId + ' .focusindicator'));
  1233. }
  1234. } else if (isModerator) {
  1235. // We are moderator, but user is not - add menu
  1236. if ($('#remote_popupmenu_' + resourceJid).length <= 0) {
  1237. addRemoteVideoMenu(
  1238. jid,
  1239. document.getElementById('participant_' + resourceJid));
  1240. }
  1241. }
  1242. });
  1243. };
  1244. /**
  1245. * Shows video muted indicator over small videos.
  1246. */
  1247. my.showVideoIndicator = function(videoSpanId, isMuted) {
  1248. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  1249. if (isMuted === 'false') {
  1250. if (videoMutedSpan.length > 0) {
  1251. videoMutedSpan.remove();
  1252. }
  1253. }
  1254. else {
  1255. if(videoMutedSpan.length == 0) {
  1256. videoMutedSpan = document.createElement('span');
  1257. videoMutedSpan.className = 'videoMuted';
  1258. $('#' + videoSpanId)[0].appendChild(videoMutedSpan);
  1259. var mutedIndicator = document.createElement('i');
  1260. mutedIndicator.className = 'icon-camera-disabled';
  1261. UIUtil.setTooltip(mutedIndicator,
  1262. "videothumbnail.videomute",
  1263. "top");
  1264. videoMutedSpan.appendChild(mutedIndicator);
  1265. //translate texts for muted indicator
  1266. APP.translation.translateElement($('#' + videoSpanId + " > span > i"));
  1267. }
  1268. VideoLayout.updateMutePosition(videoSpanId);
  1269. }
  1270. };
  1271. my.updateMutePosition = function (videoSpanId) {
  1272. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  1273. var connectionIndicator = $('#' + videoSpanId + '>div.connectionindicator');
  1274. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  1275. if(connectionIndicator.length > 0
  1276. && connectionIndicator[0].style.display != "none") {
  1277. audioMutedSpan.css({right: "23px"});
  1278. videoMutedSpan.css({right: ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  1279. }
  1280. else
  1281. {
  1282. audioMutedSpan.css({right: "0px"});
  1283. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  1284. }
  1285. }
  1286. /**
  1287. * Shows audio muted indicator over small videos.
  1288. * @param {string} isMuted
  1289. */
  1290. my.showAudioIndicator = function(videoSpanId, isMuted) {
  1291. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  1292. if (isMuted === 'false') {
  1293. if (audioMutedSpan.length > 0) {
  1294. audioMutedSpan.popover('hide');
  1295. audioMutedSpan.remove();
  1296. }
  1297. }
  1298. else {
  1299. if(audioMutedSpan.length == 0 ) {
  1300. audioMutedSpan = document.createElement('span');
  1301. audioMutedSpan.className = 'audioMuted';
  1302. UIUtil.setTooltip(audioMutedSpan,
  1303. "videothumbnail.mute",
  1304. "top");
  1305. $('#' + videoSpanId)[0].appendChild(audioMutedSpan);
  1306. APP.translation.translateElement($('#' + videoSpanId + " > span"));
  1307. var mutedIndicator = document.createElement('i');
  1308. mutedIndicator.className = 'icon-mic-disabled';
  1309. audioMutedSpan.appendChild(mutedIndicator);
  1310. }
  1311. VideoLayout.updateMutePosition(videoSpanId);
  1312. }
  1313. };
  1314. /*
  1315. * Shows or hides the audio muted indicator over the local thumbnail video.
  1316. * @param {boolean} isMuted
  1317. */
  1318. my.showLocalAudioIndicator = function(isMuted) {
  1319. VideoLayout.showAudioIndicator('localVideoContainer', isMuted.toString());
  1320. };
  1321. /**
  1322. * Resizes the large video container.
  1323. */
  1324. my.resizeLargeVideoContainer = function () {
  1325. Chat.resizeChat();
  1326. var availableHeight = window.innerHeight;
  1327. var availableWidth = UIUtil.getAvailableVideoWidth();
  1328. if (availableWidth < 0 || availableHeight < 0) return;
  1329. $('#videospace').width(availableWidth);
  1330. $('#videospace').height(availableHeight);
  1331. $('#largeVideoContainer').width(availableWidth);
  1332. $('#largeVideoContainer').height(availableHeight);
  1333. var avatarSize = interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE;
  1334. var top = availableHeight / 2 - avatarSize / 4 * 3;
  1335. $('#activeSpeaker').css('top', top);
  1336. VideoLayout.resizeThumbnails();
  1337. };
  1338. /**
  1339. * Resizes thumbnails.
  1340. */
  1341. my.resizeThumbnails = function() {
  1342. var videoSpaceWidth = $('#remoteVideos').width();
  1343. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  1344. var width = thumbnailSize[0];
  1345. var height = thumbnailSize[1];
  1346. // size videos so that while keeping AR and max height, we have a
  1347. // nice fit
  1348. $('#remoteVideos').height(height);
  1349. $('#remoteVideos>span').width(width);
  1350. $('#remoteVideos>span').height(height);
  1351. $('.userAvatar').css('left', (width - height) / 2);
  1352. $(document).trigger("remotevideo.resized", [width, height]);
  1353. };
  1354. /**
  1355. * Enables the dominant speaker UI.
  1356. *
  1357. * @param resourceJid the jid indicating the video element to
  1358. * activate/deactivate
  1359. * @param isEnable indicates if the dominant speaker should be enabled or
  1360. * disabled
  1361. */
  1362. my.enableDominantSpeaker = function(resourceJid, isEnable) {
  1363. var videoSpanId = null;
  1364. var videoContainerId = null;
  1365. if (resourceJid
  1366. === APP.xmpp.myResource()) {
  1367. videoSpanId = 'localVideoWrapper';
  1368. videoContainerId = 'localVideoContainer';
  1369. }
  1370. else {
  1371. videoSpanId = 'participant_' + resourceJid;
  1372. videoContainerId = videoSpanId;
  1373. }
  1374. var displayName = resourceJid;
  1375. var nameSpan = $('#' + videoContainerId + '>span.displayname');
  1376. if (nameSpan.length > 0)
  1377. displayName = nameSpan.html();
  1378. console.log("UI enable dominant speaker",
  1379. displayName,
  1380. resourceJid,
  1381. isEnable);
  1382. videoSpan = document.getElementById(videoContainerId);
  1383. if (!videoSpan) {
  1384. return;
  1385. }
  1386. var video = $('#' + videoSpanId + '>video');
  1387. if (video && video.length > 0) {
  1388. if (isEnable) {
  1389. var isLargeVideoVisible = VideoLayout.isLargeVideoOnTop();
  1390. VideoLayout.showDisplayName(videoContainerId, isLargeVideoVisible);
  1391. if (!videoSpan.classList.contains("dominantspeaker"))
  1392. videoSpan.classList.add("dominantspeaker");
  1393. }
  1394. else {
  1395. VideoLayout.showDisplayName(videoContainerId, false);
  1396. if (videoSpan.classList.contains("dominantspeaker"))
  1397. videoSpan.classList.remove("dominantspeaker");
  1398. }
  1399. Avatar.showUserAvatar(
  1400. APP.xmpp.findJidFromResource(resourceJid));
  1401. }
  1402. };
  1403. /**
  1404. * Calculates the thumbnail size.
  1405. *
  1406. * @param videoSpaceWidth the width of the video space
  1407. */
  1408. my.calculateThumbnailSize = function (videoSpaceWidth) {
  1409. // Calculate the available height, which is the inner window height minus
  1410. // 39px for the header minus 2px for the delimiter lines on the top and
  1411. // bottom of the large video, minus the 36px space inside the remoteVideos
  1412. // container used for highlighting shadow.
  1413. var availableHeight = 100;
  1414. var numvids = $('#remoteVideos>span:visible').length;
  1415. if (localLastNCount && localLastNCount > 0) {
  1416. numvids = Math.min(localLastNCount + 1, numvids);
  1417. }
  1418. // Remove the 3px borders arround videos and border around the remote
  1419. // videos area and the 4 pixels between the local video and the others
  1420. //TODO: Find out where the 4 pixels come from and remove them
  1421. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70 - 4;
  1422. var availableWidth = availableWinWidth / numvids;
  1423. var aspectRatio = 16.0 / 9.0;
  1424. var maxHeight = Math.min(160, availableHeight);
  1425. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  1426. if (availableHeight < availableWidth / aspectRatio) {
  1427. availableWidth = Math.floor(availableHeight * aspectRatio);
  1428. }
  1429. return [availableWidth, availableHeight];
  1430. };
  1431. /**
  1432. * Updates the remote video menu.
  1433. *
  1434. * @param jid the jid indicating the video for which we're adding a menu.
  1435. * @param isMuted indicates the current mute state
  1436. */
  1437. my.updateRemoteVideoMenu = function(jid, isMuted) {
  1438. var muteMenuItem
  1439. = $('#remote_popupmenu_'
  1440. + Strophe.getResourceFromJid(jid)
  1441. + '>li>a.mutelink');
  1442. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  1443. if (muteMenuItem.length) {
  1444. var muteLink = muteMenuItem.get(0);
  1445. if (isMuted === 'true') {
  1446. muteLink.innerHTML = mutedIndicator + ' Muted';
  1447. muteLink.className = 'mutelink disabled';
  1448. }
  1449. else {
  1450. muteLink.innerHTML = mutedIndicator + ' Mute';
  1451. muteLink.className = 'mutelink';
  1452. }
  1453. }
  1454. };
  1455. /**
  1456. * Returns the current dominant speaker resource jid.
  1457. */
  1458. my.getDominantSpeakerResourceJid = function () {
  1459. return currentDominantSpeaker;
  1460. };
  1461. /**
  1462. * Returns the corresponding resource jid to the given peer container
  1463. * DOM element.
  1464. *
  1465. * @return the corresponding resource jid to the given peer container
  1466. * DOM element
  1467. */
  1468. my.getPeerContainerResourceJid = function (containerElement) {
  1469. var i = containerElement.id.indexOf('participant_');
  1470. if (i >= 0)
  1471. return containerElement.id.substring(i + 12);
  1472. };
  1473. /**
  1474. * On contact list item clicked.
  1475. */
  1476. $(ContactList).bind('contactclicked', function(event, jid) {
  1477. if (!jid) {
  1478. return;
  1479. }
  1480. var resource = Strophe.getResourceFromJid(jid);
  1481. var videoContainer = $("#participant_" + resource);
  1482. if (videoContainer.length > 0) {
  1483. var videoThumb = $('video', videoContainer).get(0);
  1484. // It is not always the case that a videoThumb exists (if there is
  1485. // no actual video).
  1486. if (videoThumb) {
  1487. if (videoThumb.src && videoThumb.src != '') {
  1488. // We have a video src, great! Let's update the large video
  1489. // now.
  1490. VideoLayout.handleVideoThumbClicked(
  1491. videoThumb.src,
  1492. false,
  1493. Strophe.getResourceFromJid(jid));
  1494. } else {
  1495. // If we don't have a video src for jid, there's absolutely
  1496. // no point in calling handleVideoThumbClicked; Quite
  1497. // simply, it won't work because it needs an src to attach
  1498. // to the large video.
  1499. //
  1500. // Instead, we trigger the pinned endpoint changed event to
  1501. // let the bridge adjust its lastN set for myjid and store
  1502. // the pinned user in the lastNPickupJid variable to be
  1503. // picked up later by the lastN changed event handler.
  1504. lastNPickupJid = jid;
  1505. eventEmitter.emit(UIEvents.PINNED_ENDPOINT,
  1506. Strophe.getResourceFromJid(jid));
  1507. }
  1508. } else if (jid == APP.xmpp.myJid()) {
  1509. $("#localVideoContainer").click();
  1510. }
  1511. }
  1512. });
  1513. /**
  1514. * On audio muted event.
  1515. */
  1516. $(document).bind('audiomuted.muc', function (event, jid, isMuted) {
  1517. /*
  1518. // FIXME: but focus can not mute in this case ? - check
  1519. if (jid === xmpp.myJid()) {
  1520. // The local mute indicator is controlled locally
  1521. return;
  1522. }*/
  1523. var videoSpanId = null;
  1524. if (jid === APP.xmpp.myJid()) {
  1525. videoSpanId = 'localVideoContainer';
  1526. } else {
  1527. VideoLayout.ensurePeerContainerExists(jid);
  1528. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  1529. }
  1530. mutedAudios[jid] = isMuted;
  1531. if (APP.xmpp.isModerator()) {
  1532. VideoLayout.updateRemoteVideoMenu(jid, isMuted);
  1533. }
  1534. if (videoSpanId)
  1535. VideoLayout.showAudioIndicator(videoSpanId, isMuted);
  1536. });
  1537. /**
  1538. * On video muted event.
  1539. */
  1540. $(document).bind('videomuted.muc', function (event, jid, value) {
  1541. var isMuted = (value === "true");
  1542. if(jid !== APP.xmpp.myJid() && !APP.RTC.muteRemoteVideoStream(jid, isMuted))
  1543. return;
  1544. Avatar.showUserAvatar(jid, isMuted);
  1545. var videoSpanId = null;
  1546. if (jid === APP.xmpp.myJid()) {
  1547. videoSpanId = 'localVideoContainer';
  1548. } else {
  1549. VideoLayout.ensurePeerContainerExists(jid);
  1550. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  1551. }
  1552. if (videoSpanId)
  1553. VideoLayout.showVideoIndicator(videoSpanId, value);
  1554. });
  1555. /**
  1556. * Display name changed.
  1557. */
  1558. my.onDisplayNameChanged =
  1559. function (jid, displayName, status) {
  1560. if (jid === 'localVideoContainer'
  1561. || jid === APP.xmpp.myJid()) {
  1562. setDisplayName('localVideoContainer',
  1563. displayName);
  1564. } else {
  1565. VideoLayout.ensurePeerContainerExists(jid);
  1566. setDisplayName(
  1567. 'participant_' + Strophe.getResourceFromJid(jid),
  1568. displayName,
  1569. status);
  1570. }
  1571. };
  1572. /**
  1573. * On dominant speaker changed event.
  1574. */
  1575. my.onDominantSpeakerChanged = function (resourceJid) {
  1576. // We ignore local user events.
  1577. if (resourceJid
  1578. === APP.xmpp.myResource())
  1579. return;
  1580. var members = APP.xmpp.getMembers();
  1581. // Update the current dominant speaker.
  1582. if (resourceJid !== currentDominantSpeaker) {
  1583. var oldSpeakerVideoSpanId = "participant_" + currentDominantSpeaker,
  1584. newSpeakerVideoSpanId = "participant_" + resourceJid;
  1585. var currentJID = APP.xmpp.findJidFromResource(currentDominantSpeaker);
  1586. var newJID = APP.xmpp.findJidFromResource(resourceJid);
  1587. if(currentDominantSpeaker && (!members || !members[currentJID] ||
  1588. !members[currentJID].displayName)) {
  1589. setDisplayName(oldSpeakerVideoSpanId, null);
  1590. }
  1591. if(resourceJid && (!members || !members[newJID] ||
  1592. !members[newJID].displayName)) {
  1593. setDisplayName(newSpeakerVideoSpanId, null,
  1594. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  1595. }
  1596. currentDominantSpeaker = resourceJid;
  1597. } else {
  1598. return;
  1599. }
  1600. // Obtain container for new dominant speaker.
  1601. var container = document.getElementById(
  1602. 'participant_' + resourceJid);
  1603. // Local video will not have container found, but that's ok
  1604. // since we don't want to switch to local video.
  1605. if (container && !focusedVideoInfo)
  1606. {
  1607. var video = container.getElementsByTagName("video");
  1608. // Update the large video if the video source is already available,
  1609. // otherwise wait for the "videoactive.jingle" event.
  1610. if (video.length && video[0].currentTime > 0)
  1611. VideoLayout.updateLargeVideo(APP.RTC.getVideoSrc(video[0]), resourceJid);
  1612. }
  1613. };
  1614. /**
  1615. * On last N change event.
  1616. *
  1617. * @param lastNEndpoints the list of last N endpoints
  1618. * @param endpointsEnteringLastN the list currently entering last N
  1619. * endpoints
  1620. */
  1621. my.onLastNEndpointsChanged = function ( lastNEndpoints,
  1622. endpointsEnteringLastN,
  1623. stream) {
  1624. if (lastNCount !== lastNEndpoints.length)
  1625. lastNCount = lastNEndpoints.length;
  1626. lastNEndpointsCache = lastNEndpoints;
  1627. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  1628. //
  1629. // If LastN drops to, say, 2, because of adaptivity, then E should see
  1630. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  1631. // so E sees them. C is only in E's local LastN set.
  1632. //
  1633. // If F starts talking and LastN = 3, then E should see thumbnails for
  1634. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  1635. // enters E's local LastN ejecting C.
  1636. // Increase the local LastN set size, if necessary.
  1637. if (lastNCount > localLastNCount) {
  1638. localLastNCount = lastNCount;
  1639. }
  1640. // Update the local LastN set preserving the order in which the
  1641. // endpoints appeared in the LastN/local LastN set.
  1642. var nextLocalLastNSet = lastNEndpoints.slice(0);
  1643. for (var i = 0; i < localLastNSet.length; i++) {
  1644. if (nextLocalLastNSet.length >= localLastNCount) {
  1645. break;
  1646. }
  1647. var resourceJid = localLastNSet[i];
  1648. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  1649. nextLocalLastNSet.push(resourceJid);
  1650. }
  1651. }
  1652. localLastNSet = nextLocalLastNSet;
  1653. var updateLargeVideo = false;
  1654. // Handle LastN/local LastN changes.
  1655. $('#remoteVideos>span').each(function( index, element ) {
  1656. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  1657. var isReceived = true;
  1658. if (resourceJid
  1659. && lastNEndpoints.indexOf(resourceJid) < 0
  1660. && localLastNSet.indexOf(resourceJid) < 0) {
  1661. console.log("Remove from last N", resourceJid);
  1662. showPeerContainer(resourceJid, 'hide');
  1663. isReceived = false;
  1664. } else if (resourceJid
  1665. && $('#participant_' + resourceJid).is(':visible')
  1666. && lastNEndpoints.indexOf(resourceJid) < 0
  1667. && localLastNSet.indexOf(resourceJid) >= 0) {
  1668. showPeerContainer(resourceJid, 'avatar');
  1669. isReceived = false;
  1670. }
  1671. if (!isReceived) {
  1672. // resourceJid has dropped out of the server side lastN set, so
  1673. // it is no longer being received. If resourceJid was being
  1674. // displayed in the large video we have to switch to another
  1675. // user.
  1676. var largeVideoResource = largeVideoState.userResourceJid;
  1677. if (!updateLargeVideo && resourceJid === largeVideoResource) {
  1678. updateLargeVideo = true;
  1679. }
  1680. }
  1681. });
  1682. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  1683. endpointsEnteringLastN = lastNEndpoints;
  1684. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  1685. endpointsEnteringLastN.forEach(function (resourceJid) {
  1686. var isVisible = $('#participant_' + resourceJid).is(':visible');
  1687. showPeerContainer(resourceJid, 'show');
  1688. if (!isVisible) {
  1689. console.log("Add to last N", resourceJid);
  1690. var jid = APP.xmpp.findJidFromResource(resourceJid);
  1691. var mediaStream = APP.RTC.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  1692. var sel = $('#participant_' + resourceJid + '>video');
  1693. var videoStream = APP.simulcast.getReceivingVideoStream(
  1694. mediaStream.stream);
  1695. APP.RTC.attachMediaStream(sel, videoStream);
  1696. if (lastNPickupJid == mediaStream.peerjid) {
  1697. // Clean up the lastN pickup jid.
  1698. lastNPickupJid = null;
  1699. // Don't fire the events again, they've already
  1700. // been fired in the contact list click handler.
  1701. VideoLayout.handleVideoThumbClicked(
  1702. $(sel).attr('src'),
  1703. false,
  1704. Strophe.getResourceFromJid(mediaStream.peerjid));
  1705. updateLargeVideo = false;
  1706. }
  1707. waitForRemoteVideo(sel, mediaStream.ssrc, mediaStream.stream, resourceJid);
  1708. }
  1709. })
  1710. }
  1711. // The endpoint that was being shown in the large video has dropped out
  1712. // of the lastN set and there was no lastN pickup jid. We need to update
  1713. // the large video now.
  1714. if (updateLargeVideo) {
  1715. var resource, container, src;
  1716. var myResource
  1717. = APP.xmpp.myResource();
  1718. // Find out which endpoint to show in the large video.
  1719. for (var i = 0; i < lastNEndpoints.length; i++) {
  1720. resource = lastNEndpoints[i];
  1721. if (!resource || resource === myResource)
  1722. continue;
  1723. container = $("#participant_" + resource);
  1724. if (container.length == 0)
  1725. continue;
  1726. src = $('video', container).attr('src');
  1727. if (!src)
  1728. continue;
  1729. // videoSrcToSsrc needs to be update for this call to succeed.
  1730. VideoLayout.updateLargeVideo(src);
  1731. break;
  1732. }
  1733. }
  1734. };
  1735. my.onSimulcastLayersChanging = function (endpointSimulcastLayers) {
  1736. endpointSimulcastLayers.forEach(function (esl) {
  1737. var resource = esl.endpoint;
  1738. // if lastN is enabled *and* the endpoint is *not* in the lastN set,
  1739. // then ignore the event (= do not preload anything).
  1740. //
  1741. // The bridge could probably stop sending this message if it's for
  1742. // an endpoint that's not in lastN.
  1743. if (lastNCount != -1
  1744. && (lastNCount < 1 || lastNEndpointsCache.indexOf(resource) === -1)) {
  1745. return;
  1746. }
  1747. var primarySSRC = esl.simulcastLayer.primarySSRC;
  1748. // Get session and stream from primary ssrc.
  1749. var res = APP.simulcast.getReceivingVideoStreamBySSRC(primarySSRC);
  1750. var sid = res.sid;
  1751. var electedStream = res.stream;
  1752. if (sid && electedStream) {
  1753. var msid = APP.simulcast.getRemoteVideoStreamIdBySSRC(primarySSRC);
  1754. console.info([esl, primarySSRC, msid, sid, electedStream]);
  1755. var preload = (Strophe.getResourceFromJid(APP.xmpp.getJidFromSSRC(primarySSRC)) == largeVideoState.userResourceJid);
  1756. if (preload) {
  1757. if (largeVideoState.preload)
  1758. {
  1759. $(largeVideoState.preload).remove();
  1760. }
  1761. console.info('Preloading remote video');
  1762. largeVideoState.preload = $('<video autoplay></video>');
  1763. // ssrcs are unique in an rtp session
  1764. largeVideoState.preload_ssrc = primarySSRC;
  1765. APP.RTC.attachMediaStream(largeVideoState.preload, electedStream)
  1766. }
  1767. } else {
  1768. console.error('Could not find a stream or a session.', sid, electedStream);
  1769. }
  1770. });
  1771. };
  1772. /**
  1773. * On simulcast layers changed event.
  1774. */
  1775. my.onSimulcastLayersChanged = function (endpointSimulcastLayers) {
  1776. endpointSimulcastLayers.forEach(function (esl) {
  1777. var resource = esl.endpoint;
  1778. // if lastN is enabled *and* the endpoint is *not* in the lastN set,
  1779. // then ignore the event (= do not change large video/thumbnail
  1780. // SRCs).
  1781. //
  1782. // Note that even if we ignore the "changed" event in this event
  1783. // handler, the bridge must continue sending these events because
  1784. // the simulcast code in simulcast.js uses it to know what's going
  1785. // to be streamed by the bridge when/if the endpoint gets back into
  1786. // the lastN set.
  1787. if (lastNCount != -1
  1788. && (lastNCount < 1 || lastNEndpointsCache.indexOf(resource) === -1)) {
  1789. return;
  1790. }
  1791. var primarySSRC = esl.simulcastLayer.primarySSRC;
  1792. // Get session and stream from primary ssrc.
  1793. var res = APP.simulcast.getReceivingVideoStreamBySSRC(primarySSRC);
  1794. var sid = res.sid;
  1795. var electedStream = res.stream;
  1796. if (sid && electedStream) {
  1797. var msid = APP.simulcast.getRemoteVideoStreamIdBySSRC(primarySSRC);
  1798. console.info('Switching simulcast substream.');
  1799. console.info([esl, primarySSRC, msid, sid, electedStream]);
  1800. var msidParts = msid.split(' ');
  1801. var selRemoteVideo = $(['#', 'remoteVideo_', sid, '_', msidParts[0]].join(''));
  1802. var updateLargeVideo = (Strophe.getResourceFromJid(APP.xmpp.getJidFromSSRC(primarySSRC))
  1803. == largeVideoState.userResourceJid);
  1804. var updateFocusedVideoSrc = (focusedVideoInfo && focusedVideoInfo.src && focusedVideoInfo.src != '' &&
  1805. (APP.RTC.getVideoSrc(selRemoteVideo[0]) == focusedVideoInfo.src));
  1806. var electedStreamUrl;
  1807. if (largeVideoState.preload_ssrc == primarySSRC)
  1808. {
  1809. APP.RTC.setVideoSrc(selRemoteVideo[0], APP.RTC.getVideoSrc(largeVideoState.preload[0]));
  1810. }
  1811. else
  1812. {
  1813. if (largeVideoState.preload
  1814. && largeVideoState.preload != null) {
  1815. $(largeVideoState.preload).remove();
  1816. }
  1817. largeVideoState.preload_ssrc = 0;
  1818. APP.RTC.attachMediaStream(selRemoteVideo, electedStream);
  1819. }
  1820. var jid = APP.xmpp.getJidFromSSRC(primarySSRC);
  1821. if (updateLargeVideo) {
  1822. VideoLayout.updateLargeVideo(APP.RTC.getVideoSrc(selRemoteVideo[0]), null,
  1823. Strophe.getResourceFromJid(jid));
  1824. }
  1825. if (updateFocusedVideoSrc) {
  1826. focusedVideoInfo.src = APP.RTC.getVideoSrc(selRemoteVideo[0]);
  1827. }
  1828. var videoId;
  1829. if(resource == APP.xmpp.myResource())
  1830. {
  1831. videoId = "localVideoContainer";
  1832. }
  1833. else
  1834. {
  1835. videoId = "participant_" + resource;
  1836. }
  1837. var connectionIndicator = VideoLayout.connectionIndicators[videoId];
  1838. if(connectionIndicator)
  1839. connectionIndicator.updatePopoverData();
  1840. } else {
  1841. console.error('Could not find a stream or a sid.', sid, electedStream);
  1842. }
  1843. });
  1844. };
  1845. /**
  1846. * Updates local stats
  1847. * @param percent
  1848. * @param object
  1849. */
  1850. my.updateLocalConnectionStats = function (percent, object) {
  1851. var resolution = null;
  1852. if(object.resolution !== null)
  1853. {
  1854. resolution = object.resolution;
  1855. object.resolution = resolution[APP.xmpp.myJid()];
  1856. delete resolution[APP.xmpp.myJid()];
  1857. }
  1858. updateStatsIndicator("localVideoContainer", percent, object);
  1859. for(var jid in resolution)
  1860. {
  1861. if(resolution[jid] === null)
  1862. continue;
  1863. var id = 'participant_' + Strophe.getResourceFromJid(jid);
  1864. if(VideoLayout.connectionIndicators[id])
  1865. {
  1866. VideoLayout.connectionIndicators[id].updateResolution(resolution[jid]);
  1867. }
  1868. }
  1869. };
  1870. /**
  1871. * Updates remote stats.
  1872. * @param jid the jid associated with the stats
  1873. * @param percent the connection quality percent
  1874. * @param object the stats data
  1875. */
  1876. my.updateConnectionStats = function (jid, percent, object) {
  1877. var resourceJid = Strophe.getResourceFromJid(jid);
  1878. var videoSpanId = 'participant_' + resourceJid;
  1879. updateStatsIndicator(videoSpanId, percent, object);
  1880. };
  1881. /**
  1882. * Removes the connection
  1883. * @param jid
  1884. */
  1885. my.removeConnectionIndicator = function (jid) {
  1886. if(VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)])
  1887. VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)].remove();
  1888. };
  1889. /**
  1890. * Hides the connection indicator
  1891. * @param jid
  1892. */
  1893. my.hideConnectionIndicator = function (jid) {
  1894. if(VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)])
  1895. VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)].hide();
  1896. };
  1897. /**
  1898. * Hides all the indicators
  1899. */
  1900. my.onStatsStop = function () {
  1901. for(var indicator in VideoLayout.connectionIndicators)
  1902. {
  1903. VideoLayout.connectionIndicators[indicator].hideIndicator();
  1904. }
  1905. };
  1906. my.participantLeft = function (jid) {
  1907. // Unlock large video
  1908. if (focusedVideoInfo && focusedVideoInfo.jid === jid)
  1909. {
  1910. console.info("Focused video owner has left the conference");
  1911. focusedVideoInfo = null;
  1912. }
  1913. }
  1914. my.onVideoTypeChanged = function (jid) {
  1915. if(jid &&
  1916. Strophe.getResourceFromJid(jid) === largeVideoState.userResourceJid)
  1917. {
  1918. largeVideoState.isDesktop = APP.RTC.isVideoSrcDesktop(jid);
  1919. VideoLayout.getVideoSize = largeVideoState.isDesktop
  1920. ? getDesktopVideoSize
  1921. : getCameraVideoSize;
  1922. VideoLayout.getVideoPosition = largeVideoState.isDesktop
  1923. ? getDesktopVideoPosition
  1924. : getCameraVideoPosition;
  1925. VideoLayout.positionLarge(null, null);
  1926. }
  1927. }
  1928. return my;
  1929. }(VideoLayout || {}));
  1930. module.exports = VideoLayout;