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

VideoLayout.js 33KB

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