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

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