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

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