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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /* global $, APP, interfaceConfig */
  2. import Filmstrip from './Filmstrip';
  3. import LargeContainer from './LargeContainer';
  4. import UIEvents from '../../../service/UI/UIEvents';
  5. import UIUtil from '../util/UIUtil';
  6. // FIXME should be 'video'
  7. export const VIDEO_CONTAINER_TYPE = 'camera';
  8. const FADE_DURATION_MS = 300;
  9. const logger = require('jitsi-meet-logger').getLogger(__filename);
  10. /**
  11. * Returns an array of the video dimensions, so that it keeps it's aspect
  12. * ratio and fits available area with it's larger dimension. This method
  13. * ensures that whole video will be visible and can leave empty areas.
  14. *
  15. * @param videoWidth the width of the video to position
  16. * @param videoHeight the height of the video to position
  17. * @param videoSpaceWidth the width of the available space
  18. * @param videoSpaceHeight the height of the available space
  19. * @return an array with 2 elements, the video width and the video height
  20. */
  21. function computeDesktopVideoSize( // eslint-disable-line max-params
  22. videoWidth,
  23. videoHeight,
  24. videoSpaceWidth,
  25. videoSpaceHeight) {
  26. const aspectRatio = videoWidth / videoHeight;
  27. let availableWidth = Math.max(videoWidth, videoSpaceWidth);
  28. let availableHeight = Math.max(videoHeight, videoSpaceHeight);
  29. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  30. // eslint-disable-next-line no-param-reassign
  31. videoSpaceWidth -= Filmstrip.getFilmstripWidth();
  32. } else {
  33. // eslint-disable-next-line no-param-reassign
  34. videoSpaceHeight -= Filmstrip.getFilmstripHeight();
  35. }
  36. if (availableWidth / aspectRatio >= videoSpaceHeight) {
  37. availableHeight = videoSpaceHeight;
  38. availableWidth = availableHeight * aspectRatio;
  39. }
  40. if (availableHeight * aspectRatio >= videoSpaceWidth) {
  41. availableWidth = videoSpaceWidth;
  42. availableHeight = availableWidth / aspectRatio;
  43. }
  44. return [ availableWidth, availableHeight ];
  45. }
  46. /**
  47. * Returns an array of the video dimensions. It respects the
  48. * VIDEO_LAYOUT_FIT config, to fit the video to the screen, by hiding some parts
  49. * of it, or to fit it to the height or width.
  50. *
  51. * @param videoWidth the original video width
  52. * @param videoHeight the original video height
  53. * @param videoSpaceWidth the width of the video space
  54. * @param videoSpaceHeight the height of the video space
  55. * @return an array with 2 elements, the video width and the video height
  56. */
  57. function computeCameraVideoSize( // eslint-disable-line max-params
  58. videoWidth,
  59. videoHeight,
  60. videoSpaceWidth,
  61. videoSpaceHeight,
  62. videoLayoutFit) {
  63. const aspectRatio = videoWidth / videoHeight;
  64. switch (videoLayoutFit) {
  65. case 'height':
  66. return [ videoSpaceHeight * aspectRatio, videoSpaceHeight ];
  67. case 'width':
  68. return [ videoSpaceWidth, videoSpaceWidth / aspectRatio ];
  69. case 'both': {
  70. const videoSpaceRatio = videoSpaceWidth / videoSpaceHeight;
  71. const maxZoomCoefficient = interfaceConfig.MAXIMUM_ZOOMING_COEFFICIENT
  72. || Infinity;
  73. if (videoSpaceRatio === aspectRatio) {
  74. return [ videoSpaceWidth, videoSpaceHeight ];
  75. }
  76. let [ width, height ] = computeCameraVideoSize(
  77. videoWidth,
  78. videoHeight,
  79. videoSpaceWidth,
  80. videoSpaceHeight,
  81. videoSpaceRatio < aspectRatio ? 'height' : 'width');
  82. const maxWidth = videoSpaceWidth * maxZoomCoefficient;
  83. const maxHeight = videoSpaceHeight * maxZoomCoefficient;
  84. if (width > maxWidth) {
  85. width = maxWidth;
  86. height = width / aspectRatio;
  87. } else if (height > maxHeight) {
  88. height = maxHeight;
  89. width = height * aspectRatio;
  90. }
  91. return [ width, height ];
  92. }
  93. default:
  94. return [ videoWidth, videoHeight ];
  95. }
  96. }
  97. /**
  98. * Returns an array of the video horizontal and vertical indents,
  99. * so that if fits its parent.
  100. *
  101. * @return an array with 2 elements, the horizontal indent and the vertical
  102. * indent
  103. */
  104. function getCameraVideoPosition( // eslint-disable-line max-params
  105. videoWidth,
  106. videoHeight,
  107. videoSpaceWidth,
  108. videoSpaceHeight) {
  109. // Parent height isn't completely calculated when we position the video in
  110. // full screen mode and this is why we use the screen height in this case.
  111. // Need to think it further at some point and implement it properly.
  112. if (UIUtil.isFullScreen()) {
  113. // eslint-disable-next-line no-param-reassign
  114. videoSpaceHeight = window.innerHeight;
  115. }
  116. const horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  117. const verticalIndent = (videoSpaceHeight - videoHeight) / 2;
  118. return { horizontalIndent,
  119. verticalIndent };
  120. }
  121. /**
  122. * Returns an array of the video horizontal and vertical indents.
  123. * Centers horizontally and top aligns vertically.
  124. *
  125. * @return an array with 2 elements, the horizontal indent and the vertical
  126. * indent
  127. */
  128. function getDesktopVideoPosition(videoWidth, videoHeight, videoSpaceWidth) {
  129. const horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  130. const verticalIndent = 0;// Top aligned
  131. return { horizontalIndent,
  132. verticalIndent };
  133. }
  134. /**
  135. * Container for user video.
  136. */
  137. export class VideoContainer extends LargeContainer {
  138. // FIXME: With Temasys we have to re-select everytime
  139. /**
  140. *
  141. */
  142. get $video() {
  143. return $('#largeVideo');
  144. }
  145. /**
  146. *
  147. */
  148. get $videoBackground() {
  149. return $('#largeVideoBackground');
  150. }
  151. /**
  152. *
  153. */
  154. get id() {
  155. return this.userId;
  156. }
  157. /**
  158. * Creates new VideoContainer instance.
  159. * @param resizeContainer {Function} function that takes care of the size
  160. * of the video container.
  161. * @param emitter {EventEmitter} the event emitter that will be used by
  162. * this instance.
  163. */
  164. constructor(resizeContainer, emitter) {
  165. super();
  166. this.stream = null;
  167. this.userId = null;
  168. this.videoType = null;
  169. this.localFlipX = true;
  170. this.emitter = emitter;
  171. this.resizeContainer = resizeContainer;
  172. this.isVisible = false;
  173. /**
  174. * Flag indicates whether or not the avatar is currently displayed.
  175. * @type {boolean}
  176. */
  177. this.avatarDisplayed = false;
  178. this.$avatar = $('#dominantSpeaker');
  179. /**
  180. * A jQuery selector of the remote connection message.
  181. * @type {jQuery|HTMLElement}
  182. */
  183. this.$remoteConnectionMessage = $('#remoteConnectionMessage');
  184. this.$remotePresenceMessage = $('#remotePresenceMessage');
  185. /**
  186. * Indicates whether or not the video stream attached to the video
  187. * element has started(which means that there is any image rendered
  188. * even if the video is stalled).
  189. * @type {boolean}
  190. */
  191. this.wasVideoRendered = false;
  192. this.$wrapper = $('#largeVideoWrapper');
  193. /**
  194. * FIXME: currently using parent() because I can't come up with name
  195. * for id. We'll need to probably refactor the HTML related to the large
  196. * video anyway.
  197. */
  198. this.$wrapperParent = this.$wrapper.parent();
  199. this.avatarHeight = $('#dominantSpeakerAvatar').height();
  200. const onPlayingCallback = function(event) {
  201. if (typeof resizeContainer === 'function') {
  202. resizeContainer(event);
  203. }
  204. this.wasVideoRendered = true;
  205. }.bind(this);
  206. // This does not work with Temasys plugin - has to be a property to be
  207. // copied between new <object> elements
  208. // this.$video.on('play', onPlay);
  209. this.$video[0].onplaying = onPlayingCallback;
  210. /**
  211. * A Set of functions to invoke when the video element resizes.
  212. *
  213. * @private
  214. */
  215. this._resizeListeners = new Set();
  216. // As of May 16, 2017, temasys does not support resize events.
  217. this.$video[0].onresize = this._onResize.bind(this);
  218. }
  219. /**
  220. * Adds a function to the known subscribers of video element resize
  221. * events.
  222. *
  223. * @param {Function} callback - The subscriber to notify when the video
  224. * element resizes.
  225. * @returns {void}
  226. */
  227. addResizeListener(callback) {
  228. this._resizeListeners.add(callback);
  229. }
  230. /**
  231. * Enables a filter on the video which indicates that there are some
  232. * problems with the local media connection.
  233. *
  234. * @param {boolean} enable <tt>true</tt> if the filter is to be enabled or
  235. * <tt>false</tt> otherwise.
  236. */
  237. enableLocalConnectionProblemFilter(enable) {
  238. this.$video.toggleClass('videoProblemFilter', enable);
  239. this.$videoBackground.toggleClass('videoProblemFilter', enable);
  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. /* eslint-enable max-params */
  291. if (this.stream && this.isScreenSharing()) {
  292. let availableContainerWidth = containerWidth;
  293. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  294. availableContainerWidth -= Filmstrip.getFilmstripWidth();
  295. }
  296. return getDesktopVideoPosition(width,
  297. height,
  298. availableContainerWidth,
  299. containerHeight);
  300. }
  301. return getCameraVideoPosition(width,
  302. height,
  303. containerWidth,
  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 = $('#dominantSpeakerAvatar');
  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. this._hideVideoBackground();
  346. const [ width, height ]
  347. = this.getVideoSize(containerWidth, containerHeight);
  348. if ((containerWidth > width) || (containerHeight > height)) {
  349. this._showVideoBackground();
  350. const css
  351. = containerWidth > width
  352. ? { width: '100%',
  353. height: 'auto' }
  354. : { width: 'auto',
  355. height: '100%' };
  356. this.$videoBackground.css(css);
  357. }
  358. const { horizontalIndent, verticalIndent }
  359. = this.getVideoPosition(width, height,
  360. containerWidth, containerHeight);
  361. // update avatar position
  362. const top = (containerHeight / 2) - (this.avatarHeight / 4 * 3);
  363. this.$avatar.css('top', top);
  364. this.positionRemoteStatusMessages();
  365. this.$wrapper.animate({
  366. width,
  367. height,
  368. top: verticalIndent,
  369. bottom: verticalIndent,
  370. left: horizontalIndent,
  371. right: horizontalIndent
  372. }, {
  373. queue: false,
  374. duration: animate ? 500 : 0
  375. });
  376. }
  377. /**
  378. * Removes a function from the known subscribers of video element resize
  379. * events.
  380. *
  381. * @param {Function} callback - The callback to remove from known
  382. * subscribers of video resize events.
  383. * @returns {void}
  384. */
  385. removeResizeListener(callback) {
  386. this._resizeListeners.delete(callback);
  387. }
  388. /**
  389. * Update video stream.
  390. * @param {string} userID
  391. * @param {JitsiTrack?} stream new stream
  392. * @param {string} videoType video type
  393. */
  394. setStream(userID, stream, videoType) {
  395. this.userId = userID;
  396. if (this.stream === stream) {
  397. // Handles the use case for the remote participants when the
  398. // videoType is received with delay after turning on/off the
  399. // desktop sharing.
  400. if (this.videoType !== videoType) {
  401. this.videoType = videoType;
  402. this.resizeContainer();
  403. }
  404. return;
  405. }
  406. // The stream has changed, so the image will be lost on detach
  407. this.wasVideoRendered = false;
  408. // detach old stream
  409. if (this.stream) {
  410. this.stream.detach(this.$video[0]);
  411. this.stream.detach(this.$videoBackground[0]);
  412. }
  413. this.stream = stream;
  414. this.videoType = videoType;
  415. if (!stream) {
  416. return;
  417. }
  418. stream.attach(this.$video[0]);
  419. stream.attach(this.$videoBackground[0]);
  420. this._hideVideoBackground();
  421. const flipX = stream.isLocal() && this.localFlipX;
  422. this.$video.css({
  423. transform: flipX ? 'scaleX(-1)' : 'none'
  424. });
  425. this.$videoBackground.css({
  426. transform: flipX ? 'scaleX(-1)' : 'none'
  427. });
  428. // Reset the large video background depending on the stream.
  429. this.setLargeVideoBackground(this.avatarDisplayed);
  430. }
  431. /**
  432. * Changes the flipX state of the local video.
  433. * @param val {boolean} true if flipped.
  434. */
  435. setLocalFlipX(val) {
  436. this.localFlipX = val;
  437. if (!this.$video || !this.stream || !this.stream.isLocal()) {
  438. return;
  439. }
  440. this.$video.css({
  441. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  442. });
  443. this.$videoBackground.css({
  444. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  445. });
  446. }
  447. /**
  448. * Check if current video stream is screen sharing.
  449. * @returns {boolean}
  450. */
  451. isScreenSharing() {
  452. return this.videoType === 'desktop';
  453. }
  454. /**
  455. * Show or hide user avatar.
  456. * @param {boolean} show
  457. */
  458. showAvatar(show) {
  459. // TO FIX: Video background need to be black, so that we don't have a
  460. // flickering effect when scrolling between videos and have the screen
  461. // move to grey before going back to video. Avatars though can have the
  462. // default background set.
  463. // In order to fix this code we need to introduce video background or
  464. // find a workaround for the video flickering.
  465. this.setLargeVideoBackground(show);
  466. this.$avatar.css('visibility', show ? 'visible' : 'hidden');
  467. this.avatarDisplayed = show;
  468. this.emitter.emit(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE, show);
  469. APP.API.notifyLargeVideoVisibilityChanged(show);
  470. }
  471. /**
  472. * Indicates that the remote user who is currently displayed by this video
  473. * container is having connectivity issues.
  474. *
  475. * @param {boolean} show <tt>true</tt> to show or <tt>false</tt> to hide
  476. * the indication.
  477. */
  478. showRemoteConnectionProblemIndicator(show) {
  479. this.$video.toggleClass('remoteVideoProblemFilter', show);
  480. this.$videoBackground.toggleClass('remoteVideoProblemFilter', show);
  481. this.$avatar.toggleClass('remoteVideoProblemFilter', show);
  482. }
  483. /**
  484. * We are doing fadeOut/fadeIn animations on parent div which wraps
  485. * largeVideo, because when Temasys plugin is in use it replaces
  486. * <video> elements with plugin <object> tag. In Safari jQuery is
  487. * unable to store values on this plugin object which breaks all
  488. * animation effects performed on it directly.
  489. */
  490. show() {
  491. // its already visible
  492. if (this.isVisible) {
  493. return Promise.resolve();
  494. }
  495. return new Promise(resolve => {
  496. this.$wrapperParent.css('visibility', 'visible').fadeTo(
  497. FADE_DURATION_MS,
  498. 1,
  499. () => {
  500. this.isVisible = true;
  501. resolve();
  502. }
  503. );
  504. });
  505. }
  506. /**
  507. *
  508. */
  509. hide() {
  510. // as the container is hidden/replaced by another container
  511. // hide its avatar
  512. this.showAvatar(false);
  513. // its already hidden
  514. if (!this.isVisible) {
  515. return Promise.resolve();
  516. }
  517. return new Promise(resolve => {
  518. this.$wrapperParent.fadeTo(FADE_DURATION_MS, 0, () => {
  519. this.$wrapperParent.css('visibility', 'hidden');
  520. this.isVisible = false;
  521. resolve();
  522. });
  523. });
  524. }
  525. /**
  526. * @return {boolean} switch on dominant speaker event if on stage.
  527. */
  528. stayOnStage() {
  529. return false;
  530. }
  531. /**
  532. * Sets the large video container background depending on the container
  533. * type and the parameter indicating if an avatar is currently shown on
  534. * large.
  535. *
  536. * @param {boolean} isAvatar - Indicates if the avatar is currently shown
  537. * on the large video.
  538. * @returns {void}
  539. */
  540. setLargeVideoBackground(isAvatar) {
  541. $('#largeVideoContainer').css('background',
  542. this.videoType === VIDEO_CONTAINER_TYPE && !isAvatar
  543. ? '#000' : interfaceConfig.DEFAULT_BACKGROUND);
  544. }
  545. /**
  546. * Sets the blur background to be invisible and pauses any playing video.
  547. *
  548. * @private
  549. * @returns {void}
  550. */
  551. _hideVideoBackground() {
  552. this.$videoBackground.css({ visibility: 'hidden' });
  553. this.$videoBackground[0].pause();
  554. }
  555. /**
  556. * Callback invoked when the video element changes dimensions.
  557. *
  558. * @private
  559. * @returns {void}
  560. */
  561. _onResize() {
  562. this._resizeListeners.forEach(callback => callback());
  563. }
  564. /**
  565. * Sets the blur background to be visible and starts any loaded video.
  566. *
  567. * @private
  568. * @returns {void}
  569. */
  570. _showVideoBackground() {
  571. this.$videoBackground.css({ visibility: 'visible' });
  572. // XXX HTMLMediaElement.play's Promise may be rejected. Certain
  573. // environments such as Google Chrome and React Native will report the
  574. // rejection as unhandled. And that may appear scary depending on how
  575. // the environment words the report. To reduce the risk of scaring a
  576. // developer, make sure that the rejection is handled. We cannot really
  577. // do anything substantial about the rejection and, more importantly, we
  578. // do not care. Some browsers (at this time, only Edge is known) don't
  579. // return a promise from .play(), so check before trying to catch.
  580. const res = this.$videoBackground[0].play();
  581. if (typeof res !== 'undefined') {
  582. res.catch(reason => logger.error(reason));
  583. }
  584. }
  585. }