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

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