Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LargeVideo.js 17KB

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