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 19KB

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