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

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