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

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