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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. /**
  164. * Indicates whether or not the video stream attached to the video
  165. * element has started(which means that there is any image rendered
  166. * even if the video is stalled).
  167. * @type {boolean}
  168. */
  169. this.wasVideoRendered = false;
  170. this.$wrapper = $('#largeVideoWrapper');
  171. /**
  172. * FIXME: currently using parent() because I can't come up with name
  173. * for id. We'll need to probably refactor the HTML related to the large
  174. * video anyway.
  175. */
  176. this.$wrapperParent = this.$wrapper.parent();
  177. this.avatarHeight = $("#dominantSpeakerAvatar").height();
  178. var onPlayingCallback = function (event) {
  179. if (typeof resizeContainer === 'function') {
  180. resizeContainer(event);
  181. }
  182. this.wasVideoRendered = true;
  183. }.bind(this);
  184. // This does not work with Temasys plugin - has to be a property to be
  185. // copied between new <object> elements
  186. //this.$video.on('play', onPlay);
  187. this.$video[0].onplaying = onPlayingCallback;
  188. /**
  189. * A Set of functions to invoke when the video element resizes.
  190. *
  191. * @private
  192. */
  193. this._resizeListeners = new Set();
  194. // As of May 16, 2017, temasys does not support resize events.
  195. this.$video[0].onresize = this._onResize.bind(this);
  196. }
  197. /**
  198. * Adds a function to the known subscribers of video element resize
  199. * events.
  200. *
  201. * @param {Function} callback - The subscriber to notify when the video
  202. * element resizes.
  203. * @returns {void}
  204. */
  205. addResizeListener(callback) {
  206. this._resizeListeners.add(callback);
  207. }
  208. /**
  209. * Enables a filter on the video which indicates that there are some
  210. * problems with the local media connection.
  211. *
  212. * @param {boolean} enable <tt>true</tt> if the filter is to be enabled or
  213. * <tt>false</tt> otherwise.
  214. */
  215. enableLocalConnectionProblemFilter (enable) {
  216. this.$video.toggleClass("videoProblemFilter", enable);
  217. this.$videoBackground.toggleClass("videoProblemFilter", enable);
  218. }
  219. /**
  220. * Obtains media stream ID of the underlying {@link JitsiTrack}.
  221. * @return {string|null}
  222. */
  223. getStreamID() {
  224. return this.stream ? this.stream.getId() : null;
  225. }
  226. /**
  227. * Get size of video element.
  228. * @returns {{width, height}}
  229. */
  230. getStreamSize () {
  231. let video = this.$video[0];
  232. return {
  233. width: video.videoWidth,
  234. height: video.videoHeight
  235. };
  236. }
  237. /**
  238. * Calculate optimal video size for specified container size.
  239. * @param {number} containerWidth container width
  240. * @param {number} containerHeight container height
  241. * @returns {{availableWidth, availableHeight}}
  242. */
  243. getVideoSize(containerWidth, containerHeight) {
  244. let { width, height } = this.getStreamSize();
  245. if (this.stream && this.isScreenSharing()) {
  246. return computeDesktopVideoSize(width,
  247. height,
  248. containerWidth,
  249. containerHeight);
  250. }
  251. return computeCameraVideoSize(width,
  252. height,
  253. containerWidth,
  254. containerHeight,
  255. interfaceConfig.VIDEO_LAYOUT_FIT);
  256. }
  257. /**
  258. * Calculate optimal video position (offset for top left corner)
  259. * for specified video size and container size.
  260. * @param {number} width video width
  261. * @param {number} height video height
  262. * @param {number} containerWidth container width
  263. * @param {number} containerHeight container height
  264. * @returns {{horizontalIndent, verticalIndent}}
  265. */
  266. getVideoPosition (width, height, containerWidth, containerHeight) {
  267. if (this.stream && this.isScreenSharing()) {
  268. return getDesktopVideoPosition( width,
  269. height,
  270. containerWidth,
  271. containerHeight);
  272. } else {
  273. return getCameraVideoPosition( width,
  274. height,
  275. containerWidth,
  276. containerHeight);
  277. }
  278. }
  279. /**
  280. * Update position of the remote connection message which describes that
  281. * the remote user is having connectivity issues.
  282. */
  283. positionRemoteConnectionMessage () {
  284. if (this.avatarDisplayed) {
  285. let $avatarImage = $("#dominantSpeakerAvatar");
  286. this.$remoteConnectionMessage.css(
  287. 'top',
  288. $avatarImage.offset().top + $avatarImage.height() + 10);
  289. } else {
  290. let height = this.$remoteConnectionMessage.height();
  291. let parentHeight = this.$remoteConnectionMessage.parent().height();
  292. this.$remoteConnectionMessage.css(
  293. 'top', (parentHeight/2) - (height/2));
  294. }
  295. let width = this.$remoteConnectionMessage.width();
  296. let parentWidth = this.$remoteConnectionMessage.parent().width();
  297. this.$remoteConnectionMessage.css(
  298. 'left', ((parentWidth/2) - (width/2)));
  299. }
  300. resize (containerWidth, containerHeight, animate = false) {
  301. // XXX Prevent TypeError: undefined is not an object when the Web
  302. // browser does not support WebRTC (yet).
  303. if (this.$video.length === 0) {
  304. return;
  305. }
  306. this._hideVideoBackground();
  307. let [ width, height ]
  308. = this.getVideoSize(containerWidth, containerHeight);
  309. if ((containerWidth > width) || (containerHeight > height)) {
  310. this._showVideoBackground();
  311. const css = containerWidth > width
  312. ? {width: '100%', height: 'auto'} : {width: 'auto', height: '100%'};
  313. this.$videoBackground.css(css);
  314. }
  315. let { horizontalIndent, verticalIndent }
  316. = this.getVideoPosition(width, height,
  317. containerWidth, containerHeight);
  318. // update avatar position
  319. let top = containerHeight / 2 - this.avatarHeight / 4 * 3;
  320. this.$avatar.css('top', top);
  321. this.positionRemoteConnectionMessage();
  322. this.$wrapper.animate({
  323. width: width,
  324. height: height,
  325. top: verticalIndent,
  326. bottom: verticalIndent,
  327. left: horizontalIndent,
  328. right: horizontalIndent
  329. }, {
  330. queue: false,
  331. duration: animate ? 500 : 0
  332. });
  333. }
  334. /**
  335. * Removes a function from the known subscribers of video element resize
  336. * events.
  337. *
  338. * @param {Function} callback - The callback to remove from known
  339. * subscribers of video resize events.
  340. * @returns {void}
  341. */
  342. removeResizeListener(callback) {
  343. this._resizeListeners.delete(callback);
  344. }
  345. /**
  346. * Update video stream.
  347. * @param {string} userID
  348. * @param {JitsiTrack?} stream new stream
  349. * @param {string} videoType video type
  350. */
  351. setStream (userID, stream, videoType) {
  352. this.userId = userID;
  353. if (this.stream === stream) {
  354. // Handles the use case for the remote participants when the
  355. // videoType is received with delay after turning on/off the
  356. // desktop sharing.
  357. if(this.videoType !== videoType) {
  358. this.videoType = videoType;
  359. this.resizeContainer();
  360. }
  361. return;
  362. } else {
  363. // The stream has changed, so the image will be lost on detach
  364. this.wasVideoRendered = false;
  365. }
  366. // detach old stream
  367. if (this.stream) {
  368. this.stream.detach(this.$video[0]);
  369. this.stream.detach(this.$videoBackground[0]);
  370. }
  371. this.stream = stream;
  372. this.videoType = videoType;
  373. if (!stream) {
  374. return;
  375. }
  376. stream.attach(this.$video[0]);
  377. stream.attach(this.$videoBackground[0]);
  378. this._hideVideoBackground();
  379. const flipX = stream.isLocal() && this.localFlipX;
  380. this.$video.css({
  381. transform: flipX ? 'scaleX(-1)' : 'none'
  382. });
  383. this.$videoBackground.css({
  384. transform: flipX ? 'scaleX(-1)' : 'none'
  385. });
  386. // Reset the large video background depending on the stream.
  387. this.setLargeVideoBackground(this.avatarDisplayed);
  388. }
  389. /**
  390. * Changes the flipX state of the local video.
  391. * @param val {boolean} true if flipped.
  392. */
  393. setLocalFlipX(val) {
  394. this.localFlipX = val;
  395. if(!this.$video || !this.stream || !this.stream.isLocal())
  396. return;
  397. this.$video.css({
  398. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  399. });
  400. this.$videoBackground.css({
  401. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  402. });
  403. }
  404. /**
  405. * Check if current video stream is screen sharing.
  406. * @returns {boolean}
  407. */
  408. isScreenSharing () {
  409. return this.videoType === 'desktop';
  410. }
  411. /**
  412. * Show or hide user avatar.
  413. * @param {boolean} show
  414. */
  415. showAvatar (show) {
  416. // TO FIX: Video background need to be black, so that we don't have a
  417. // flickering effect when scrolling between videos and have the screen
  418. // move to grey before going back to video. Avatars though can have the
  419. // default background set.
  420. // In order to fix this code we need to introduce video background or
  421. // find a workaround for the video flickering.
  422. this.setLargeVideoBackground(show);
  423. this.$avatar.css("visibility", show ? "visible" : "hidden");
  424. this.avatarDisplayed = show;
  425. this.emitter.emit(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE, show);
  426. }
  427. /**
  428. * Indicates that the remote user who is currently displayed by this video
  429. * container is having connectivity issues.
  430. *
  431. * @param {boolean} show <tt>true</tt> to show or <tt>false</tt> to hide
  432. * the indication.
  433. */
  434. showRemoteConnectionProblemIndicator (show) {
  435. this.$video.toggleClass("remoteVideoProblemFilter", show);
  436. this.$videoBackground.toggleClass("remoteVideoProblemFilter", show);
  437. this.$avatar.toggleClass("remoteVideoProblemFilter", show);
  438. }
  439. // We are doing fadeOut/fadeIn animations on parent div which wraps
  440. // largeVideo, because when Temasys plugin is in use it replaces
  441. // <video> elements with plugin <object> tag. In Safari jQuery is
  442. // unable to store values on this plugin object which breaks all
  443. // animation effects performed on it directly.
  444. show () {
  445. // its already visible
  446. if (this.isVisible) {
  447. return Promise.resolve();
  448. }
  449. return new Promise((resolve) => {
  450. this.$wrapperParent.css('visibility', 'visible').fadeTo(
  451. FADE_DURATION_MS,
  452. 1,
  453. () => {
  454. this.isVisible = true;
  455. resolve();
  456. }
  457. );
  458. });
  459. }
  460. hide () {
  461. // as the container is hidden/replaced by another container
  462. // hide its avatar
  463. this.showAvatar(false);
  464. // its already hidden
  465. if (!this.isVisible) {
  466. return Promise.resolve();
  467. }
  468. return new Promise((resolve) => {
  469. this.$wrapperParent.fadeTo(FADE_DURATION_MS, 0, () => {
  470. this.$wrapperParent.css('visibility', 'hidden');
  471. this.isVisible = false;
  472. resolve();
  473. });
  474. });
  475. }
  476. /**
  477. * @return {boolean} switch on dominant speaker event if on stage.
  478. */
  479. stayOnStage () {
  480. return false;
  481. }
  482. /**
  483. * Sets the large video container background depending on the container
  484. * type and the parameter indicating if an avatar is currently shown on
  485. * large.
  486. *
  487. * @param {boolean} isAvatar - Indicates if the avatar is currently shown
  488. * on the large video.
  489. * @returns {void}
  490. */
  491. setLargeVideoBackground (isAvatar) {
  492. $("#largeVideoContainer").css("background",
  493. (this.videoType === VIDEO_CONTAINER_TYPE && !isAvatar)
  494. ? "#000" : interfaceConfig.DEFAULT_BACKGROUND);
  495. }
  496. /**
  497. * Sets the blur background to be invisible and pauses any playing video.
  498. *
  499. * @private
  500. * @returns {void}
  501. */
  502. _hideVideoBackground() {
  503. this.$videoBackground.css({ visibility: 'hidden' });
  504. this.$videoBackground[0].pause();
  505. }
  506. /**
  507. * Callback invoked when the video element changes dimensions.
  508. *
  509. * @private
  510. * @returns {void}
  511. */
  512. _onResize() {
  513. this._resizeListeners.forEach(callback => callback());
  514. }
  515. /**
  516. * Sets the blur background to be visible and starts any loaded video.
  517. *
  518. * @private
  519. * @returns {void}
  520. */
  521. _showVideoBackground() {
  522. this.$videoBackground.css({ visibility: 'visible' });
  523. this.$videoBackground[0].play();
  524. }
  525. }