Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LargeVideo.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 BottomToolbar from '../toolbars/BottomToolbar';
  7. import Avatar from "../avatar/Avatar";
  8. import {createDeferred} from '../../util/helpers';
  9. const RTCBrowserType = require("../../RTC/RTCBrowserType");
  10. const avatarSize = interfaceConfig.DOMINANT_SPEAKER_AVATAR_SIZE;
  11. const FADE_DURATION_MS = 300;
  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 -= BottomToolbar.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. export const VideoContainerType = "video";
  128. /**
  129. * Container for user video.
  130. */
  131. class VideoContainer extends LargeContainer {
  132. // FIXME: With Temasys we have to re-select everytime
  133. get $video () {
  134. return $('#largeVideo');
  135. }
  136. get id () {
  137. return getStreamOwnerId(this.stream);
  138. }
  139. constructor (onPlay) {
  140. super();
  141. this.stream = null;
  142. this.videoType = null;
  143. this.$avatar = $('#dominantSpeaker');
  144. this.$wrapper = $('#largeVideoWrapper');
  145. if (!RTCBrowserType.isIExplorer()) {
  146. this.$video.volume = 0;
  147. }
  148. // This does not work with Temasys plugin - has to be a property to be
  149. // copied between new <object> elements
  150. //this.$video.on('play', onPlay);
  151. this.$video[0].onplay = onPlay;
  152. }
  153. /**
  154. * Get size of video element.
  155. * @returns {{width, height}}
  156. */
  157. getStreamSize () {
  158. let video = this.$video[0];
  159. return {
  160. width: video.videoWidth,
  161. height: video.videoHeight
  162. };
  163. }
  164. /**
  165. * Calculate optimal video size for specified container size.
  166. * @param {number} containerWidth container width
  167. * @param {number} containerHeight container height
  168. * @returns {{availableWidth, availableHeight}}
  169. */
  170. getVideoSize (containerWidth, containerHeight) {
  171. let { width, height } = this.getStreamSize();
  172. if (this.stream && this.isScreenSharing()) {
  173. return getDesktopVideoSize( width,
  174. height,
  175. containerWidth,
  176. containerHeight);
  177. } else {
  178. return getCameraVideoSize( width,
  179. height,
  180. containerWidth,
  181. containerHeight);
  182. }
  183. }
  184. /**
  185. * Calculate optimal video position (offset for top left corner)
  186. * for specified video size and container size.
  187. * @param {number} width video width
  188. * @param {number} height video height
  189. * @param {number} containerWidth container width
  190. * @param {number} containerHeight container height
  191. * @returns {{horizontalIndent, verticalIndent}}
  192. */
  193. getVideoPosition (width, height, containerWidth, containerHeight) {
  194. if (this.stream && this.isScreenSharing()) {
  195. return getDesktopVideoPosition( width,
  196. height,
  197. containerWidth,
  198. containerHeight);
  199. } else {
  200. return getCameraVideoPosition( width,
  201. height,
  202. containerWidth,
  203. containerHeight);
  204. }
  205. }
  206. resize (containerWidth, containerHeight, animate = false) {
  207. let [width, height]
  208. = this.getVideoSize(containerWidth, containerHeight);
  209. let { horizontalIndent, verticalIndent }
  210. = this.getVideoPosition(width, height,
  211. containerWidth, containerHeight);
  212. // update avatar position
  213. let top = containerHeight / 2 - avatarSize / 4 * 3;
  214. this.$avatar.css('top', top);
  215. this.$wrapper.animate({
  216. width: width,
  217. height: height,
  218. top: verticalIndent,
  219. bottom: verticalIndent,
  220. left: horizontalIndent,
  221. right: horizontalIndent
  222. }, {
  223. queue: false,
  224. duration: animate ? 500 : 0
  225. });
  226. }
  227. /**
  228. * Update video stream.
  229. * @param {JitsiTrack?} stream new stream
  230. * @param {string} videoType video type
  231. */
  232. setStream (stream, videoType) {
  233. this.stream = stream;
  234. this.videoType = videoType;
  235. stream.attach(this.$video);
  236. let flipX = stream.isLocal() && !this.isScreenSharing();
  237. this.$video.css({
  238. transform: flipX ? 'scaleX(-1)' : 'none'
  239. });
  240. }
  241. /**
  242. * Check if current video stream is screen sharing.
  243. * @returns {boolean}
  244. */
  245. isScreenSharing () {
  246. return this.videoType === 'desktop';
  247. }
  248. /**
  249. * Show or hide user avatar.
  250. * @param {boolean} show
  251. */
  252. showAvatar (show) {
  253. this.$avatar.css("visibility", show ? "visible" : "hidden");
  254. }
  255. // We are doing fadeOut/fadeIn animations on parent div which wraps
  256. // largeVideo, because when Temasys plugin is in use it replaces
  257. // <video> elements with plugin <object> tag. In Safari jQuery is
  258. // unable to store values on this plugin object which breaks all
  259. // animation effects performed on it directly.
  260. show () {
  261. let $wrapper = this.$wrapper;
  262. return new Promise(function(resolve) {
  263. $wrapper.css({visibility: 'visible'});
  264. $wrapper.fadeTo(FADE_DURATION_MS, 1, function () {
  265. $('.watermark').css({visibility: 'visible'});
  266. resolve();
  267. });
  268. });
  269. }
  270. hide () {
  271. let $wrapper = this.$wrapper;
  272. let id = this.id;
  273. return new Promise(function(resolve) {
  274. $wrapper.fadeTo(id ? FADE_DURATION_MS : 1, 0, function () {
  275. $wrapper.css({visibility: 'hidden'});
  276. $('.watermark').css({visibility: 'hidden'});
  277. resolve();
  278. });
  279. });
  280. }
  281. }
  282. /**
  283. * Manager for all Large containers.
  284. */
  285. export default class LargeVideoManager {
  286. constructor () {
  287. this.containers = {};
  288. this.state = VideoContainerType;
  289. this.videoContainer = new VideoContainer(() => this.resizeContainer(VideoContainerType));
  290. this.addContainer(VideoContainerType, this.videoContainer);
  291. this.width = 0;
  292. this.height = 0;
  293. this.$container = $('#largeVideoContainer');
  294. this.$container.css({
  295. display: 'inline-block'
  296. });
  297. if (interfaceConfig.SHOW_JITSI_WATERMARK) {
  298. let leftWatermarkDiv = this.$container.find("div.watermark.leftwatermark");
  299. leftWatermarkDiv.css({display: 'block'});
  300. leftWatermarkDiv.parent().attr('href', interfaceConfig.JITSI_WATERMARK_LINK);
  301. }
  302. if (interfaceConfig.SHOW_BRAND_WATERMARK) {
  303. let rightWatermarkDiv = this.$container.find("div.watermark.rightwatermark");
  304. rightWatermarkDiv.css({
  305. display: 'block',
  306. backgroundImage: 'url(images/rightwatermark.png)'
  307. });
  308. rightWatermarkDiv.parent().attr('href', interfaceConfig.BRAND_WATERMARK_LINK);
  309. }
  310. if (interfaceConfig.SHOW_POWERED_BY) {
  311. this.$container.children("a.poweredby").css({display: 'block'});
  312. }
  313. this.$container.hover(
  314. e => this.onHoverIn(e),
  315. e => this.onHoverOut(e)
  316. );
  317. }
  318. onHoverIn (e) {
  319. if (!this.state) {
  320. return;
  321. }
  322. let container = this.getContainer(this.state);
  323. container.onHoverIn(e);
  324. }
  325. onHoverOut (e) {
  326. if (!this.state) {
  327. return;
  328. }
  329. let container = this.getContainer(this.state);
  330. container.onHoverOut(e);
  331. }
  332. get id () {
  333. return this.videoContainer.id;
  334. }
  335. scheduleLargeVideoUpdate () {
  336. if (this.updateInProcess || !this.newStreamData) {
  337. return;
  338. }
  339. this.updateInProcess = true;
  340. let container = this.getContainer(this.state);
  341. container.hide().then(() => {
  342. let {id, stream, videoType, resolve} = this.newStreamData;
  343. this.newStreamData = null;
  344. console.info("hover in %s", id);
  345. this.state = VideoContainerType;
  346. this.videoContainer.setStream(stream, videoType);
  347. // change the avatar url on large
  348. this.updateAvatar(Avatar.getAvatarUrl(id));
  349. let isVideoMuted = stream.isMuted();
  350. // show the avatar on large if needed
  351. this.videoContainer.showAvatar(isVideoMuted);
  352. // do not show stream if video is muted
  353. let promise = isVideoMuted ? Promise.resolve() : this.videoContainer.show();
  354. // resolve updateLargeVideo promise after everything is done
  355. promise.then(resolve);
  356. return promise;
  357. }).then(() => {
  358. // after everything is done check again if there are any pending new streams.
  359. this.updateInProcess = false;
  360. this.scheduleLargeVideoUpdate();
  361. });
  362. }
  363. /**
  364. * Update large video.
  365. * Switches to large video even if previously other container was visible.
  366. * @param {JitsiTrack?} stream new stream
  367. * @param {string?} videoType new video type
  368. * @returns {Promise}
  369. */
  370. updateLargeVideo (stream, videoType) {
  371. let id = getStreamOwnerId(stream);
  372. if (this.newStreamData) {
  373. this.newStreamData.reject();
  374. }
  375. this.newStreamData = createDeferred();
  376. this.newStreamData.id = id;
  377. this.newStreamData.stream = stream;
  378. this.newStreamData.videoType = videoType;
  379. this.scheduleLargeVideoUpdate();
  380. return this.newStreamData.promise;
  381. }
  382. /**
  383. * Update container size optionally taking side bar size into account.
  384. * @param {boolean} isSideBarVisible if side bar is visible.
  385. */
  386. updateContainerSize (isSideBarVisible) {
  387. this.width = UIUtil.getAvailableVideoWidth(isSideBarVisible);
  388. this.height = window.innerHeight;
  389. }
  390. /**
  391. * Resize Large container of specified type.
  392. * @param {string} type type of container which should be resized.
  393. * @param {boolean} [animate=false] if resize process should be animated.
  394. */
  395. resizeContainer (type, animate = false) {
  396. let container = this.getContainer(type);
  397. container.resize(this.width, this.height, animate);
  398. }
  399. /**
  400. * Resize all Large containers.
  401. * @param {boolean} animate if resize process should be animated.
  402. */
  403. resize (animate) {
  404. // resize all containers
  405. Object.keys(this.containers)
  406. .forEach(type => this.resizeContainer(type, animate));
  407. this.$container.animate({
  408. width: this.width,
  409. height: this.height
  410. }, {
  411. queue: false,
  412. duration: animate ? 500 : 0
  413. });
  414. }
  415. /**
  416. * Enables/disables the filter indicating a video problem to the user.
  417. *
  418. * @param enable <tt>true</tt> to enable, <tt>false</tt> to disable
  419. */
  420. enableVideoProblemFilter (enable) {
  421. this.videoContainer.$video.toggleClass("videoProblemFilter", enable);
  422. }
  423. /**
  424. * Updates the src of the dominant speaker avatar
  425. */
  426. updateAvatar (avatarUrl) {
  427. $("#dominantSpeakerAvatar").attr('src', avatarUrl);
  428. }
  429. /**
  430. * Show avatar on Large video container or not.
  431. * @param {boolean} show
  432. */
  433. showAvatar (show) {
  434. this.videoContainer.showAvatar(show);
  435. }
  436. /**
  437. * Add container of specified type.
  438. * @param {string} type container type
  439. * @param {LargeContainer} container container to add.
  440. */
  441. addContainer (type, container) {
  442. if (this.containers[type]) {
  443. throw new Error(`container of type ${type} already exist`);
  444. }
  445. this.containers[type] = container;
  446. this.resizeContainer(type);
  447. }
  448. /**
  449. * Get Large container of specified type.
  450. * @param {string} type container type.
  451. * @returns {LargeContainer}
  452. */
  453. getContainer (type) {
  454. let container = this.containers[type];
  455. if (!container) {
  456. throw new Error(`container of type ${type} doesn't exist`);
  457. }
  458. return container;
  459. }
  460. /**
  461. * Remove Large container of specified type.
  462. * @param {string} type container type.
  463. */
  464. removeContainer (type) {
  465. if (!this.containers[type]) {
  466. throw new Error(`container of type ${type} doesn't exist`);
  467. }
  468. delete this.containers[type];
  469. }
  470. /**
  471. * Show Large container of specified type.
  472. * Does nothing if such container is already visible.
  473. * @param {string} type container type.
  474. * @returns {Promise}
  475. */
  476. showContainer (type) {
  477. if (this.state === type) {
  478. return Promise.resolve();
  479. }
  480. let oldContainer = this.containers[this.state];
  481. oldContainer.hide();
  482. this.state = type;
  483. let container = this.getContainer(type);
  484. return container.show();
  485. }
  486. }