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

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