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

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