Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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