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

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