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

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