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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 UIEvents from '../../../service/UI/UIEvents';
  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. * 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. this._isHidden = false;
  182. /**
  183. * Flag indicates whether or not the avatar is currently displayed.
  184. * @type {boolean}
  185. */
  186. this.avatarDisplayed = false;
  187. this.$avatar = $('#dominantSpeaker');
  188. /**
  189. * A jQuery selector of the remote connection message.
  190. * @type {jQuery|HTMLElement}
  191. */
  192. this.$remoteConnectionMessage = $('#remoteConnectionMessage');
  193. this.$remotePresenceMessage = $('#remotePresenceMessage');
  194. /**
  195. * Indicates whether or not the video stream attached to the video
  196. * element has started(which means that there is any image rendered
  197. * even if the video is stalled).
  198. * @type {boolean}
  199. */
  200. this.wasVideoRendered = false;
  201. this.$wrapper = $('#largeVideoWrapper');
  202. /**
  203. * FIXME: currently using parent() because I can't come up with name
  204. * for id. We'll need to probably refactor the HTML related to the large
  205. * video anyway.
  206. */
  207. this.$wrapperParent = this.$wrapper.parent();
  208. this.avatarHeight = $('#dominantSpeakerAvatarContainer').height();
  209. const onPlayingCallback = function(event) {
  210. if (typeof resizeContainer === 'function') {
  211. resizeContainer(event);
  212. }
  213. this.wasVideoRendered = true;
  214. }.bind(this);
  215. this.$video[0].onplaying = onPlayingCallback;
  216. /**
  217. * A Set of functions to invoke when the video element resizes.
  218. *
  219. * @private
  220. */
  221. this._resizeListeners = new Set();
  222. this.$video[0].onresize = this._onResize.bind(this);
  223. }
  224. /**
  225. * Adds a function to the known subscribers of video element resize
  226. * events.
  227. *
  228. * @param {Function} callback - The subscriber to notify when the video
  229. * element resizes.
  230. * @returns {void}
  231. */
  232. addResizeListener(callback) {
  233. this._resizeListeners.add(callback);
  234. }
  235. /**
  236. * Obtains media stream ID of the underlying {@link JitsiTrack}.
  237. * @return {string|null}
  238. */
  239. getStreamID() {
  240. return this.stream ? this.stream.getId() : null;
  241. }
  242. /**
  243. * Get size of video element.
  244. * @returns {{width, height}}
  245. */
  246. getStreamSize() {
  247. const video = this.$video[0];
  248. return {
  249. width: video.videoWidth,
  250. height: video.videoHeight
  251. };
  252. }
  253. /**
  254. * Calculate optimal video size for specified container size.
  255. * @param {number} containerWidth container width
  256. * @param {number} containerHeight container height
  257. * @returns {{availableWidth, availableHeight}}
  258. */
  259. _getVideoSize(containerWidth, containerHeight) {
  260. const { width, height } = this.getStreamSize();
  261. if (this.stream && this.isScreenSharing()) {
  262. return computeDesktopVideoSize(width,
  263. height,
  264. containerWidth,
  265. containerHeight);
  266. }
  267. return computeCameraVideoSize(width,
  268. height,
  269. containerWidth,
  270. containerHeight,
  271. interfaceConfig.VIDEO_LAYOUT_FIT);
  272. }
  273. /* eslint-disable max-params */
  274. /**
  275. * Calculate optimal video position (offset for top left corner)
  276. * for specified video size and container size.
  277. * @param {number} width video width
  278. * @param {number} height video height
  279. * @param {number} containerWidth container width
  280. * @param {number} containerHeight container height
  281. * @returns {{horizontalIndent, verticalIndent}}
  282. */
  283. getVideoPosition(width, height, containerWidth, containerHeight) {
  284. let containerWidthToUse = containerWidth;
  285. /* eslint-enable max-params */
  286. if (this.stream && this.isScreenSharing()) {
  287. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  288. containerWidthToUse -= Filmstrip.getVerticalFilmstripWidth();
  289. }
  290. return getCameraVideoPosition(width,
  291. height,
  292. containerWidthToUse,
  293. containerHeight);
  294. }
  295. return getCameraVideoPosition(width,
  296. height,
  297. containerWidthToUse,
  298. containerHeight);
  299. }
  300. /**
  301. * Updates the positioning of the remote connection presence message and the
  302. * connection status message which escribes that the remote user is having
  303. * connectivity issues.
  304. *
  305. * @returns {void}
  306. */
  307. positionRemoteStatusMessages() {
  308. this._positionParticipantStatus(this.$remoteConnectionMessage);
  309. this._positionParticipantStatus(this.$remotePresenceMessage);
  310. }
  311. /**
  312. * Modifies the position of the passed in jQuery object so it displays
  313. * in the middle of the video container or below the avatar.
  314. *
  315. * @private
  316. * @returns {void}
  317. */
  318. _positionParticipantStatus($element) {
  319. if (this.avatarDisplayed) {
  320. const $avatarImage = $('#dominantSpeakerAvatarContainer');
  321. $element.css(
  322. 'top',
  323. $avatarImage.offset().top + $avatarImage.height() + 10);
  324. } else {
  325. const height = $element.height();
  326. const parentHeight = $element.parent().height();
  327. $element.css('top', (parentHeight / 2) - (height / 2));
  328. }
  329. }
  330. /**
  331. *
  332. */
  333. resize(containerWidth, containerHeight, animate = false) {
  334. // XXX Prevent TypeError: undefined is not an object when the Web
  335. // browser does not support WebRTC (yet).
  336. if (this.$video.length === 0) {
  337. return;
  338. }
  339. const currentLayout = getCurrentLayout(APP.store.getState());
  340. if (currentLayout === LAYOUTS.TILE_VIEW) {
  341. // We don't need to resize the large video since it won't be displayed and we'll resize when returning back
  342. // to stage view.
  343. return;
  344. }
  345. this.positionRemoteStatusMessages();
  346. const [ width, height ] = this._getVideoSize(containerWidth, containerHeight);
  347. if (width === 0 || height === 0) {
  348. // We don't need to set 0 for width or height since the visibility is controled by the visibility css prop
  349. // on the largeVideoElementsContainer. Also if the width/height of the video element is 0 the attached
  350. // stream won't be played. Normally if we attach a new stream we won't resize the video element until the
  351. // stream has been played. But setting width/height to 0 will prevent the video from playing.
  352. return;
  353. }
  354. if ((containerWidth > width) || (containerHeight > height)) {
  355. this._backgroundOrientation = containerWidth > width ? ORIENTATION.LANDSCAPE : ORIENTATION.PORTRAIT;
  356. this._hideBackground = false;
  357. } else {
  358. this._hideBackground = true;
  359. }
  360. this._updateBackground();
  361. const { horizontalIndent, verticalIndent }
  362. = this.getVideoPosition(width, height, containerWidth, containerHeight);
  363. this.$wrapper.animate({
  364. width,
  365. height,
  366. top: verticalIndent,
  367. bottom: verticalIndent,
  368. left: horizontalIndent,
  369. right: horizontalIndent
  370. }, {
  371. queue: false,
  372. duration: animate ? 500 : 0
  373. });
  374. }
  375. /**
  376. * Removes a function from the known subscribers of video element resize
  377. * events.
  378. *
  379. * @param {Function} callback - The callback to remove from known
  380. * subscribers of video resize events.
  381. * @returns {void}
  382. */
  383. removeResizeListener(callback) {
  384. this._resizeListeners.delete(callback);
  385. }
  386. /**
  387. * Update video stream.
  388. * @param {string} userID
  389. * @param {JitsiTrack?} stream new stream
  390. * @param {string} videoType video type
  391. */
  392. setStream(userID, stream, videoType) {
  393. this.userId = userID;
  394. if (this.stream === stream) {
  395. // Handles the use case for the remote participants when the
  396. // videoType is received with delay after turning on/off the
  397. // desktop sharing.
  398. if (this.videoType !== videoType) {
  399. this.videoType = videoType;
  400. this.resizeContainer();
  401. }
  402. return;
  403. }
  404. // The stream has changed, so the image will be lost on detach
  405. this.wasVideoRendered = false;
  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;
  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. this.emitter.emit(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE, show);
  451. APP.API.notifyLargeVideoVisibilityChanged(show);
  452. }
  453. /**
  454. * We are doing fadeOut/fadeIn animations on parent div which wraps
  455. * largeVideo, because when Temasys plugin is in use it replaces
  456. * <video> elements with plugin <object> tag. In Safari jQuery is
  457. * unable to store values on this plugin object which breaks all
  458. * animation effects performed on it directly.
  459. *
  460. * TODO: refactor this since Temasys is no longer supported.
  461. */
  462. show() {
  463. return new Promise(resolve => {
  464. this.$wrapperParent.css('visibility', 'visible').fadeTo(
  465. FADE_DURATION_MS,
  466. 1,
  467. () => {
  468. this._isHidden = false;
  469. this._updateBackground();
  470. resolve();
  471. }
  472. );
  473. });
  474. }
  475. /**
  476. *
  477. */
  478. hide() {
  479. // as the container is hidden/replaced by another container
  480. // hide its avatar
  481. this.showAvatar(false);
  482. return new Promise(resolve => {
  483. this.$wrapperParent.fadeTo(FADE_DURATION_MS, 0, () => {
  484. this.$wrapperParent.css('visibility', 'hidden');
  485. this._isHidden = true;
  486. this._updateBackground();
  487. resolve();
  488. });
  489. });
  490. }
  491. /**
  492. * @return {boolean} switch on dominant speaker event if on stage.
  493. */
  494. stayOnStage() {
  495. return false;
  496. }
  497. /**
  498. * Callback invoked when the video element changes dimensions.
  499. *
  500. * @private
  501. * @returns {void}
  502. */
  503. _onResize() {
  504. this._resizeListeners.forEach(callback => callback());
  505. }
  506. /**
  507. * Attaches and/or updates a React Component to be used as a background for
  508. * the large video, to display blurred video and fill up empty space not
  509. * taken up by the large video.
  510. *
  511. * @private
  512. * @returns {void}
  513. */
  514. _updateBackground() {
  515. // Do not the background display on browsers that might experience
  516. // performance issues from the presence of the background or if
  517. // explicitly disabled.
  518. if (interfaceConfig.DISABLE_VIDEO_BACKGROUND
  519. || browser.isFirefox()
  520. || browser.isSafari()) {
  521. return;
  522. }
  523. ReactDOM.render(
  524. <LargeVideoBackground
  525. hidden = { this._hideBackground || this._isHidden }
  526. mirror = {
  527. this.stream
  528. && this.stream.isLocal()
  529. && this.localFlipX
  530. }
  531. orientationFit = { this._backgroundOrientation }
  532. videoElement = { this.$video && this.$video[0] }
  533. videoTrack = { this.stream } />,
  534. document.getElementById('largeVideoBackgroundContainer')
  535. );
  536. }
  537. }