選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VideoContainer.js 21KB

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