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

VideoContainer.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. /**
  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. if (isTestModeEnabled(APP.store.getState())) {
  233. const cb = name => APP.store.dispatch(updateLastLargeVideoMediaEvent(name));
  234. containerEvents.forEach(event => {
  235. this.$video[0].addEventListener(event, cb.bind(this, event));
  236. });
  237. }
  238. }
  239. /**
  240. * Adds a function to the known subscribers of video element resize
  241. * events.
  242. *
  243. * @param {Function} callback - The subscriber to notify when the video
  244. * element resizes.
  245. * @returns {void}
  246. */
  247. addResizeListener(callback) {
  248. this._resizeListeners.add(callback);
  249. }
  250. /**
  251. * Obtains media stream ID of the underlying {@link JitsiTrack}.
  252. * @return {string|null}
  253. */
  254. getStreamID() {
  255. return this.stream ? this.stream.getId() : null;
  256. }
  257. /**
  258. * Get size of video element.
  259. * @returns {{width, height}}
  260. */
  261. getStreamSize() {
  262. const video = this.$video[0];
  263. return {
  264. width: video.videoWidth,
  265. height: video.videoHeight
  266. };
  267. }
  268. /**
  269. * Calculate optimal video size for specified container size.
  270. * @param {number} containerWidth container width
  271. * @param {number} containerHeight container height
  272. * @returns {{availableWidth, availableHeight}}
  273. */
  274. _getVideoSize(containerWidth, containerHeight) {
  275. const { width, height } = this.getStreamSize();
  276. if (this.stream && this.isScreenSharing()) {
  277. return computeDesktopVideoSize(width,
  278. height,
  279. containerWidth,
  280. containerHeight);
  281. }
  282. return computeCameraVideoSize(width,
  283. height,
  284. containerWidth,
  285. containerHeight,
  286. interfaceConfig.VIDEO_LAYOUT_FIT);
  287. }
  288. /* eslint-disable max-params */
  289. /**
  290. * Calculate optimal video position (offset for top left corner)
  291. * for specified video size and container size.
  292. * @param {number} width video width
  293. * @param {number} height video height
  294. * @param {number} containerWidth container width
  295. * @param {number} containerHeight container height
  296. * @returns {{horizontalIndent, verticalIndent}}
  297. */
  298. getVideoPosition(width, height, containerWidth, containerHeight) {
  299. let containerWidthToUse = containerWidth;
  300. /* eslint-enable max-params */
  301. if (this.stream && this.isScreenSharing()) {
  302. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  303. containerWidthToUse -= Filmstrip.getVerticalFilmstripWidth();
  304. }
  305. return getCameraVideoPosition(width,
  306. height,
  307. containerWidthToUse,
  308. containerHeight);
  309. }
  310. return getCameraVideoPosition(width,
  311. height,
  312. containerWidthToUse,
  313. containerHeight);
  314. }
  315. /**
  316. * Updates the positioning of the remote connection presence message and the
  317. * connection status message which escribes that the remote user is having
  318. * connectivity issues.
  319. *
  320. * @returns {void}
  321. */
  322. positionRemoteStatusMessages() {
  323. this._positionParticipantStatus(this.$remoteConnectionMessage);
  324. this._positionParticipantStatus(this.$remotePresenceMessage);
  325. }
  326. /**
  327. * Modifies the position of the passed in jQuery object so it displays
  328. * in the middle of the video container or below the avatar.
  329. *
  330. * @private
  331. * @returns {void}
  332. */
  333. _positionParticipantStatus($element) {
  334. if (this.avatarDisplayed) {
  335. const $avatarImage = $('#dominantSpeakerAvatarContainer');
  336. $element.css(
  337. 'top',
  338. $avatarImage.offset().top + $avatarImage.height() + 10);
  339. } else {
  340. const height = $element.height();
  341. const parentHeight = $element.parent().height();
  342. $element.css('top', (parentHeight / 2) - (height / 2));
  343. }
  344. }
  345. /**
  346. *
  347. */
  348. resize(containerWidth, containerHeight, animate = false) {
  349. // XXX Prevent TypeError: undefined is not an object when the Web
  350. // browser does not support WebRTC (yet).
  351. if (this.$video.length === 0) {
  352. return;
  353. }
  354. const currentLayout = getCurrentLayout(APP.store.getState());
  355. if (currentLayout === LAYOUTS.TILE_VIEW) {
  356. // We don't need to resize the large video since it won't be displayed and we'll resize when returning back
  357. // to stage view.
  358. return;
  359. }
  360. this.positionRemoteStatusMessages();
  361. const [ width, height ] = this._getVideoSize(containerWidth, containerHeight);
  362. if (width === 0 || height === 0) {
  363. // We don't need to set 0 for width or height since the visibility is controled by the visibility css prop
  364. // on the largeVideoElementsContainer. Also if the width/height of the video element is 0 the attached
  365. // stream won't be played. Normally if we attach a new stream we won't resize the video element until the
  366. // stream has been played. But setting width/height to 0 will prevent the video from playing.
  367. return;
  368. }
  369. if ((containerWidth > width) || (containerHeight > height)) {
  370. this._backgroundOrientation = containerWidth > width ? ORIENTATION.LANDSCAPE : ORIENTATION.PORTRAIT;
  371. this._hideBackground = false;
  372. } else {
  373. this._hideBackground = true;
  374. }
  375. this._updateBackground();
  376. const { horizontalIndent, verticalIndent }
  377. = this.getVideoPosition(width, height, containerWidth, containerHeight);
  378. this.$wrapper.animate({
  379. width,
  380. height,
  381. top: verticalIndent,
  382. bottom: verticalIndent,
  383. left: horizontalIndent,
  384. right: horizontalIndent
  385. }, {
  386. queue: false,
  387. duration: animate ? 500 : 0
  388. });
  389. }
  390. /**
  391. * Removes a function from the known subscribers of video element resize
  392. * events.
  393. *
  394. * @param {Function} callback - The callback to remove from known
  395. * subscribers of video resize events.
  396. * @returns {void}
  397. */
  398. removeResizeListener(callback) {
  399. this._resizeListeners.delete(callback);
  400. }
  401. /**
  402. * Update video stream.
  403. * @param {string} userID
  404. * @param {JitsiTrack?} stream new stream
  405. * @param {string} videoType video type
  406. */
  407. setStream(userID, stream, videoType) {
  408. this.userId = userID;
  409. if (this.stream === stream) {
  410. // Handles the use case for the remote participants when the
  411. // videoType is received with delay after turning on/off the
  412. // desktop sharing.
  413. if (this.videoType !== videoType) {
  414. this.videoType = videoType;
  415. this.resizeContainer();
  416. }
  417. return;
  418. }
  419. // The stream has changed, so the image will be lost on detach
  420. this.wasVideoRendered = false;
  421. // detach old stream
  422. if (this.stream) {
  423. this.stream.detach(this.$video[0]);
  424. }
  425. this.stream = stream;
  426. this.videoType = videoType;
  427. if (!stream) {
  428. return;
  429. }
  430. stream.attach(this.$video[0]);
  431. const flipX = stream.isLocal() && this.localFlipX;
  432. this.$video.css({
  433. transform: flipX ? 'scaleX(-1)' : 'none'
  434. });
  435. this._updateBackground();
  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. this.$avatar.css('visibility', show ? 'visible' : 'hidden');
  464. this.avatarDisplayed = show;
  465. this.emitter.emit(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE, show);
  466. APP.API.notifyLargeVideoVisibilityChanged(show);
  467. }
  468. /**
  469. * We are doing fadeOut/fadeIn animations on parent div which wraps
  470. * largeVideo, because when Temasys plugin is in use it replaces
  471. * <video> elements with plugin <object> tag. In Safari jQuery is
  472. * unable to store values on this plugin object which breaks all
  473. * animation effects performed on it directly.
  474. *
  475. * TODO: refactor this since Temasys is no longer supported.
  476. */
  477. show() {
  478. return new Promise(resolve => {
  479. this.$wrapperParent.css('visibility', 'visible').fadeTo(
  480. FADE_DURATION_MS,
  481. 1,
  482. () => {
  483. this._isHidden = false;
  484. this._updateBackground();
  485. resolve();
  486. }
  487. );
  488. });
  489. }
  490. /**
  491. *
  492. */
  493. hide() {
  494. // as the container is hidden/replaced by another container
  495. // hide its avatar
  496. this.showAvatar(false);
  497. return new Promise(resolve => {
  498. this.$wrapperParent.fadeTo(FADE_DURATION_MS, 0, () => {
  499. this.$wrapperParent.css('visibility', 'hidden');
  500. this._isHidden = true;
  501. this._updateBackground();
  502. resolve();
  503. });
  504. });
  505. }
  506. /**
  507. * @return {boolean} switch on dominant speaker event if on stage.
  508. */
  509. stayOnStage() {
  510. return false;
  511. }
  512. /**
  513. * Callback invoked when the video element changes dimensions.
  514. *
  515. * @private
  516. * @returns {void}
  517. */
  518. _onResize() {
  519. this._resizeListeners.forEach(callback => callback());
  520. }
  521. /**
  522. * Attaches and/or updates a React Component to be used as a background for
  523. * the large video, to display blurred video and fill up empty space not
  524. * taken up by the large video.
  525. *
  526. * @private
  527. * @returns {void}
  528. */
  529. _updateBackground() {
  530. // Do not the background display on browsers that might experience
  531. // performance issues from the presence of the background or if
  532. // explicitly disabled.
  533. if (interfaceConfig.DISABLE_VIDEO_BACKGROUND
  534. || browser.isFirefox()
  535. || browser.isSafari()) {
  536. return;
  537. }
  538. ReactDOM.render(
  539. <LargeVideoBackground
  540. hidden = { this._hideBackground || this._isHidden }
  541. mirror = {
  542. this.stream
  543. && this.stream.isLocal()
  544. && this.localFlipX
  545. }
  546. orientationFit = { this._backgroundOrientation }
  547. videoElement = { this.$video && this.$video[0] }
  548. videoTrack = { this.stream } />,
  549. document.getElementById('largeVideoBackgroundContainer')
  550. );
  551. }
  552. }