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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. const RTCBrowserType = require("../../RTC/RTCBrowserType");
  9. const avatarSize = interfaceConfig.DOMINANT_SPEAKER_AVATAR_SIZE;
  10. /**
  11. * Get stream id.
  12. * @param {JitsiTrack?} stream
  13. */
  14. function getStreamId(stream) {
  15. if(!stream)
  16. return;
  17. if (stream.isLocal()) {
  18. return APP.conference.localId;
  19. } else {
  20. return stream.getParticipantId();
  21. }
  22. }
  23. /**
  24. * Returns an array of the video dimensions, so that it keeps it's aspect
  25. * ratio and fits available area with it's larger dimension. This method
  26. * ensures that whole video will be visible and can leave empty areas.
  27. *
  28. * @return an array with 2 elements, the video width and the video height
  29. */
  30. function getDesktopVideoSize(videoWidth,
  31. videoHeight,
  32. videoSpaceWidth,
  33. videoSpaceHeight) {
  34. let aspectRatio = videoWidth / videoHeight;
  35. let availableWidth = Math.max(videoWidth, videoSpaceWidth);
  36. let availableHeight = Math.max(videoHeight, videoSpaceHeight);
  37. videoSpaceHeight -= BottomToolbar.getFilmStripHeight();
  38. if (availableWidth / aspectRatio >= videoSpaceHeight) {
  39. availableHeight = videoSpaceHeight;
  40. availableWidth = availableHeight * aspectRatio;
  41. }
  42. if (availableHeight * aspectRatio >= videoSpaceWidth) {
  43. availableWidth = videoSpaceWidth;
  44. availableHeight = availableWidth / aspectRatio;
  45. }
  46. return { availableWidth, availableHeight };
  47. }
  48. /**
  49. * Returns an array of the video dimensions. It respects the
  50. * VIDEO_LAYOUT_FIT config, to fit the video to the screen, by hiding some parts
  51. * of it, or to fit it to the height or width.
  52. *
  53. * @param videoWidth the original video width
  54. * @param videoHeight the original video height
  55. * @param videoSpaceWidth the width of the video space
  56. * @param videoSpaceHeight the height of the video space
  57. * @return an array with 2 elements, the video width and the video height
  58. */
  59. function getCameraVideoSize(videoWidth,
  60. videoHeight,
  61. videoSpaceWidth,
  62. videoSpaceHeight) {
  63. let aspectRatio = videoWidth / videoHeight;
  64. let availableWidth = videoWidth;
  65. let availableHeight = videoHeight;
  66. if (interfaceConfig.VIDEO_LAYOUT_FIT == 'height') {
  67. availableHeight = videoSpaceHeight;
  68. availableWidth = availableHeight*aspectRatio;
  69. }
  70. else if (interfaceConfig.VIDEO_LAYOUT_FIT == 'width') {
  71. availableWidth = videoSpaceWidth;
  72. availableHeight = availableWidth/aspectRatio;
  73. }
  74. else if (interfaceConfig.VIDEO_LAYOUT_FIT == 'both') {
  75. availableWidth = Math.max(videoWidth, videoSpaceWidth);
  76. availableHeight = Math.max(videoHeight, videoSpaceHeight);
  77. if (availableWidth / aspectRatio < videoSpaceHeight) {
  78. availableHeight = videoSpaceHeight;
  79. availableWidth = availableHeight * aspectRatio;
  80. }
  81. if (availableHeight * aspectRatio < videoSpaceWidth) {
  82. availableWidth = videoSpaceWidth;
  83. availableHeight = availableWidth / aspectRatio;
  84. }
  85. }
  86. return { availableWidth, availableHeight };
  87. }
  88. /**
  89. * Returns an array of the video horizontal and vertical indents,
  90. * so that if fits its parent.
  91. *
  92. * @return an array with 2 elements, the horizontal indent and the vertical
  93. * indent
  94. */
  95. function getCameraVideoPosition(videoWidth,
  96. videoHeight,
  97. videoSpaceWidth,
  98. videoSpaceHeight) {
  99. // Parent height isn't completely calculated when we position the video in
  100. // full screen mode and this is why we use the screen height in this case.
  101. // Need to think it further at some point and implement it properly.
  102. if (UIUtil.isFullScreen()) {
  103. videoSpaceHeight = window.innerHeight;
  104. }
  105. let horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  106. let verticalIndent = (videoSpaceHeight - videoHeight) / 2;
  107. return { horizontalIndent, verticalIndent };
  108. }
  109. /**
  110. * Returns an array of the video horizontal and vertical indents.
  111. * Centers horizontally and top aligns vertically.
  112. *
  113. * @return an array with 2 elements, the horizontal indent and the vertical
  114. * indent
  115. */
  116. function getDesktopVideoPosition(videoWidth,
  117. videoHeight,
  118. videoSpaceWidth,
  119. videoSpaceHeight) {
  120. let horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  121. let verticalIndent = 0;// Top aligned
  122. return { horizontalIndent, verticalIndent };
  123. }
  124. export const VideoContainerType = "video";
  125. /**
  126. * Container for user video.
  127. */
  128. class VideoContainer extends LargeContainer {
  129. // FIXME: With Temasys we have to re-select everytime
  130. get $video () {
  131. return $('#largeVideo');
  132. }
  133. get id () {
  134. if (this.stream) {
  135. return getStreamId(this.stream);
  136. }
  137. }
  138. constructor (onPlay) {
  139. super();
  140. this.stream = null;
  141. this.videoType = null;
  142. this.$avatar = $('#dominantSpeaker');
  143. this.$wrapper = $('#largeVideoWrapper');
  144. if (!RTCBrowserType.isIExplorer()) {
  145. this.$video.volume = 0;
  146. }
  147. this.$video.on('play', 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,
  213. 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. stream.attach(this.$video);
  232. let flipX = stream.isLocal() && !this.isScreenSharing();
  233. this.$video.css({
  234. transform: flipX ? 'scaleX(-1)' : 'none'
  235. });
  236. }
  237. /**
  238. * Check if current video stream is screen sharing.
  239. * @returns {boolean}
  240. */
  241. isScreenSharing () {
  242. return this.videoType === 'desktop';
  243. }
  244. /**
  245. * Show or hide user avatar.
  246. * @param {boolean} show
  247. */
  248. showAvatar (show) {
  249. this.$avatar.css("visibility", show ? "visible" : "hidden");
  250. }
  251. // We are doing fadeOut/fadeIn animations on parent div which wraps
  252. // largeVideo, because when Temasys plugin is in use it replaces
  253. // <video> elements with plugin <object> tag. In Safari jQuery is
  254. // unable to store values on this plugin object which breaks all
  255. // animation effects performed on it directly.
  256. show () {
  257. let $wrapper = this.$wrapper;
  258. return new Promise(function(resolve) {
  259. $wrapper.fadeIn(300, function () {
  260. $wrapper.css({visibility: 'visible'});
  261. $('.watermark').css({visibility: 'visible'});
  262. });
  263. resolve();
  264. });
  265. }
  266. hide () {
  267. let $wrapper = this.$wrapper;
  268. return new Promise(function(resolve) {
  269. $wrapper.fadeOut(300, function () {
  270. $wrapper.css({visibility: 'hidden'});
  271. $('.watermark').css({visibility: 'hidden'});
  272. resolve();
  273. });
  274. });
  275. }
  276. }
  277. /**
  278. * Manager for all Large containers.
  279. */
  280. export default class LargeVideoManager {
  281. constructor () {
  282. this.containers = {};
  283. this.state = VideoContainerType;
  284. this.videoContainer = new VideoContainer(() => this.resizeContainer(VideoContainerType));
  285. this.addContainer(VideoContainerType, this.videoContainer);
  286. this.width = 0;
  287. this.height = 0;
  288. this.$container = $('#largeVideoContainer');
  289. this.$container.css({
  290. display: 'inline-block'
  291. });
  292. if (interfaceConfig.SHOW_JITSI_WATERMARK) {
  293. let leftWatermarkDiv = this.$container.find("div.watermark.leftwatermark");
  294. leftWatermarkDiv.css({display: 'block'});
  295. leftWatermarkDiv.parent().attr('href', interfaceConfig.JITSI_WATERMARK_LINK);
  296. }
  297. if (interfaceConfig.SHOW_BRAND_WATERMARK) {
  298. let rightWatermarkDiv = this.$container.find("div.watermark.rightwatermark");
  299. rightWatermarkDiv.css({
  300. display: 'block',
  301. backgroundImage: 'url(images/rightwatermark.png)'
  302. });
  303. rightWatermarkDiv.parent().attr('href', interfaceConfig.BRAND_WATERMARK_LINK);
  304. }
  305. if (interfaceConfig.SHOW_POWERED_BY) {
  306. this.$container.children("a.poweredby").css({display: 'block'});
  307. }
  308. this.$container.hover(
  309. e => this.onHoverIn(e),
  310. e => this.onHoverOut(e)
  311. );
  312. }
  313. onHoverIn (e) {
  314. if (!this.state) {
  315. return;
  316. }
  317. let container = this.getContainer(this.state);
  318. container.onHoverIn(e);
  319. }
  320. onHoverOut (e) {
  321. if (!this.state) {
  322. return;
  323. }
  324. let container = this.getContainer(this.state);
  325. container.onHoverOut(e);
  326. }
  327. get id () {
  328. return this.videoContainer.id;
  329. }
  330. /**
  331. * Update large video.
  332. * Switches to large video even if previously other container was visible.
  333. * @param {JitsiTrack?} stream new stream
  334. * @param {string?} videoType new video type
  335. * @returns {Promise}
  336. */
  337. updateLargeVideo (smallVideo, videoType, largeVideoUpdatedCallBack) {
  338. let id = getStreamId(smallVideo.stream);
  339. let container = this.getContainer(this.state);
  340. container.hide().then(() => {
  341. console.info("hover in %s", id);
  342. this.state = VideoContainerType;
  343. this.videoContainer.setStream(smallVideo.stream, videoType);
  344. // change the avatar url on large
  345. this.updateAvatar(Avatar.getAvatarUrl(smallVideo.id));
  346. var isVideoMuted = smallVideo.stream.isMuted()
  347. // show the avatar on large if needed
  348. this.videoContainer.showAvatar(isVideoMuted);
  349. if (!isVideoMuted)
  350. this.videoContainer.show();
  351. largeVideoUpdatedCallBack();
  352. });
  353. }
  354. /**
  355. * Update container size optionally taking side bar size into account.
  356. * @param {boolean} isSideBarVisible if side bar is visible.
  357. */
  358. updateContainerSize (isSideBarVisible) {
  359. this.width = UIUtil.getAvailableVideoWidth(isSideBarVisible);
  360. this.height = window.innerHeight;
  361. }
  362. /**
  363. * Resize Large container of specified type.
  364. * @param {string} type type of container which should be resized.
  365. * @param {boolean} [animate=false] if resize process should be animated.
  366. */
  367. resizeContainer (type, animate = false) {
  368. let container = this.getContainer(type);
  369. container.resize(this.width, this.height, animate);
  370. }
  371. /**
  372. * Resize all Large containers.
  373. * @param {boolean} animate if resize process should be animated.
  374. */
  375. resize (animate) {
  376. // resize all containers
  377. Object.keys(this.containers)
  378. .forEach(type => this.resizeContainer(type, animate));
  379. this.$container.animate({
  380. width: this.width,
  381. height: this.height
  382. }, {
  383. queue: false,
  384. duration: animate ? 500 : 0
  385. });
  386. }
  387. /**
  388. * Enables/disables the filter indicating a video problem to the user.
  389. *
  390. * @param enable <tt>true</tt> to enable, <tt>false</tt> to disable
  391. */
  392. enableVideoProblemFilter (enable) {
  393. this.videoContainer.$video.toggleClass("videoProblemFilter", enable);
  394. }
  395. /**
  396. * Updates the src of the dominant speaker avatar
  397. */
  398. updateAvatar (avatarUrl) {
  399. $("#dominantSpeakerAvatar").attr('src', avatarUrl);
  400. }
  401. /**
  402. * Show avatar on Large video container or not.
  403. * @param {boolean} show
  404. */
  405. showAvatar (show) {
  406. show ? this.videoContainer.hide() : this.videoContainer.show();
  407. this.videoContainer.showAvatar(show);
  408. }
  409. /**
  410. * Add container of specified type.
  411. * @param {string} type container type
  412. * @param {LargeContainer} container container to add.
  413. */
  414. addContainer (type, container) {
  415. if (this.containers[type]) {
  416. throw new Error(`container of type ${type} already exist`);
  417. }
  418. this.containers[type] = container;
  419. this.resizeContainer(type);
  420. }
  421. /**
  422. * Get Large container of specified type.
  423. * @param {string} type container type.
  424. * @returns {LargeContainer}
  425. */
  426. getContainer (type) {
  427. let container = this.containers[type];
  428. if (!container) {
  429. throw new Error(`container of type ${type} doesn't exist`);
  430. }
  431. return container;
  432. }
  433. /**
  434. * Remove Large container of specified type.
  435. * @param {string} type container type.
  436. */
  437. removeContainer (type) {
  438. if (!this.containers[type]) {
  439. throw new Error(`container of type ${type} doesn't exist`);
  440. }
  441. delete this.containers[type];
  442. }
  443. /**
  444. * Show Large container of specified type.
  445. * Does nothing if such container is already visible.
  446. * @param {string} type container type.
  447. * @returns {Promise}
  448. */
  449. showContainer (type) {
  450. if (this.state === type) {
  451. return Promise.resolve();
  452. }
  453. let container = this.getContainer(type);
  454. if (this.state) {
  455. let oldContainer = this.containers[this.state];
  456. if (oldContainer) {
  457. oldContainer.hide();
  458. }
  459. }
  460. this.state = type;
  461. return container.show();
  462. }
  463. }