Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

VideoContainer.js 19KB

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