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.

VideoContainer.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /* global $, APP, interfaceConfig */
  2. /* eslint-disable no-unused-vars */
  3. import React from 'react';
  4. import ReactDOM from 'react-dom';
  5. import { browser } from '../../../react/features/base/lib-jitsi-meet';
  6. import { isTestModeEnabled } from '../../../react/features/base/testing';
  7. import { FILMSTRIP_BREAKPOINT } from '../../../react/features/filmstrip';
  8. import { ORIENTATION, LargeVideoBackground, updateLastLargeVideoMediaEvent } from '../../../react/features/large-video';
  9. import { LAYOUTS, getCurrentLayout } from '../../../react/features/video-layout';
  10. /* eslint-enable no-unused-vars */
  11. import UIUtil from '../util/UIUtil';
  12. import Filmstrip from './Filmstrip';
  13. import LargeContainer from './LargeContainer';
  14. // FIXME should be 'video'
  15. export const VIDEO_CONTAINER_TYPE = 'camera';
  16. const FADE_DURATION_MS = 300;
  17. /**
  18. * List of container events that we are going to process, will be added as listener to the
  19. * container for every event in the list. The latest event will be stored in redux.
  20. */
  21. const containerEvents = [
  22. 'abort', 'canplay', 'canplaythrough', 'emptied', 'ended', 'error', 'loadeddata', 'loadedmetadata', 'loadstart',
  23. 'pause', 'play', 'playing', 'ratechange', 'stalled', 'suspend', 'waiting'
  24. ];
  25. /**
  26. * Returns an array of the video dimensions, so that it keeps it's aspect
  27. * ratio and fits available area with it's larger dimension. This method
  28. * ensures that whole video will be visible and can leave empty areas.
  29. *
  30. * @param videoWidth the width of the video to position
  31. * @param videoHeight the height of the video to position
  32. * @param videoSpaceWidth the width of the available space
  33. * @param videoSpaceHeight the height of the available space
  34. * @param subtractFilmstrip whether to subtract the filmstrip or not
  35. * @return an array with 2 elements, the video width and the video height
  36. */
  37. function computeDesktopVideoSize( // eslint-disable-line max-params
  38. videoWidth,
  39. videoHeight,
  40. videoSpaceWidth,
  41. videoSpaceHeight,
  42. subtractFilmstrip) {
  43. if (videoWidth === 0 || videoHeight === 0 || videoSpaceWidth === 0 || videoSpaceHeight === 0) {
  44. // Avoid NaN values caused by division by 0.
  45. return [ 0, 0 ];
  46. }
  47. const aspectRatio = videoWidth / videoHeight;
  48. let availableWidth = Math.max(videoWidth, videoSpaceWidth);
  49. let availableHeight = Math.max(videoHeight, videoSpaceHeight);
  50. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  51. if (subtractFilmstrip) {
  52. // eslint-disable-next-line no-param-reassign
  53. videoSpaceWidth -= Filmstrip.getVerticalFilmstripWidth();
  54. }
  55. } else {
  56. // eslint-disable-next-line no-param-reassign
  57. videoSpaceHeight -= Filmstrip.getFilmstripHeight();
  58. }
  59. if (availableWidth / aspectRatio >= videoSpaceHeight) {
  60. availableHeight = videoSpaceHeight;
  61. availableWidth = availableHeight * aspectRatio;
  62. }
  63. if (availableHeight * aspectRatio >= videoSpaceWidth) {
  64. availableWidth = videoSpaceWidth;
  65. availableHeight = availableWidth / aspectRatio;
  66. }
  67. return [ availableWidth, availableHeight ];
  68. }
  69. /**
  70. * Returns an array of the video dimensions. It respects the
  71. * VIDEO_LAYOUT_FIT config, to fit the video to the screen, by hiding some parts
  72. * of it, or to fit it to the height or width.
  73. *
  74. * @param videoWidth the original video width
  75. * @param videoHeight the original video height
  76. * @param videoSpaceWidth the width of the video space
  77. * @param videoSpaceHeight the height of the video space
  78. * @return an array with 2 elements, the video width and the video height
  79. */
  80. function computeCameraVideoSize( // eslint-disable-line max-params
  81. videoWidth,
  82. videoHeight,
  83. videoSpaceWidth,
  84. videoSpaceHeight,
  85. videoLayoutFit) {
  86. if (videoWidth === 0 || videoHeight === 0 || videoSpaceWidth === 0 || videoSpaceHeight === 0) {
  87. // Avoid NaN values caused by division by 0.
  88. return [ 0, 0 ];
  89. }
  90. const aspectRatio = videoWidth / videoHeight;
  91. const videoSpaceRatio = videoSpaceWidth / videoSpaceHeight;
  92. switch (videoLayoutFit) {
  93. case 'height':
  94. return [ videoSpaceHeight * aspectRatio, videoSpaceHeight ];
  95. case 'width':
  96. return [ videoSpaceWidth, videoSpaceWidth / aspectRatio ];
  97. case 'nocrop':
  98. return computeCameraVideoSize(
  99. videoWidth,
  100. videoHeight,
  101. videoSpaceWidth,
  102. videoSpaceHeight,
  103. videoSpaceRatio < aspectRatio ? 'width' : 'height');
  104. case 'both': {
  105. const maxZoomCoefficient = interfaceConfig.MAXIMUM_ZOOMING_COEFFICIENT
  106. || Infinity;
  107. if (videoSpaceRatio === aspectRatio) {
  108. return [ videoSpaceWidth, videoSpaceHeight ];
  109. }
  110. let [ width, height ] = computeCameraVideoSize(
  111. videoWidth,
  112. videoHeight,
  113. videoSpaceWidth,
  114. videoSpaceHeight,
  115. videoSpaceRatio < aspectRatio ? 'height' : 'width');
  116. const maxWidth = videoSpaceWidth * maxZoomCoefficient;
  117. const maxHeight = videoSpaceHeight * maxZoomCoefficient;
  118. if (width > maxWidth) {
  119. width = maxWidth;
  120. height = width / aspectRatio;
  121. } else if (height > maxHeight) {
  122. height = maxHeight;
  123. width = height * aspectRatio;
  124. }
  125. return [ width, height ];
  126. }
  127. default:
  128. return [ videoWidth, videoHeight ];
  129. }
  130. }
  131. /**
  132. * Returns an array of the video horizontal and vertical indents,
  133. * so that if fits its parent.
  134. *
  135. * @return an array with 2 elements, the horizontal indent and the vertical
  136. * indent
  137. */
  138. function getCameraVideoPosition( // eslint-disable-line max-params
  139. videoWidth,
  140. videoHeight,
  141. videoSpaceWidth,
  142. videoSpaceHeight) {
  143. // Parent height isn't completely calculated when we position the video in
  144. // full screen mode and this is why we use the screen height in this case.
  145. // Need to think it further at some point and implement it properly.
  146. if (UIUtil.isFullScreen()) {
  147. // eslint-disable-next-line no-param-reassign
  148. videoSpaceHeight = window.innerHeight;
  149. }
  150. const horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  151. const verticalIndent = (videoSpaceHeight - videoHeight) / 2;
  152. return { horizontalIndent,
  153. verticalIndent };
  154. }
  155. /**
  156. * Container for user video.
  157. */
  158. export class VideoContainer extends LargeContainer {
  159. /**
  160. *
  161. */
  162. get $video() {
  163. return $('#largeVideo');
  164. }
  165. /**
  166. *
  167. */
  168. get id() {
  169. return this.userId;
  170. }
  171. /**
  172. * Creates new VideoContainer instance.
  173. * @param resizeContainer {Function} function that takes care of the size
  174. * of the video container.
  175. */
  176. constructor(resizeContainer) {
  177. super();
  178. this.stream = null;
  179. this.userId = null;
  180. this.videoType = null;
  181. this.localFlipX = true;
  182. this.resizeContainer = resizeContainer;
  183. /**
  184. * Whether the background should fit the height of the container
  185. * (portrait) or fit the width of the container (landscape).
  186. *
  187. * @private
  188. * @type {string|null}
  189. */
  190. this._backgroundOrientation = null;
  191. /**
  192. * Flag indicates whether or not the background should be rendered.
  193. * If the background will not be visible then it is hidden to save
  194. * on performance.
  195. * @type {boolean}
  196. */
  197. this._hideBackground = true;
  198. this._isHidden = false;
  199. /**
  200. * Flag indicates whether or not the avatar is currently displayed.
  201. * @type {boolean}
  202. */
  203. this.avatarDisplayed = false;
  204. this.$avatar = $('#dominantSpeaker');
  205. /**
  206. * A jQuery selector of the remote connection message.
  207. * @type {jQuery|HTMLElement}
  208. */
  209. this.$remoteConnectionMessage = $('#remoteConnectionMessage');
  210. this.$remotePresenceMessage = $('#remotePresenceMessage');
  211. this.$wrapper = $('#largeVideoWrapper');
  212. /**
  213. * FIXME: currently using parent() because I can't come up with name
  214. * for id. We'll need to probably refactor the HTML related to the large
  215. * video anyway.
  216. */
  217. this.$wrapperParent = this.$wrapper.parent();
  218. this.avatarHeight = $('#dominantSpeakerAvatarContainer').height();
  219. this.$video[0].onplaying = function(event) {
  220. if (typeof resizeContainer === 'function') {
  221. resizeContainer(event);
  222. }
  223. };
  224. /**
  225. * A Set of functions to invoke when the video element resizes.
  226. *
  227. * @private
  228. */
  229. this._resizeListeners = new Set();
  230. this.$video[0].onresize = this._onResize.bind(this);
  231. if (isTestModeEnabled(APP.store.getState())) {
  232. const cb = name => APP.store.dispatch(updateLastLargeVideoMediaEvent(name));
  233. containerEvents.forEach(event => {
  234. this.$video[0].addEventListener(event, cb.bind(this, event));
  235. });
  236. }
  237. }
  238. /**
  239. * Adds a function to the known subscribers of video element resize
  240. * events.
  241. *
  242. * @param {Function} callback - The subscriber to notify when the video
  243. * element resizes.
  244. * @returns {void}
  245. */
  246. addResizeListener(callback) {
  247. this._resizeListeners.add(callback);
  248. }
  249. /**
  250. * Obtains media stream ID of the underlying {@link JitsiTrack}.
  251. * @return {string|null}
  252. */
  253. getStreamID() {
  254. return this.stream ? this.stream.getId() : null;
  255. }
  256. /**
  257. * Get size of video element.
  258. * @returns {{width, height}}
  259. */
  260. getStreamSize() {
  261. const video = this.$video[0];
  262. return {
  263. width: video.videoWidth,
  264. height: video.videoHeight
  265. };
  266. }
  267. /**
  268. * Calculate optimal video size for specified container size.
  269. * @param {number} containerWidth container width
  270. * @param {number} containerHeight container height
  271. * @param {number} verticalFilmstripWidth current width of the vertical filmstrip
  272. * @returns {{availableWidth, availableHeight}}
  273. */
  274. _getVideoSize(containerWidth, containerHeight, verticalFilmstripWidth) {
  275. const { width, height } = this.getStreamSize();
  276. if (this.stream && this.isScreenSharing()) {
  277. return computeDesktopVideoSize(width,
  278. height,
  279. containerWidth,
  280. containerHeight,
  281. verticalFilmstripWidth < FILMSTRIP_BREAKPOINT);
  282. }
  283. return computeCameraVideoSize(width,
  284. height,
  285. containerWidth,
  286. containerHeight,
  287. interfaceConfig.VIDEO_LAYOUT_FIT);
  288. }
  289. /* eslint-disable max-params */
  290. /**
  291. * Calculate optimal video position (offset for top left corner)
  292. * for specified video size and container size.
  293. * @param {number} width video width
  294. * @param {number} height video height
  295. * @param {number} containerWidth container width
  296. * @param {number} containerHeight container height
  297. * @param {number} verticalFilmstripWidth current width of the vertical filmstrip
  298. * @returns {{horizontalIndent, verticalIndent}}
  299. */
  300. getVideoPosition(width, height, containerWidth, containerHeight, verticalFilmstripWidth) {
  301. let containerWidthToUse = containerWidth;
  302. /* eslint-enable max-params */
  303. if (this.stream && this.isScreenSharing()) {
  304. if (interfaceConfig.VERTICAL_FILMSTRIP && verticalFilmstripWidth < FILMSTRIP_BREAKPOINT) {
  305. containerWidthToUse -= Filmstrip.getVerticalFilmstripWidth();
  306. }
  307. return getCameraVideoPosition(width,
  308. height,
  309. containerWidthToUse,
  310. containerHeight);
  311. }
  312. return getCameraVideoPosition(width,
  313. height,
  314. containerWidthToUse,
  315. containerHeight);
  316. }
  317. /**
  318. * Updates the positioning of the remote connection presence message and the
  319. * connection status message which escribes that the remote user is having
  320. * connectivity issues.
  321. *
  322. * @returns {void}
  323. */
  324. positionRemoteStatusMessages() {
  325. this._positionParticipantStatus(this.$remoteConnectionMessage);
  326. this._positionParticipantStatus(this.$remotePresenceMessage);
  327. }
  328. /**
  329. * Modifies the position of the passed in jQuery object so it displays
  330. * in the middle of the video container or below the avatar.
  331. *
  332. * @private
  333. * @returns {void}
  334. */
  335. _positionParticipantStatus($element) {
  336. if (this.avatarDisplayed) {
  337. const $avatarImage = $('#dominantSpeakerAvatarContainer');
  338. $element.css(
  339. 'top',
  340. $avatarImage.offset().top + $avatarImage.height() + 10);
  341. } else {
  342. const height = $element.height();
  343. const parentHeight = $element.parent().height();
  344. $element.css('top', (parentHeight / 2) - (height / 2));
  345. }
  346. }
  347. /**
  348. *
  349. */
  350. resize(containerWidth, containerHeight, animate = false) {
  351. // XXX Prevent TypeError: undefined is not an object when the Web
  352. // browser does not support WebRTC (yet).
  353. if (this.$video.length === 0) {
  354. return;
  355. }
  356. const state = APP.store.getState();
  357. const currentLayout = getCurrentLayout(state);
  358. const verticalFilmstripWidth = state['features/filmstrip'].width?.current;
  359. if (currentLayout === LAYOUTS.TILE_VIEW || currentLayout === LAYOUTS.STAGE_FILMSTRIP_VIEW) {
  360. // We don't need to resize the large video since it won't be displayed and we'll resize when returning back
  361. // to stage view.
  362. return;
  363. }
  364. this.positionRemoteStatusMessages();
  365. const [ width, height ] = this._getVideoSize(containerWidth, containerHeight, verticalFilmstripWidth);
  366. if (width === 0 || height === 0) {
  367. // We don't need to set 0 for width or height since the visibility is controlled by the visibility css prop
  368. // on the largeVideoElementsContainer. Also if the width/height of the video element is 0 the attached
  369. // stream won't be played. Normally if we attach a new stream we won't resize the video element until the
  370. // stream has been played. But setting width/height to 0 will prevent the video from playing.
  371. return;
  372. }
  373. if ((containerWidth > width) || (containerHeight > height)) {
  374. this._backgroundOrientation = containerWidth > width ? ORIENTATION.LANDSCAPE : ORIENTATION.PORTRAIT;
  375. this._hideBackground = false;
  376. } else {
  377. this._hideBackground = true;
  378. }
  379. this._updateBackground();
  380. const { horizontalIndent, verticalIndent }
  381. = this.getVideoPosition(width, height, containerWidth, containerHeight, verticalFilmstripWidth);
  382. this.$wrapper.animate({
  383. width,
  384. height,
  385. top: verticalIndent,
  386. bottom: verticalIndent,
  387. left: horizontalIndent,
  388. right: horizontalIndent
  389. }, {
  390. queue: false,
  391. duration: animate ? 500 : 0
  392. });
  393. }
  394. /**
  395. * Removes a function from the known subscribers of video element resize
  396. * events.
  397. *
  398. * @param {Function} callback - The callback to remove from known
  399. * subscribers of video resize events.
  400. * @returns {void}
  401. */
  402. removeResizeListener(callback) {
  403. this._resizeListeners.delete(callback);
  404. }
  405. /**
  406. * Update video stream.
  407. * @param {string} userID
  408. * @param {JitsiTrack?} stream new stream
  409. * @param {string} videoType video type
  410. */
  411. setStream(userID, stream, videoType) {
  412. this.userId = userID;
  413. if (this.stream === stream) {
  414. // Handles the use case for the remote participants when the
  415. // videoType is received with delay after turning on/off the
  416. // desktop sharing.
  417. if (this.videoType !== videoType) {
  418. this.videoType = videoType;
  419. this.resizeContainer();
  420. }
  421. return;
  422. }
  423. // detach old stream
  424. if (this.stream) {
  425. this.stream.detach(this.$video[0]);
  426. }
  427. this.stream = stream;
  428. this.videoType = videoType;
  429. if (!stream) {
  430. return;
  431. }
  432. stream.attach(this.$video[0]);
  433. // Ensure large video gets play() called on it when a new stream is attached to it. This is necessary in the
  434. // case of Safari as autoplay doesn't kick-in automatically on Safari 15 and newer versions.
  435. browser.isWebKitBased() && this.$video[0].play();
  436. const flipX = stream.isLocal() && this.localFlipX && !this.isScreenSharing();
  437. this.$video.css({
  438. transform: flipX ? 'scaleX(-1)' : 'none'
  439. });
  440. this._updateBackground();
  441. }
  442. /**
  443. * Changes the flipX state of the local video.
  444. * @param val {boolean} true if flipped.
  445. */
  446. setLocalFlipX(val) {
  447. this.localFlipX = val;
  448. if (!this.$video || !this.stream || !this.stream.isLocal()) {
  449. return;
  450. }
  451. this.$video.css({
  452. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  453. });
  454. this._updateBackground();
  455. }
  456. /**
  457. * Check if current video stream is screen sharing.
  458. * @returns {boolean}
  459. */
  460. isScreenSharing() {
  461. return this.videoType === 'desktop';
  462. }
  463. /**
  464. * Show or hide user avatar.
  465. * @param {boolean} show
  466. */
  467. showAvatar(show) {
  468. this.$avatar.css('visibility', show ? 'visible' : 'hidden');
  469. this.avatarDisplayed = show;
  470. APP.API.notifyLargeVideoVisibilityChanged(show);
  471. }
  472. /**
  473. * We are doing fadeOut/fadeIn animations on parent div which wraps
  474. * largeVideo, because when Temasys plugin is in use it replaces
  475. * <video> elements with plugin <object> tag. In Safari jQuery is
  476. * unable to store values on this plugin object which breaks all
  477. * animation effects performed on it directly.
  478. *
  479. * TODO: refactor this since Temasys is no longer supported.
  480. */
  481. show() {
  482. return new Promise(resolve => {
  483. this.$wrapperParent.css('visibility', 'visible').fadeTo(
  484. FADE_DURATION_MS,
  485. 1,
  486. () => {
  487. this._isHidden = false;
  488. this._updateBackground();
  489. resolve();
  490. }
  491. );
  492. });
  493. }
  494. /**
  495. *
  496. */
  497. hide() {
  498. // as the container is hidden/replaced by another container
  499. // hide its avatar
  500. this.showAvatar(false);
  501. return new Promise(resolve => {
  502. this.$wrapperParent.fadeTo(FADE_DURATION_MS, 0, () => {
  503. this.$wrapperParent.css('visibility', 'hidden');
  504. this._isHidden = true;
  505. this._updateBackground();
  506. resolve();
  507. });
  508. });
  509. }
  510. /**
  511. * @return {boolean} switch on dominant speaker event if on stage.
  512. */
  513. stayOnStage() {
  514. return false;
  515. }
  516. /**
  517. * Callback invoked when the video element changes dimensions.
  518. *
  519. * @private
  520. * @returns {void}
  521. */
  522. _onResize() {
  523. this._resizeListeners.forEach(callback => callback());
  524. }
  525. /**
  526. * Attaches and/or updates a React Component to be used as a background for
  527. * the large video, to display blurred video and fill up empty space not
  528. * taken up by the large video.
  529. *
  530. * @private
  531. * @returns {void}
  532. */
  533. _updateBackground() {
  534. // Do not the background display on browsers that might experience
  535. // performance issues from the presence of the background or if
  536. // explicitly disabled.
  537. if (interfaceConfig.DISABLE_VIDEO_BACKGROUND
  538. || browser.isFirefox()
  539. || browser.isWebKitBased()) {
  540. return;
  541. }
  542. ReactDOM.render(
  543. <LargeVideoBackground
  544. hidden = { this._hideBackground || this._isHidden }
  545. mirror = {
  546. this.stream
  547. && this.stream.isLocal()
  548. && this.localFlipX
  549. }
  550. orientationFit = { this._backgroundOrientation }
  551. videoElement = { this.$video && this.$video[0] }
  552. videoTrack = { this.stream } />,
  553. document.getElementById('largeVideoBackgroundContainer')
  554. );
  555. }
  556. }