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

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