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

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