You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VideoLayout.js 82KB

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