Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

VideoContainer.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. console.log("addResizeListener wdev",callback)
  248. this._resizeListeners.add(callback);
  249. }
  250. /**
  251. * Obtains media stream ID of the underlying {@link JitsiTrack}.
  252. * @return {string|null}
  253. */
  254. getStreamID() {
  255. return this.stream ? this.stream.getId() : null;
  256. }
  257. /**
  258. * Get size of video element.
  259. * @returns {{width, height}}
  260. */
  261. getStreamSize() {
  262. const video = this.$video[0];
  263. return {
  264. width: video.videoWidth,
  265. height: video.videoHeight
  266. };
  267. }
  268. /**
  269. * Calculate optimal video size for specified container size.
  270. * @param {number} containerWidth container width
  271. * @param {number} containerHeight container height
  272. * @param {number} verticalFilmstripWidth current width of the vertical filmstrip
  273. * @returns {{availableWidth, availableHeight}}
  274. */
  275. _getVideoSize(containerWidth, containerHeight, verticalFilmstripWidth) {
  276. const { width, height } = this.getStreamSize();
  277. if (this.stream && this.isScreenSharing()) {
  278. return computeDesktopVideoSize(width,
  279. height,
  280. containerWidth,
  281. containerHeight,
  282. verticalFilmstripWidth < FILMSTRIP_BREAKPOINT);
  283. }
  284. return computeCameraVideoSize(width,
  285. height,
  286. containerWidth,
  287. containerHeight,
  288. interfaceConfig.VIDEO_LAYOUT_FIT);
  289. }
  290. /* eslint-disable max-params */
  291. /**
  292. * Calculate optimal video position (offset for top left corner)
  293. * for specified video size and container size.
  294. * @param {number} width video width
  295. * @param {number} height video height
  296. * @param {number} containerWidth container width
  297. * @param {number} containerHeight container height
  298. * @param {number} verticalFilmstripWidth current width of the vertical filmstrip
  299. * @returns {{horizontalIndent, verticalIndent}}
  300. */
  301. getVideoPosition(width, height, containerWidth, containerHeight, verticalFilmstripWidth) {
  302. let containerWidthToUse = containerWidth;
  303. /* eslint-enable max-params */
  304. if (this.stream && this.isScreenSharing()) {
  305. if (interfaceConfig.VERTICAL_FILMSTRIP && verticalFilmstripWidth < FILMSTRIP_BREAKPOINT) {
  306. containerWidthToUse -= Filmstrip.getVerticalFilmstripWidth();
  307. }
  308. return getCameraVideoPosition(width,
  309. height,
  310. containerWidthToUse,
  311. containerHeight);
  312. }
  313. return getCameraVideoPosition(width,
  314. height,
  315. containerWidthToUse,
  316. containerHeight);
  317. }
  318. /**
  319. * Updates the positioning of the remote connection presence message and the
  320. * connection status message which escribes that the remote user is having
  321. * connectivity issues.
  322. *
  323. * @returns {void}
  324. */
  325. positionRemoteStatusMessages() {
  326. this._positionParticipantStatus(this.$remoteConnectionMessage);
  327. this._positionParticipantStatus(this.$remotePresenceMessage);
  328. }
  329. /**
  330. * Modifies the position of the passed in jQuery object so it displays
  331. * in the middle of the video container or below the avatar.
  332. *
  333. * @private
  334. * @returns {void}
  335. */
  336. _positionParticipantStatus($element) {
  337. if (this.avatarDisplayed) {
  338. const $avatarImage = $('#dominantSpeakerAvatarContainer');
  339. $element.css(
  340. 'top',
  341. $avatarImage.offset().top + $avatarImage.height() + 10);
  342. } else {
  343. const height = $element.height();
  344. const parentHeight = $element.parent().height();
  345. $element.css('top', (parentHeight / 2) - (height / 2));
  346. }
  347. }
  348. /**
  349. *
  350. */
  351. resize(containerWidth, containerHeight, animate = false) {
  352. // XXX Prevent TypeError: undefined is not an object when the Web
  353. // browser does not support WebRTC (yet).
  354. if (this.$video.length === 0) {
  355. return;
  356. }
  357. const state = APP.store.getState();
  358. const currentLayout = getCurrentLayout(state);
  359. const verticalFilmstripWidth = state['features/filmstrip'].width?.current;
  360. if (currentLayout === LAYOUTS.TILE_VIEW || currentLayout === LAYOUTS.STAGE_FILMSTRIP_VIEW) {
  361. // We don't need to resize the large video since it won't be displayed and we'll resize when returning back
  362. // to stage view.
  363. return;
  364. }
  365. this.positionRemoteStatusMessages();
  366. const [ width, height ] = this._getVideoSize(containerWidth, containerHeight, verticalFilmstripWidth);
  367. if (width === 0 || height === 0) {
  368. // We don't need to set 0 for width or height since the visibility is controlled by the visibility css prop
  369. // on the largeVideoElementsContainer. Also if the width/height of the video element is 0 the attached
  370. // stream won't be played. Normally if we attach a new stream we won't resize the video element until the
  371. // stream has been played. But setting width/height to 0 will prevent the video from playing.
  372. return;
  373. }
  374. if ((containerWidth > width) || (containerHeight > height)) {
  375. this._backgroundOrientation = containerWidth > width ? ORIENTATION.LANDSCAPE : ORIENTATION.PORTRAIT;
  376. this._hideBackground = false;
  377. } else {
  378. this._hideBackground = true;
  379. }
  380. this._updateBackground();
  381. const { horizontalIndent, verticalIndent }
  382. = this.getVideoPosition(width, height, containerWidth, containerHeight, verticalFilmstripWidth);
  383. this.$wrapper.animate({
  384. width,
  385. height,
  386. top: verticalIndent,
  387. bottom: verticalIndent,
  388. left: horizontalIndent,
  389. right: horizontalIndent
  390. }, {
  391. queue: false,
  392. duration: animate ? 500 : 0
  393. });
  394. }
  395. /**
  396. * Removes a function from the known subscribers of video element resize
  397. * events.
  398. *
  399. * @param {Function} callback - The callback to remove from known
  400. * subscribers of video resize events.
  401. * @returns {void}
  402. */
  403. removeResizeListener(callback) {
  404. this._resizeListeners.delete(callback);
  405. }
  406. /**
  407. * Update video stream.
  408. * @param {string} userID
  409. * @param {JitsiTrack?} stream new stream
  410. * @param {string} videoType video type
  411. */
  412. setStream(userID, stream, videoType) {
  413. this.userId = userID;
  414. if (this.stream === stream) {
  415. // Handles the use case for the remote participants when the
  416. // videoType is received with delay after turning on/off the
  417. // desktop sharing.
  418. if (this.videoType !== videoType) {
  419. this.videoType = videoType;
  420. this.resizeContainer();
  421. }
  422. return;
  423. }
  424. // detach old stream
  425. if (this.stream) {
  426. this.stream.detach(this.$video[0]);
  427. }
  428. this.stream = stream;
  429. this.videoType = videoType;
  430. if (!stream) {
  431. return;
  432. }
  433. stream.attach(this.$video[0]);
  434. // Ensure large video gets play() called on it when a new stream is attached to it. This is necessary in the
  435. // case of Safari as autoplay doesn't kick-in automatically on Safari 15 and newer versions.
  436. browser.isWebKitBased() && this.$video[0].play();
  437. const flipX = stream.isLocal() && this.localFlipX && !this.isScreenSharing();
  438. this.$video.css({
  439. transform: flipX ? 'scaleX(-1)' : 'none'
  440. });
  441. this._updateBackground();
  442. }
  443. /**
  444. * Changes the flipX state of the local video.
  445. * @param val {boolean} true if flipped.
  446. */
  447. setLocalFlipX(val) {
  448. this.localFlipX = val;
  449. if (!this.$video || !this.stream || !this.stream.isLocal()) {
  450. return;
  451. }
  452. this.$video.css({
  453. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  454. });
  455. this._updateBackground();
  456. }
  457. /**
  458. * Check if current video stream is screen sharing.
  459. * @returns {boolean}
  460. */
  461. isScreenSharing() {
  462. return this.videoType === 'desktop';
  463. }
  464. /**
  465. * Show or hide user avatar.
  466. * @param {boolean} show
  467. */
  468. showAvatar(show) {
  469. this.$avatar.css('visibility', show ? 'visible' : 'hidden');
  470. this.avatarDisplayed = show;
  471. APP.API.notifyLargeVideoVisibilityChanged(show);
  472. }
  473. /**
  474. * We are doing fadeOut/fadeIn animations on parent div which wraps
  475. * largeVideo, because when Temasys plugin is in use it replaces
  476. * <video> elements with plugin <object> tag. In Safari jQuery is
  477. * unable to store values on this plugin object which breaks all
  478. * animation effects performed on it directly.
  479. *
  480. * TODO: refactor this since Temasys is no longer supported.
  481. */
  482. show() {
  483. return new Promise(resolve => {
  484. this.$wrapperParent.css('visibility', 'visible').fadeTo(
  485. FADE_DURATION_MS,
  486. 1,
  487. () => {
  488. this._isHidden = false;
  489. this._updateBackground();
  490. resolve();
  491. }
  492. );
  493. });
  494. }
  495. /**
  496. *
  497. */
  498. hide() {
  499. // as the container is hidden/replaced by another container
  500. // hide its avatar
  501. this.showAvatar(false);
  502. return new Promise(resolve => {
  503. this.$wrapperParent.fadeTo(FADE_DURATION_MS, 0, () => {
  504. this.$wrapperParent.css('visibility', 'hidden');
  505. this._isHidden = true;
  506. this._updateBackground();
  507. resolve();
  508. });
  509. });
  510. }
  511. /**
  512. * @return {boolean} switch on dominant speaker event if on stage.
  513. */
  514. stayOnStage() {
  515. return false;
  516. }
  517. /**
  518. * Callback invoked when the video element changes dimensions.
  519. *
  520. * @private
  521. * @returns {void}
  522. */
  523. _onResize() {
  524. this._resizeListeners.forEach(callback => callback());
  525. }
  526. /**
  527. * Attaches and/or updates a React Component to be used as a background for
  528. * the large video, to display blurred video and fill up empty space not
  529. * taken up by the large video.
  530. *
  531. * @private
  532. * @returns {void}
  533. */
  534. _updateBackground() {
  535. // Do not the background display on browsers that might experience
  536. // performance issues from the presence of the background or if
  537. // explicitly disabled.
  538. if (interfaceConfig.DISABLE_VIDEO_BACKGROUND
  539. || browser.isFirefox()
  540. || browser.isWebKitBased()) {
  541. return;
  542. }
  543. ReactDOM.render(
  544. <LargeVideoBackground
  545. hidden = { this._hideBackground || this._isHidden }
  546. mirror = {
  547. this.stream
  548. && this.stream.isLocal()
  549. && this.localFlipX
  550. }
  551. orientationFit = { this._backgroundOrientation }
  552. videoElement = { this.$video && this.$video[0] }
  553. videoTrack = { this.stream } />,
  554. document.getElementById('largeVideoBackgroundContainer')
  555. );
  556. }
  557. }