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 20KB

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