您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VideoLayout.js 34KB

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