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

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