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

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