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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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 remote video.
  19. *
  20. * @private
  21. * @type {string}
  22. */
  23. const REMOTE_PROBLEM_FILTER_CLASS = 'remoteVideoProblemFilter';
  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 devision 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.getFilmstripWidth();
  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 devision 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. * @param emitter {EventEmitter} the event emitter that will be used by
  164. * this instance.
  165. */
  166. constructor(resizeContainer, emitter) {
  167. super();
  168. this.stream = null;
  169. this.userId = null;
  170. this.videoType = null;
  171. this.localFlipX = true;
  172. this.emitter = emitter;
  173. this.resizeContainer = resizeContainer;
  174. /**
  175. * Whether the background should fit the height of the container
  176. * (portrait) or fit the width of the container (landscape).
  177. *
  178. * @private
  179. * @type {string|null}
  180. */
  181. this._backgroundOrientation = null;
  182. /**
  183. * Flag indicates whether or not the background should be rendered.
  184. * If the background will not be visible then it is hidden to save
  185. * on performance.
  186. * @type {boolean}
  187. */
  188. this._hideBackground = true;
  189. /**
  190. * Flag indicates whether or not the avatar is currently displayed.
  191. * @type {boolean}
  192. */
  193. this.avatarDisplayed = false;
  194. this.$avatar = $('#dominantSpeaker');
  195. /**
  196. * A jQuery selector of the remote connection message.
  197. * @type {jQuery|HTMLElement}
  198. */
  199. this.$remoteConnectionMessage = $('#remoteConnectionMessage');
  200. this.$remotePresenceMessage = $('#remotePresenceMessage');
  201. /**
  202. * Indicates whether or not the video stream attached to the video
  203. * element has started(which means that there is any image rendered
  204. * even if the video is stalled).
  205. * @type {boolean}
  206. */
  207. this.wasVideoRendered = false;
  208. this.$wrapper = $('#largeVideoWrapper');
  209. /**
  210. * FIXME: currently using parent() because I can't come up with name
  211. * for id. We'll need to probably refactor the HTML related to the large
  212. * video anyway.
  213. */
  214. this.$wrapperParent = this.$wrapper.parent();
  215. this.avatarHeight = $('#dominantSpeakerAvatarContainer').height();
  216. const onPlayingCallback = function(event) {
  217. if (typeof resizeContainer === 'function') {
  218. resizeContainer(event);
  219. }
  220. this.wasVideoRendered = true;
  221. }.bind(this);
  222. this.$video[0].onplaying = onPlayingCallback;
  223. /**
  224. * A Set of functions to invoke when the video element resizes.
  225. *
  226. * @private
  227. */
  228. this._resizeListeners = new Set();
  229. this.$video[0].onresize = this._onResize.bind(this);
  230. }
  231. /**
  232. * Adds a function to the known subscribers of video element resize
  233. * events.
  234. *
  235. * @param {Function} callback - The subscriber to notify when the video
  236. * element resizes.
  237. * @returns {void}
  238. */
  239. addResizeListener(callback) {
  240. this._resizeListeners.add(callback);
  241. }
  242. /**
  243. * Obtains media stream ID of the underlying {@link JitsiTrack}.
  244. * @return {string|null}
  245. */
  246. getStreamID() {
  247. return this.stream ? this.stream.getId() : null;
  248. }
  249. /**
  250. * Get size of video element.
  251. * @returns {{width, height}}
  252. */
  253. getStreamSize() {
  254. const video = this.$video[0];
  255. return {
  256. width: video.videoWidth,
  257. height: video.videoHeight
  258. };
  259. }
  260. /**
  261. * Calculate optimal video size for specified container size.
  262. * @param {number} containerWidth container width
  263. * @param {number} containerHeight container height
  264. * @returns {{availableWidth, availableHeight}}
  265. */
  266. _getVideoSize(containerWidth, containerHeight) {
  267. const { width, height } = this.getStreamSize();
  268. if (this.stream && this.isScreenSharing()) {
  269. return computeDesktopVideoSize(width,
  270. height,
  271. containerWidth,
  272. containerHeight);
  273. }
  274. return computeCameraVideoSize(width,
  275. height,
  276. containerWidth,
  277. containerHeight,
  278. interfaceConfig.VIDEO_LAYOUT_FIT);
  279. }
  280. /* eslint-disable max-params */
  281. /**
  282. * Calculate optimal video position (offset for top left corner)
  283. * for specified video size and container size.
  284. * @param {number} width video width
  285. * @param {number} height video height
  286. * @param {number} containerWidth container width
  287. * @param {number} containerHeight container height
  288. * @returns {{horizontalIndent, verticalIndent}}
  289. */
  290. getVideoPosition(width, height, containerWidth, containerHeight) {
  291. let containerWidthToUse = containerWidth;
  292. /* eslint-enable max-params */
  293. if (this.stream && this.isScreenSharing()) {
  294. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  295. containerWidthToUse -= Filmstrip.getFilmstripWidth();
  296. }
  297. return getCameraVideoPosition(width,
  298. height,
  299. containerWidthToUse,
  300. containerHeight);
  301. }
  302. return getCameraVideoPosition(width,
  303. height,
  304. containerWidthToUse,
  305. containerHeight);
  306. }
  307. /**
  308. * Updates the positioning of the remote connection presence message and the
  309. * connection status message which escribes that the remote user is having
  310. * connectivity issues.
  311. *
  312. * @returns {void}
  313. */
  314. positionRemoteStatusMessages() {
  315. this._positionParticipantStatus(this.$remoteConnectionMessage);
  316. this._positionParticipantStatus(this.$remotePresenceMessage);
  317. }
  318. /**
  319. * Modifies the position of the passed in jQuery object so it displays
  320. * in the middle of the video container or below the avatar.
  321. *
  322. * @private
  323. * @returns {void}
  324. */
  325. _positionParticipantStatus($element) {
  326. if (this.avatarDisplayed) {
  327. const $avatarImage = $('#dominantSpeakerAvatarContainer');
  328. $element.css(
  329. 'top',
  330. $avatarImage.offset().top + $avatarImage.height() + 10);
  331. } else {
  332. const height = $element.height();
  333. const parentHeight = $element.parent().height();
  334. $element.css('top', (parentHeight / 2) - (height / 2));
  335. }
  336. }
  337. /**
  338. *
  339. */
  340. resize(containerWidth, containerHeight, animate = false) {
  341. // XXX Prevent TypeError: undefined is not an object when the Web
  342. // browser does not support WebRTC (yet).
  343. if (this.$video.length === 0) {
  344. return;
  345. }
  346. const currentLayout = getCurrentLayout(APP.store.getState());
  347. if (currentLayout === LAYOUTS.TILE_VIEW) {
  348. // We don't need to resize the large video since it won't be displayed and we'll resize when returning back
  349. // to stage view.
  350. return;
  351. }
  352. this.positionRemoteStatusMessages();
  353. const [ width, height ] = this._getVideoSize(containerWidth, containerHeight);
  354. if (width === 0 || height === 0) {
  355. // We don't need to set 0 for width or height since the visibility is controled by the visibility css prop
  356. // on the largeVideoElementsContainer. Also if the width/height of the video element is 0 the attached
  357. // stream won't be played. Normally if we attach a new stream we won't resize the video element until the
  358. // stream has been played. But setting width/height to 0 will prevent the video from playing.
  359. return;
  360. }
  361. if ((containerWidth > width) || (containerHeight > height)) {
  362. this._backgroundOrientation = containerWidth > width ? ORIENTATION.LANDSCAPE : ORIENTATION.PORTRAIT;
  363. this._hideBackground = false;
  364. } else {
  365. this._hideBackground = true;
  366. }
  367. this._updateBackground();
  368. const { horizontalIndent, verticalIndent }
  369. = this.getVideoPosition(width, height, containerWidth, containerHeight);
  370. this.$wrapper.animate({
  371. width,
  372. height,
  373. top: verticalIndent,
  374. bottom: verticalIndent,
  375. left: horizontalIndent,
  376. right: horizontalIndent
  377. }, {
  378. queue: false,
  379. duration: animate ? 500 : 0
  380. });
  381. }
  382. /**
  383. * Removes a function from the known subscribers of video element resize
  384. * events.
  385. *
  386. * @param {Function} callback - The callback to remove from known
  387. * subscribers of video resize events.
  388. * @returns {void}
  389. */
  390. removeResizeListener(callback) {
  391. this._resizeListeners.delete(callback);
  392. }
  393. /**
  394. * Update video stream.
  395. * @param {string} userID
  396. * @param {JitsiTrack?} stream new stream
  397. * @param {string} videoType video type
  398. */
  399. setStream(userID, stream, videoType) {
  400. this.userId = userID;
  401. if (this.stream === stream) {
  402. // Handles the use case for the remote participants when the
  403. // videoType is received with delay after turning on/off the
  404. // desktop sharing.
  405. if (this.videoType !== videoType) {
  406. this.videoType = videoType;
  407. this.resizeContainer();
  408. }
  409. return;
  410. }
  411. // The stream has changed, so the image will be lost on detach
  412. this.wasVideoRendered = false;
  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;
  424. this.$video.css({
  425. transform: flipX ? 'scaleX(-1)' : 'none'
  426. });
  427. this._updateBackground();
  428. // Reset the large video background depending on the stream.
  429. this.setLargeVideoBackground(this.avatarDisplayed);
  430. }
  431. /**
  432. * Changes the flipX state of the local video.
  433. * @param val {boolean} true if flipped.
  434. */
  435. setLocalFlipX(val) {
  436. this.localFlipX = val;
  437. if (!this.$video || !this.stream || !this.stream.isLocal()) {
  438. return;
  439. }
  440. this.$video.css({
  441. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  442. });
  443. this._updateBackground();
  444. }
  445. /**
  446. * Check if current video stream is screen sharing.
  447. * @returns {boolean}
  448. */
  449. isScreenSharing() {
  450. return this.videoType === 'desktop';
  451. }
  452. /**
  453. * Show or hide user avatar.
  454. * @param {boolean} show
  455. */
  456. showAvatar(show) {
  457. // TO FIX: Video background need to be black, so that we don't have a
  458. // flickering effect when scrolling between videos and have the screen
  459. // move to grey before going back to video. Avatars though can have the
  460. // default background set.
  461. // In order to fix this code we need to introduce video background or
  462. // find a workaround for the video flickering.
  463. this.setLargeVideoBackground(show);
  464. this.$avatar.css('visibility', show ? 'visible' : 'hidden');
  465. this.avatarDisplayed = show;
  466. this.emitter.emit(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE, show);
  467. APP.API.notifyLargeVideoVisibilityChanged(show);
  468. }
  469. /**
  470. * Indicates that the remote user who is currently displayed by this video
  471. * container is having connectivity issues.
  472. *
  473. * @param {boolean} show <tt>true</tt> to show or <tt>false</tt> to hide
  474. * the indication.
  475. */
  476. showRemoteConnectionProblemIndicator(show) {
  477. this.$video.toggleClass(REMOTE_PROBLEM_FILTER_CLASS, show);
  478. this.$avatar.toggleClass(REMOTE_PROBLEM_FILTER_CLASS, show);
  479. this._updateBackground();
  480. }
  481. /**
  482. * We are doing fadeOut/fadeIn animations on parent div which wraps
  483. * largeVideo, because when Temasys plugin is in use it replaces
  484. * <video> elements with plugin <object> tag. In Safari jQuery is
  485. * unable to store values on this plugin object which breaks all
  486. * animation effects performed on it directly.
  487. *
  488. * TODO: refactor this since Temasys is no longer supported.
  489. */
  490. show() {
  491. return new Promise(resolve => {
  492. this.$wrapperParent.css('visibility', 'visible').fadeTo(
  493. FADE_DURATION_MS,
  494. 1,
  495. () => {
  496. resolve();
  497. }
  498. );
  499. });
  500. }
  501. /**
  502. *
  503. */
  504. hide() {
  505. // as the container is hidden/replaced by another container
  506. // hide its avatar
  507. this.showAvatar(false);
  508. return new Promise(resolve => {
  509. this.$wrapperParent.fadeTo(FADE_DURATION_MS, 0, () => {
  510. this.$wrapperParent.css('visibility', 'hidden');
  511. resolve();
  512. });
  513. });
  514. }
  515. /**
  516. * @return {boolean} switch on dominant speaker event if on stage.
  517. */
  518. stayOnStage() {
  519. return false;
  520. }
  521. /**
  522. * Sets the large video container background depending on the container
  523. * type and the parameter indicating if an avatar is currently shown on
  524. * large.
  525. *
  526. * @param {boolean} isAvatar - Indicates if the avatar is currently shown
  527. * on the large video.
  528. * @returns {void}
  529. */
  530. setLargeVideoBackground(isAvatar) {
  531. $('#largeVideoContainer').css('background',
  532. this.videoType === VIDEO_CONTAINER_TYPE && !isAvatar
  533. ? '#000' : interfaceConfig.DEFAULT_BACKGROUND);
  534. }
  535. /**
  536. * Callback invoked when the video element changes dimensions.
  537. *
  538. * @private
  539. * @returns {void}
  540. */
  541. _onResize() {
  542. this._resizeListeners.forEach(callback => callback());
  543. }
  544. /**
  545. * Attaches and/or updates a React Component to be used as a background for
  546. * the large video, to display blurred video and fill up empty space not
  547. * taken up by the large video.
  548. *
  549. * @private
  550. * @returns {void}
  551. */
  552. _updateBackground() {
  553. // Do not the background display on browsers that might experience
  554. // performance issues from the presence of the background or if
  555. // explicitly disabled.
  556. if (interfaceConfig.DISABLE_VIDEO_BACKGROUND
  557. || browser.isFirefox()
  558. || browser.isSafariWithWebrtc()) {
  559. return;
  560. }
  561. ReactDOM.render(
  562. <LargeVideoBackground
  563. hidden = { this._hideBackground }
  564. mirror = {
  565. this.stream
  566. && this.stream.isLocal()
  567. && this.localFlipX
  568. }
  569. orientationFit = { this._backgroundOrientation }
  570. showRemoteProblemFilter
  571. = { this.$video.hasClass(REMOTE_PROBLEM_FILTER_CLASS) }
  572. videoElement = { this.$video && this.$video[0] }
  573. videoTrack = { this.stream } />,
  574. document.getElementById('largeVideoBackgroundContainer')
  575. );
  576. }
  577. }