Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

VideoLayout.js 77KB

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