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

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