Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VideoLayout.js 82KB

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