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

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