Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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