Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VideoLayout.js 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. /* global APP, $, interfaceConfig */
  2. const logger = require('jitsi-meet-logger').getLogger(__filename);
  3. import {
  4. JitsiParticipantConnectionStatus
  5. } from '../../../react/features/base/lib-jitsi-meet';
  6. import {
  7. getPinnedParticipant,
  8. pinParticipant
  9. } from '../../../react/features/base/participants';
  10. import Filmstrip from './Filmstrip';
  11. import UIEvents from '../../../service/UI/UIEvents';
  12. import UIUtil from '../util/UIUtil';
  13. import RemoteVideo from './RemoteVideo';
  14. import LargeVideoManager from './LargeVideoManager';
  15. import { VIDEO_CONTAINER_TYPE } from './VideoContainer';
  16. import LocalVideo from './LocalVideo';
  17. const remoteVideos = {};
  18. let localVideoThumbnail = null;
  19. let eventEmitter = null;
  20. let largeVideo;
  21. /**
  22. * flipX state of the localVideo
  23. */
  24. let localFlipX = null;
  25. /**
  26. * Handler for local flip X changed event.
  27. * @param {Object} val
  28. */
  29. function onLocalFlipXChanged(val) {
  30. localFlipX = val;
  31. if (largeVideo) {
  32. largeVideo.onLocalFlipXChange(val);
  33. }
  34. }
  35. /**
  36. * Returns the redux representation of all known users.
  37. *
  38. * @private
  39. * @returns {Array}
  40. */
  41. function getAllParticipants() {
  42. return APP.store.getState()['features/base/participants'];
  43. }
  44. /**
  45. * Returns an array of all thumbnails in the filmstrip.
  46. *
  47. * @private
  48. * @returns {Array}
  49. */
  50. function getAllThumbnails() {
  51. return [
  52. localVideoThumbnail,
  53. ...Object.values(remoteVideos)
  54. ];
  55. }
  56. /**
  57. * Returns the user ID of the remote participant that is current the dominant
  58. * speaker.
  59. *
  60. * @private
  61. * @returns {string|null}
  62. */
  63. function getCurrentRemoteDominantSpeakerID() {
  64. const dominantSpeaker = getAllParticipants()
  65. .find(participant => participant.dominantSpeaker);
  66. if (dominantSpeaker) {
  67. return dominantSpeaker.local ? null : dominantSpeaker.id;
  68. }
  69. return null;
  70. }
  71. /**
  72. * Returns the corresponding resource id to the given peer container
  73. * DOM element.
  74. *
  75. * @return the corresponding resource id to the given peer container
  76. * DOM element
  77. */
  78. function getPeerContainerResourceId(containerElement) {
  79. if (localVideoThumbnail.container === containerElement) {
  80. return localVideoThumbnail.id;
  81. }
  82. const i = containerElement.id.indexOf('participant_');
  83. if (i >= 0) {
  84. return containerElement.id.substring(i + 12);
  85. }
  86. }
  87. const VideoLayout = {
  88. init(emitter) {
  89. eventEmitter = emitter;
  90. localVideoThumbnail = new LocalVideo(
  91. VideoLayout,
  92. emitter,
  93. this._updateLargeVideoIfDisplayed.bind(this));
  94. // sets default video type of local video
  95. // FIXME container type is totally different thing from the video type
  96. localVideoThumbnail.setVideoType(VIDEO_CONTAINER_TYPE);
  97. // if we do not resize the thumbs here, if there is no video device
  98. // the local video thumb maybe one pixel
  99. this.resizeThumbnails(true);
  100. this.registerListeners();
  101. },
  102. /**
  103. * Cleans up any existing largeVideo instance.
  104. *
  105. * @returns {void}
  106. */
  107. resetLargeVideo() {
  108. if (largeVideo) {
  109. largeVideo.destroy();
  110. }
  111. largeVideo = null;
  112. },
  113. /**
  114. * Registering listeners for UI events in Video layout component.
  115. *
  116. * @returns {void}
  117. */
  118. registerListeners() {
  119. eventEmitter.addListener(UIEvents.LOCAL_FLIPX_CHANGED,
  120. onLocalFlipXChanged);
  121. },
  122. initLargeVideo() {
  123. this.resetLargeVideo();
  124. largeVideo = new LargeVideoManager(eventEmitter);
  125. if (localFlipX) {
  126. largeVideo.onLocalFlipXChange(localFlipX);
  127. }
  128. largeVideo.updateContainerSize();
  129. },
  130. /**
  131. * Sets the audio level of the video elements associated to the given id.
  132. *
  133. * @param id the video identifier in the form it comes from the library
  134. * @param lvl the new audio level to update to
  135. */
  136. setAudioLevel(id, lvl) {
  137. const smallVideo = this.getSmallVideo(id);
  138. if (smallVideo) {
  139. smallVideo.updateAudioLevelIndicator(lvl);
  140. }
  141. if (largeVideo && id === largeVideo.id) {
  142. largeVideo.updateLargeVideoAudioLevel(lvl);
  143. }
  144. },
  145. changeLocalVideo(stream) {
  146. const localId = APP.conference.getMyUserId();
  147. this.onVideoTypeChanged(localId, stream.videoType);
  148. localVideoThumbnail.changeVideo(stream);
  149. this._updateLargeVideoIfDisplayed(localId);
  150. },
  151. /**
  152. * Get's the localID of the conference and set it to the local video
  153. * (small one). This needs to be called as early as possible, when muc is
  154. * actually joined. Otherwise events can come with information like email
  155. * and setting them assume the id is already set.
  156. */
  157. mucJoined() {
  158. if (largeVideo && !largeVideo.id) {
  159. this.updateLargeVideo(APP.conference.getMyUserId(), true);
  160. }
  161. // FIXME: replace this call with a generic update call once SmallVideo
  162. // only contains a ReactElement. Then remove this call once the
  163. // Filmstrip is fully in React.
  164. localVideoThumbnail.updateIndicators();
  165. },
  166. /**
  167. * Adds or removes icons for not available camera and microphone.
  168. * @param resourceJid the jid of user
  169. * @param devices available devices
  170. */
  171. setDeviceAvailabilityIcons(id, devices) {
  172. if (APP.conference.isLocalId(id)) {
  173. localVideoThumbnail.setDeviceAvailabilityIcons(devices);
  174. return;
  175. }
  176. const video = remoteVideos[id];
  177. if (!video) {
  178. return;
  179. }
  180. video.setDeviceAvailabilityIcons(devices);
  181. },
  182. /**
  183. * Enables/disables device availability icons for the given participant id.
  184. * The default value is {true}.
  185. * @param id the identifier of the participant
  186. * @param enable {true} to enable device availability icons
  187. */
  188. enableDeviceAvailabilityIcons(id, enable) {
  189. let video;
  190. if (APP.conference.isLocalId(id)) {
  191. video = localVideoThumbnail;
  192. } else {
  193. video = remoteVideos[id];
  194. }
  195. if (video) {
  196. video.enableDeviceAvailabilityIcons(enable);
  197. }
  198. },
  199. /**
  200. * Shows/hides local video.
  201. * @param {boolean} true to make the local video visible, false - otherwise
  202. */
  203. setLocalVideoVisible(visible) {
  204. localVideoThumbnail.setVisible(visible);
  205. },
  206. /**
  207. * Checks if removed video is currently displayed and tries to display
  208. * another one instead.
  209. * Uses focusedID if any or dominantSpeakerID if any,
  210. * otherwise elects new video, in this order.
  211. */
  212. _updateAfterThumbRemoved(id) {
  213. // Always trigger an update if large video is empty.
  214. if (!largeVideo
  215. || (this.getLargeVideoID() && !this.isCurrentlyOnLarge(id))) {
  216. return;
  217. }
  218. const pinnedId = this.getPinnedId();
  219. let newId;
  220. if (pinnedId) {
  221. newId = pinnedId;
  222. } else if (getCurrentRemoteDominantSpeakerID()) {
  223. newId = getCurrentRemoteDominantSpeakerID();
  224. } else { // Otherwise select last visible video
  225. newId = this.electLastVisibleVideo();
  226. }
  227. this.updateLargeVideo(newId);
  228. },
  229. electLastVisibleVideo() {
  230. // pick the last visible video in the row
  231. // if nobody else is left, this picks the local video
  232. const remoteThumbs = Filmstrip.getThumbs(true).remoteThumbs;
  233. let thumbs = remoteThumbs.filter('[id!="mixedstream"]');
  234. const lastVisible = thumbs.filter(':visible:last');
  235. if (lastVisible.length) {
  236. const id = getPeerContainerResourceId(lastVisible[0]);
  237. if (remoteVideos[id]) {
  238. logger.info(`electLastVisibleVideo: ${id}`);
  239. return id;
  240. }
  241. // The RemoteVideo was removed (but the DOM elements may still
  242. // exist).
  243. }
  244. logger.info('Last visible video no longer exists');
  245. thumbs = Filmstrip.getThumbs().remoteThumbs;
  246. if (thumbs.length) {
  247. const id = getPeerContainerResourceId(thumbs[0]);
  248. if (remoteVideos[id]) {
  249. logger.info(`electLastVisibleVideo: ${id}`);
  250. return id;
  251. }
  252. // The RemoteVideo was removed (but the DOM elements may
  253. // still exist).
  254. }
  255. // Go with local video
  256. logger.info('Fallback to local video...');
  257. const id = APP.conference.getMyUserId();
  258. logger.info(`electLastVisibleVideo: ${id}`);
  259. return id;
  260. },
  261. onRemoteStreamAdded(stream) {
  262. const id = stream.getParticipantId();
  263. const remoteVideo = remoteVideos[id];
  264. if (!remoteVideo) {
  265. return;
  266. }
  267. remoteVideo.addRemoteStreamElement(stream);
  268. // Make sure track's muted state is reflected
  269. if (stream.getType() === 'audio') {
  270. this.onAudioMute(stream.getParticipantId(), stream.isMuted());
  271. } else {
  272. this.onVideoMute(stream.getParticipantId(), stream.isMuted());
  273. }
  274. },
  275. onRemoteStreamRemoved(stream) {
  276. const id = stream.getParticipantId();
  277. const remoteVideo = remoteVideos[id];
  278. // Remote stream may be removed after participant left the conference.
  279. if (remoteVideo) {
  280. remoteVideo.removeRemoteStreamElement(stream);
  281. }
  282. if (stream.isVideoTrack()) {
  283. this._updateLargeVideoIfDisplayed(id);
  284. }
  285. this.updateMutedForNoTracks(id, stream.getType());
  286. },
  287. /**
  288. * FIXME get rid of this method once muted indicator are reactified (by
  289. * making sure that user with no tracks is displayed as muted )
  290. *
  291. * If participant has no tracks will make the UI display muted status.
  292. * @param {string} participantId
  293. * @param {string} mediaType 'audio' or 'video'
  294. */
  295. updateMutedForNoTracks(participantId, mediaType) {
  296. const participant = APP.conference.getParticipantById(participantId);
  297. if (participant
  298. && !participant.getTracksByMediaType(mediaType).length) {
  299. if (mediaType === 'audio') {
  300. APP.UI.setAudioMuted(participantId, true);
  301. } else if (mediaType === 'video') {
  302. APP.UI.setVideoMuted(participantId, true);
  303. } else {
  304. logger.error(`Unsupported media type: ${mediaType}`);
  305. }
  306. }
  307. },
  308. /**
  309. * Return the type of the remote video.
  310. * @param id the id for the remote video
  311. * @returns {String} the video type video or screen.
  312. */
  313. getRemoteVideoType(id) {
  314. const smallVideo = VideoLayout.getSmallVideo(id);
  315. return smallVideo ? smallVideo.getVideoType() : null;
  316. },
  317. isPinned(id) {
  318. return id === this.getPinnedId();
  319. },
  320. getPinnedId() {
  321. const { id } = getPinnedParticipant(APP.store.getState()) || {};
  322. return id || null;
  323. },
  324. /**
  325. * Callback invoked to update display when the pin participant has changed.
  326. *
  327. * @paramn {string|null} pinnedParticipantID - The participant ID of the
  328. * participant that is pinned or null if no one is pinned.
  329. * @returns {void}
  330. */
  331. onPinChange(pinnedParticipantID) {
  332. if (interfaceConfig.filmStripOnly) {
  333. return;
  334. }
  335. getAllThumbnails().forEach(thumbnail =>
  336. thumbnail.focus(pinnedParticipantID === thumbnail.getId()));
  337. if (pinnedParticipantID) {
  338. this.updateLargeVideo(pinnedParticipantID);
  339. } else {
  340. const currentDominantSpeakerID
  341. = getCurrentRemoteDominantSpeakerID();
  342. if (currentDominantSpeakerID) {
  343. this.updateLargeVideo(currentDominantSpeakerID);
  344. } else {
  345. // if there is no currentDominantSpeakerID, it can also be
  346. // that local participant is the dominant speaker
  347. // we should act as a participant has left and was on large
  348. // and we should choose somebody (electLastVisibleVideo)
  349. this.updateLargeVideo(this.electLastVisibleVideo());
  350. }
  351. }
  352. },
  353. /**
  354. * Creates or adds a participant container for the given id and smallVideo.
  355. *
  356. * @param {JitsiParticipant} user the participant to add
  357. */
  358. addParticipantContainer(user) {
  359. const id = user.getId();
  360. const remoteVideo = new RemoteVideo(user, VideoLayout, eventEmitter);
  361. this._setRemoteControlProperties(user, remoteVideo);
  362. this.addRemoteVideoContainer(id, remoteVideo);
  363. this.updateMutedForNoTracks(id, 'audio');
  364. this.updateMutedForNoTracks(id, 'video');
  365. const remoteVideosCount = Object.keys(remoteVideos).length;
  366. if (remoteVideosCount === 1) {
  367. window.setTimeout(() => {
  368. const updatedRemoteVideosCount
  369. = Object.keys(remoteVideos).length;
  370. if (updatedRemoteVideosCount === 1 && remoteVideos[id]) {
  371. this._maybePlaceParticipantOnLargeVideo(id);
  372. }
  373. }, 3000);
  374. }
  375. },
  376. /**
  377. * Adds remote video container for the given id and <tt>SmallVideo</tt>.
  378. *
  379. * @param {string} the id of the video to add
  380. * @param {SmallVideo} smallVideo the small video instance to add as a
  381. * remote video
  382. */
  383. addRemoteVideoContainer(id, remoteVideo) {
  384. remoteVideos[id] = remoteVideo;
  385. if (!remoteVideo.getVideoType()) {
  386. // make video type the default one (camera)
  387. // FIXME container type is not a video type
  388. remoteVideo.setVideoType(VIDEO_CONTAINER_TYPE);
  389. }
  390. VideoLayout.resizeThumbnails(true);
  391. // Initialize the view
  392. remoteVideo.updateView();
  393. },
  394. // FIXME: what does this do???
  395. remoteVideoActive(videoElement, resourceJid) {
  396. logger.info(`${resourceJid} video is now active`, videoElement);
  397. VideoLayout.resizeThumbnails(
  398. false, () => {
  399. if (videoElement) {
  400. $(videoElement).show();
  401. }
  402. });
  403. this._maybePlaceParticipantOnLargeVideo(resourceJid);
  404. },
  405. /**
  406. * Update the large video to the last added video only if there's no current
  407. * dominant, focused speaker or update it to the current dominant speaker.
  408. *
  409. * @params {string} resourceJid - The id of the user to maybe display on
  410. * large video.
  411. * @returns {void}
  412. */
  413. _maybePlaceParticipantOnLargeVideo(resourceJid) {
  414. const pinnedId = this.getPinnedId();
  415. if ((!pinnedId
  416. && !getCurrentRemoteDominantSpeakerID()
  417. && this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE))
  418. || pinnedId === resourceJid
  419. || (!pinnedId && resourceJid
  420. && getCurrentRemoteDominantSpeakerID() === resourceJid)
  421. /* Playback started while we're on the stage - may need to update
  422. video source with the new stream */
  423. || this.isCurrentlyOnLarge(resourceJid)) {
  424. this.updateLargeVideo(resourceJid, true);
  425. }
  426. },
  427. /**
  428. * Shows a visual indicator for the moderator of the conference.
  429. * On local or remote participants.
  430. */
  431. showModeratorIndicator() {
  432. const isModerator = APP.conference.isModerator;
  433. if (isModerator) {
  434. localVideoThumbnail.addModeratorIndicator();
  435. } else {
  436. localVideoThumbnail.removeModeratorIndicator();
  437. }
  438. APP.conference.listMembers().forEach(member => {
  439. const id = member.getId();
  440. const remoteVideo = remoteVideos[id];
  441. if (!remoteVideo) {
  442. return;
  443. }
  444. if (member.isModerator()) {
  445. remoteVideo.addModeratorIndicator();
  446. }
  447. remoteVideo.updateRemoteVideoMenu();
  448. });
  449. },
  450. /*
  451. * Shows or hides the audio muted indicator over the local thumbnail video.
  452. * @param {boolean} isMuted
  453. */
  454. showLocalAudioIndicator(isMuted) {
  455. localVideoThumbnail.showAudioIndicator(isMuted);
  456. },
  457. /**
  458. * Shows/hides the indication about local connection being interrupted.
  459. *
  460. * @param {boolean} isInterrupted <tt>true</tt> if local connection is
  461. * currently in the interrupted state or <tt>false</tt> if the connection
  462. * is fine.
  463. */
  464. showLocalConnectionInterrupted(isInterrupted) {
  465. // Currently local video thumbnail displays only "active" or
  466. // "interrupted" despite the fact that ConnectionIndicator supports more
  467. // states.
  468. const status
  469. = isInterrupted
  470. ? JitsiParticipantConnectionStatus.INTERRUPTED
  471. : JitsiParticipantConnectionStatus.ACTIVE;
  472. localVideoThumbnail.updateConnectionStatus(status);
  473. },
  474. /**
  475. * Resizes thumbnails.
  476. */
  477. resizeThumbnails(
  478. forceUpdate = false,
  479. onComplete = null) {
  480. const { localVideo, remoteVideo }
  481. = Filmstrip.calculateThumbnailSize();
  482. Filmstrip.resizeThumbnails(localVideo, remoteVideo, forceUpdate);
  483. if (onComplete && typeof onComplete === 'function') {
  484. onComplete();
  485. }
  486. return { localVideo,
  487. remoteVideo };
  488. },
  489. /**
  490. * On audio muted event.
  491. */
  492. onAudioMute(id, isMuted) {
  493. if (APP.conference.isLocalId(id)) {
  494. localVideoThumbnail.showAudioIndicator(isMuted);
  495. } else {
  496. const remoteVideo = remoteVideos[id];
  497. if (!remoteVideo) {
  498. return;
  499. }
  500. remoteVideo.showAudioIndicator(isMuted);
  501. remoteVideo.updateRemoteVideoMenu(isMuted);
  502. }
  503. },
  504. /**
  505. * On video muted event.
  506. */
  507. onVideoMute(id, value) {
  508. if (APP.conference.isLocalId(id)) {
  509. localVideoThumbnail.setVideoMutedView(value);
  510. } else {
  511. const remoteVideo = remoteVideos[id];
  512. if (remoteVideo) {
  513. remoteVideo.setVideoMutedView(value);
  514. }
  515. }
  516. if (this.isCurrentlyOnLarge(id)) {
  517. // large video will show avatar instead of muted stream
  518. this.updateLargeVideo(id, true);
  519. }
  520. },
  521. /**
  522. * Display name changed.
  523. */
  524. onDisplayNameChanged(id, displayName, status) {
  525. if (id === 'localVideoContainer'
  526. || APP.conference.isLocalId(id)) {
  527. localVideoThumbnail.setDisplayName(displayName);
  528. } else {
  529. const remoteVideo = remoteVideos[id];
  530. if (remoteVideo) {
  531. remoteVideo.setDisplayName(displayName, status);
  532. }
  533. }
  534. },
  535. /**
  536. * Sets the "raised hand" status for a participant identified by 'id'.
  537. */
  538. setRaisedHandStatus(id, raisedHandStatus) {
  539. const video
  540. = APP.conference.isLocalId(id)
  541. ? localVideoThumbnail : remoteVideos[id];
  542. if (video) {
  543. video.showRaisedHandIndicator(raisedHandStatus);
  544. if (raisedHandStatus) {
  545. video.showDominantSpeakerIndicator(false);
  546. }
  547. }
  548. },
  549. /**
  550. * On dominant speaker changed event.
  551. *
  552. * @param {string} id - The participant ID of the new dominant speaker.
  553. * @returns {void}
  554. */
  555. onDominantSpeakerChanged(id) {
  556. getAllThumbnails().forEach(thumbnail =>
  557. thumbnail.showDominantSpeakerIndicator(id === thumbnail.getId()));
  558. if (!remoteVideos[id]) {
  559. return;
  560. }
  561. // Local video will not have container found, but that's ok
  562. // since we don't want to switch to local video.
  563. if (!interfaceConfig.filmStripOnly && !this.getPinnedId()
  564. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  565. this.updateLargeVideo(id);
  566. }
  567. },
  568. /**
  569. * Shows/hides warning about remote user's connectivity issues.
  570. *
  571. * @param {string} id the ID of the remote participant(MUC nickname)
  572. */
  573. // eslint-disable-next-line no-unused-vars
  574. onParticipantConnectionStatusChanged(id) {
  575. // Show/hide warning on the large video
  576. if (this.isCurrentlyOnLarge(id)) {
  577. if (largeVideo) {
  578. // We have to trigger full large video update to transition from
  579. // avatar to video on connectivity restored.
  580. this.updateLargeVideo(id, true /* force update */);
  581. }
  582. }
  583. // Show/hide warning on the thumbnail
  584. const remoteVideo = remoteVideos[id];
  585. if (remoteVideo) {
  586. // Updating only connection status indicator is not enough, because
  587. // when we the connection is restored while the avatar was displayed
  588. // (due to 'muted while disconnected' condition) we may want to show
  589. // the video stream again and in order to do that the display mode
  590. // must be updated.
  591. // remoteVideo.updateConnectionStatusIndicator(isActive);
  592. remoteVideo.updateView();
  593. }
  594. },
  595. /**
  596. * On last N change event.
  597. *
  598. * @param endpointsLeavingLastN the list currently leaving last N
  599. * endpoints
  600. * @param endpointsEnteringLastN the list currently entering last N
  601. * endpoints
  602. */
  603. onLastNEndpointsChanged(endpointsLeavingLastN, endpointsEnteringLastN) {
  604. if (endpointsLeavingLastN) {
  605. endpointsLeavingLastN.forEach(this._updateRemoteVideo, this);
  606. }
  607. if (endpointsEnteringLastN) {
  608. endpointsEnteringLastN.forEach(this._updateRemoteVideo, this);
  609. }
  610. },
  611. /**
  612. * Updates remote video by id if it exists.
  613. * @param {string} id of the remote video
  614. * @private
  615. */
  616. _updateRemoteVideo(id) {
  617. const remoteVideo = remoteVideos[id];
  618. if (remoteVideo) {
  619. remoteVideo.updateView();
  620. if (remoteVideo.isCurrentlyOnLargeVideo()) {
  621. this.updateLargeVideo(id);
  622. }
  623. }
  624. },
  625. /**
  626. * Hides the connection indicator
  627. * @param id
  628. */
  629. hideConnectionIndicator(id) {
  630. const remoteVideo = remoteVideos[id];
  631. if (remoteVideo) {
  632. remoteVideo.removeConnectionIndicator();
  633. }
  634. },
  635. /**
  636. * Hides all the indicators
  637. */
  638. hideStats() {
  639. for (const video in remoteVideos) { // eslint-disable-line guard-for-in
  640. const remoteVideo = remoteVideos[video];
  641. if (remoteVideo) {
  642. remoteVideo.removeConnectionIndicator();
  643. }
  644. }
  645. localVideoThumbnail.removeConnectionIndicator();
  646. },
  647. removeParticipantContainer(id) {
  648. // Unlock large video
  649. if (this.getPinnedId() === id) {
  650. logger.info('Focused video owner has left the conference');
  651. APP.store.dispatch(pinParticipant(null));
  652. }
  653. const remoteVideo = remoteVideos[id];
  654. if (remoteVideo) {
  655. // Remove remote video
  656. logger.info(`Removing remote video: ${id}`);
  657. delete remoteVideos[id];
  658. remoteVideo.remove();
  659. } else {
  660. logger.warn(`No remote video for ${id}`);
  661. }
  662. VideoLayout.resizeThumbnails();
  663. VideoLayout._updateAfterThumbRemoved(id);
  664. },
  665. onVideoTypeChanged(id, newVideoType) {
  666. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  667. return;
  668. }
  669. logger.info('Peer video type changed: ', id, newVideoType);
  670. let smallVideo;
  671. if (APP.conference.isLocalId(id)) {
  672. if (!localVideoThumbnail) {
  673. logger.warn('Local video not ready yet');
  674. return;
  675. }
  676. smallVideo = localVideoThumbnail;
  677. } else if (remoteVideos[id]) {
  678. smallVideo = remoteVideos[id];
  679. } else {
  680. return;
  681. }
  682. smallVideo.setVideoType(newVideoType);
  683. if (this.isCurrentlyOnLarge(id)) {
  684. this.updateLargeVideo(id, true);
  685. }
  686. },
  687. /**
  688. * Resizes the video area.
  689. *
  690. * TODO: Remove the "animate" param as it is no longer passed in as true.
  691. *
  692. * @param forceUpdate indicates that hidden thumbnails will be shown
  693. */
  694. resizeVideoArea(
  695. forceUpdate = false,
  696. animate = false) {
  697. if (largeVideo) {
  698. largeVideo.updateContainerSize();
  699. largeVideo.resize(animate);
  700. }
  701. // Calculate available width and height.
  702. const availableHeight = window.innerHeight;
  703. const availableWidth = UIUtil.getAvailableVideoWidth();
  704. if (availableWidth < 0 || availableHeight < 0) {
  705. return;
  706. }
  707. // Resize the thumbnails first.
  708. this.resizeThumbnails(forceUpdate);
  709. },
  710. getSmallVideo(id) {
  711. if (APP.conference.isLocalId(id)) {
  712. return localVideoThumbnail;
  713. }
  714. return remoteVideos[id];
  715. },
  716. changeUserAvatar(id, avatarUrl) {
  717. const smallVideo = VideoLayout.getSmallVideo(id);
  718. if (smallVideo) {
  719. smallVideo.avatarChanged(avatarUrl);
  720. } else {
  721. logger.warn(
  722. `Missed avatar update - no small video yet for ${id}`
  723. );
  724. }
  725. if (this.isCurrentlyOnLarge(id)) {
  726. largeVideo.updateAvatar(avatarUrl);
  727. }
  728. },
  729. /**
  730. * Indicates that the video has been interrupted.
  731. */
  732. onVideoInterrupted() {
  733. if (largeVideo) {
  734. largeVideo.onVideoInterrupted();
  735. }
  736. },
  737. /**
  738. * Indicates that the video has been restored.
  739. */
  740. onVideoRestored() {
  741. if (largeVideo) {
  742. largeVideo.onVideoRestored();
  743. }
  744. },
  745. isLargeVideoVisible() {
  746. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  747. },
  748. /**
  749. * @return {LargeContainer} the currently displayed container on large
  750. * video.
  751. */
  752. getCurrentlyOnLargeContainer() {
  753. return largeVideo.getCurrentContainer();
  754. },
  755. isCurrentlyOnLarge(id) {
  756. return largeVideo && largeVideo.id === id;
  757. },
  758. /**
  759. * Triggers an update of remote video and large video displays so they may
  760. * pick up any state changes that have occurred elsewhere.
  761. *
  762. * @returns {void}
  763. */
  764. updateAllVideos() {
  765. const displayedUserId = this.getLargeVideoID();
  766. if (displayedUserId) {
  767. this.updateLargeVideo(displayedUserId, true);
  768. }
  769. Object.keys(remoteVideos).forEach(video => {
  770. remoteVideos[video].updateView();
  771. });
  772. },
  773. updateLargeVideo(id, forceUpdate) {
  774. if (!largeVideo) {
  775. return;
  776. }
  777. const currentContainer = largeVideo.getCurrentContainer();
  778. const currentContainerType = largeVideo.getCurrentContainerType();
  779. const currentId = largeVideo.id;
  780. const isOnLarge = this.isCurrentlyOnLarge(id);
  781. const smallVideo = this.getSmallVideo(id);
  782. if (isOnLarge && !forceUpdate
  783. && LargeVideoManager.isVideoContainer(currentContainerType)
  784. && smallVideo) {
  785. const currentStreamId = currentContainer.getStreamID();
  786. const newStreamId
  787. = smallVideo.videoStream
  788. ? smallVideo.videoStream.getId() : null;
  789. // FIXME it might be possible to get rid of 'forceUpdate' argument
  790. if (currentStreamId !== newStreamId) {
  791. logger.debug('Enforcing large video update for stream change');
  792. forceUpdate = true; // eslint-disable-line no-param-reassign
  793. }
  794. }
  795. if (!isOnLarge || forceUpdate) {
  796. const videoType = this.getRemoteVideoType(id);
  797. // FIXME video type is not the same thing as container type
  798. if (id !== currentId && videoType === VIDEO_CONTAINER_TYPE) {
  799. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  800. }
  801. let oldSmallVideo;
  802. if (currentId) {
  803. oldSmallVideo = this.getSmallVideo(currentId);
  804. }
  805. smallVideo.waitForResolutionChange();
  806. if (oldSmallVideo) {
  807. oldSmallVideo.waitForResolutionChange();
  808. }
  809. largeVideo.updateLargeVideo(
  810. id,
  811. smallVideo.videoStream,
  812. videoType
  813. ).then(() => {
  814. // update current small video and the old one
  815. smallVideo.updateView();
  816. oldSmallVideo && oldSmallVideo.updateView();
  817. }, () => {
  818. // use clicked other video during update, nothing to do.
  819. });
  820. } else if (currentId) {
  821. const currentSmallVideo = this.getSmallVideo(currentId);
  822. currentSmallVideo.updateView();
  823. }
  824. },
  825. addLargeVideoContainer(type, container) {
  826. largeVideo && largeVideo.addContainer(type, container);
  827. },
  828. removeLargeVideoContainer(type) {
  829. largeVideo && largeVideo.removeContainer(type);
  830. },
  831. /**
  832. * @returns Promise
  833. */
  834. showLargeVideoContainer(type, show) {
  835. if (!largeVideo) {
  836. return Promise.reject();
  837. }
  838. const isVisible = this.isLargeContainerTypeVisible(type);
  839. if (isVisible === show) {
  840. return Promise.resolve();
  841. }
  842. const currentId = largeVideo.id;
  843. let oldSmallVideo;
  844. if (currentId) {
  845. oldSmallVideo = this.getSmallVideo(currentId);
  846. }
  847. let containerTypeToShow = type;
  848. // if we are hiding a container and there is focusedVideo
  849. // (pinned remote video) use its video type,
  850. // if not then use default type - large video
  851. if (!show) {
  852. const pinnedId = this.getPinnedId();
  853. if (pinnedId) {
  854. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  855. } else {
  856. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  857. }
  858. }
  859. return largeVideo.showContainer(containerTypeToShow)
  860. .then(() => {
  861. if (oldSmallVideo) {
  862. oldSmallVideo && oldSmallVideo.updateView();
  863. }
  864. });
  865. },
  866. isLargeContainerTypeVisible(type) {
  867. return largeVideo && largeVideo.state === type;
  868. },
  869. /**
  870. * Returns the id of the current video shown on large.
  871. * Currently used by tests (torture).
  872. */
  873. getLargeVideoID() {
  874. return largeVideo && largeVideo.id;
  875. },
  876. /**
  877. * Returns the the current video shown on large.
  878. * Currently used by tests (torture).
  879. */
  880. getLargeVideo() {
  881. return largeVideo;
  882. },
  883. /**
  884. * Sets the flipX state of the local video.
  885. * @param {boolean} true for flipped otherwise false;
  886. */
  887. setLocalFlipX(val) {
  888. this.localFlipX = val;
  889. },
  890. getEventEmitter() {
  891. return eventEmitter;
  892. },
  893. /**
  894. * Handles user's features changes.
  895. */
  896. onUserFeaturesChanged(user) {
  897. const video = this.getSmallVideo(user.getId());
  898. if (!video) {
  899. return;
  900. }
  901. this._setRemoteControlProperties(user, video);
  902. },
  903. /**
  904. * Sets the remote control properties (checks whether remote control
  905. * is supported and executes remoteVideo.setRemoteControlSupport).
  906. * @param {JitsiParticipant} user the user that will be checked for remote
  907. * control support.
  908. * @param {RemoteVideo} remoteVideo the remoteVideo on which the properties
  909. * will be set.
  910. */
  911. _setRemoteControlProperties(user, remoteVideo) {
  912. APP.remoteControl.checkUserRemoteControlSupport(user).then(result =>
  913. remoteVideo.setRemoteControlSupport(result));
  914. },
  915. /**
  916. * Returns the wrapper jquery selector for the largeVideo
  917. * @returns {JQuerySelector} the wrapper jquery selector for the largeVideo
  918. */
  919. getLargeVideoWrapper() {
  920. return this.getCurrentlyOnLargeContainer().$wrapper;
  921. },
  922. /**
  923. * Returns the number of remove video ids.
  924. *
  925. * @returns {number} The number of remote videos.
  926. */
  927. getRemoteVideosCount() {
  928. return Object.keys(remoteVideos).length;
  929. },
  930. /**
  931. * Sets the remote control active status for a remote participant.
  932. *
  933. * @param {string} participantID - The id of the remote participant.
  934. * @param {boolean} isActive - The new remote control active status.
  935. * @returns {void}
  936. */
  937. setRemoteControlActiveStatus(participantID, isActive) {
  938. remoteVideos[participantID].setRemoteControlActiveStatus(isActive);
  939. },
  940. /**
  941. * Sets the remote control active status for the local participant.
  942. *
  943. * @returns {void}
  944. */
  945. setLocalRemoteControlActiveChanged() {
  946. Object.values(remoteVideos).forEach(
  947. remoteVideo => remoteVideo.updateRemoteVideoMenu()
  948. );
  949. },
  950. /**
  951. * Triggers an update of large video if the passed in participant is
  952. * currently displayed on large video.
  953. *
  954. * @param {string} participantId - The participant ID that should trigger an
  955. * update of large video if displayed.
  956. * @returns {void}
  957. */
  958. _updateLargeVideoIfDisplayed(participantId) {
  959. if (this.isCurrentlyOnLarge(participantId)) {
  960. this.updateLargeVideo(participantId);
  961. }
  962. }
  963. };
  964. export default VideoLayout;