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

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