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

VideoLayout.js 76KB

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