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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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._onContactClicked = onContactClicked.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._onContactClicked);
  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._onContactClicked);
  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. if (smallVideo && smallVideo.hasVideo()) {
  333. this.updateLargeVideo(currentDominantSpeaker);
  334. }
  335. } else {
  336. // if there is no currentDominantSpeaker, it can also be
  337. // that local participant is the dominant speaker
  338. // we should act as a participant has left and was on large
  339. // and we should choose somebody (electLastVisibleVideo)
  340. this.updateLargeVideo(this.electLastVisibleVideo());
  341. }
  342. return;
  343. }
  344. // Update focused/pinned interface.
  345. if (id) {
  346. if (smallVideo && !interfaceConfig.filmStripOnly) {
  347. smallVideo.focus(true);
  348. this.pinParticipant(id);
  349. }
  350. }
  351. this.updateLargeVideo(id);
  352. },
  353. /**
  354. * Creates or adds a participant container for the given id and smallVideo.
  355. *
  356. * @param {JitsiParticipant} user the participant to add
  357. * @param {SmallVideo} smallVideo optional small video instance to add as a
  358. * remote video, if undefined <tt>RemoteVideo</tt> will be created
  359. */
  360. addParticipantContainer(user, smallVideo) {
  361. const id = user.getId();
  362. let remoteVideo;
  363. if (smallVideo) {
  364. remoteVideo = smallVideo;
  365. } else {
  366. remoteVideo = new RemoteVideo(user, VideoLayout, eventEmitter);
  367. }
  368. this._setRemoteControlProperties(user, remoteVideo);
  369. this.addRemoteVideoContainer(id, remoteVideo);
  370. this.updateMutedForNoTracks(id, 'audio');
  371. this.updateMutedForNoTracks(id, 'video');
  372. const remoteVideosCount = Object.keys(remoteVideos).length;
  373. if (remoteVideosCount === 1) {
  374. window.setTimeout(() => {
  375. const updatedRemoteVideosCount
  376. = Object.keys(remoteVideos).length;
  377. if (updatedRemoteVideosCount === 1 && remoteVideos[id]) {
  378. this._maybePlaceParticipantOnLargeVideo(id);
  379. }
  380. }, 3000);
  381. }
  382. },
  383. /**
  384. * Adds remote video container for the given id and <tt>SmallVideo</tt>.
  385. *
  386. * @param {string} the id of the video to add
  387. * @param {SmallVideo} smallVideo the small video instance to add as a
  388. * remote video
  389. */
  390. addRemoteVideoContainer(id, remoteVideo) {
  391. remoteVideos[id] = remoteVideo;
  392. if (!remoteVideo.getVideoType()) {
  393. // make video type the default one (camera)
  394. // FIXME container type is not a video type
  395. remoteVideo.setVideoType(VIDEO_CONTAINER_TYPE);
  396. }
  397. VideoLayout.resizeThumbnails(false, true);
  398. // Initialize the view
  399. remoteVideo.updateView();
  400. },
  401. // FIXME: what does this do???
  402. remoteVideoActive(videoElement, resourceJid) {
  403. logger.info(`${resourceJid} video is now active`, videoElement);
  404. VideoLayout.resizeThumbnails(
  405. false, false, () => {
  406. if (videoElement) {
  407. $(videoElement).show();
  408. }
  409. });
  410. this._maybePlaceParticipantOnLargeVideo(resourceJid);
  411. },
  412. /**
  413. * Update the large video to the last added video only if there's no current
  414. * dominant, focused speaker or update it to the current dominant speaker.
  415. *
  416. * @params {string} resourceJid - The id of the user to maybe display on
  417. * large video.
  418. * @returns {void}
  419. */
  420. _maybePlaceParticipantOnLargeVideo(resourceJid) {
  421. const pinnedId = this.getPinnedId();
  422. if ((!pinnedId
  423. && !currentDominantSpeaker
  424. && this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE))
  425. || pinnedId === resourceJid
  426. || (!pinnedId && resourceJid
  427. && currentDominantSpeaker === resourceJid)
  428. /* Playback started while we're on the stage - may need to update
  429. video source with the new stream */
  430. || this.isCurrentlyOnLarge(resourceJid)) {
  431. this.updateLargeVideo(resourceJid, true);
  432. }
  433. },
  434. /**
  435. * Shows a visual indicator for the moderator of the conference.
  436. * On local or remote participants.
  437. */
  438. showModeratorIndicator() {
  439. const isModerator = APP.conference.isModerator;
  440. if (isModerator) {
  441. localVideoThumbnail.addModeratorIndicator();
  442. } else {
  443. localVideoThumbnail.removeModeratorIndicator();
  444. }
  445. APP.conference.listMembers().forEach(member => {
  446. const id = member.getId();
  447. const remoteVideo = remoteVideos[id];
  448. if (!remoteVideo) {
  449. return;
  450. }
  451. if (member.isModerator()) {
  452. remoteVideo.addModeratorIndicator();
  453. }
  454. remoteVideo.updateRemoteVideoMenu();
  455. });
  456. },
  457. /*
  458. * Shows or hides the audio muted indicator over the local thumbnail video.
  459. * @param {boolean} isMuted
  460. */
  461. showLocalAudioIndicator(isMuted) {
  462. localVideoThumbnail.showAudioIndicator(isMuted);
  463. },
  464. /**
  465. * Shows/hides the indication about local connection being interrupted.
  466. *
  467. * @param {boolean} isInterrupted <tt>true</tt> if local connection is
  468. * currently in the interrupted state or <tt>false</tt> if the connection
  469. * is fine.
  470. */
  471. showLocalConnectionInterrupted(isInterrupted) {
  472. // Currently local video thumbnail displays only "active" or
  473. // "interrupted" despite the fact that ConnectionIndicator supports more
  474. // states.
  475. const status
  476. = isInterrupted
  477. ? JitsiParticipantConnectionStatus.INTERRUPTED
  478. : JitsiParticipantConnectionStatus.ACTIVE;
  479. localVideoThumbnail.updateConnectionStatus(status);
  480. },
  481. /**
  482. * Resizes thumbnails.
  483. */
  484. resizeThumbnails(
  485. animate = false,
  486. forceUpdate = false,
  487. onComplete = null) {
  488. const { localVideo, remoteVideo }
  489. = Filmstrip.calculateThumbnailSize();
  490. Filmstrip.resizeThumbnails(localVideo, remoteVideo,
  491. animate, forceUpdate)
  492. .then(() => {
  493. if (onComplete && typeof onComplete === 'function') {
  494. onComplete();
  495. }
  496. });
  497. return { localVideo,
  498. remoteVideo };
  499. },
  500. /**
  501. * On audio muted event.
  502. */
  503. onAudioMute(id, isMuted) {
  504. if (APP.conference.isLocalId(id)) {
  505. localVideoThumbnail.showAudioIndicator(isMuted);
  506. } else {
  507. const remoteVideo = remoteVideos[id];
  508. if (!remoteVideo) {
  509. return;
  510. }
  511. remoteVideo.showAudioIndicator(isMuted);
  512. remoteVideo.updateRemoteVideoMenu(isMuted);
  513. }
  514. },
  515. /**
  516. * On video muted event.
  517. */
  518. onVideoMute(id, value) {
  519. if (APP.conference.isLocalId(id)) {
  520. localVideoThumbnail.setVideoMutedView(value);
  521. } else {
  522. const remoteVideo = remoteVideos[id];
  523. if (remoteVideo) {
  524. remoteVideo.setVideoMutedView(value);
  525. }
  526. }
  527. if (this.isCurrentlyOnLarge(id)) {
  528. // large video will show avatar instead of muted stream
  529. this.updateLargeVideo(id, true);
  530. }
  531. },
  532. /**
  533. * Display name changed.
  534. */
  535. onDisplayNameChanged(id, displayName, status) {
  536. if (id === 'localVideoContainer'
  537. || APP.conference.isLocalId(id)) {
  538. localVideoThumbnail.setDisplayName(displayName);
  539. } else {
  540. const remoteVideo = remoteVideos[id];
  541. if (remoteVideo) {
  542. remoteVideo.setDisplayName(displayName, status);
  543. }
  544. }
  545. },
  546. /**
  547. * Sets the "raised hand" status for a participant identified by 'id'.
  548. */
  549. setRaisedHandStatus(id, raisedHandStatus) {
  550. const video
  551. = APP.conference.isLocalId(id)
  552. ? localVideoThumbnail : remoteVideos[id];
  553. if (video) {
  554. video.showRaisedHandIndicator(raisedHandStatus);
  555. if (raisedHandStatus) {
  556. video.showDominantSpeakerIndicator(false);
  557. }
  558. }
  559. },
  560. /**
  561. * On dominant speaker changed event.
  562. */
  563. onDominantSpeakerChanged(id) {
  564. if (id === currentDominantSpeaker) {
  565. return;
  566. }
  567. const oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  568. // We ignore local user events, but just unmark remote user as dominant
  569. // while we are talking
  570. if (APP.conference.isLocalId(id)) {
  571. if (oldSpeakerRemoteVideo) {
  572. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  573. currentDominantSpeaker = null;
  574. }
  575. localVideoThumbnail.showDominantSpeakerIndicator(true);
  576. return;
  577. }
  578. const remoteVideo = remoteVideos[id];
  579. if (!remoteVideo) {
  580. return;
  581. }
  582. // Update the current dominant speaker.
  583. remoteVideo.showDominantSpeakerIndicator(true);
  584. localVideoThumbnail.showDominantSpeakerIndicator(false);
  585. // let's remove the indications from the remote video if any
  586. if (oldSpeakerRemoteVideo) {
  587. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  588. }
  589. currentDominantSpeaker = id;
  590. // Local video will not have container found, but that's ok
  591. // since we don't want to switch to local video.
  592. // Update the large video if the video source is already available,
  593. // otherwise wait for the "videoactive.jingle" event.
  594. // FIXME: there is no "videoactive.jingle" event.
  595. if (!interfaceConfig.filmStripOnly && !this.getPinnedId()
  596. && remoteVideo.hasVideoStarted()
  597. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  598. this.updateLargeVideo(id);
  599. }
  600. },
  601. /**
  602. * Shows/hides warning about remote user's connectivity issues.
  603. *
  604. * @param {string} id the ID of the remote participant(MUC nickname)
  605. */
  606. // eslint-disable-next-line no-unused-vars
  607. onParticipantConnectionStatusChanged(id) {
  608. // Show/hide warning on the large video
  609. if (this.isCurrentlyOnLarge(id)) {
  610. if (largeVideo) {
  611. // We have to trigger full large video update to transition from
  612. // avatar to video on connectivity restored.
  613. this.updateLargeVideo(id, true /* force update */);
  614. }
  615. }
  616. // Show/hide warning on the thumbnail
  617. const remoteVideo = remoteVideos[id];
  618. if (remoteVideo) {
  619. // Updating only connection status indicator is not enough, because
  620. // when we the connection is restored while the avatar was displayed
  621. // (due to 'muted while disconnected' condition) we may want to show
  622. // the video stream again and in order to do that the display mode
  623. // must be updated.
  624. // remoteVideo.updateConnectionStatusIndicator(isActive);
  625. remoteVideo.updateView();
  626. }
  627. },
  628. /**
  629. * On last N change event.
  630. *
  631. * @param endpointsLeavingLastN the list currently leaving last N
  632. * endpoints
  633. * @param endpointsEnteringLastN the list currently entering last N
  634. * endpoints
  635. */
  636. onLastNEndpointsChanged(endpointsLeavingLastN, endpointsEnteringLastN) {
  637. if (endpointsLeavingLastN) {
  638. endpointsLeavingLastN.forEach(this._updateRemoteVideo, this);
  639. }
  640. if (endpointsEnteringLastN) {
  641. endpointsEnteringLastN.forEach(this._updateRemoteVideo, this);
  642. }
  643. },
  644. /**
  645. * Updates remote video by id if it exists.
  646. * @param {string} id of the remote video
  647. * @private
  648. */
  649. _updateRemoteVideo(id) {
  650. const remoteVideo = remoteVideos[id];
  651. if (remoteVideo) {
  652. remoteVideo.updateView();
  653. if (remoteVideo.isCurrentlyOnLargeVideo()) {
  654. this.updateLargeVideo(id);
  655. }
  656. }
  657. },
  658. /**
  659. * Hides the connection indicator
  660. * @param id
  661. */
  662. hideConnectionIndicator(id) {
  663. const remoteVideo = remoteVideos[id];
  664. if (remoteVideo) {
  665. remoteVideo.removeConnectionIndicator();
  666. }
  667. },
  668. /**
  669. * Hides all the indicators
  670. */
  671. hideStats() {
  672. for (const video in remoteVideos) { // eslint-disable-line guard-for-in
  673. const remoteVideo = remoteVideos[video];
  674. if (remoteVideo) {
  675. remoteVideo.removeConnectionIndicator();
  676. }
  677. }
  678. localVideoThumbnail.removeConnectionIndicator();
  679. },
  680. removeParticipantContainer(id) {
  681. // Unlock large video
  682. if (this.getPinnedId() === id) {
  683. logger.info('Focused video owner has left the conference');
  684. this.pinParticipant(null);
  685. }
  686. if (currentDominantSpeaker === id) {
  687. logger.info('Dominant speaker has left the conference');
  688. currentDominantSpeaker = null;
  689. }
  690. const remoteVideo = remoteVideos[id];
  691. if (remoteVideo) {
  692. // Remove remote video
  693. logger.info(`Removing remote video: ${id}`);
  694. delete remoteVideos[id];
  695. remoteVideo.remove();
  696. } else {
  697. logger.warn(`No remote video for ${id}`);
  698. }
  699. VideoLayout.resizeThumbnails();
  700. },
  701. onVideoTypeChanged(id, newVideoType) {
  702. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  703. return;
  704. }
  705. logger.info('Peer video type changed: ', id, newVideoType);
  706. let smallVideo;
  707. if (APP.conference.isLocalId(id)) {
  708. if (!localVideoThumbnail) {
  709. logger.warn('Local video not ready yet');
  710. return;
  711. }
  712. smallVideo = localVideoThumbnail;
  713. } else if (remoteVideos[id]) {
  714. smallVideo = remoteVideos[id];
  715. } else {
  716. return;
  717. }
  718. smallVideo.setVideoType(newVideoType);
  719. if (this.isCurrentlyOnLarge(id)) {
  720. this.updateLargeVideo(id, true);
  721. }
  722. },
  723. /**
  724. * Resizes the video area.
  725. *
  726. * @param forceUpdate indicates that hidden thumbnails will be shown
  727. * @param completeFunction a function to be called when the video area is
  728. * resized.
  729. */
  730. resizeVideoArea(
  731. forceUpdate = false,
  732. animate = false,
  733. completeFunction = null) {
  734. if (largeVideo) {
  735. largeVideo.updateContainerSize();
  736. largeVideo.resize(animate);
  737. }
  738. // Calculate available width and height.
  739. const availableHeight = window.innerHeight;
  740. const availableWidth = UIUtil.getAvailableVideoWidth();
  741. if (availableWidth < 0 || availableHeight < 0) {
  742. return;
  743. }
  744. // Resize the thumbnails first.
  745. this.resizeThumbnails(false, forceUpdate);
  746. // Resize the video area element.
  747. $('#videospace').animate({
  748. right: window.innerWidth - availableWidth,
  749. width: availableWidth,
  750. height: availableHeight
  751. }, {
  752. queue: false,
  753. duration: animate ? 500 : 1,
  754. complete: completeFunction
  755. });
  756. },
  757. getSmallVideo(id) {
  758. if (APP.conference.isLocalId(id)) {
  759. return localVideoThumbnail;
  760. }
  761. return remoteVideos[id];
  762. },
  763. changeUserAvatar(id, avatarUrl) {
  764. const smallVideo = VideoLayout.getSmallVideo(id);
  765. if (smallVideo) {
  766. smallVideo.avatarChanged(avatarUrl);
  767. } else {
  768. logger.warn(
  769. `Missed avatar update - no small video yet for ${id}`
  770. );
  771. }
  772. if (this.isCurrentlyOnLarge(id)) {
  773. largeVideo.updateAvatar(avatarUrl);
  774. }
  775. },
  776. /**
  777. * Indicates that the video has been interrupted.
  778. */
  779. onVideoInterrupted() {
  780. if (largeVideo) {
  781. largeVideo.onVideoInterrupted();
  782. }
  783. },
  784. /**
  785. * Indicates that the video has been restored.
  786. */
  787. onVideoRestored() {
  788. if (largeVideo) {
  789. largeVideo.onVideoRestored();
  790. }
  791. },
  792. isLargeVideoVisible() {
  793. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  794. },
  795. /**
  796. * @return {LargeContainer} the currently displayed container on large
  797. * video.
  798. */
  799. getCurrentlyOnLargeContainer() {
  800. return largeVideo.getCurrentContainer();
  801. },
  802. isCurrentlyOnLarge(id) {
  803. return largeVideo && largeVideo.id === id;
  804. },
  805. /**
  806. * Triggers an update of remote video and large video displays so they may
  807. * pick up any state changes that have occurred elsewhere.
  808. *
  809. * @returns {void}
  810. */
  811. updateAllVideos() {
  812. const displayedUserId = this.getLargeVideoID();
  813. if (displayedUserId) {
  814. this.updateLargeVideo(displayedUserId, true);
  815. }
  816. Object.keys(remoteVideos).forEach(video => {
  817. remoteVideos[video].updateView();
  818. });
  819. },
  820. updateLargeVideo(id, forceUpdate) {
  821. if (!largeVideo) {
  822. return;
  823. }
  824. const currentContainer = largeVideo.getCurrentContainer();
  825. const currentContainerType = largeVideo.getCurrentContainerType();
  826. const currentId = largeVideo.id;
  827. const isOnLarge = this.isCurrentlyOnLarge(id);
  828. const smallVideo = this.getSmallVideo(id);
  829. if (isOnLarge && !forceUpdate
  830. && LargeVideoManager.isVideoContainer(currentContainerType)
  831. && smallVideo) {
  832. const currentStreamId = currentContainer.getStreamID();
  833. const newStreamId
  834. = smallVideo.videoStream
  835. ? smallVideo.videoStream.getId() : null;
  836. // FIXME it might be possible to get rid of 'forceUpdate' argument
  837. if (currentStreamId !== newStreamId) {
  838. logger.debug('Enforcing large video update for stream change');
  839. forceUpdate = true; // eslint-disable-line no-param-reassign
  840. }
  841. }
  842. if (!isOnLarge || forceUpdate) {
  843. const videoType = this.getRemoteVideoType(id);
  844. // FIXME video type is not the same thing as container type
  845. if (id !== currentId && videoType === VIDEO_CONTAINER_TYPE) {
  846. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  847. }
  848. let oldSmallVideo;
  849. if (currentId) {
  850. oldSmallVideo = this.getSmallVideo(currentId);
  851. }
  852. smallVideo.waitForResolutionChange();
  853. if (oldSmallVideo) {
  854. oldSmallVideo.waitForResolutionChange();
  855. }
  856. largeVideo.updateLargeVideo(
  857. id,
  858. smallVideo.videoStream,
  859. videoType
  860. ).then(() => {
  861. // update current small video and the old one
  862. smallVideo.updateView();
  863. oldSmallVideo && oldSmallVideo.updateView();
  864. }, () => {
  865. // use clicked other video during update, nothing to do.
  866. });
  867. } else if (currentId) {
  868. const currentSmallVideo = this.getSmallVideo(currentId);
  869. currentSmallVideo.updateView();
  870. }
  871. },
  872. addLargeVideoContainer(type, container) {
  873. largeVideo && largeVideo.addContainer(type, container);
  874. },
  875. removeLargeVideoContainer(type) {
  876. largeVideo && largeVideo.removeContainer(type);
  877. },
  878. /**
  879. * @returns Promise
  880. */
  881. showLargeVideoContainer(type, show) {
  882. if (!largeVideo) {
  883. return Promise.reject();
  884. }
  885. const isVisible = this.isLargeContainerTypeVisible(type);
  886. if (isVisible === show) {
  887. return Promise.resolve();
  888. }
  889. const currentId = largeVideo.id;
  890. let oldSmallVideo;
  891. if (currentId) {
  892. oldSmallVideo = this.getSmallVideo(currentId);
  893. }
  894. let containerTypeToShow = type;
  895. // if we are hiding a container and there is focusedVideo
  896. // (pinned remote video) use its video type,
  897. // if not then use default type - large video
  898. if (!show) {
  899. const pinnedId = this.getPinnedId();
  900. if (pinnedId) {
  901. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  902. } else {
  903. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  904. }
  905. }
  906. return largeVideo.showContainer(containerTypeToShow)
  907. .then(() => {
  908. if (oldSmallVideo) {
  909. oldSmallVideo && oldSmallVideo.updateView();
  910. }
  911. });
  912. },
  913. isLargeContainerTypeVisible(type) {
  914. return largeVideo && largeVideo.state === type;
  915. },
  916. /**
  917. * Returns the id of the current video shown on large.
  918. * Currently used by tests (torture).
  919. */
  920. getLargeVideoID() {
  921. return largeVideo.id;
  922. },
  923. /**
  924. * Returns the the current video shown on large.
  925. * Currently used by tests (torture).
  926. */
  927. getLargeVideo() {
  928. return largeVideo;
  929. },
  930. /**
  931. * Sets the flipX state of the local video.
  932. * @param {boolean} true for flipped otherwise false;
  933. */
  934. setLocalFlipX(val) {
  935. this.localFlipX = val;
  936. },
  937. getEventEmitter() {
  938. return eventEmitter;
  939. },
  940. /**
  941. * Handles user's features changes.
  942. */
  943. onUserFeaturesChanged(user) {
  944. const video = this.getSmallVideo(user.getId());
  945. if (!video) {
  946. return;
  947. }
  948. this._setRemoteControlProperties(user, video);
  949. },
  950. /**
  951. * Sets the remote control properties (checks whether remote control
  952. * is supported and executes remoteVideo.setRemoteControlSupport).
  953. * @param {JitsiParticipant} user the user that will be checked for remote
  954. * control support.
  955. * @param {RemoteVideo} remoteVideo the remoteVideo on which the properties
  956. * will be set.
  957. */
  958. _setRemoteControlProperties(user, remoteVideo) {
  959. APP.remoteControl.checkUserRemoteControlSupport(user).then(result =>
  960. remoteVideo.setRemoteControlSupport(result));
  961. },
  962. /**
  963. * Returns the wrapper jquery selector for the largeVideo
  964. * @returns {JQuerySelector} the wrapper jquery selector for the largeVideo
  965. */
  966. getLargeVideoWrapper() {
  967. return this.getCurrentlyOnLargeContainer().$wrapper;
  968. },
  969. /**
  970. * Returns the number of remove video ids.
  971. *
  972. * @returns {number} The number of remote videos.
  973. */
  974. getRemoteVideosCount() {
  975. return Object.keys(remoteVideos).length;
  976. },
  977. /**
  978. * Sets the remote control active status for a remote participant.
  979. *
  980. * @param {string} participantID - The id of the remote participant.
  981. * @param {boolean} isActive - The new remote control active status.
  982. * @returns {void}
  983. */
  984. setRemoteControlActiveStatus(participantID, isActive) {
  985. remoteVideos[participantID].setRemoteControlActiveStatus(isActive);
  986. },
  987. /**
  988. * Sets the remote control active status for the local participant.
  989. *
  990. * @returns {void}
  991. */
  992. setLocalRemoteControlActiveChanged() {
  993. Object.values(remoteVideos).forEach(
  994. remoteVideo => remoteVideo.updateRemoteVideoMenu()
  995. );
  996. }
  997. };
  998. /**
  999. * On contact list item clicked.
  1000. */
  1001. function onContactClicked(id) {
  1002. if (APP.conference.isLocalId(id)) {
  1003. $('#localVideoContainer').click();
  1004. return;
  1005. }
  1006. const remoteVideo = remoteVideos[id];
  1007. if (remoteVideo && remoteVideo.hasVideo()) {
  1008. // It is not always the case that a videoThumb exists (if there is
  1009. // no actual video).
  1010. if (remoteVideo.hasVideoStarted()) {
  1011. // We have a video src, great! Let's update the large video
  1012. // now.
  1013. VideoLayout.handleVideoThumbClicked(id);
  1014. } else {
  1015. // If we don't have a video src for jid, there's absolutely
  1016. // no point in calling handleVideoThumbClicked; Quite
  1017. // simply, it won't work because it needs an src to attach
  1018. // to the large video.
  1019. //
  1020. // Instead, we trigger the pinned endpoint changed event to
  1021. // let the bridge adjust its lastN set for myjid and store
  1022. // the pinned user in the lastNPickupId variable to be
  1023. // picked up later by the lastN changed event handler.
  1024. // eslint-disable-next-line no-invalid-this
  1025. this.pinParticipant(remoteVideo.id);
  1026. }
  1027. }
  1028. }
  1029. export default VideoLayout;