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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /* global config, APP, $, interfaceConfig */
  2. /* jshint -W101 */
  3. import AudioLevels from "../audio_levels/AudioLevels";
  4. import Avatar from "../avatar/Avatar";
  5. import BottomToolbar from "../toolbars/BottomToolbar";
  6. import UIEvents from "../../../service/UI/UIEvents";
  7. import UIUtil from "../util/UIUtil";
  8. import RemoteVideo from "./RemoteVideo";
  9. import LargeVideoManager, {VideoContainerType} from "./LargeVideo";
  10. import {PreziContainerType} from '../prezi/Prezi';
  11. import LocalVideo from "./LocalVideo";
  12. var RTCBrowserType = require('../../RTC/RTCBrowserType');
  13. var remoteVideos = {};
  14. var remoteVideoTypes = {};
  15. var localVideoThumbnail = null;
  16. var currentDominantSpeaker = null;
  17. var lastNCount = config.channelLastN;
  18. var localLastNCount = config.channelLastN;
  19. var localLastNSet = [];
  20. var lastNEndpointsCache = [];
  21. var lastNPickupId = null;
  22. var eventEmitter = null;
  23. /**
  24. * Currently focused video jid
  25. * @type {String}
  26. */
  27. var focusedVideoResourceJid = null;
  28. const thumbAspectRatio = 16.0 / 9.0;
  29. /**
  30. * On contact list item clicked.
  31. */
  32. function onContactClicked (id) {
  33. if (APP.conference.isLocalId(id)) {
  34. $("#localVideoContainer").click();
  35. return;
  36. }
  37. let remoteVideo = remoteVideos[id];
  38. if (remoteVideo && remoteVideo.selectVideoElement().length) {
  39. let videoThumb = remoteVideo.selectVideoElement()[0];
  40. // It is not always the case that a videoThumb exists (if there is
  41. // no actual video).
  42. if (RTC.getVideoSrc(videoThumb)) {
  43. // We have a video src, great! Let's update the large video
  44. // now.
  45. VideoLayout.handleVideoThumbClicked(false, id);
  46. } else {
  47. // If we don't have a video src for jid, there's absolutely
  48. // no point in calling handleVideoThumbClicked; Quite
  49. // simply, it won't work because it needs an src to attach
  50. // to the large video.
  51. //
  52. // Instead, we trigger the pinned endpoint changed event to
  53. // let the bridge adjust its lastN set for myjid and store
  54. // the pinned user in the lastNPickupId variable to be
  55. // picked up later by the lastN changed event handler.
  56. lastNPickupId = id;
  57. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, id);
  58. }
  59. }
  60. }
  61. /**
  62. * Returns the corresponding resource id to the given peer container
  63. * DOM element.
  64. *
  65. * @return the corresponding resource id to the given peer container
  66. * DOM element
  67. */
  68. function getPeerContainerResourceId (containerElement) {
  69. if (localVideoThumbnail.container === containerElement) {
  70. return localVideoThumbnail.id;
  71. }
  72. let i = containerElement.id.indexOf('participant_');
  73. if (i >= 0) {
  74. return containerElement.id.substring(i + 12);
  75. }
  76. }
  77. let largeVideo;
  78. var VideoLayout = {
  79. init (emitter) {
  80. eventEmitter = emitter;
  81. localVideoThumbnail = new LocalVideo(VideoLayout, emitter);
  82. emitter.addListener(UIEvents.CONTACT_CLICKED, onContactClicked);
  83. },
  84. initLargeVideo (isSideBarVisible) {
  85. largeVideo = new LargeVideoManager();
  86. largeVideo.updateContainerSize(isSideBarVisible);
  87. AudioLevels.init();
  88. },
  89. setAudioLevel(id, lvl) {
  90. if (!largeVideo) {
  91. return;
  92. }
  93. AudioLevels.updateAudioLevel(
  94. id, lvl, largeVideo.id
  95. );
  96. },
  97. isInLastN (resource) {
  98. return lastNCount < 0 || // lastN is disabled
  99. // lastNEndpoints cache not built yet
  100. (lastNCount > 0 && !lastNEndpointsCache.length) ||
  101. (lastNEndpointsCache &&
  102. lastNEndpointsCache.indexOf(resource) !== -1);
  103. },
  104. changeLocalAudio (stream) {
  105. let localAudio = document.getElementById('localAudio');
  106. stream.attach($(localAudio));
  107. //return; // FIXME maybe move this into the library?
  108. // Writing volume not allowed in IE
  109. if (!RTCBrowserType.isIExplorer()) {
  110. localAudio.autoplay = true;
  111. localAudio.volume = 0;
  112. }
  113. // Now when Temasys plugin is converting also <audio> elements to
  114. // plugin's <object>s, in current layout it will capture click events
  115. // before it reaches the local video object. We hide it here in order
  116. // to prevent that.
  117. if (RTCBrowserType.isIExplorer()) {
  118. // The issue is not present on Safari. Also if we hide it in Safari
  119. // then the local audio track will have 'enabled' flag set to false
  120. // which will result in audio mute issues
  121. $('#localAudio').hide();
  122. }
  123. },
  124. changeLocalVideo (stream) {
  125. // Set default display name.
  126. localVideoThumbnail.setDisplayName();
  127. localVideoThumbnail.createConnectionIndicator();
  128. let localId = APP.conference.localId;
  129. this.onVideoTypeChanged(localId, stream.videoType);
  130. let {thumbWidth, thumbHeight} = this.calculateThumbnailSize();
  131. AudioLevels.updateAudioLevelCanvas(null, thumbWidth, thumbHeight);
  132. if (!stream.isMuted()) {
  133. localVideoThumbnail.changeVideo(stream);
  134. }
  135. /* force update if we're currently being displayed */
  136. if (this.isCurrentlyOnLarge(localId)) {
  137. this.updateLargeVideo(localId, true);
  138. }
  139. },
  140. /**
  141. * Get's the localID of the conference and set it to the local video
  142. * (small one). This needs to be called as early as possible, when muc is
  143. * actually joined. Otherwise events can come with information like email
  144. * and setting them assume the id is already set.
  145. */
  146. mucJoined () {
  147. let id = APP.conference.localId;
  148. localVideoThumbnail.joined(id);
  149. if (largeVideo && !largeVideo.id) {
  150. this.updateLargeVideo(id, true);
  151. }
  152. },
  153. /**
  154. * Adds or removes icons for not available camera and microphone.
  155. * @param resourceJid the jid of user
  156. * @param devices available devices
  157. */
  158. setDeviceAvailabilityIcons (resourceJid, devices) {
  159. if(!devices)
  160. return;
  161. if(!resourceJid) {
  162. localVideoThumbnail.setDeviceAvailabilityIcons(devices);
  163. } else {
  164. if(remoteVideos[resourceJid])
  165. remoteVideos[resourceJid].setDeviceAvailabilityIcons(devices);
  166. }
  167. },
  168. /**
  169. * Checks if removed video is currently displayed and tries to display
  170. * another one instead.
  171. */
  172. updateRemovedVideo (id) {
  173. if (!this.isCurrentlyOnLarge(id)) {
  174. return;
  175. }
  176. let newId;
  177. // We'll show user's avatar if he is the dominant speaker or if
  178. // his video thumbnail is pinned
  179. if (remoteVideos[id] && (id === focusedVideoResourceJid || id === currentDominantSpeaker)) {
  180. newId = id;
  181. } else {
  182. // Otherwise select last visible video
  183. newId = this.electLastVisibleVideo();
  184. }
  185. this.updateLargeVideo(newId);
  186. },
  187. electLastVisibleVideo () {
  188. // pick the last visible video in the row
  189. // if nobody else is left, this picks the local video
  190. let thumbs = BottomToolbar.getThumbs(true).filter('[id!="mixedstream"]');
  191. let lastVisible = thumbs.filter(':visible:last');
  192. if (lastVisible.length) {
  193. let id = getPeerContainerResourceId(lastVisible[0]);
  194. if (remoteVideos[id]) {
  195. console.info("electLastVisibleVideo: " + id);
  196. return id;
  197. }
  198. // The RemoteVideo was removed (but the DOM elements may still
  199. // exist).
  200. }
  201. console.info("Last visible video no longer exists");
  202. thumbs = BottomToolbar.getThumbs();
  203. if (thumbs.length) {
  204. let id = getPeerContainerResourceId(thumbs[0]);
  205. if (remoteVideos[id]) {
  206. console.info("electLastVisibleVideo: " + id);
  207. return id;
  208. }
  209. // The RemoteVideo was removed (but the DOM elements may
  210. // still exist).
  211. }
  212. // Go with local video
  213. console.info("Fallback to local video...");
  214. let id = APP.conference.localId;
  215. console.info("electLastVisibleVideo: " + id);
  216. return id;
  217. },
  218. onRemoteStreamAdded (stream) {
  219. let id = stream.getParticipantId();
  220. remoteVideos[id].addRemoteStreamElement(stream);
  221. // if track is muted make sure we reflect that
  222. if(stream.isMuted())
  223. {
  224. if(stream.getType() === "audio")
  225. this.onAudioMute(stream.getParticipantId(), true);
  226. else
  227. this.onVideoMute(stream.getParticipantId(), true);
  228. }
  229. },
  230. /**
  231. * Return the type of the remote video.
  232. * @param id the id for the remote video
  233. * @returns the video type video or screen.
  234. */
  235. getRemoteVideoType (id) {
  236. return remoteVideoTypes[id];
  237. },
  238. handleVideoThumbClicked (noPinnedEndpointChangedEvent,
  239. resourceJid) {
  240. if(focusedVideoResourceJid) {
  241. var oldSmallVideo
  242. = VideoLayout.getSmallVideo(focusedVideoResourceJid);
  243. if (oldSmallVideo && !interfaceConfig.filmStripOnly)
  244. oldSmallVideo.focus(false);
  245. }
  246. var smallVideo = VideoLayout.getSmallVideo(resourceJid);
  247. // Unlock current focused.
  248. if (focusedVideoResourceJid === resourceJid)
  249. {
  250. focusedVideoResourceJid = null;
  251. // Enable the currently set dominant speaker.
  252. if (currentDominantSpeaker) {
  253. if(smallVideo && smallVideo.hasVideo()) {
  254. this.updateLargeVideo(currentDominantSpeaker);
  255. }
  256. }
  257. if (!noPinnedEndpointChangedEvent) {
  258. eventEmitter.emit(UIEvents.PINNED_ENDPOINT);
  259. }
  260. return;
  261. }
  262. // Lock new video
  263. focusedVideoResourceJid = resourceJid;
  264. // Update focused/pinned interface.
  265. if (resourceJid) {
  266. if (smallVideo && !interfaceConfig.filmStripOnly)
  267. smallVideo.focus(true);
  268. if (!noPinnedEndpointChangedEvent) {
  269. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, resourceJid);
  270. }
  271. }
  272. this.updateLargeVideo(resourceJid);
  273. // Writing volume not allowed in IE
  274. if (!RTCBrowserType.isIExplorer()) {
  275. $('audio').each(function (idx, el) {
  276. el.volume = 0;
  277. el.volume = 1;
  278. });
  279. }
  280. },
  281. /**
  282. * Checks if container for participant identified by given id exists
  283. * in the document and creates it eventually.
  284. *
  285. * @return Returns <tt>true</tt> if the peer container exists,
  286. * <tt>false</tt> - otherwise
  287. */
  288. addParticipantContainer (id) {
  289. let remoteVideo = new RemoteVideo(id, VideoLayout, eventEmitter);
  290. remoteVideos[id] = remoteVideo;
  291. let videoType = remoteVideoTypes[id];
  292. if (videoType) {
  293. remoteVideo.setVideoType(videoType);
  294. }
  295. // In case this is not currently in the last n we don't show it.
  296. if (localLastNCount && localLastNCount > 0 &&
  297. BottomToolbar.getThumbs().length >= localLastNCount + 2) {
  298. remoteVideo.showPeerContainer('hide');
  299. } else {
  300. VideoLayout.resizeThumbnails();
  301. }
  302. },
  303. inputDisplayNameHandler (name) {
  304. localVideoThumbnail.inputDisplayNameHandler(name);
  305. },
  306. videoactive (videoelem, resourceJid) {
  307. console.info(resourceJid + " video is now active");
  308. videoelem.show();
  309. VideoLayout.resizeThumbnails();
  310. // Update the large video to the last added video only if there's no
  311. // current dominant, focused speaker or prezi playing or update it to
  312. // the current dominant speaker.
  313. if ((!focusedVideoResourceJid &&
  314. !currentDominantSpeaker &&
  315. !this.isLargeContainerTypeVisible(PreziContainerType)) ||
  316. focusedVideoResourceJid === resourceJid ||
  317. (resourceJid &&
  318. currentDominantSpeaker === resourceJid)) {
  319. this.updateLargeVideo(resourceJid, true);
  320. }
  321. },
  322. /**
  323. * Shows the presence status message for the given video.
  324. */
  325. setPresenceStatus (resourceJid, statusMsg) {
  326. remoteVideos[resourceJid].setPresenceStatus(statusMsg);
  327. },
  328. /**
  329. * Shows a visual indicator for the moderator of the conference.
  330. * On local or remote participants.
  331. */
  332. showModeratorIndicator () {
  333. let isModerator = APP.conference.isModerator;
  334. if (isModerator) {
  335. localVideoThumbnail.createModeratorIndicatorElement();
  336. }
  337. APP.conference.listMembers().forEach(function (member) {
  338. let id = member.getId();
  339. if (member.isModerator()) {
  340. remoteVideos[id].removeRemoteVideoMenu();
  341. remoteVideos[id].createModeratorIndicatorElement();
  342. } else if (isModerator) {
  343. // We are moderator, but user is not - add menu
  344. if ($(`#remote_popupmenu_${id}`).length <= 0) {
  345. remoteVideos[id].addRemoteVideoMenu();
  346. }
  347. }
  348. });
  349. },
  350. /*
  351. * Shows or hides the audio muted indicator over the local thumbnail video.
  352. * @param {boolean} isMuted
  353. */
  354. showLocalAudioIndicator (isMuted) {
  355. localVideoThumbnail.showAudioIndicator(isMuted);
  356. },
  357. /**
  358. * Resizes the large video container.
  359. */
  360. resizeLargeVideoContainer (isSideBarVisible) {
  361. if (largeVideo) {
  362. largeVideo.updateContainerSize(isSideBarVisible);
  363. largeVideo.resize(false);
  364. } else {
  365. this.resizeVideoSpace(false, isSideBarVisible);
  366. }
  367. this.resizeThumbnails(false);
  368. },
  369. /**
  370. * Resizes thumbnails.
  371. */
  372. resizeThumbnails (animate = false) {
  373. let {thumbWidth, thumbHeight} = this.calculateThumbnailSize();
  374. $('.userAvatar').css('left', (thumbWidth - thumbHeight) / 2);
  375. BottomToolbar.resizeThumbnails(thumbWidth, thumbHeight, animate).then(function () {
  376. BottomToolbar.resizeToolbar(thumbWidth, thumbHeight);
  377. AudioLevels.updateCanvasSize(thumbWidth, thumbHeight);
  378. });
  379. },
  380. /**
  381. * Calculates the thumbnail size.
  382. *
  383. */
  384. calculateThumbnailSize () {
  385. let videoSpaceWidth = BottomToolbar.getFilmStripWidth();
  386. // Calculate the available height, which is the inner window height
  387. // minus 39px for the header minus 2px for the delimiter lines on the
  388. // top and bottom of the large video, minus the 36px space inside the
  389. // remoteVideos container used for highlighting shadow.
  390. let availableHeight = 100;
  391. let numvids = BottomToolbar.getThumbs().length;
  392. if (localLastNCount && localLastNCount > 0) {
  393. numvids = Math.min(localLastNCount + 1, numvids);
  394. }
  395. // Remove the 3px borders arround videos and border around the remote
  396. // videos area and the 4 pixels between the local video and the others
  397. //TODO: Find out where the 4 pixels come from and remove them
  398. let availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70 - 4;
  399. let availableWidth = availableWinWidth / numvids;
  400. let maxHeight = Math.min(160, availableHeight);
  401. availableHeight
  402. = Math.min( maxHeight,
  403. availableWidth / thumbAspectRatio,
  404. window.innerHeight - 18);
  405. if (availableHeight < availableWidth / thumbAspectRatio) {
  406. availableWidth = Math.floor(availableHeight * thumbAspectRatio);
  407. }
  408. return {
  409. thumbWidth: availableWidth,
  410. thumbHeight: availableHeight
  411. };
  412. },
  413. /**
  414. * On audio muted event.
  415. */
  416. onAudioMute (id, isMuted) {
  417. if (APP.conference.isLocalId(id)) {
  418. localVideoThumbnail.showAudioIndicator(isMuted);
  419. } else {
  420. remoteVideos[id].showAudioIndicator(isMuted);
  421. if (APP.conference.isModerator) {
  422. remoteVideos[id].updateRemoteVideoMenu(isMuted);
  423. }
  424. }
  425. },
  426. /**
  427. * On video muted event.
  428. */
  429. onVideoMute (id, value) {
  430. if (APP.conference.isLocalId(id)) {
  431. localVideoThumbnail.setMutedView(value);
  432. } else {
  433. var remoteVideo = remoteVideos[id];
  434. remoteVideo.setMutedView(value);
  435. var el = remoteVideo.selectVideoElement();
  436. if (!value)
  437. el.show();
  438. else
  439. el.hide();
  440. }
  441. if(this.isCurrentlyOnLarge(id))
  442. largeVideo.showAvatar(value);
  443. },
  444. /**
  445. * Display name changed.
  446. */
  447. onDisplayNameChanged (id, displayName, status) {
  448. if (id === 'localVideoContainer' ||
  449. APP.conference.isLocalId(id)) {
  450. localVideoThumbnail.setDisplayName(displayName);
  451. } else {
  452. remoteVideos[id].setDisplayName(displayName, status);
  453. }
  454. },
  455. /**
  456. * On dominant speaker changed event.
  457. */
  458. onDominantSpeakerChanged (id) {
  459. // We ignore local user events.
  460. if (APP.conference.isLocalId(id) || (id === currentDominantSpeaker)) {
  461. return;
  462. }
  463. let remoteVideo = remoteVideos[id];
  464. if (!remoteVideo) {
  465. return;
  466. }
  467. // Update the current dominant speaker.
  468. remoteVideo.updateDominantSpeakerIndicator(true);
  469. // let's remove the indications from the remote video if any
  470. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  471. if (oldSpeakerRemoteVideo) {
  472. oldSpeakerRemoteVideo.updateDominantSpeakerIndicator(false);
  473. }
  474. currentDominantSpeaker = id;
  475. // Obtain container for new dominant speaker.
  476. let videoSel = remoteVideo.selectVideoElement();
  477. // Local video will not have container found, but that's ok
  478. // since we don't want to switch to local video.
  479. if (!focusedVideoResourceJid && videoSel.length) {
  480. // Update the large video if the video source is already available,
  481. // otherwise wait for the "videoactive.jingle" event.
  482. if (videoSel[0].currentTime > 0) {
  483. this.updateLargeVideo(id);
  484. }
  485. }
  486. },
  487. /**
  488. * On last N change event.
  489. *
  490. * @param lastNEndpoints the list of last N endpoints
  491. * @param endpointsEnteringLastN the list currently entering last N
  492. * endpoints
  493. */
  494. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  495. if (lastNCount !== lastNEndpoints.length)
  496. lastNCount = lastNEndpoints.length;
  497. lastNEndpointsCache = lastNEndpoints;
  498. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  499. //
  500. // If LastN drops to, say, 2, because of adaptivity, then E should see
  501. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  502. // so E sees them. C is only in E's local LastN set.
  503. //
  504. // If F starts talking and LastN = 3, then E should see thumbnails for
  505. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  506. // enters E's local LastN ejecting C.
  507. // Increase the local LastN set size, if necessary.
  508. if (lastNCount > localLastNCount) {
  509. localLastNCount = lastNCount;
  510. }
  511. // Update the local LastN set preserving the order in which the
  512. // endpoints appeared in the LastN/local LastN set.
  513. var nextLocalLastNSet = lastNEndpoints.slice(0);
  514. for (var i = 0; i < localLastNSet.length; i++) {
  515. if (nextLocalLastNSet.length >= localLastNCount) {
  516. break;
  517. }
  518. var resourceJid = localLastNSet[i];
  519. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  520. nextLocalLastNSet.push(resourceJid);
  521. }
  522. }
  523. localLastNSet = nextLocalLastNSet;
  524. var updateLargeVideo = false;
  525. // Handle LastN/local LastN changes.
  526. BottomToolbar.getThumbs().each(function( index, element ) {
  527. var resourceJid = getPeerContainerResourceId(element);
  528. // We do not want to process any logic for our own(local) video
  529. // because the local participant is never in the lastN set.
  530. // The code of this function might detect that the local participant
  531. // has been dropped out of the lastN set and will update the large
  532. // video
  533. // Detected from avatar tests, where lastN event override
  534. // local video pinning
  535. if(APP.conference.isLocalId(resourceJid))
  536. return;
  537. var isReceived = true;
  538. if (resourceJid &&
  539. lastNEndpoints.indexOf(resourceJid) < 0 &&
  540. localLastNSet.indexOf(resourceJid) < 0) {
  541. console.log("Remove from last N", resourceJid);
  542. if (remoteVideos[resourceJid])
  543. remoteVideos[resourceJid].showPeerContainer('hide');
  544. else if (!APP.conference.isLocalId(resourceJid))
  545. console.error("No remote video for: " + resourceJid);
  546. isReceived = false;
  547. } else if (resourceJid &&
  548. $('#participant_' + resourceJid).is(':visible') &&
  549. lastNEndpoints.indexOf(resourceJid) < 0 &&
  550. localLastNSet.indexOf(resourceJid) >= 0) {
  551. if (remoteVideos[resourceJid])
  552. remoteVideos[resourceJid].showPeerContainer('avatar');
  553. else if (!APP.conference.isLocalId(resourceJid))
  554. console.error("No remote video for: " + resourceJid);
  555. isReceived = false;
  556. }
  557. if (!isReceived) {
  558. // resourceJid has dropped out of the server side lastN set, so
  559. // it is no longer being received. If resourceJid was being
  560. // displayed in the large video we have to switch to another
  561. // user.
  562. if (!updateLargeVideo &&
  563. this.isCurrentlyOnLarge(resourceJid)) {
  564. updateLargeVideo = true;
  565. }
  566. }
  567. });
  568. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  569. endpointsEnteringLastN = lastNEndpoints;
  570. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  571. endpointsEnteringLastN.forEach(function (resourceJid) {
  572. var isVisible = $('#participant_' + resourceJid).is(':visible');
  573. var remoteVideo = remoteVideos[resourceJid];
  574. remoteVideo.showPeerContainer('show');
  575. if (!isVisible) {
  576. console.log("Add to last N", resourceJid);
  577. var jid = APP.xmpp.findJidFromResource(resourceJid);
  578. var mediaStream =
  579. APP.RTC.remoteStreams[jid]['video'];
  580. var sel = remoteVideo.selectVideoElement();
  581. APP.RTC.attachMediaStream(sel, mediaStream.stream);
  582. if (lastNPickupId == mediaStream.peerjid) {
  583. // Clean up the lastN pickup id.
  584. lastNPickupId = null;
  585. // Don't fire the events again, they've already
  586. // been fired in the contact list click handler.
  587. VideoLayout.handleVideoThumbClicked(
  588. false,
  589. Strophe.getResourceFromJid(mediaStream.peerjid));
  590. updateLargeVideo = false;
  591. }
  592. remoteVideos[resourceJid].
  593. waitForPlayback(sel, mediaStream);
  594. }
  595. });
  596. }
  597. // The endpoint that was being shown in the large video has dropped out
  598. // of the lastN set and there was no lastN pickup jid. We need to update
  599. // the large video now.
  600. if (updateLargeVideo) {
  601. var resource;
  602. // Find out which endpoint to show in the large video.
  603. for (i = 0; i < lastNEndpoints.length; i++) {
  604. resource = lastNEndpoints[i];
  605. if (!resource || APP.conference.isLocalId(resource))
  606. continue;
  607. // videoSrcToSsrc needs to be update for this call to succeed.
  608. this.updateLargeVideo(resource);
  609. break;
  610. }
  611. }
  612. },
  613. /**
  614. * Updates local stats
  615. * @param percent
  616. * @param object
  617. */
  618. updateLocalConnectionStats (percent, object) {
  619. let resolutions = {};
  620. if (object.resolution !== null) {
  621. resolutions = object.resolution;
  622. object.resolution = resolutions[APP.conference.localId];
  623. }
  624. localVideoThumbnail.updateStatsIndicator(percent, object);
  625. Object.keys(resolutions).forEach(function (id) {
  626. if (APP.conference.isLocalId(id)) {
  627. return;
  628. }
  629. let resolution = resolutions[id];
  630. let remoteVideo = remoteVideos[id];
  631. if (resolution && remoteVideo) {
  632. remoteVideo.updateResolution(resolution);
  633. }
  634. });
  635. },
  636. /**
  637. * Updates remote stats.
  638. * @param id the id associated with the stats
  639. * @param percent the connection quality percent
  640. * @param object the stats data
  641. */
  642. updateConnectionStats (id, percent, object) {
  643. if (remoteVideos[id]) {
  644. remoteVideos[id].updateStatsIndicator(percent, object);
  645. }
  646. },
  647. /**
  648. * Hides the connection indicator
  649. * @param id
  650. */
  651. hideConnectionIndicator (id) {
  652. remoteVideos[id].hideConnectionIndicator();
  653. },
  654. /**
  655. * Hides all the indicators
  656. */
  657. hideStats () {
  658. for(var video in remoteVideos) {
  659. remoteVideos[video].hideIndicator();
  660. }
  661. localVideoThumbnail.hideIndicator();
  662. },
  663. removeParticipantContainer (id) {
  664. // Unlock large video
  665. if (focusedVideoResourceJid === id) {
  666. console.info("Focused video owner has left the conference");
  667. focusedVideoResourceJid = null;
  668. }
  669. if (currentDominantSpeaker === id) {
  670. console.info("Dominant speaker has left the conference");
  671. currentDominantSpeaker = null;
  672. }
  673. var remoteVideo = remoteVideos[id];
  674. if (remoteVideo) {
  675. // Remove remote video
  676. console.info("Removing remote video: " + id);
  677. delete remoteVideos[id];
  678. remoteVideo.remove();
  679. } else {
  680. console.warn("No remote video for " + id);
  681. }
  682. VideoLayout.resizeThumbnails();
  683. },
  684. onVideoTypeChanged (id, newVideoType) {
  685. if (remoteVideoTypes[id] === newVideoType) {
  686. return;
  687. }
  688. console.info("Peer video type changed: ", id, newVideoType);
  689. remoteVideoTypes[id] = newVideoType;
  690. var smallVideo;
  691. if (APP.conference.isLocalId(id)) {
  692. if (!localVideoThumbnail) {
  693. console.warn("Local video not ready yet");
  694. return;
  695. }
  696. smallVideo = localVideoThumbnail;
  697. } else if (remoteVideos[id]) {
  698. smallVideo = remoteVideos[id];
  699. } else {
  700. return;
  701. }
  702. smallVideo.setVideoType(newVideoType);
  703. if (this.isCurrentlyOnLarge(id)) {
  704. this.updateLargeVideo(id, true);
  705. }
  706. },
  707. showMore (jid) {
  708. if (jid === 'local') {
  709. localVideoThumbnail.connectionIndicator.showMore();
  710. } else {
  711. var remoteVideo = remoteVideos[Strophe.getResourceFromJid(jid)];
  712. if (remoteVideo) {
  713. remoteVideo.connectionIndicator.showMore();
  714. } else {
  715. console.info("Error - no remote video for jid: " + jid);
  716. }
  717. }
  718. },
  719. addRemoteVideoContainer (id) {
  720. return RemoteVideo.createContainer(id);
  721. },
  722. /**
  723. * Resizes the video area.
  724. *
  725. * @param isSideBarVisible indicates if the side bar is currently visible
  726. * @param callback a function to be called when the video space is
  727. * resized.
  728. */
  729. resizeVideoArea (isSideBarVisible, callback) {
  730. let animate = true;
  731. if (largeVideo) {
  732. largeVideo.updateContainerSize(isSideBarVisible);
  733. largeVideo.resize(animate);
  734. this.resizeVideoSpace(animate, isSideBarVisible, callback);
  735. }
  736. VideoLayout.resizeThumbnails(animate);
  737. },
  738. /**
  739. * Resizes the #videospace html element
  740. * @param animate boolean property that indicates whether the resize should
  741. * be animated or not.
  742. * @param isChatVisible boolean property that indicates whether the chat
  743. * area is displayed or not.
  744. * If that parameter is null the method will check the chat panel
  745. * visibility.
  746. * @param completeFunction a function to be called when the video space
  747. * is resized.
  748. */
  749. resizeVideoSpace (animate, isChatVisible, completeFunction) {
  750. var availableHeight = window.innerHeight;
  751. var availableWidth = UIUtil.getAvailableVideoWidth(isChatVisible);
  752. if (availableWidth < 0 || availableHeight < 0) return;
  753. if(animate) {
  754. $('#videospace').animate({
  755. right: window.innerWidth - availableWidth,
  756. width: availableWidth,
  757. height: availableHeight
  758. },
  759. {
  760. queue: false,
  761. duration: 500,
  762. complete: completeFunction
  763. });
  764. } else {
  765. $('#videospace').width(availableWidth).height(availableHeight);
  766. }
  767. },
  768. getSmallVideo (id) {
  769. if (APP.conference.isLocalId(id)) {
  770. return localVideoThumbnail;
  771. } else {
  772. return remoteVideos[id];
  773. }
  774. },
  775. changeUserAvatar (id, thumbUrl) {
  776. var smallVideo = VideoLayout.getSmallVideo(id);
  777. if (smallVideo) {
  778. smallVideo.avatarChanged(thumbUrl);
  779. } else {
  780. console.warn(
  781. "Missed avatar update - no small video yet for " + id
  782. );
  783. }
  784. if (this.isCurrentlyOnLarge(id)) {
  785. largeVideo.updateAvatar(thumbUrl);
  786. }
  787. },
  788. /**
  789. * Indicates that the video has been interrupted.
  790. */
  791. onVideoInterrupted () {
  792. this.enableVideoProblemFilter(true);
  793. let reconnectingKey = "connection.RECONNECTING";
  794. $('#videoConnectionMessage')
  795. .attr("data-i18n", reconnectingKey)
  796. .text(APP.translation.translateString(reconnectingKey))
  797. .css({display: "block"});
  798. },
  799. /**
  800. * Indicates that the video has been restored.
  801. */
  802. onVideoRestored () {
  803. this.enableVideoProblemFilter(false);
  804. $('#videoConnectionMessage').css({display: "none"});
  805. },
  806. enableVideoProblemFilter (enable) {
  807. if (!largeVideo) {
  808. return;
  809. }
  810. largeVideo.enableVideoProblemFilter(enable);
  811. },
  812. isLargeVideoVisible () {
  813. return this.isLargeContainerTypeVisible(VideoContainerType);
  814. },
  815. isCurrentlyOnLarge (id) {
  816. return largeVideo && largeVideo.id === id;
  817. },
  818. updateLargeVideo (id, forceUpdate) {
  819. if (!largeVideo) {
  820. return;
  821. }
  822. let isOnLarge = this.isCurrentlyOnLarge(id);
  823. let currentId = largeVideo.id;
  824. if (!isOnLarge || forceUpdate) {
  825. if (id !== currentId) {
  826. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  827. }
  828. if (currentId) {
  829. var oldSmallVideo = this.getSmallVideo(currentId);
  830. }
  831. let smallVideo = this.getSmallVideo(id);
  832. let videoType = this.getRemoteVideoType(id);
  833. largeVideo.updateLargeVideo(
  834. smallVideo.stream,
  835. videoType,
  836. // LargeVideoUpdatedCallBack
  837. function() {
  838. // update current small video and the old one
  839. smallVideo.updateView();
  840. oldSmallVideo && oldSmallVideo.updateView();
  841. // change the avatar url on large
  842. largeVideo.updateAvatar(Avatar.getThumbUrl(smallVideo.id));
  843. // show the avatar on large if needed
  844. largeVideo.showAvatar(smallVideo.stream.isMuted());
  845. });
  846. } else if (currentId) {
  847. let currentSmallVideo = this.getSmallVideo(currentId);
  848. currentSmallVideo.updateView();
  849. }
  850. },
  851. addLargeVideoContainer (type, container) {
  852. largeVideo && largeVideo.addContainer(type, container);
  853. },
  854. removeLargeVideoContainer (type) {
  855. largeVideo && largeVideo.removeContainer(type);
  856. },
  857. /**
  858. * @returns Promise
  859. */
  860. showLargeVideoContainer (type, show) {
  861. if (!largeVideo) {
  862. return Promise.reject();
  863. }
  864. let isVisible = this.isLargeContainerTypeVisible(type);
  865. if (isVisible === show) {
  866. return Promise.resolve();
  867. }
  868. // if !show then use default type - large video
  869. return largeVideo.showContainer(show ? type : VideoContainerType);
  870. },
  871. isLargeContainerTypeVisible (type) {
  872. return largeVideo && largeVideo.state === type;
  873. },
  874. /**
  875. * Returns the id of the current video shown on large.
  876. * Currently used by tests (troture).
  877. */
  878. getLargeVideoID () {
  879. return largeVideo.id;
  880. }
  881. };
  882. export default VideoLayout;