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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /* global $, interfaceConfig */
  2. /* jshint -W101 */
  3. import Filmstrip from './Filmstrip';
  4. import LargeContainer from './LargeContainer';
  5. import UIEvents from "../../../service/UI/UIEvents";
  6. import UIUtil from "../util/UIUtil";
  7. // FIXME should be 'video'
  8. export const VIDEO_CONTAINER_TYPE = "camera";
  9. const FADE_DURATION_MS = 300;
  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 = containerWidth > width
  322. ? {width: '100%', height: 'auto'} : {width: 'auto', height: '100%'};
  323. this.$videoBackground.css(css);
  324. }
  325. let { horizontalIndent, verticalIndent }
  326. = this.getVideoPosition(width, height,
  327. containerWidth, containerHeight);
  328. // update avatar position
  329. let top = containerHeight / 2 - this.avatarHeight / 4 * 3;
  330. this.$avatar.css('top', top);
  331. this.positionRemoteStatusMessages();
  332. this.$wrapper.animate({
  333. width: width,
  334. height: height,
  335. top: verticalIndent,
  336. bottom: verticalIndent,
  337. left: horizontalIndent,
  338. right: horizontalIndent
  339. }, {
  340. queue: false,
  341. duration: animate ? 500 : 0
  342. });
  343. }
  344. /**
  345. * Removes a function from the known subscribers of video element resize
  346. * events.
  347. *
  348. * @param {Function} callback - The callback to remove from known
  349. * subscribers of video resize events.
  350. * @returns {void}
  351. */
  352. removeResizeListener(callback) {
  353. this._resizeListeners.delete(callback);
  354. }
  355. /**
  356. * Update video stream.
  357. * @param {string} userID
  358. * @param {JitsiTrack?} stream new stream
  359. * @param {string} videoType video type
  360. */
  361. setStream (userID, stream, videoType) {
  362. this.userId = userID;
  363. if (this.stream === stream) {
  364. // Handles the use case for the remote participants when the
  365. // videoType is received with delay after turning on/off the
  366. // desktop sharing.
  367. if(this.videoType !== videoType) {
  368. this.videoType = videoType;
  369. this.resizeContainer();
  370. }
  371. return;
  372. } else {
  373. // The stream has changed, so the image will be lost on detach
  374. this.wasVideoRendered = false;
  375. }
  376. // detach old stream
  377. if (this.stream) {
  378. this.stream.detach(this.$video[0]);
  379. this.stream.detach(this.$videoBackground[0]);
  380. }
  381. this.stream = stream;
  382. this.videoType = videoType;
  383. if (!stream) {
  384. return;
  385. }
  386. stream.attach(this.$video[0]);
  387. stream.attach(this.$videoBackground[0]);
  388. this._hideVideoBackground();
  389. const flipX = stream.isLocal() && this.localFlipX;
  390. this.$video.css({
  391. transform: flipX ? 'scaleX(-1)' : 'none'
  392. });
  393. this.$videoBackground.css({
  394. transform: flipX ? 'scaleX(-1)' : 'none'
  395. });
  396. // Reset the large video background depending on the stream.
  397. this.setLargeVideoBackground(this.avatarDisplayed);
  398. }
  399. /**
  400. * Changes the flipX state of the local video.
  401. * @param val {boolean} true if flipped.
  402. */
  403. setLocalFlipX(val) {
  404. this.localFlipX = val;
  405. if(!this.$video || !this.stream || !this.stream.isLocal())
  406. return;
  407. this.$video.css({
  408. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  409. });
  410. this.$videoBackground.css({
  411. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  412. });
  413. }
  414. /**
  415. * Check if current video stream is screen sharing.
  416. * @returns {boolean}
  417. */
  418. isScreenSharing () {
  419. return this.videoType === 'desktop';
  420. }
  421. /**
  422. * Show or hide user avatar.
  423. * @param {boolean} show
  424. */
  425. showAvatar (show) {
  426. // TO FIX: Video background need to be black, so that we don't have a
  427. // flickering effect when scrolling between videos and have the screen
  428. // move to grey before going back to video. Avatars though can have the
  429. // default background set.
  430. // In order to fix this code we need to introduce video background or
  431. // find a workaround for the video flickering.
  432. this.setLargeVideoBackground(show);
  433. this.$avatar.css("visibility", show ? "visible" : "hidden");
  434. this.avatarDisplayed = show;
  435. this.emitter.emit(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE, show);
  436. }
  437. /**
  438. * Indicates that the remote user who is currently displayed by this video
  439. * container is having connectivity issues.
  440. *
  441. * @param {boolean} show <tt>true</tt> to show or <tt>false</tt> to hide
  442. * the indication.
  443. */
  444. showRemoteConnectionProblemIndicator (show) {
  445. this.$video.toggleClass("remoteVideoProblemFilter", show);
  446. this.$videoBackground.toggleClass("remoteVideoProblemFilter", show);
  447. this.$avatar.toggleClass("remoteVideoProblemFilter", show);
  448. }
  449. // We are doing fadeOut/fadeIn animations on parent div which wraps
  450. // largeVideo, because when Temasys plugin is in use it replaces
  451. // <video> elements with plugin <object> tag. In Safari jQuery is
  452. // unable to store values on this plugin object which breaks all
  453. // animation effects performed on it directly.
  454. show () {
  455. // its already visible
  456. if (this.isVisible) {
  457. return Promise.resolve();
  458. }
  459. return new Promise((resolve) => {
  460. this.$wrapperParent.css('visibility', 'visible').fadeTo(
  461. FADE_DURATION_MS,
  462. 1,
  463. () => {
  464. this.isVisible = true;
  465. resolve();
  466. }
  467. );
  468. });
  469. }
  470. hide () {
  471. // as the container is hidden/replaced by another container
  472. // hide its avatar
  473. this.showAvatar(false);
  474. // its already hidden
  475. if (!this.isVisible) {
  476. return Promise.resolve();
  477. }
  478. return new Promise((resolve) => {
  479. this.$wrapperParent.fadeTo(FADE_DURATION_MS, 0, () => {
  480. this.$wrapperParent.css('visibility', 'hidden');
  481. this.isVisible = false;
  482. resolve();
  483. });
  484. });
  485. }
  486. /**
  487. * @return {boolean} switch on dominant speaker event if on stage.
  488. */
  489. stayOnStage () {
  490. return false;
  491. }
  492. /**
  493. * Sets the large video container background depending on the container
  494. * type and the parameter indicating if an avatar is currently shown on
  495. * large.
  496. *
  497. * @param {boolean} isAvatar - Indicates if the avatar is currently shown
  498. * on the large video.
  499. * @returns {void}
  500. */
  501. setLargeVideoBackground (isAvatar) {
  502. $("#largeVideoContainer").css("background",
  503. (this.videoType === VIDEO_CONTAINER_TYPE && !isAvatar)
  504. ? "#000" : interfaceConfig.DEFAULT_BACKGROUND);
  505. }
  506. /**
  507. * Sets the blur background to be invisible and pauses any playing video.
  508. *
  509. * @private
  510. * @returns {void}
  511. */
  512. _hideVideoBackground() {
  513. this.$videoBackground.css({ visibility: 'hidden' });
  514. this.$videoBackground[0].pause();
  515. }
  516. /**
  517. * Callback invoked when the video element changes dimensions.
  518. *
  519. * @private
  520. * @returns {void}
  521. */
  522. _onResize() {
  523. this._resizeListeners.forEach(callback => callback());
  524. }
  525. /**
  526. * Sets the blur background to be visible and starts any loaded video.
  527. *
  528. * @private
  529. * @returns {void}
  530. */
  531. _showVideoBackground() {
  532. this.$videoBackground.css({ visibility: 'visible' });
  533. this.$videoBackground[0].play();
  534. }
  535. }