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

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