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

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