選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VideoContainer.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /* global $, APP, interfaceConfig */
  2. /* jshint -W101 */
  3. import Filmstrip from './Filmstrip';
  4. import LargeContainer from './LargeContainer';
  5. import UIEvents from "../../../service/UI/UIEvents";
  6. import UIUtil from "../util/UIUtil";
  7. // FIXME should be 'video'
  8. export const VIDEO_CONTAINER_TYPE = "camera";
  9. const FADE_DURATION_MS = 300;
  10. /**
  11. * Get stream id.
  12. * @param {JitsiTrack?} stream
  13. */
  14. function getStreamOwnerId(stream) {
  15. if (!stream) {
  16. return;
  17. }
  18. // local stream doesn't have method "getParticipantId"
  19. if (stream.isLocal()) {
  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. * @param videoWidth the width of the video to position
  31. * @param videoHeight the height of the video to position
  32. * @param videoSpaceWidth the width of the available space
  33. * @param videoSpaceHeight the height of the available space
  34. * @return an array with 2 elements, the video width and the video height
  35. */
  36. function getDesktopVideoSize(videoWidth,
  37. videoHeight,
  38. videoSpaceWidth,
  39. videoSpaceHeight) {
  40. let aspectRatio = videoWidth / videoHeight;
  41. let availableWidth = Math.max(videoWidth, videoSpaceWidth);
  42. let availableHeight = Math.max(videoHeight, videoSpaceHeight);
  43. videoSpaceHeight -= Filmstrip.getFilmstripHeight();
  44. if (availableWidth / aspectRatio >= videoSpaceHeight) {
  45. availableHeight = videoSpaceHeight;
  46. availableWidth = availableHeight * aspectRatio;
  47. }
  48. if (availableHeight * aspectRatio >= videoSpaceWidth) {
  49. availableWidth = videoSpaceWidth;
  50. availableHeight = availableWidth / aspectRatio;
  51. }
  52. return [ availableWidth, availableHeight ];
  53. }
  54. /**
  55. * Returns an array of the video dimensions. It respects the
  56. * VIDEO_LAYOUT_FIT config, to fit the video to the screen, by hiding some parts
  57. * of it, or to fit it to the height or width.
  58. *
  59. * @param videoWidth the original video width
  60. * @param videoHeight the original video height
  61. * @param videoSpaceWidth the width of the video space
  62. * @param videoSpaceHeight the height of the video space
  63. * @return an array with 2 elements, the video width and the video height
  64. */
  65. function getCameraVideoSize(videoWidth,
  66. videoHeight,
  67. videoSpaceWidth,
  68. videoSpaceHeight) {
  69. let aspectRatio = videoWidth / videoHeight;
  70. let availableWidth = videoWidth;
  71. let availableHeight = videoHeight;
  72. if (interfaceConfig.VIDEO_LAYOUT_FIT == 'height') {
  73. availableHeight = videoSpaceHeight;
  74. availableWidth = availableHeight*aspectRatio;
  75. }
  76. else if (interfaceConfig.VIDEO_LAYOUT_FIT == 'width') {
  77. availableWidth = videoSpaceWidth;
  78. availableHeight = availableWidth/aspectRatio;
  79. }
  80. else if (interfaceConfig.VIDEO_LAYOUT_FIT == 'both') {
  81. availableWidth = Math.max(videoWidth, videoSpaceWidth);
  82. availableHeight = Math.max(videoHeight, videoSpaceHeight);
  83. if (availableWidth / aspectRatio < videoSpaceHeight) {
  84. availableHeight = videoSpaceHeight;
  85. availableWidth = availableHeight * aspectRatio;
  86. }
  87. if (availableHeight * aspectRatio < videoSpaceWidth) {
  88. availableWidth = videoSpaceWidth;
  89. availableHeight = availableWidth / aspectRatio;
  90. }
  91. }
  92. return [ availableWidth, availableHeight ];
  93. }
  94. /**
  95. * Returns an array of the video horizontal and vertical indents,
  96. * so that if fits its parent.
  97. *
  98. * @return an array with 2 elements, the horizontal indent and the vertical
  99. * indent
  100. */
  101. function getCameraVideoPosition(videoWidth,
  102. videoHeight,
  103. videoSpaceWidth,
  104. videoSpaceHeight) {
  105. // Parent height isn't completely calculated when we position the video in
  106. // full screen mode and this is why we use the screen height in this case.
  107. // Need to think it further at some point and implement it properly.
  108. if (UIUtil.isFullScreen()) {
  109. videoSpaceHeight = window.innerHeight;
  110. }
  111. let horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  112. let verticalIndent = (videoSpaceHeight - videoHeight) / 2;
  113. return { horizontalIndent, verticalIndent };
  114. }
  115. /**
  116. * Returns an array of the video horizontal and vertical indents.
  117. * Centers horizontally and top aligns vertically.
  118. *
  119. * @return an array with 2 elements, the horizontal indent and the vertical
  120. * indent
  121. */
  122. function getDesktopVideoPosition(videoWidth, videoHeight, videoSpaceWidth) {
  123. let horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  124. let verticalIndent = 0;// Top aligned
  125. return { horizontalIndent, verticalIndent };
  126. }
  127. /**
  128. * Container for user video.
  129. */
  130. export 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. /**
  139. * Creates new VideoContainer instance.
  140. * @param resizeContainer {Function} function that takes care of the size
  141. * of the video container.
  142. * @param emitter {EventEmitter} the event emitter that will be used by
  143. * this instance.
  144. */
  145. constructor (resizeContainer, emitter) {
  146. super();
  147. this.stream = null;
  148. this.videoType = null;
  149. this.localFlipX = true;
  150. this.emitter = emitter;
  151. this.resizeContainer = resizeContainer;
  152. this.isVisible = false;
  153. /**
  154. * Flag indicates whether or not the avatar is currently displayed.
  155. * @type {boolean}
  156. */
  157. this.avatarDisplayed = false;
  158. this.$avatar = $('#dominantSpeaker');
  159. /**
  160. * A jQuery selector of the remote connection message.
  161. * @type {jQuery|HTMLElement}
  162. */
  163. this.$remoteConnectionMessage = $('#remoteConnectionMessage');
  164. /**
  165. * Indicates whether or not the video stream attached to the video
  166. * element has started(which means that there is any image rendered
  167. * even if the video is stalled).
  168. * @type {boolean}
  169. */
  170. this.wasVideoRendered = false;
  171. this.$wrapper = $('#largeVideoWrapper');
  172. this.avatarHeight = $("#dominantSpeakerAvatar").height();
  173. var onPlayCallback = function (event) {
  174. if (typeof resizeContainer === 'function') {
  175. resizeContainer(event);
  176. }
  177. this.wasVideoRendered = true;
  178. }.bind(this);
  179. // This does not work with Temasys plugin - has to be a property to be
  180. // copied between new <object> elements
  181. //this.$video.on('play', onPlay);
  182. this.$video[0].onplay = onPlayCallback;
  183. }
  184. /**
  185. * Enables a filter on the video which indicates that there are some
  186. * problems with the local media connection.
  187. *
  188. * @param {boolean} enable <tt>true</tt> if the filter is to be enabled or
  189. * <tt>false</tt> otherwise.
  190. */
  191. enableLocalConnectionProblemFilter (enable) {
  192. this.$video.toggleClass("videoProblemFilter", enable);
  193. }
  194. /**
  195. * Get size of video element.
  196. * @returns {{width, height}}
  197. */
  198. getStreamSize () {
  199. let video = this.$video[0];
  200. return {
  201. width: video.videoWidth,
  202. height: video.videoHeight
  203. };
  204. }
  205. /**
  206. * Calculate optimal video size for specified container size.
  207. * @param {number} containerWidth container width
  208. * @param {number} containerHeight container height
  209. * @returns {{availableWidth, availableHeight}}
  210. */
  211. getVideoSize (containerWidth, containerHeight) {
  212. let { width, height } = this.getStreamSize();
  213. if (this.stream && this.isScreenSharing()) {
  214. return getDesktopVideoSize( width,
  215. height,
  216. containerWidth,
  217. containerHeight);
  218. } else {
  219. return getCameraVideoSize( width,
  220. height,
  221. containerWidth,
  222. containerHeight);
  223. }
  224. }
  225. /**
  226. * Calculate optimal video position (offset for top left corner)
  227. * for specified video size and container size.
  228. * @param {number} width video width
  229. * @param {number} height video height
  230. * @param {number} containerWidth container width
  231. * @param {number} containerHeight container height
  232. * @returns {{horizontalIndent, verticalIndent}}
  233. */
  234. getVideoPosition (width, height, containerWidth, containerHeight) {
  235. if (this.stream && this.isScreenSharing()) {
  236. return getDesktopVideoPosition( width,
  237. height,
  238. containerWidth,
  239. containerHeight);
  240. } else {
  241. return getCameraVideoPosition( width,
  242. height,
  243. containerWidth,
  244. containerHeight);
  245. }
  246. }
  247. /**
  248. * Update position of the remote connection message which describes that
  249. * the remote user is having connectivity issues.
  250. */
  251. positionRemoteConnectionMessage () {
  252. if (this.avatarDisplayed) {
  253. let $avatarImage = $("#dominantSpeakerAvatar");
  254. this.$remoteConnectionMessage.css(
  255. 'top',
  256. $avatarImage.offset().top + $avatarImage.height() + 10);
  257. } else {
  258. let height = this.$remoteConnectionMessage.height();
  259. let parentHeight = this.$remoteConnectionMessage.parent().height();
  260. this.$remoteConnectionMessage.css(
  261. 'top', (parentHeight/2) - (height/2));
  262. }
  263. let width = this.$remoteConnectionMessage.width();
  264. let parentWidth = this.$remoteConnectionMessage.parent().width();
  265. this.$remoteConnectionMessage.css(
  266. 'left', ((parentWidth/2) - (width/2)));
  267. }
  268. resize (containerWidth, containerHeight, animate = false) {
  269. // XXX Prevent TypeError: undefined is not an object when the Web
  270. // browser does not support WebRTC (yet).
  271. if (this.$video.length === 0) {
  272. return;
  273. }
  274. let [width, height]
  275. = this.getVideoSize(containerWidth, containerHeight);
  276. let { horizontalIndent, verticalIndent }
  277. = this.getVideoPosition(width, height,
  278. containerWidth, containerHeight);
  279. // update avatar position
  280. let top = containerHeight / 2 - this.avatarHeight / 4 * 3;
  281. this.$avatar.css('top', top);
  282. this.positionRemoteConnectionMessage();
  283. this.$wrapper.animate({
  284. width: width,
  285. height: height,
  286. top: verticalIndent,
  287. bottom: verticalIndent,
  288. left: horizontalIndent,
  289. right: horizontalIndent
  290. }, {
  291. queue: false,
  292. duration: animate ? 500 : 0
  293. });
  294. }
  295. /**
  296. * Update video stream.
  297. * @param {JitsiTrack?} stream new stream
  298. * @param {string} videoType video type
  299. */
  300. setStream (stream, videoType) {
  301. if (this.stream === stream) {
  302. // Handles the use case for the remote participants when the
  303. // videoType is received with delay after turning on/off the
  304. // desktop sharing.
  305. if(this.videoType !== videoType) {
  306. this.videoType = videoType;
  307. this.resizeContainer();
  308. }
  309. return;
  310. } else {
  311. // The stream has changed, so the image will be lost on detach
  312. this.wasVideoRendered = false;
  313. }
  314. // detach old stream
  315. if (this.stream) {
  316. this.stream.detach(this.$video[0]);
  317. }
  318. this.stream = stream;
  319. this.videoType = videoType;
  320. if (!stream) {
  321. return;
  322. }
  323. stream.attach(this.$video[0]);
  324. let flipX = stream.isLocal() && this.localFlipX;
  325. this.$video.css({
  326. transform: flipX ? 'scaleX(-1)' : 'none'
  327. });
  328. // Reset the large video background depending on the stream.
  329. this.setLargeVideoBackground(this.avatarDisplayed);
  330. }
  331. /**
  332. * Changes the flipX state of the local video.
  333. * @param val {boolean} true if flipped.
  334. */
  335. setLocalFlipX(val) {
  336. this.localFlipX = val;
  337. if(!this.$video || !this.stream || !this.stream.isLocal())
  338. return;
  339. this.$video.css({
  340. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  341. });
  342. }
  343. /**
  344. * Check if current video stream is screen sharing.
  345. * @returns {boolean}
  346. */
  347. isScreenSharing () {
  348. return this.videoType === 'desktop';
  349. }
  350. /**
  351. * Show or hide user avatar.
  352. * @param {boolean} show
  353. */
  354. showAvatar (show) {
  355. // TO FIX: Video background need to be black, so that we don't have a
  356. // flickering effect when scrolling between videos and have the screen
  357. // move to grey before going back to video. Avatars though can have the
  358. // default background set.
  359. // In order to fix this code we need to introduce video background or
  360. // find a workaround for the video flickering.
  361. this.setLargeVideoBackground(show);
  362. this.$avatar.css("visibility", show ? "visible" : "hidden");
  363. this.avatarDisplayed = show;
  364. this.emitter.emit(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE, show);
  365. }
  366. /**
  367. * Indicates that the remote user who is currently displayed by this video
  368. * container is having connectivity issues.
  369. *
  370. * @param {boolean} show <tt>true</tt> to show or <tt>false</tt> to hide
  371. * the indication.
  372. */
  373. showRemoteConnectionProblemIndicator (show) {
  374. this.$video.toggleClass("remoteVideoProblemFilter", show);
  375. this.$avatar.toggleClass("remoteVideoProblemFilter", show);
  376. }
  377. // We are doing fadeOut/fadeIn animations on parent div which wraps
  378. // largeVideo, because when Temasys plugin is in use it replaces
  379. // <video> elements with plugin <object> tag. In Safari jQuery is
  380. // unable to store values on this plugin object which breaks all
  381. // animation effects performed on it directly.
  382. show () {
  383. // its already visible
  384. if (this.isVisible) {
  385. return Promise.resolve();
  386. }
  387. return new Promise((resolve) => {
  388. this.$wrapper.css('visibility', 'visible').fadeTo(
  389. FADE_DURATION_MS,
  390. 1,
  391. () => {
  392. this.isVisible = true;
  393. resolve();
  394. }
  395. );
  396. });
  397. }
  398. hide () {
  399. // as the container is hidden/replaced by another container
  400. // hide its avatar
  401. this.showAvatar(false);
  402. // its already hidden
  403. if (!this.isVisible) {
  404. return Promise.resolve();
  405. }
  406. return new Promise((resolve) => {
  407. this.$wrapper.fadeTo(FADE_DURATION_MS, 0, () => {
  408. this.$wrapper.css('visibility', 'hidden');
  409. this.isVisible = false;
  410. resolve();
  411. });
  412. });
  413. }
  414. /**
  415. * @return {boolean} switch on dominant speaker event if on stage.
  416. */
  417. stayOnStage () {
  418. return false;
  419. }
  420. /**
  421. * Sets the large video container background depending on the container
  422. * type and the parameter indicating if an avatar is currently shown on
  423. * large.
  424. *
  425. * @param {boolean} isAvatar - Indicates if the avatar is currently shown
  426. * on the large video.
  427. * @returns {void}
  428. */
  429. setLargeVideoBackground (isAvatar) {
  430. $("#largeVideoContainer").css("background",
  431. (this.videoType === VIDEO_CONTAINER_TYPE && !isAvatar)
  432. ? "#000" : interfaceConfig.DEFAULT_BACKGROUND);
  433. }
  434. }