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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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) {
  138. super();
  139. this.stream = null;
  140. this.videoType = null;
  141. this.localFlipX = true;
  142. this.isVisible = false;
  143. this.$avatar = $('#dominantSpeaker');
  144. this.$wrapper = $('#largeVideoWrapper');
  145. this.avatarHeight = $("#dominantSpeakerAvatar").height();
  146. // This does not work with Temasys plugin - has to be a property to be
  147. // copied between new <object> elements
  148. //this.$video.on('play', onPlay);
  149. this.$video[0].onplay = onPlay;
  150. }
  151. /**
  152. * Get size of video element.
  153. * @returns {{width, height}}
  154. */
  155. getStreamSize () {
  156. let video = this.$video[0];
  157. return {
  158. width: video.videoWidth,
  159. height: video.videoHeight
  160. };
  161. }
  162. /**
  163. * Calculate optimal video size for specified container size.
  164. * @param {number} containerWidth container width
  165. * @param {number} containerHeight container height
  166. * @returns {{availableWidth, availableHeight}}
  167. */
  168. getVideoSize (containerWidth, containerHeight) {
  169. let { width, height } = this.getStreamSize();
  170. if (this.stream && this.isScreenSharing()) {
  171. return getDesktopVideoSize( width,
  172. height,
  173. containerWidth,
  174. containerHeight);
  175. } else {
  176. return getCameraVideoSize( width,
  177. height,
  178. containerWidth,
  179. containerHeight);
  180. }
  181. }
  182. /**
  183. * Calculate optimal video position (offset for top left corner)
  184. * for specified video size and container size.
  185. * @param {number} width video width
  186. * @param {number} height video height
  187. * @param {number} containerWidth container width
  188. * @param {number} containerHeight container height
  189. * @returns {{horizontalIndent, verticalIndent}}
  190. */
  191. getVideoPosition (width, height, containerWidth, containerHeight) {
  192. if (this.stream && this.isScreenSharing()) {
  193. return getDesktopVideoPosition( width,
  194. height,
  195. containerWidth,
  196. containerHeight);
  197. } else {
  198. return getCameraVideoPosition( width,
  199. height,
  200. containerWidth,
  201. containerHeight);
  202. }
  203. }
  204. resize (containerWidth, containerHeight, animate = false) {
  205. let [width, height]
  206. = this.getVideoSize(containerWidth, containerHeight);
  207. let { horizontalIndent, verticalIndent }
  208. = this.getVideoPosition(width, height,
  209. containerWidth, containerHeight);
  210. // update avatar position
  211. let top = containerHeight / 2 - this.avatarHeight / 4 * 3;
  212. this.$avatar.css('top', top);
  213. this.$wrapper.animate({
  214. width: width,
  215. height: height,
  216. top: verticalIndent,
  217. bottom: verticalIndent,
  218. left: horizontalIndent,
  219. right: horizontalIndent
  220. }, {
  221. queue: false,
  222. duration: animate ? 500 : 0
  223. });
  224. }
  225. /**
  226. * Update video stream.
  227. * @param {JitsiTrack?} stream new stream
  228. * @param {string} videoType video type
  229. */
  230. setStream (stream, videoType) {
  231. // detach old stream
  232. if (this.stream) {
  233. this.stream.detach(this.$video[0]);
  234. }
  235. this.stream = stream;
  236. this.videoType = videoType;
  237. if (!stream) {
  238. return;
  239. }
  240. stream.attach(this.$video[0]);
  241. let flipX = stream.isLocal() && this.localFlipX;
  242. this.$video.css({
  243. transform: flipX ? 'scaleX(-1)' : 'none'
  244. });
  245. }
  246. /**
  247. * Changes the flipX state of the local video.
  248. * @param val {boolean} true if flipped.
  249. */
  250. setLocalFlipX(val) {
  251. this.localFlipX = val;
  252. if(!this.$video || !this.stream || !this.stream.isLocal())
  253. return;
  254. this.$video.css({
  255. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  256. });
  257. }
  258. /**
  259. * Check if current video stream is screen sharing.
  260. * @returns {boolean}
  261. */
  262. isScreenSharing () {
  263. return this.videoType === 'desktop';
  264. }
  265. /**
  266. * Show or hide user avatar.
  267. * @param {boolean} show
  268. */
  269. showAvatar (show) {
  270. this.$avatar.css("visibility", show ? "visible" : "hidden");
  271. }
  272. // We are doing fadeOut/fadeIn animations on parent div which wraps
  273. // largeVideo, because when Temasys plugin is in use it replaces
  274. // <video> elements with plugin <object> tag. In Safari jQuery is
  275. // unable to store values on this plugin object which breaks all
  276. // animation effects performed on it directly.
  277. show () {
  278. // its already visible
  279. if (this.isVisible) {
  280. return Promise.resolve();
  281. }
  282. let $wrapper = this.$wrapper;
  283. return new Promise((resolve) => {
  284. this.$wrapper.css('visibility', 'visible').fadeTo(
  285. FADE_DURATION_MS,
  286. 1,
  287. () => {
  288. this.isVisible = true;
  289. resolve();
  290. }
  291. );
  292. });
  293. }
  294. hide () {
  295. // as the container is hidden/replaced by another container
  296. // hide its avatar
  297. this.showAvatar(false);
  298. // its already hidden
  299. if (!this.isVisible) {
  300. return Promise.resolve();
  301. }
  302. return new Promise((resolve) => {
  303. this.$wrapper.fadeTo(FADE_DURATION_MS, 0, () => {
  304. this.$wrapper.css('visibility', 'hidden');
  305. this.isVisible = false;
  306. resolve();
  307. });
  308. });
  309. }
  310. /**
  311. * @return {boolean} switch on dominant speaker event if on stage.
  312. */
  313. stayOnStage () {
  314. return false;
  315. }
  316. }
  317. /**
  318. * Manager for all Large containers.
  319. */
  320. export default class LargeVideoManager {
  321. constructor () {
  322. this.containers = {};
  323. this.state = VIDEO_CONTAINER_TYPE;
  324. this.videoContainer = new VideoContainer(
  325. () => this.resizeContainer(VIDEO_CONTAINER_TYPE));
  326. this.addContainer(VIDEO_CONTAINER_TYPE, this.videoContainer);
  327. // use the same video container to handle and desktop tracks
  328. this.addContainer("desktop", this.videoContainer);
  329. this.width = 0;
  330. this.height = 0;
  331. this.$container = $('#largeVideoContainer');
  332. this.$container.css({
  333. display: 'inline-block'
  334. });
  335. if (interfaceConfig.SHOW_JITSI_WATERMARK) {
  336. let leftWatermarkDiv
  337. = this.$container.find("div.watermark.leftwatermark");
  338. leftWatermarkDiv.css({display: 'block'});
  339. leftWatermarkDiv.parent().attr(
  340. 'href', interfaceConfig.JITSI_WATERMARK_LINK);
  341. }
  342. if (interfaceConfig.SHOW_BRAND_WATERMARK) {
  343. let rightWatermarkDiv
  344. = this.$container.find("div.watermark.rightwatermark");
  345. rightWatermarkDiv.css({
  346. display: 'block',
  347. backgroundImage: 'url(images/rightwatermark.png)'
  348. });
  349. rightWatermarkDiv.parent().attr(
  350. 'href', interfaceConfig.BRAND_WATERMARK_LINK);
  351. }
  352. if (interfaceConfig.SHOW_POWERED_BY) {
  353. this.$container.children("a.poweredby").css({display: 'block'});
  354. }
  355. this.$container.hover(
  356. e => this.onHoverIn(e),
  357. e => this.onHoverOut(e)
  358. );
  359. }
  360. onHoverIn (e) {
  361. if (!this.state) {
  362. return;
  363. }
  364. let container = this.getContainer(this.state);
  365. container.onHoverIn(e);
  366. }
  367. onHoverOut (e) {
  368. if (!this.state) {
  369. return;
  370. }
  371. let container = this.getContainer(this.state);
  372. container.onHoverOut(e);
  373. }
  374. get id () {
  375. let container = this.getContainer(this.state);
  376. return container.id;
  377. }
  378. scheduleLargeVideoUpdate () {
  379. if (this.updateInProcess || !this.newStreamData) {
  380. return;
  381. }
  382. this.updateInProcess = true;
  383. let container = this.getContainer(this.state);
  384. // Include hide()/fadeOut only if we're switching between users
  385. let preUpdate;
  386. if (this.newStreamData.id != this.id) {
  387. preUpdate = container.hide();
  388. } else {
  389. preUpdate = Promise.resolve();
  390. }
  391. preUpdate.then(() => {
  392. let {id, stream, videoType, resolve} = this.newStreamData;
  393. this.newStreamData = null;
  394. console.info("hover in %s", id);
  395. this.state = videoType;
  396. let container = this.getContainer(this.state);
  397. container.setStream(stream, videoType);
  398. // change the avatar url on large
  399. this.updateAvatar(Avatar.getAvatarUrl(id));
  400. // If we the continer is VIDEO_CONTAINER_TYPE, we need to check
  401. // its stream whether exist and is muted to set isVideoMuted
  402. // in rest of the cases it is false
  403. let isVideoMuted = false;
  404. if (videoType == VIDEO_CONTAINER_TYPE)
  405. isVideoMuted = stream ? stream.isMuted() : true;
  406. // show the avatar on large if needed
  407. container.showAvatar(isVideoMuted);
  408. let promise;
  409. // do not show stream if video is muted
  410. // but we still should show watermark
  411. if (isVideoMuted) {
  412. this.showWatermark(true);
  413. promise = Promise.resolve();
  414. } else {
  415. promise = container.show();
  416. }
  417. // resolve updateLargeVideo promise after everything is done
  418. promise.then(resolve);
  419. return promise;
  420. }).then(() => {
  421. // after everything is done check again if there are any pending
  422. // new streams.
  423. this.updateInProcess = false;
  424. this.scheduleLargeVideoUpdate();
  425. });
  426. }
  427. /**
  428. * Update large video.
  429. * Switches to large video even if previously other container was visible.
  430. * @param userID the userID of the participant associated with the stream
  431. * @param {JitsiTrack?} stream new stream
  432. * @param {string?} videoType new video type
  433. * @returns {Promise}
  434. */
  435. updateLargeVideo (userID, stream, videoType) {
  436. if (this.newStreamData) {
  437. this.newStreamData.reject();
  438. }
  439. this.newStreamData = createDeferred();
  440. this.newStreamData.id = userID;
  441. this.newStreamData.stream = stream;
  442. this.newStreamData.videoType = videoType;
  443. this.scheduleLargeVideoUpdate();
  444. return this.newStreamData.promise;
  445. }
  446. /**
  447. * Update container size.
  448. */
  449. updateContainerSize () {
  450. this.width = UIUtil.getAvailableVideoWidth();
  451. this.height = window.innerHeight;
  452. }
  453. /**
  454. * Resize Large container of specified type.
  455. * @param {string} type type of container which should be resized.
  456. * @param {boolean} [animate=false] if resize process should be animated.
  457. */
  458. resizeContainer (type, animate = false) {
  459. let container = this.getContainer(type);
  460. container.resize(this.width, this.height, animate);
  461. }
  462. /**
  463. * Resize all Large containers.
  464. * @param {boolean} animate if resize process should be animated.
  465. */
  466. resize (animate) {
  467. // resize all containers
  468. Object.keys(this.containers)
  469. .forEach(type => this.resizeContainer(type, animate));
  470. this.$container.animate({
  471. width: this.width,
  472. height: this.height
  473. }, {
  474. queue: false,
  475. duration: animate ? 500 : 0
  476. });
  477. }
  478. /**
  479. * Enables/disables the filter indicating a video problem to the user.
  480. *
  481. * @param enable <tt>true</tt> to enable, <tt>false</tt> to disable
  482. */
  483. enableVideoProblemFilter (enable) {
  484. let container = this.getContainer(this.state);
  485. container.$video.toggleClass("videoProblemFilter", enable);
  486. }
  487. /**
  488. * Updates the src of the dominant speaker avatar
  489. */
  490. updateAvatar (avatarUrl) {
  491. $("#dominantSpeakerAvatar").attr('src', avatarUrl);
  492. }
  493. /**
  494. * Show or hide watermark.
  495. * @param {boolean} show
  496. */
  497. showWatermark (show) {
  498. $('.watermark').css('visibility', show ? 'visible' : 'hidden');
  499. }
  500. /**
  501. * Add container of specified type.
  502. * @param {string} type container type
  503. * @param {LargeContainer} container container to add.
  504. */
  505. addContainer (type, container) {
  506. if (this.containers[type]) {
  507. throw new Error(`container of type ${type} already exist`);
  508. }
  509. this.containers[type] = container;
  510. this.resizeContainer(type);
  511. }
  512. /**
  513. * Get Large container of specified type.
  514. * @param {string} type container type.
  515. * @returns {LargeContainer}
  516. */
  517. getContainer (type) {
  518. let container = this.containers[type];
  519. if (!container) {
  520. throw new Error(`container of type ${type} doesn't exist`);
  521. }
  522. return container;
  523. }
  524. /**
  525. * Remove Large container of specified type.
  526. * @param {string} type container type.
  527. */
  528. removeContainer (type) {
  529. if (!this.containers[type]) {
  530. throw new Error(`container of type ${type} doesn't exist`);
  531. }
  532. delete this.containers[type];
  533. }
  534. /**
  535. * Show Large container of specified type.
  536. * Does nothing if such container is already visible.
  537. * @param {string} type container type.
  538. * @returns {Promise}
  539. */
  540. showContainer (type) {
  541. if (this.state === type) {
  542. return Promise.resolve();
  543. }
  544. let oldContainer = this.containers[this.state];
  545. if (this.state === VIDEO_CONTAINER_TYPE) {
  546. this.showWatermark(false);
  547. }
  548. oldContainer.hide();
  549. this.state = type;
  550. let container = this.getContainer(type);
  551. return container.show().then(() => {
  552. if (type === VIDEO_CONTAINER_TYPE) {
  553. this.showWatermark(true);
  554. }
  555. });
  556. }
  557. /**
  558. * Changes the flipX state of the local video.
  559. * @param val {boolean} true if flipped.
  560. */
  561. onLocalFlipXChange(val) {
  562. this.videoContainer.setLocalFlipX(val);
  563. }
  564. }