您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VideoContainer.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 { isTestModeEnabled } from '../../../react/features/base/testing';
  7. import { ORIENTATION, LargeVideoBackground, updateLastLargeVideoMediaEvent } from '../../../react/features/large-video';
  8. import { LAYOUTS, getCurrentLayout } from '../../../react/features/video-layout';
  9. /* eslint-enable no-unused-vars */
  10. import UIEvents from '../../../service/UI/UIEvents';
  11. import UIUtil from '../util/UIUtil';
  12. import Filmstrip from './Filmstrip';
  13. import LargeContainer from './LargeContainer';
  14. // FIXME should be 'video'
  15. export const VIDEO_CONTAINER_TYPE = 'camera';
  16. const FADE_DURATION_MS = 300;
  17. /**
  18. * List of container events that we are going to process, will be added as listener to the
  19. * container for every event in the list. The latest event will be stored in redux.
  20. */
  21. const containerEvents = [
  22. 'abort', 'canplay', 'canplaythrough', 'emptied', 'ended', 'error', 'loadeddata', 'loadedmetadata', 'loadstart',
  23. 'pause', 'play', 'playing', 'ratechange', 'stalled', 'suspend', 'waiting'
  24. ];
  25. /**
  26. * Returns an array of the video dimensions, so that it keeps it's aspect
  27. * ratio and fits available area with it's larger dimension. This method
  28. * ensures that whole video will be visible and can leave empty areas.
  29. *
  30. * @param videoWidth the width of the video to position
  31. * @param videoHeight the height of the video to position
  32. * @param videoSpaceWidth the width of the available space
  33. * @param videoSpaceHeight the height of the available space
  34. * @return an array with 2 elements, the video width and the video height
  35. */
  36. function computeDesktopVideoSize( // eslint-disable-line max-params
  37. videoWidth,
  38. videoHeight,
  39. videoSpaceWidth,
  40. videoSpaceHeight) {
  41. if (videoWidth === 0 || videoHeight === 0 || videoSpaceWidth === 0 || videoSpaceHeight === 0) {
  42. // Avoid NaN values caused by devision by 0.
  43. return [ 0, 0 ];
  44. }
  45. const aspectRatio = videoWidth / videoHeight;
  46. let availableWidth = Math.max(videoWidth, videoSpaceWidth);
  47. let availableHeight = Math.max(videoHeight, videoSpaceHeight);
  48. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  49. // eslint-disable-next-line no-param-reassign
  50. videoSpaceWidth -= Filmstrip.getVerticalFilmstripWidth();
  51. } else {
  52. // eslint-disable-next-line no-param-reassign
  53. videoSpaceHeight -= Filmstrip.getFilmstripHeight();
  54. }
  55. if (availableWidth / aspectRatio >= videoSpaceHeight) {
  56. availableHeight = videoSpaceHeight;
  57. availableWidth = availableHeight * aspectRatio;
  58. }
  59. if (availableHeight * aspectRatio >= videoSpaceWidth) {
  60. availableWidth = videoSpaceWidth;
  61. availableHeight = availableWidth / aspectRatio;
  62. }
  63. return [ availableWidth, availableHeight ];
  64. }
  65. /**
  66. * Returns an array of the video dimensions. It respects the
  67. * VIDEO_LAYOUT_FIT config, to fit the video to the screen, by hiding some parts
  68. * of it, or to fit it to the height or width.
  69. *
  70. * @param videoWidth the original video width
  71. * @param videoHeight the original video height
  72. * @param videoSpaceWidth the width of the video space
  73. * @param videoSpaceHeight the height of the video space
  74. * @return an array with 2 elements, the video width and the video height
  75. */
  76. function computeCameraVideoSize( // eslint-disable-line max-params
  77. videoWidth,
  78. videoHeight,
  79. videoSpaceWidth,
  80. videoSpaceHeight,
  81. videoLayoutFit) {
  82. if (videoWidth === 0 || videoHeight === 0 || videoSpaceWidth === 0 || videoSpaceHeight === 0) {
  83. // Avoid NaN values caused by devision by 0.
  84. return [ 0, 0 ];
  85. }
  86. const aspectRatio = videoWidth / videoHeight;
  87. switch (videoLayoutFit) {
  88. case 'height':
  89. return [ videoSpaceHeight * aspectRatio, videoSpaceHeight ];
  90. case 'width':
  91. return [ videoSpaceWidth, videoSpaceWidth / aspectRatio ];
  92. case 'both': {
  93. const videoSpaceRatio = videoSpaceWidth / videoSpaceHeight;
  94. const maxZoomCoefficient = interfaceConfig.MAXIMUM_ZOOMING_COEFFICIENT
  95. || Infinity;
  96. if (videoSpaceRatio === aspectRatio) {
  97. return [ videoSpaceWidth, videoSpaceHeight ];
  98. }
  99. let [ width, height ] = computeCameraVideoSize(
  100. videoWidth,
  101. videoHeight,
  102. videoSpaceWidth,
  103. videoSpaceHeight,
  104. videoSpaceRatio < aspectRatio ? 'height' : 'width');
  105. const maxWidth = videoSpaceWidth * maxZoomCoefficient;
  106. const maxHeight = videoSpaceHeight * maxZoomCoefficient;
  107. if (width > maxWidth) {
  108. width = maxWidth;
  109. height = width / aspectRatio;
  110. } else if (height > maxHeight) {
  111. height = maxHeight;
  112. width = height * aspectRatio;
  113. }
  114. return [ width, height ];
  115. }
  116. default:
  117. return [ videoWidth, videoHeight ];
  118. }
  119. }
  120. /**
  121. * Returns an array of the video horizontal and vertical indents,
  122. * so that if fits its parent.
  123. *
  124. * @return an array with 2 elements, the horizontal indent and the vertical
  125. * indent
  126. */
  127. function getCameraVideoPosition( // eslint-disable-line max-params
  128. videoWidth,
  129. videoHeight,
  130. videoSpaceWidth,
  131. videoSpaceHeight) {
  132. // Parent height isn't completely calculated when we position the video in
  133. // full screen mode and this is why we use the screen height in this case.
  134. // Need to think it further at some point and implement it properly.
  135. if (UIUtil.isFullScreen()) {
  136. // eslint-disable-next-line no-param-reassign
  137. videoSpaceHeight = window.innerHeight;
  138. }
  139. const horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  140. const verticalIndent = (videoSpaceHeight - videoHeight) / 2;
  141. return { horizontalIndent,
  142. verticalIndent };
  143. }
  144. /**
  145. * Container for user video.
  146. */
  147. export class VideoContainer extends LargeContainer {
  148. /**
  149. *
  150. */
  151. get $video() {
  152. return $('#largeVideo');
  153. }
  154. /**
  155. *
  156. */
  157. get id() {
  158. return this.userId;
  159. }
  160. /**
  161. * Creates new VideoContainer instance.
  162. * @param resizeContainer {Function} function that takes care of the size
  163. * of the video container.
  164. * @param emitter {EventEmitter} the event emitter that will be used by
  165. * this instance.
  166. */
  167. constructor(resizeContainer, emitter) {
  168. super();
  169. this.stream = null;
  170. this.userId = null;
  171. this.videoType = null;
  172. this.localFlipX = true;
  173. this.emitter = emitter;
  174. this.resizeContainer = resizeContainer;
  175. /**
  176. * Whether the background should fit the height of the container
  177. * (portrait) or fit the width of the container (landscape).
  178. *
  179. * @private
  180. * @type {string|null}
  181. */
  182. this._backgroundOrientation = null;
  183. /**
  184. * Flag indicates whether or not the background should be rendered.
  185. * If the background will not be visible then it is hidden to save
  186. * on performance.
  187. * @type {boolean}
  188. */
  189. this._hideBackground = true;
  190. this._isHidden = false;
  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. this.$wrapper = $('#largeVideoWrapper');
  204. /**
  205. * FIXME: currently using parent() because I can't come up with name
  206. * for id. We'll need to probably refactor the HTML related to the large
  207. * video anyway.
  208. */
  209. this.$wrapperParent = this.$wrapper.parent();
  210. this.avatarHeight = $('#dominantSpeakerAvatarContainer').height();
  211. this.$video[0].onplaying = function(event) {
  212. if (typeof resizeContainer === 'function') {
  213. resizeContainer(event);
  214. }
  215. };
  216. /**
  217. * A Set of functions to invoke when the video element resizes.
  218. *
  219. * @private
  220. */
  221. this._resizeListeners = new Set();
  222. this.$video[0].onresize = this._onResize.bind(this);
  223. if (isTestModeEnabled(APP.store.getState())) {
  224. const cb = name => APP.store.dispatch(updateLastLargeVideoMediaEvent(name));
  225. containerEvents.forEach(event => {
  226. this.$video[0].addEventListener(event, cb.bind(this, event));
  227. });
  228. }
  229. }
  230. /**
  231. * Adds a function to the known subscribers of video element resize
  232. * events.
  233. *
  234. * @param {Function} callback - The subscriber to notify when the video
  235. * element resizes.
  236. * @returns {void}
  237. */
  238. addResizeListener(callback) {
  239. this._resizeListeners.add(callback);
  240. }
  241. /**
  242. * Obtains media stream ID of the underlying {@link JitsiTrack}.
  243. * @return {string|null}
  244. */
  245. getStreamID() {
  246. return this.stream ? this.stream.getId() : null;
  247. }
  248. /**
  249. * Get size of video element.
  250. * @returns {{width, height}}
  251. */
  252. getStreamSize() {
  253. const video = this.$video[0];
  254. return {
  255. width: video.videoWidth,
  256. height: video.videoHeight
  257. };
  258. }
  259. /**
  260. * Calculate optimal video size for specified container size.
  261. * @param {number} containerWidth container width
  262. * @param {number} containerHeight container height
  263. * @returns {{availableWidth, availableHeight}}
  264. */
  265. _getVideoSize(containerWidth, containerHeight) {
  266. const { width, height } = this.getStreamSize();
  267. if (this.stream && this.isScreenSharing()) {
  268. return computeDesktopVideoSize(width,
  269. height,
  270. containerWidth,
  271. containerHeight);
  272. }
  273. return computeCameraVideoSize(width,
  274. height,
  275. containerWidth,
  276. containerHeight,
  277. interfaceConfig.VIDEO_LAYOUT_FIT);
  278. }
  279. /* eslint-disable max-params */
  280. /**
  281. * Calculate optimal video position (offset for top left corner)
  282. * for specified video size and container size.
  283. * @param {number} width video width
  284. * @param {number} height video height
  285. * @param {number} containerWidth container width
  286. * @param {number} containerHeight container height
  287. * @returns {{horizontalIndent, verticalIndent}}
  288. */
  289. getVideoPosition(width, height, containerWidth, containerHeight) {
  290. let containerWidthToUse = containerWidth;
  291. /* eslint-enable max-params */
  292. if (this.stream && this.isScreenSharing()) {
  293. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  294. containerWidthToUse -= Filmstrip.getVerticalFilmstripWidth();
  295. }
  296. return getCameraVideoPosition(width,
  297. height,
  298. containerWidthToUse,
  299. containerHeight);
  300. }
  301. return getCameraVideoPosition(width,
  302. height,
  303. containerWidthToUse,
  304. containerHeight);
  305. }
  306. /**
  307. * Updates the positioning of the remote connection presence message and the
  308. * connection status message which escribes that the remote user is having
  309. * connectivity issues.
  310. *
  311. * @returns {void}
  312. */
  313. positionRemoteStatusMessages() {
  314. this._positionParticipantStatus(this.$remoteConnectionMessage);
  315. this._positionParticipantStatus(this.$remotePresenceMessage);
  316. }
  317. /**
  318. * Modifies the position of the passed in jQuery object so it displays
  319. * in the middle of the video container or below the avatar.
  320. *
  321. * @private
  322. * @returns {void}
  323. */
  324. _positionParticipantStatus($element) {
  325. if (this.avatarDisplayed) {
  326. const $avatarImage = $('#dominantSpeakerAvatarContainer');
  327. $element.css(
  328. 'top',
  329. $avatarImage.offset().top + $avatarImage.height() + 10);
  330. } else {
  331. const height = $element.height();
  332. const parentHeight = $element.parent().height();
  333. $element.css('top', (parentHeight / 2) - (height / 2));
  334. }
  335. }
  336. /**
  337. *
  338. */
  339. resize(containerWidth, containerHeight, animate = false) {
  340. // XXX Prevent TypeError: undefined is not an object when the Web
  341. // browser does not support WebRTC (yet).
  342. if (this.$video.length === 0) {
  343. return;
  344. }
  345. const currentLayout = getCurrentLayout(APP.store.getState());
  346. if (currentLayout === LAYOUTS.TILE_VIEW) {
  347. // We don't need to resize the large video since it won't be displayed and we'll resize when returning back
  348. // to stage view.
  349. return;
  350. }
  351. this.positionRemoteStatusMessages();
  352. const [ width, height ] = this._getVideoSize(containerWidth, containerHeight);
  353. if (width === 0 || height === 0) {
  354. // We don't need to set 0 for width or height since the visibility is controled by the visibility css prop
  355. // on the largeVideoElementsContainer. Also if the width/height of the video element is 0 the attached
  356. // stream won't be played. Normally if we attach a new stream we won't resize the video element until the
  357. // stream has been played. But setting width/height to 0 will prevent the video from playing.
  358. return;
  359. }
  360. if ((containerWidth > width) || (containerHeight > height)) {
  361. this._backgroundOrientation = containerWidth > width ? ORIENTATION.LANDSCAPE : ORIENTATION.PORTRAIT;
  362. this._hideBackground = false;
  363. } else {
  364. this._hideBackground = true;
  365. }
  366. this._updateBackground();
  367. const { horizontalIndent, verticalIndent }
  368. = this.getVideoPosition(width, height, containerWidth, containerHeight);
  369. this.$wrapper.animate({
  370. width,
  371. height,
  372. top: verticalIndent,
  373. bottom: verticalIndent,
  374. left: horizontalIndent,
  375. right: horizontalIndent
  376. }, {
  377. queue: false,
  378. duration: animate ? 500 : 0
  379. });
  380. }
  381. /**
  382. * Removes a function from the known subscribers of video element resize
  383. * events.
  384. *
  385. * @param {Function} callback - The callback to remove from known
  386. * subscribers of video resize events.
  387. * @returns {void}
  388. */
  389. removeResizeListener(callback) {
  390. this._resizeListeners.delete(callback);
  391. }
  392. /**
  393. * Update video stream.
  394. * @param {string} userID
  395. * @param {JitsiTrack?} stream new stream
  396. * @param {string} videoType video type
  397. */
  398. setStream(userID, stream, videoType) {
  399. this.userId = userID;
  400. if (this.stream === stream) {
  401. // Handles the use case for the remote participants when the
  402. // videoType is received with delay after turning on/off the
  403. // desktop sharing.
  404. if (this.videoType !== videoType) {
  405. this.videoType = videoType;
  406. this.resizeContainer();
  407. }
  408. return;
  409. }
  410. // detach old stream
  411. if (this.stream) {
  412. this.stream.detach(this.$video[0]);
  413. }
  414. this.stream = stream;
  415. this.videoType = videoType;
  416. if (!stream) {
  417. return;
  418. }
  419. stream.attach(this.$video[0]);
  420. const flipX = stream.isLocal() && this.localFlipX;
  421. this.$video.css({
  422. transform: flipX ? 'scaleX(-1)' : 'none'
  423. });
  424. this._updateBackground();
  425. }
  426. /**
  427. * Changes the flipX state of the local video.
  428. * @param val {boolean} true if flipped.
  429. */
  430. setLocalFlipX(val) {
  431. this.localFlipX = val;
  432. if (!this.$video || !this.stream || !this.stream.isLocal()) {
  433. return;
  434. }
  435. this.$video.css({
  436. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  437. });
  438. this._updateBackground();
  439. }
  440. /**
  441. * Check if current video stream is screen sharing.
  442. * @returns {boolean}
  443. */
  444. isScreenSharing() {
  445. return this.videoType === 'desktop';
  446. }
  447. /**
  448. * Show or hide user avatar.
  449. * @param {boolean} show
  450. */
  451. showAvatar(show) {
  452. this.$avatar.css('visibility', show ? 'visible' : 'hidden');
  453. this.avatarDisplayed = show;
  454. this.emitter.emit(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE, show);
  455. APP.API.notifyLargeVideoVisibilityChanged(show);
  456. }
  457. /**
  458. * We are doing fadeOut/fadeIn animations on parent div which wraps
  459. * largeVideo, because when Temasys plugin is in use it replaces
  460. * <video> elements with plugin <object> tag. In Safari jQuery is
  461. * unable to store values on this plugin object which breaks all
  462. * animation effects performed on it directly.
  463. *
  464. * TODO: refactor this since Temasys is no longer supported.
  465. */
  466. show() {
  467. return new Promise(resolve => {
  468. this.$wrapperParent.css('visibility', 'visible').fadeTo(
  469. FADE_DURATION_MS,
  470. 1,
  471. () => {
  472. this._isHidden = false;
  473. this._updateBackground();
  474. resolve();
  475. }
  476. );
  477. });
  478. }
  479. /**
  480. *
  481. */
  482. hide() {
  483. // as the container is hidden/replaced by another container
  484. // hide its avatar
  485. this.showAvatar(false);
  486. return new Promise(resolve => {
  487. this.$wrapperParent.fadeTo(FADE_DURATION_MS, 0, () => {
  488. this.$wrapperParent.css('visibility', 'hidden');
  489. this._isHidden = true;
  490. this._updateBackground();
  491. resolve();
  492. });
  493. });
  494. }
  495. /**
  496. * @return {boolean} switch on dominant speaker event if on stage.
  497. */
  498. stayOnStage() {
  499. return false;
  500. }
  501. /**
  502. * Callback invoked when the video element changes dimensions.
  503. *
  504. * @private
  505. * @returns {void}
  506. */
  507. _onResize() {
  508. this._resizeListeners.forEach(callback => callback());
  509. }
  510. /**
  511. * Attaches and/or updates a React Component to be used as a background for
  512. * the large video, to display blurred video and fill up empty space not
  513. * taken up by the large video.
  514. *
  515. * @private
  516. * @returns {void}
  517. */
  518. _updateBackground() {
  519. // Do not the background display on browsers that might experience
  520. // performance issues from the presence of the background or if
  521. // explicitly disabled.
  522. if (interfaceConfig.DISABLE_VIDEO_BACKGROUND
  523. || browser.isFirefox()
  524. || browser.isSafari()) {
  525. return;
  526. }
  527. ReactDOM.render(
  528. <LargeVideoBackground
  529. hidden = { this._hideBackground || this._isHidden }
  530. mirror = {
  531. this.stream
  532. && this.stream.isLocal()
  533. && this.localFlipX
  534. }
  535. orientationFit = { this._backgroundOrientation }
  536. videoElement = { this.$video && this.$video[0] }
  537. videoTrack = { this.stream } />,
  538. document.getElementById('largeVideoBackgroundContainer')
  539. );
  540. }
  541. }