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

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