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 20KB

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