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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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 {
  7. ORIENTATION,
  8. LargeVideoBackground
  9. } from '../../../react/features/large-video';
  10. /* eslint-enable no-unused-vars */
  11. import Filmstrip from './Filmstrip';
  12. import LargeContainer from './LargeContainer';
  13. import UIEvents from '../../../service/UI/UIEvents';
  14. import UIUtil from '../util/UIUtil';
  15. // FIXME should be 'video'
  16. export const VIDEO_CONTAINER_TYPE = 'camera';
  17. const FADE_DURATION_MS = 300;
  18. /**
  19. * The CSS class used to add a filter effect on the large video when there is
  20. * a problem with local video.
  21. *
  22. * @private
  23. * @type {string}
  24. */
  25. const LOCAL_PROBLEM_FILTER_CLASS = 'videoProblemFilter';
  26. /**
  27. * The CSS class used to add a filter effect on the large video when there is
  28. * a problem with remote video.
  29. *
  30. * @private
  31. * @type {string}
  32. */
  33. const REMOTE_PROBLEM_FILTER_CLASS = 'remoteVideoProblemFilter';
  34. /**
  35. * Returns an array of the video dimensions, so that it keeps it's aspect
  36. * ratio and fits available area with it's larger dimension. This method
  37. * ensures that whole video will be visible and can leave empty areas.
  38. *
  39. * @param videoWidth the width of the video to position
  40. * @param videoHeight the height of the video to position
  41. * @param videoSpaceWidth the width of the available space
  42. * @param videoSpaceHeight the height of the available space
  43. * @return an array with 2 elements, the video width and the video height
  44. */
  45. function computeDesktopVideoSize( // eslint-disable-line max-params
  46. videoWidth,
  47. videoHeight,
  48. videoSpaceWidth,
  49. videoSpaceHeight) {
  50. const aspectRatio = videoWidth / videoHeight;
  51. let availableWidth = Math.max(videoWidth, videoSpaceWidth);
  52. let availableHeight = Math.max(videoHeight, videoSpaceHeight);
  53. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  54. // eslint-disable-next-line no-param-reassign
  55. videoSpaceWidth -= Filmstrip.getFilmstripWidth();
  56. } else {
  57. // eslint-disable-next-line no-param-reassign
  58. videoSpaceHeight -= Filmstrip.getFilmstripHeight();
  59. }
  60. if (availableWidth / aspectRatio >= videoSpaceHeight) {
  61. availableHeight = videoSpaceHeight;
  62. availableWidth = availableHeight * aspectRatio;
  63. }
  64. if (availableHeight * aspectRatio >= videoSpaceWidth) {
  65. availableWidth = videoSpaceWidth;
  66. availableHeight = availableWidth / aspectRatio;
  67. }
  68. return [ availableWidth, availableHeight ];
  69. }
  70. /**
  71. * Returns an array of the video dimensions. It respects the
  72. * VIDEO_LAYOUT_FIT config, to fit the video to the screen, by hiding some parts
  73. * of it, or to fit it to the height or width.
  74. *
  75. * @param videoWidth the original video width
  76. * @param videoHeight the original video height
  77. * @param videoSpaceWidth the width of the video space
  78. * @param videoSpaceHeight the height of the video space
  79. * @return an array with 2 elements, the video width and the video height
  80. */
  81. function computeCameraVideoSize( // eslint-disable-line max-params
  82. videoWidth,
  83. videoHeight,
  84. videoSpaceWidth,
  85. videoSpaceHeight,
  86. videoLayoutFit) {
  87. const aspectRatio = videoWidth / videoHeight;
  88. switch (videoLayoutFit) {
  89. case 'height':
  90. return [ videoSpaceHeight * aspectRatio, videoSpaceHeight ];
  91. case 'width':
  92. return [ videoSpaceWidth, videoSpaceWidth / aspectRatio ];
  93. case 'both': {
  94. const videoSpaceRatio = videoSpaceWidth / videoSpaceHeight;
  95. const maxZoomCoefficient = interfaceConfig.MAXIMUM_ZOOMING_COEFFICIENT
  96. || Infinity;
  97. if (videoSpaceRatio === aspectRatio) {
  98. return [ videoSpaceWidth, videoSpaceHeight ];
  99. }
  100. let [ width, height ] = computeCameraVideoSize(
  101. videoWidth,
  102. videoHeight,
  103. videoSpaceWidth,
  104. videoSpaceHeight,
  105. videoSpaceRatio < aspectRatio ? 'height' : 'width');
  106. const maxWidth = videoSpaceWidth * maxZoomCoefficient;
  107. const maxHeight = videoSpaceHeight * maxZoomCoefficient;
  108. if (width > maxWidth) {
  109. width = maxWidth;
  110. height = width / aspectRatio;
  111. } else if (height > maxHeight) {
  112. height = maxHeight;
  113. width = height * aspectRatio;
  114. }
  115. return [ width, height ];
  116. }
  117. default:
  118. return [ videoWidth, videoHeight ];
  119. }
  120. }
  121. /**
  122. * Returns an array of the video horizontal and vertical indents,
  123. * so that if fits its parent.
  124. *
  125. * @return an array with 2 elements, the horizontal indent and the vertical
  126. * indent
  127. */
  128. function getCameraVideoPosition( // eslint-disable-line max-params
  129. videoWidth,
  130. videoHeight,
  131. videoSpaceWidth,
  132. videoSpaceHeight) {
  133. // Parent height isn't completely calculated when we position the video in
  134. // full screen mode and this is why we use the screen height in this case.
  135. // Need to think it further at some point and implement it properly.
  136. if (UIUtil.isFullScreen()) {
  137. // eslint-disable-next-line no-param-reassign
  138. videoSpaceHeight = window.innerHeight;
  139. }
  140. const horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  141. const verticalIndent = (videoSpaceHeight - videoHeight) / 2;
  142. return { horizontalIndent,
  143. verticalIndent };
  144. }
  145. /**
  146. * Returns an array of the video horizontal and vertical indents.
  147. * Centers horizontally and top aligns vertically.
  148. *
  149. * @return an array with 2 elements, the horizontal indent and the vertical
  150. * indent
  151. */
  152. function getDesktopVideoPosition(videoWidth, videoHeight, videoSpaceWidth) {
  153. const horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  154. const verticalIndent = 0;// Top aligned
  155. return { horizontalIndent,
  156. verticalIndent };
  157. }
  158. /**
  159. * Container for user video.
  160. */
  161. export class VideoContainer extends LargeContainer {
  162. /**
  163. *
  164. */
  165. get $video() {
  166. return $('#largeVideo');
  167. }
  168. /**
  169. *
  170. */
  171. get id() {
  172. return this.userId;
  173. }
  174. /**
  175. * Creates new VideoContainer instance.
  176. * @param resizeContainer {Function} function that takes care of the size
  177. * of the video container.
  178. * @param emitter {EventEmitter} the event emitter that will be used by
  179. * this instance.
  180. */
  181. constructor(resizeContainer, emitter) {
  182. super();
  183. this.stream = null;
  184. this.userId = null;
  185. this.videoType = null;
  186. this.localFlipX = true;
  187. this.emitter = emitter;
  188. this.resizeContainer = resizeContainer;
  189. /**
  190. * Whether the background should fit the height of the container
  191. * (portrait) or fit the width of the container (landscape).
  192. *
  193. * @private
  194. * @type {string|null}
  195. */
  196. this._backgroundOrientation = null;
  197. /**
  198. * Flag indicates whether or not the background should be rendered.
  199. * If the background will not be visible then it is hidden to save
  200. * on performance.
  201. * @type {boolean}
  202. */
  203. this._hideBackground = true;
  204. /**
  205. * Flag indicates whether or not the avatar is currently displayed.
  206. * @type {boolean}
  207. */
  208. this.avatarDisplayed = false;
  209. this.$avatar = $('#dominantSpeaker');
  210. /**
  211. * A jQuery selector of the remote connection message.
  212. * @type {jQuery|HTMLElement}
  213. */
  214. this.$remoteConnectionMessage = $('#remoteConnectionMessage');
  215. this.$remotePresenceMessage = $('#remotePresenceMessage');
  216. /**
  217. * Indicates whether or not the video stream attached to the video
  218. * element has started(which means that there is any image rendered
  219. * even if the video is stalled).
  220. * @type {boolean}
  221. */
  222. this.wasVideoRendered = false;
  223. this.$wrapper = $('#largeVideoWrapper');
  224. /**
  225. * FIXME: currently using parent() because I can't come up with name
  226. * for id. We'll need to probably refactor the HTML related to the large
  227. * video anyway.
  228. */
  229. this.$wrapperParent = this.$wrapper.parent();
  230. this.avatarHeight = $('#dominantSpeakerAvatar').height();
  231. const onPlayingCallback = function(event) {
  232. if (typeof resizeContainer === 'function') {
  233. resizeContainer(event);
  234. }
  235. this.wasVideoRendered = true;
  236. }.bind(this);
  237. this.$video[0].onplaying = onPlayingCallback;
  238. /**
  239. * A Set of functions to invoke when the video element resizes.
  240. *
  241. * @private
  242. */
  243. this._resizeListeners = new Set();
  244. this.$video[0].onresize = this._onResize.bind(this);
  245. }
  246. /**
  247. * Adds a function to the known subscribers of video element resize
  248. * events.
  249. *
  250. * @param {Function} callback - The subscriber to notify when the video
  251. * element resizes.
  252. * @returns {void}
  253. */
  254. addResizeListener(callback) {
  255. this._resizeListeners.add(callback);
  256. }
  257. /**
  258. * Enables a filter on the video which indicates that there are some
  259. * problems with the local media connection.
  260. *
  261. * @param {boolean} enable <tt>true</tt> if the filter is to be enabled or
  262. * <tt>false</tt> otherwise.
  263. */
  264. enableLocalConnectionProblemFilter(enable) {
  265. this.$video.toggleClass(LOCAL_PROBLEM_FILTER_CLASS, enable);
  266. this._updateBackground();
  267. }
  268. /**
  269. * Obtains media stream ID of the underlying {@link JitsiTrack}.
  270. * @return {string|null}
  271. */
  272. getStreamID() {
  273. return this.stream ? this.stream.getId() : null;
  274. }
  275. /**
  276. * Get size of video element.
  277. * @returns {{width, height}}
  278. */
  279. getStreamSize() {
  280. const video = this.$video[0];
  281. return {
  282. width: video.videoWidth,
  283. height: video.videoHeight
  284. };
  285. }
  286. /**
  287. * Calculate optimal video size for specified container size.
  288. * @param {number} containerWidth container width
  289. * @param {number} containerHeight container height
  290. * @returns {{availableWidth, availableHeight}}
  291. */
  292. getVideoSize(containerWidth, containerHeight) {
  293. const { width, height } = this.getStreamSize();
  294. if (this.stream && this.isScreenSharing()) {
  295. return computeDesktopVideoSize(width,
  296. height,
  297. containerWidth,
  298. containerHeight);
  299. }
  300. return computeCameraVideoSize(width,
  301. height,
  302. containerWidth,
  303. containerHeight,
  304. interfaceConfig.VIDEO_LAYOUT_FIT);
  305. }
  306. /* eslint-disable max-params */
  307. /**
  308. * Calculate optimal video position (offset for top left corner)
  309. * for specified video size and container size.
  310. * @param {number} width video width
  311. * @param {number} height video height
  312. * @param {number} containerWidth container width
  313. * @param {number} containerHeight container height
  314. * @returns {{horizontalIndent, verticalIndent}}
  315. */
  316. getVideoPosition(width, height, containerWidth, containerHeight) {
  317. /* eslint-enable max-params */
  318. if (this.stream && this.isScreenSharing()) {
  319. let availableContainerWidth = containerWidth;
  320. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  321. availableContainerWidth -= Filmstrip.getFilmstripWidth();
  322. }
  323. return getDesktopVideoPosition(width,
  324. height,
  325. availableContainerWidth,
  326. containerHeight);
  327. }
  328. return getCameraVideoPosition(width,
  329. height,
  330. containerWidth,
  331. containerHeight);
  332. }
  333. /**
  334. * Updates the positioning of the remote connection presence message and the
  335. * connection status message which escribes that the remote user is having
  336. * connectivity issues.
  337. *
  338. * @returns {void}
  339. */
  340. positionRemoteStatusMessages() {
  341. this._positionParticipantStatus(this.$remoteConnectionMessage);
  342. this._positionParticipantStatus(this.$remotePresenceMessage);
  343. }
  344. /**
  345. * Modifies the position of the passed in jQuery object so it displays
  346. * in the middle of the video container or below the avatar.
  347. *
  348. * @private
  349. * @returns {void}
  350. */
  351. _positionParticipantStatus($element) {
  352. if (this.avatarDisplayed) {
  353. const $avatarImage = $('#dominantSpeakerAvatar');
  354. $element.css(
  355. 'top',
  356. $avatarImage.offset().top + $avatarImage.height() + 10);
  357. } else {
  358. const height = $element.height();
  359. const parentHeight = $element.parent().height();
  360. $element.css('top', (parentHeight / 2) - (height / 2));
  361. }
  362. }
  363. /**
  364. *
  365. */
  366. resize(containerWidth, containerHeight, animate = false) {
  367. // XXX Prevent TypeError: undefined is not an object when the Web
  368. // browser does not support WebRTC (yet).
  369. if (this.$video.length === 0) {
  370. return;
  371. }
  372. const [ width, height ]
  373. = this.getVideoSize(containerWidth, containerHeight);
  374. if ((containerWidth > width) || (containerHeight > height)) {
  375. this._backgroundOrientation = containerWidth > width
  376. ? ORIENTATION.LANDSCAPE : ORIENTATION.PORTRAIT;
  377. this._hideBackground = false;
  378. } else {
  379. this._hideBackground = true;
  380. }
  381. this._updateBackground();
  382. const { horizontalIndent, verticalIndent }
  383. = this.getVideoPosition(width, height,
  384. containerWidth, containerHeight);
  385. // update avatar position
  386. const top = (containerHeight / 2) - (this.avatarHeight / 4 * 3);
  387. this.$avatar.css('top', top);
  388. this.positionRemoteStatusMessages();
  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. }