Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VideoContainer.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. /**
  10. * Returns an array of the video dimensions, so that it keeps it's aspect
  11. * ratio and fits available area with it's larger dimension. This method
  12. * ensures that whole video will be visible and can leave empty areas.
  13. *
  14. * @param videoWidth the width of the video to position
  15. * @param videoHeight the height of the video to position
  16. * @param videoSpaceWidth the width of the available space
  17. * @param videoSpaceHeight the height of the available space
  18. * @return an array with 2 elements, the video width and the video height
  19. */
  20. function computeDesktopVideoSize(videoWidth,
  21. videoHeight,
  22. videoSpaceWidth,
  23. videoSpaceHeight) {
  24. let aspectRatio = videoWidth / videoHeight;
  25. let availableWidth = Math.max(videoWidth, videoSpaceWidth);
  26. let availableHeight = Math.max(videoHeight, videoSpaceHeight);
  27. videoSpaceHeight -= Filmstrip.getFilmstripHeight();
  28. if (availableWidth / aspectRatio >= videoSpaceHeight) {
  29. availableHeight = videoSpaceHeight;
  30. availableWidth = availableHeight * aspectRatio;
  31. }
  32. if (availableHeight * aspectRatio >= videoSpaceWidth) {
  33. availableWidth = videoSpaceWidth;
  34. availableHeight = availableWidth / aspectRatio;
  35. }
  36. return [ availableWidth, availableHeight ];
  37. }
  38. /**
  39. * Returns an array of the video dimensions. It respects the
  40. * VIDEO_LAYOUT_FIT config, to fit the video to the screen, by hiding some parts
  41. * of it, or to fit it to the height or width.
  42. *
  43. * @param videoWidth the original video width
  44. * @param videoHeight the original video height
  45. * @param videoSpaceWidth the width of the video space
  46. * @param videoSpaceHeight the height of the video space
  47. * @return an array with 2 elements, the video width and the video height
  48. */
  49. function computeCameraVideoSize(videoWidth,
  50. videoHeight,
  51. videoSpaceWidth,
  52. videoSpaceHeight,
  53. videoLayoutFit) {
  54. const aspectRatio = videoWidth / videoHeight;
  55. switch (videoLayoutFit) {
  56. case 'height':
  57. return [ videoSpaceHeight * aspectRatio, videoSpaceHeight ];
  58. case 'width':
  59. return [ videoSpaceWidth, videoSpaceWidth / aspectRatio ];
  60. case 'both': {
  61. const videoSpaceRatio = videoSpaceWidth / videoSpaceHeight;
  62. const maxZoomCoefficient = interfaceConfig.MAXIMUM_ZOOMING_COEFFICIENT
  63. || Infinity;
  64. if (videoSpaceRatio === aspectRatio) {
  65. return [videoSpaceWidth, videoSpaceHeight];
  66. }
  67. let [ width, height] = computeCameraVideoSize(
  68. videoWidth,
  69. videoHeight,
  70. videoSpaceWidth,
  71. videoSpaceHeight,
  72. videoSpaceRatio < aspectRatio ? 'height' : 'width');
  73. const maxWidth = videoSpaceWidth * maxZoomCoefficient;
  74. const maxHeight = videoSpaceHeight * maxZoomCoefficient;
  75. if (width > maxWidth) {
  76. width = maxWidth;
  77. height = width / aspectRatio;
  78. } else if (height > maxHeight) {
  79. height = maxHeight;
  80. width = height * aspectRatio;
  81. }
  82. return [width, height];
  83. }
  84. default:
  85. return [ videoWidth, videoHeight ];
  86. }
  87. }
  88. /**
  89. * Returns an array of the video horizontal and vertical indents,
  90. * so that if fits its parent.
  91. *
  92. * @return an array with 2 elements, the horizontal indent and the vertical
  93. * indent
  94. */
  95. function getCameraVideoPosition(videoWidth,
  96. videoHeight,
  97. videoSpaceWidth,
  98. videoSpaceHeight) {
  99. // Parent height isn't completely calculated when we position the video in
  100. // full screen mode and this is why we use the screen height in this case.
  101. // Need to think it further at some point and implement it properly.
  102. if (UIUtil.isFullScreen()) {
  103. videoSpaceHeight = window.innerHeight;
  104. }
  105. let horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  106. let verticalIndent = (videoSpaceHeight - videoHeight) / 2;
  107. return { horizontalIndent, verticalIndent };
  108. }
  109. /**
  110. * Returns an array of the video horizontal and vertical indents.
  111. * Centers horizontally and top aligns vertically.
  112. *
  113. * @return an array with 2 elements, the horizontal indent and the vertical
  114. * indent
  115. */
  116. function getDesktopVideoPosition(videoWidth, videoHeight, videoSpaceWidth) {
  117. let horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  118. let verticalIndent = 0;// Top aligned
  119. return { horizontalIndent, verticalIndent };
  120. }
  121. /**
  122. * Container for user video.
  123. */
  124. export class VideoContainer extends LargeContainer {
  125. // FIXME: With Temasys we have to re-select everytime
  126. get $video () {
  127. return $('#largeVideo');
  128. }
  129. get $videoBackground() {
  130. return $('#largeVideoBackground');
  131. }
  132. get id () {
  133. return this.userId;
  134. }
  135. /**
  136. * Creates new VideoContainer instance.
  137. * @param resizeContainer {Function} function that takes care of the size
  138. * of the video container.
  139. * @param emitter {EventEmitter} the event emitter that will be used by
  140. * this instance.
  141. */
  142. constructor (resizeContainer, emitter) {
  143. super();
  144. this.stream = null;
  145. this.userId = null;
  146. this.videoType = null;
  147. this.localFlipX = true;
  148. this.emitter = emitter;
  149. this.resizeContainer = resizeContainer;
  150. this.isVisible = false;
  151. /**
  152. * Flag indicates whether or not the avatar is currently displayed.
  153. * @type {boolean}
  154. */
  155. this.avatarDisplayed = false;
  156. this.$avatar = $('#dominantSpeaker');
  157. /**
  158. * A jQuery selector of the remote connection message.
  159. * @type {jQuery|HTMLElement}
  160. */
  161. this.$remoteConnectionMessage = $('#remoteConnectionMessage');
  162. this.$remotePresenceMessage = $('#remotePresenceMessage');
  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. * Updates the positioning of the remote connection presence message and the
  281. * connection status message which escribes that the remote user is having
  282. * connectivity issues.
  283. *
  284. * @returns {void}
  285. */
  286. positionRemoteStatusMessages() {
  287. this._positionParticipantStatus(this.$remoteConnectionMessage);
  288. this._positionParticipantStatus(this.$remotePresenceMessage);
  289. }
  290. /**
  291. * Modifies the position of the passed in jQuery object so it displays
  292. * in the middle of the video container or below the avatar.
  293. *
  294. * @private
  295. * @returns {void}
  296. */
  297. _positionParticipantStatus($element) {
  298. if (this.avatarDisplayed) {
  299. let $avatarImage = $('#dominantSpeakerAvatar');
  300. $element.css(
  301. 'top',
  302. $avatarImage.offset().top + $avatarImage.height() + 10);
  303. } else {
  304. let height = $element.height();
  305. let parentHeight = $element.parent().height();
  306. $element.css('top', (parentHeight/2) - (height/2));
  307. }
  308. }
  309. resize (containerWidth, containerHeight, animate = false) {
  310. // XXX Prevent TypeError: undefined is not an object when the Web
  311. // browser does not support WebRTC (yet).
  312. if (this.$video.length === 0) {
  313. return;
  314. }
  315. this._hideVideoBackground();
  316. let [ width, height ]
  317. = this.getVideoSize(containerWidth, containerHeight);
  318. if ((containerWidth > width) || (containerHeight > height)) {
  319. this._showVideoBackground();
  320. const css
  321. = containerWidth > width
  322. ? { width: '100%', height: 'auto' }
  323. : { width: 'auto', height: '100%' };
  324. this.$videoBackground.css(css);
  325. }
  326. let { horizontalIndent, verticalIndent }
  327. = this.getVideoPosition(width, height,
  328. containerWidth, containerHeight);
  329. // update avatar position
  330. let top = containerHeight / 2 - this.avatarHeight / 4 * 3;
  331. this.$avatar.css('top', top);
  332. this.positionRemoteStatusMessages();
  333. this.$wrapper.animate({
  334. width: width,
  335. height: height,
  336. top: verticalIndent,
  337. bottom: verticalIndent,
  338. left: horizontalIndent,
  339. right: horizontalIndent
  340. }, {
  341. queue: false,
  342. duration: animate ? 500 : 0
  343. });
  344. }
  345. /**
  346. * Removes a function from the known subscribers of video element resize
  347. * events.
  348. *
  349. * @param {Function} callback - The callback to remove from known
  350. * subscribers of video resize events.
  351. * @returns {void}
  352. */
  353. removeResizeListener(callback) {
  354. this._resizeListeners.delete(callback);
  355. }
  356. /**
  357. * Update video stream.
  358. * @param {string} userID
  359. * @param {JitsiTrack?} stream new stream
  360. * @param {string} videoType video type
  361. */
  362. setStream (userID, stream, videoType) {
  363. this.userId = userID;
  364. if (this.stream === stream) {
  365. // Handles the use case for the remote participants when the
  366. // videoType is received with delay after turning on/off the
  367. // desktop sharing.
  368. if(this.videoType !== videoType) {
  369. this.videoType = videoType;
  370. this.resizeContainer();
  371. }
  372. return;
  373. } else {
  374. // The stream has changed, so the image will be lost on detach
  375. this.wasVideoRendered = false;
  376. }
  377. // detach old stream
  378. if (this.stream) {
  379. this.stream.detach(this.$video[0]);
  380. this.stream.detach(this.$videoBackground[0]);
  381. }
  382. this.stream = stream;
  383. this.videoType = videoType;
  384. if (!stream) {
  385. return;
  386. }
  387. stream.attach(this.$video[0]);
  388. stream.attach(this.$videoBackground[0]);
  389. this._hideVideoBackground();
  390. const flipX = stream.isLocal() && this.localFlipX;
  391. this.$video.css({
  392. transform: flipX ? 'scaleX(-1)' : 'none'
  393. });
  394. this.$videoBackground.css({
  395. transform: flipX ? 'scaleX(-1)' : 'none'
  396. });
  397. // Reset the large video background depending on the stream.
  398. this.setLargeVideoBackground(this.avatarDisplayed);
  399. }
  400. /**
  401. * Changes the flipX state of the local video.
  402. * @param val {boolean} true if flipped.
  403. */
  404. setLocalFlipX(val) {
  405. this.localFlipX = val;
  406. if(!this.$video || !this.stream || !this.stream.isLocal())
  407. return;
  408. this.$video.css({
  409. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  410. });
  411. this.$videoBackground.css({
  412. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  413. });
  414. }
  415. /**
  416. * Check if current video stream is screen sharing.
  417. * @returns {boolean}
  418. */
  419. isScreenSharing () {
  420. return this.videoType === 'desktop';
  421. }
  422. /**
  423. * Show or hide user avatar.
  424. * @param {boolean} show
  425. */
  426. showAvatar (show) {
  427. // TO FIX: Video background need to be black, so that we don't have a
  428. // flickering effect when scrolling between videos and have the screen
  429. // move to grey before going back to video. Avatars though can have the
  430. // default background set.
  431. // In order to fix this code we need to introduce video background or
  432. // find a workaround for the video flickering.
  433. this.setLargeVideoBackground(show);
  434. this.$avatar.css('visibility', show ? 'visible' : 'hidden');
  435. this.avatarDisplayed = show;
  436. this.emitter.emit(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE, show);
  437. }
  438. /**
  439. * Indicates that the remote user who is currently displayed by this video
  440. * container is having connectivity issues.
  441. *
  442. * @param {boolean} show <tt>true</tt> to show or <tt>false</tt> to hide
  443. * the indication.
  444. */
  445. showRemoteConnectionProblemIndicator (show) {
  446. this.$video.toggleClass('remoteVideoProblemFilter', show);
  447. this.$videoBackground.toggleClass('remoteVideoProblemFilter', show);
  448. this.$avatar.toggleClass('remoteVideoProblemFilter', show);
  449. }
  450. // We are doing fadeOut/fadeIn animations on parent div which wraps
  451. // largeVideo, because when Temasys plugin is in use it replaces
  452. // <video> elements with plugin <object> tag. In Safari jQuery is
  453. // unable to store values on this plugin object which breaks all
  454. // animation effects performed on it directly.
  455. show () {
  456. // its already visible
  457. if (this.isVisible) {
  458. return Promise.resolve();
  459. }
  460. return new Promise((resolve) => {
  461. this.$wrapperParent.css('visibility', 'visible').fadeTo(
  462. FADE_DURATION_MS,
  463. 1,
  464. () => {
  465. this.isVisible = true;
  466. resolve();
  467. }
  468. );
  469. });
  470. }
  471. hide () {
  472. // as the container is hidden/replaced by another container
  473. // hide its avatar
  474. this.showAvatar(false);
  475. // its already hidden
  476. if (!this.isVisible) {
  477. return Promise.resolve();
  478. }
  479. return new Promise((resolve) => {
  480. this.$wrapperParent.fadeTo(FADE_DURATION_MS, 0, () => {
  481. this.$wrapperParent.css('visibility', 'hidden');
  482. this.isVisible = false;
  483. resolve();
  484. });
  485. });
  486. }
  487. /**
  488. * @return {boolean} switch on dominant speaker event if on stage.
  489. */
  490. stayOnStage () {
  491. return false;
  492. }
  493. /**
  494. * Sets the large video container background depending on the container
  495. * type and the parameter indicating if an avatar is currently shown on
  496. * large.
  497. *
  498. * @param {boolean} isAvatar - Indicates if the avatar is currently shown
  499. * on the large video.
  500. * @returns {void}
  501. */
  502. setLargeVideoBackground (isAvatar) {
  503. $('#largeVideoContainer').css('background',
  504. (this.videoType === VIDEO_CONTAINER_TYPE && !isAvatar)
  505. ? '#000' : interfaceConfig.DEFAULT_BACKGROUND);
  506. }
  507. /**
  508. * Sets the blur background to be invisible and pauses any playing video.
  509. *
  510. * @private
  511. * @returns {void}
  512. */
  513. _hideVideoBackground() {
  514. this.$videoBackground.css({ visibility: 'hidden' });
  515. this.$videoBackground[0].pause();
  516. }
  517. /**
  518. * Callback invoked when the video element changes dimensions.
  519. *
  520. * @private
  521. * @returns {void}
  522. */
  523. _onResize() {
  524. this._resizeListeners.forEach(callback => callback());
  525. }
  526. /**
  527. * Sets the blur background to be visible and starts any loaded video.
  528. *
  529. * @private
  530. * @returns {void}
  531. */
  532. _showVideoBackground() {
  533. this.$videoBackground.css({ visibility: 'visible' });
  534. this.$videoBackground[0].play();
  535. }
  536. }