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

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