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.

LargeVideo.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /* global $, APP, interfaceConfig */
  2. /* jshint -W101 */
  3. import UIUtil from "../util/UIUtil";
  4. import UIEvents from "../../../service/UI/UIEvents";
  5. import LargeContainer from './LargeContainer';
  6. import FilmStrip from './FilmStrip';
  7. import Avatar from "../avatar/Avatar";
  8. import {createDeferred} from '../../util/helpers';
  9. const avatarSize = interfaceConfig.DOMINANT_SPEAKER_AVATAR_SIZE;
  10. const FADE_DURATION_MS = 300;
  11. export const VIDEO_CONTAINER_TYPE = "camera";
  12. /**
  13. * Get stream id.
  14. * @param {JitsiTrack?} stream
  15. */
  16. function getStreamOwnerId(stream) {
  17. if (!stream) {
  18. return;
  19. }
  20. if (stream.isLocal()) { // local stream doesn't have method "getParticipantId"
  21. return APP.conference.localId;
  22. } else {
  23. return stream.getParticipantId();
  24. }
  25. }
  26. /**
  27. * Returns an array of the video dimensions, so that it keeps it's aspect
  28. * ratio and fits available area with it's larger dimension. This method
  29. * ensures that whole video will be visible and can leave empty areas.
  30. *
  31. * @return an array with 2 elements, the video width and the video height
  32. */
  33. function getDesktopVideoSize(videoWidth,
  34. videoHeight,
  35. videoSpaceWidth,
  36. videoSpaceHeight) {
  37. let aspectRatio = videoWidth / videoHeight;
  38. let availableWidth = Math.max(videoWidth, videoSpaceWidth);
  39. let availableHeight = Math.max(videoHeight, videoSpaceHeight);
  40. videoSpaceHeight -= FilmStrip.getFilmStripHeight();
  41. if (availableWidth / aspectRatio >= videoSpaceHeight) {
  42. availableHeight = videoSpaceHeight;
  43. availableWidth = availableHeight * aspectRatio;
  44. }
  45. if (availableHeight * aspectRatio >= videoSpaceWidth) {
  46. availableWidth = videoSpaceWidth;
  47. availableHeight = availableWidth / aspectRatio;
  48. }
  49. return [ availableWidth, availableHeight ];
  50. }
  51. /**
  52. * Returns an array of the video dimensions. It respects the
  53. * VIDEO_LAYOUT_FIT config, to fit the video to the screen, by hiding some parts
  54. * of it, or to fit it to the height or width.
  55. *
  56. * @param videoWidth the original video width
  57. * @param videoHeight the original video height
  58. * @param videoSpaceWidth the width of the video space
  59. * @param videoSpaceHeight the height of the video space
  60. * @return an array with 2 elements, the video width and the video height
  61. */
  62. function getCameraVideoSize(videoWidth,
  63. videoHeight,
  64. videoSpaceWidth,
  65. videoSpaceHeight) {
  66. let aspectRatio = videoWidth / videoHeight;
  67. let availableWidth = videoWidth;
  68. let availableHeight = videoHeight;
  69. if (interfaceConfig.VIDEO_LAYOUT_FIT == 'height') {
  70. availableHeight = videoSpaceHeight;
  71. availableWidth = availableHeight*aspectRatio;
  72. }
  73. else if (interfaceConfig.VIDEO_LAYOUT_FIT == 'width') {
  74. availableWidth = videoSpaceWidth;
  75. availableHeight = availableWidth/aspectRatio;
  76. }
  77. else if (interfaceConfig.VIDEO_LAYOUT_FIT == 'both') {
  78. availableWidth = Math.max(videoWidth, videoSpaceWidth);
  79. availableHeight = Math.max(videoHeight, videoSpaceHeight);
  80. if (availableWidth / aspectRatio < videoSpaceHeight) {
  81. availableHeight = videoSpaceHeight;
  82. availableWidth = availableHeight * aspectRatio;
  83. }
  84. if (availableHeight * aspectRatio < videoSpaceWidth) {
  85. availableWidth = videoSpaceWidth;
  86. availableHeight = availableWidth / aspectRatio;
  87. }
  88. }
  89. return [ availableWidth, availableHeight ];
  90. }
  91. /**
  92. * Returns an array of the video horizontal and vertical indents,
  93. * so that if fits its parent.
  94. *
  95. * @return an array with 2 elements, the horizontal indent and the vertical
  96. * indent
  97. */
  98. function getCameraVideoPosition(videoWidth,
  99. videoHeight,
  100. videoSpaceWidth,
  101. videoSpaceHeight) {
  102. // Parent height isn't completely calculated when we position the video in
  103. // full screen mode and this is why we use the screen height in this case.
  104. // Need to think it further at some point and implement it properly.
  105. if (UIUtil.isFullScreen()) {
  106. videoSpaceHeight = window.innerHeight;
  107. }
  108. let horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  109. let verticalIndent = (videoSpaceHeight - videoHeight) / 2;
  110. return { horizontalIndent, verticalIndent };
  111. }
  112. /**
  113. * Returns an array of the video horizontal and vertical indents.
  114. * Centers horizontally and top aligns vertically.
  115. *
  116. * @return an array with 2 elements, the horizontal indent and the vertical
  117. * indent
  118. */
  119. function getDesktopVideoPosition(videoWidth,
  120. videoHeight,
  121. videoSpaceWidth,
  122. videoSpaceHeight) {
  123. let horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  124. let verticalIndent = 0;// Top aligned
  125. return { horizontalIndent, verticalIndent };
  126. }
  127. /**
  128. * Container for user video.
  129. */
  130. class VideoContainer extends LargeContainer {
  131. // FIXME: With Temasys we have to re-select everytime
  132. get $video () {
  133. return $('#largeVideo');
  134. }
  135. get id () {
  136. return getStreamOwnerId(this.stream);
  137. }
  138. constructor (onPlay) {
  139. super();
  140. this.stream = null;
  141. this.videoType = null;
  142. this.isVisible = false;
  143. this.$avatar = $('#dominantSpeaker');
  144. this.$wrapper = $('#largeVideoWrapper');
  145. // This does not work with Temasys plugin - has to be a property to be
  146. // copied between new <object> elements
  147. //this.$video.on('play', onPlay);
  148. this.$video[0].onplay = onPlay;
  149. }
  150. /**
  151. * Get size of video element.
  152. * @returns {{width, height}}
  153. */
  154. getStreamSize () {
  155. let video = this.$video[0];
  156. return {
  157. width: video.videoWidth,
  158. height: video.videoHeight
  159. };
  160. }
  161. /**
  162. * Calculate optimal video size for specified container size.
  163. * @param {number} containerWidth container width
  164. * @param {number} containerHeight container height
  165. * @returns {{availableWidth, availableHeight}}
  166. */
  167. getVideoSize (containerWidth, containerHeight) {
  168. let { width, height } = this.getStreamSize();
  169. if (this.stream && this.isScreenSharing()) {
  170. return getDesktopVideoSize( width,
  171. height,
  172. containerWidth,
  173. containerHeight);
  174. } else {
  175. return getCameraVideoSize( width,
  176. height,
  177. containerWidth,
  178. containerHeight);
  179. }
  180. }
  181. /**
  182. * Calculate optimal video position (offset for top left corner)
  183. * for specified video size and container size.
  184. * @param {number} width video width
  185. * @param {number} height video height
  186. * @param {number} containerWidth container width
  187. * @param {number} containerHeight container height
  188. * @returns {{horizontalIndent, verticalIndent}}
  189. */
  190. getVideoPosition (width, height, containerWidth, containerHeight) {
  191. if (this.stream && this.isScreenSharing()) {
  192. return getDesktopVideoPosition( width,
  193. height,
  194. containerWidth,
  195. containerHeight);
  196. } else {
  197. return getCameraVideoPosition( width,
  198. height,
  199. containerWidth,
  200. containerHeight);
  201. }
  202. }
  203. resize (containerWidth, containerHeight, animate = false) {
  204. let [width, height]
  205. = this.getVideoSize(containerWidth, containerHeight);
  206. let { horizontalIndent, verticalIndent }
  207. = this.getVideoPosition(width, height,
  208. containerWidth, containerHeight);
  209. // update avatar position
  210. let top = containerHeight / 2 - avatarSize / 4 * 3;
  211. this.$avatar.css('top', top);
  212. this.$wrapper.animate({
  213. width: width,
  214. height: height,
  215. top: verticalIndent,
  216. bottom: verticalIndent,
  217. left: horizontalIndent,
  218. right: horizontalIndent
  219. }, {
  220. queue: false,
  221. duration: animate ? 500 : 0
  222. });
  223. }
  224. /**
  225. * Update video stream.
  226. * @param {JitsiTrack?} stream new stream
  227. * @param {string} videoType video type
  228. */
  229. setStream (stream, videoType) {
  230. // detach old stream
  231. if (this.stream) {
  232. this.stream.detach(this.$video[0]);
  233. }
  234. this.stream = stream;
  235. this.videoType = videoType;
  236. if (!stream) {
  237. return;
  238. }
  239. stream.attach(this.$video[0]);
  240. let flipX = stream.isLocal() && !this.isScreenSharing();
  241. this.$video.css({
  242. transform: flipX ? 'scaleX(-1)' : 'none'
  243. });
  244. }
  245. /**
  246. * Check if current video stream is screen sharing.
  247. * @returns {boolean}
  248. */
  249. isScreenSharing () {
  250. return this.videoType === 'desktop';
  251. }
  252. /**
  253. * Show or hide user avatar.
  254. * @param {boolean} show
  255. */
  256. showAvatar (show) {
  257. this.$avatar.css("visibility", show ? "visible" : "hidden");
  258. }
  259. // We are doing fadeOut/fadeIn animations on parent div which wraps
  260. // largeVideo, because when Temasys plugin is in use it replaces
  261. // <video> elements with plugin <object> tag. In Safari jQuery is
  262. // unable to store values on this plugin object which breaks all
  263. // animation effects performed on it directly.
  264. show () {
  265. // its already visible
  266. if (this.isVisible) {
  267. return Promise.resolve();
  268. }
  269. let $wrapper = this.$wrapper;
  270. return new Promise((resolve) => {
  271. this.$wrapper.css('visibility', 'visible').fadeTo(
  272. FADE_DURATION_MS,
  273. 1,
  274. () => {
  275. this.isVisible = true;
  276. resolve();
  277. }
  278. );
  279. });
  280. }
  281. hide () {
  282. // as the container is hidden/replaced by another container
  283. // hide its avatar
  284. this.showAvatar(false);
  285. // its already hidden
  286. if (!this.isVisible) {
  287. return Promise.resolve();
  288. }
  289. return new Promise((resolve) => {
  290. this.$wrapper.fadeTo(FADE_DURATION_MS, 0, () => {
  291. this.$wrapper.css('visibility', 'hidden');
  292. this.isVisible = false;
  293. resolve();
  294. });
  295. });
  296. }
  297. /**
  298. * @return {boolean} switch on dominant speaker event if on stage.
  299. */
  300. stayOnStage () {
  301. return false;
  302. }
  303. }
  304. /**
  305. * Manager for all Large containers.
  306. */
  307. export default class LargeVideoManager {
  308. constructor () {
  309. this.containers = {};
  310. this.state = VIDEO_CONTAINER_TYPE;
  311. this.videoContainer = new VideoContainer(
  312. () => this.resizeContainer(VIDEO_CONTAINER_TYPE));
  313. this.addContainer(VIDEO_CONTAINER_TYPE, this.videoContainer);
  314. // use the same video container to handle and desktop tracks
  315. this.addContainer("desktop", this.videoContainer);
  316. this.width = 0;
  317. this.height = 0;
  318. this.$container = $('#largeVideoContainer');
  319. this.$container.css({
  320. display: 'inline-block'
  321. });
  322. if (interfaceConfig.SHOW_JITSI_WATERMARK) {
  323. let leftWatermarkDiv = this.$container.find("div.watermark.leftwatermark");
  324. leftWatermarkDiv.css({display: 'block'});
  325. leftWatermarkDiv.parent().attr('href', interfaceConfig.JITSI_WATERMARK_LINK);
  326. }
  327. if (interfaceConfig.SHOW_BRAND_WATERMARK) {
  328. let rightWatermarkDiv = this.$container.find("div.watermark.rightwatermark");
  329. rightWatermarkDiv.css({
  330. display: 'block',
  331. backgroundImage: 'url(images/rightwatermark.png)'
  332. });
  333. rightWatermarkDiv.parent().attr('href', interfaceConfig.BRAND_WATERMARK_LINK);
  334. }
  335. if (interfaceConfig.SHOW_POWERED_BY) {
  336. this.$container.children("a.poweredby").css({display: 'block'});
  337. }
  338. this.$container.hover(
  339. e => this.onHoverIn(e),
  340. e => this.onHoverOut(e)
  341. );
  342. }
  343. onHoverIn (e) {
  344. if (!this.state) {
  345. return;
  346. }
  347. let container = this.getContainer(this.state);
  348. container.onHoverIn(e);
  349. }
  350. onHoverOut (e) {
  351. if (!this.state) {
  352. return;
  353. }
  354. let container = this.getContainer(this.state);
  355. container.onHoverOut(e);
  356. }
  357. get id () {
  358. let container = this.getContainer(this.state);
  359. return container.id;
  360. }
  361. scheduleLargeVideoUpdate () {
  362. if (this.updateInProcess || !this.newStreamData) {
  363. return;
  364. }
  365. this.updateInProcess = true;
  366. let container = this.getContainer(this.state);
  367. container.hide().then(() => {
  368. let {id, stream, videoType, resolve} = this.newStreamData;
  369. this.newStreamData = null;
  370. console.info("hover in %s", id);
  371. this.state = videoType;
  372. let container = this.getContainer(this.state);
  373. container.setStream(stream, videoType);
  374. // change the avatar url on large
  375. this.updateAvatar(Avatar.getAvatarUrl(id));
  376. // If we the continer is VIDEO_CONTAINER_TYPE, we need to check
  377. // its stream whether exist and is muted to set isVideoMuted
  378. // in rest of the cases it is false
  379. let isVideoMuted = false;
  380. if (videoType == VIDEO_CONTAINER_TYPE)
  381. isVideoMuted = stream ? stream.isMuted() : true;
  382. // show the avatar on large if needed
  383. container.showAvatar(isVideoMuted);
  384. let promise;
  385. // do not show stream if video is muted
  386. // but we still should show watermark
  387. if (isVideoMuted) {
  388. this.showWatermark(true);
  389. promise = Promise.resolve();
  390. } else {
  391. promise = container.show();
  392. }
  393. // resolve updateLargeVideo promise after everything is done
  394. promise.then(resolve);
  395. return promise;
  396. }).then(() => {
  397. // after everything is done check again if there are any pending new streams.
  398. this.updateInProcess = false;
  399. this.scheduleLargeVideoUpdate();
  400. });
  401. }
  402. /**
  403. * Update large video.
  404. * Switches to large video even if previously other container was visible.
  405. * @param userID the userID of the participant associated with the stream
  406. * @param {JitsiTrack?} stream new stream
  407. * @param {string?} videoType new video type
  408. * @returns {Promise}
  409. */
  410. updateLargeVideo (userID, stream, videoType) {
  411. if (this.newStreamData) {
  412. this.newStreamData.reject();
  413. }
  414. this.newStreamData = createDeferred();
  415. this.newStreamData.id = userID;
  416. this.newStreamData.stream = stream;
  417. this.newStreamData.videoType = videoType;
  418. this.scheduleLargeVideoUpdate();
  419. return this.newStreamData.promise;
  420. }
  421. /**
  422. * Update container size optionally taking side bar size into account.
  423. * @param {boolean} isSideBarVisible if side bar is visible.
  424. */
  425. updateContainerSize (isSideBarVisible) {
  426. this.width = UIUtil.getAvailableVideoWidth(isSideBarVisible);
  427. this.height = window.innerHeight;
  428. }
  429. /**
  430. * Resize Large container of specified type.
  431. * @param {string} type type of container which should be resized.
  432. * @param {boolean} [animate=false] if resize process should be animated.
  433. */
  434. resizeContainer (type, animate = false) {
  435. let container = this.getContainer(type);
  436. container.resize(this.width, this.height, animate);
  437. }
  438. /**
  439. * Resize all Large containers.
  440. * @param {boolean} animate if resize process should be animated.
  441. */
  442. resize (animate) {
  443. // resize all containers
  444. Object.keys(this.containers)
  445. .forEach(type => this.resizeContainer(type, animate));
  446. this.$container.animate({
  447. width: this.width,
  448. height: this.height
  449. }, {
  450. queue: false,
  451. duration: animate ? 500 : 0
  452. });
  453. }
  454. /**
  455. * Enables/disables the filter indicating a video problem to the user.
  456. *
  457. * @param enable <tt>true</tt> to enable, <tt>false</tt> to disable
  458. */
  459. enableVideoProblemFilter (enable) {
  460. let container = this.getContainer(this.state);
  461. container.$video.toggleClass("videoProblemFilter", enable);
  462. }
  463. /**
  464. * Updates the src of the dominant speaker avatar
  465. */
  466. updateAvatar (avatarUrl) {
  467. $("#dominantSpeakerAvatar").attr('src', avatarUrl);
  468. }
  469. /**
  470. * Show or hide watermark.
  471. * @param {boolean} show
  472. */
  473. showWatermark (show) {
  474. $('.watermark').css('visibility', show ? 'visible' : 'hidden');
  475. }
  476. /**
  477. * Add container of specified type.
  478. * @param {string} type container type
  479. * @param {LargeContainer} container container to add.
  480. */
  481. addContainer (type, container) {
  482. if (this.containers[type]) {
  483. throw new Error(`container of type ${type} already exist`);
  484. }
  485. this.containers[type] = container;
  486. this.resizeContainer(type);
  487. }
  488. /**
  489. * Get Large container of specified type.
  490. * @param {string} type container type.
  491. * @returns {LargeContainer}
  492. */
  493. getContainer (type) {
  494. let container = this.containers[type];
  495. if (!container) {
  496. throw new Error(`container of type ${type} doesn't exist`);
  497. }
  498. return container;
  499. }
  500. /**
  501. * Remove Large container of specified type.
  502. * @param {string} type container type.
  503. */
  504. removeContainer (type) {
  505. if (!this.containers[type]) {
  506. throw new Error(`container of type ${type} doesn't exist`);
  507. }
  508. delete this.containers[type];
  509. }
  510. /**
  511. * Show Large container of specified type.
  512. * Does nothing if such container is already visible.
  513. * @param {string} type container type.
  514. * @returns {Promise}
  515. */
  516. showContainer (type) {
  517. if (this.state === type) {
  518. return Promise.resolve();
  519. }
  520. let oldContainer = this.containers[this.state];
  521. if (this.state === VIDEO_CONTAINER_TYPE) {
  522. this.showWatermark(false);
  523. }
  524. oldContainer.hide();
  525. this.state = type;
  526. let container = this.getContainer(type);
  527. return container.show().then(() => {
  528. if (type === VIDEO_CONTAINER_TYPE) {
  529. this.showWatermark(true);
  530. }
  531. });
  532. }
  533. }