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

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