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.

VideoContainer.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. * @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, videoHeight, videoSpaceWidth) {
  119. let horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  120. let verticalIndent = 0;// Top aligned
  121. return { horizontalIndent, verticalIndent };
  122. }
  123. /**
  124. * Container for user video.
  125. */
  126. export class VideoContainer extends LargeContainer {
  127. // FIXME: With Temasys we have to re-select everytime
  128. get $video () {
  129. return $('#largeVideo');
  130. }
  131. get id () {
  132. return getStreamOwnerId(this.stream);
  133. }
  134. constructor (onPlay, emitter) {
  135. super();
  136. this.stream = null;
  137. this.videoType = null;
  138. this.localFlipX = true;
  139. this.emitter = emitter;
  140. this.isVisible = false;
  141. /**
  142. * Flag indicates whether or not the avatar is currently displayed.
  143. * @type {boolean}
  144. */
  145. this.avatarDisplayed = false;
  146. this.$avatar = $('#dominantSpeaker');
  147. /**
  148. * A jQuery selector of the remote connection message.
  149. * @type {jQuery|HTMLElement}
  150. */
  151. this.$remoteConnectionMessage = $('#remoteConnectionMessage');
  152. /**
  153. * Indicates whether or not the video stream attached to the video
  154. * element has started(which means that there is any image rendered
  155. * even if the video is stalled).
  156. * @type {boolean}
  157. */
  158. this.wasVideoRendered = false;
  159. this.$wrapper = $('#largeVideoWrapper');
  160. this.avatarHeight = $("#dominantSpeakerAvatar").height();
  161. var onPlayCallback = function (event) {
  162. if (typeof onPlay === 'function') {
  163. onPlay(event);
  164. }
  165. this.wasVideoRendered = true;
  166. }.bind(this);
  167. // This does not work with Temasys plugin - has to be a property to be
  168. // copied between new <object> elements
  169. //this.$video.on('play', onPlay);
  170. this.$video[0].onplay = onPlayCallback;
  171. }
  172. /**
  173. * Enables a filter on the video which indicates that there are some
  174. * problems with the local media connection.
  175. *
  176. * @param {boolean} enable <tt>true</tt> if the filter is to be enabled or
  177. * <tt>false</tt> otherwise.
  178. */
  179. enableLocalConnectionProblemFilter (enable) {
  180. this.$video.toggleClass("videoProblemFilter", enable);
  181. }
  182. /**
  183. * Get size of video element.
  184. * @returns {{width, height}}
  185. */
  186. getStreamSize () {
  187. let video = this.$video[0];
  188. return {
  189. width: video.videoWidth,
  190. height: video.videoHeight
  191. };
  192. }
  193. /**
  194. * Calculate optimal video size for specified container size.
  195. * @param {number} containerWidth container width
  196. * @param {number} containerHeight container height
  197. * @returns {{availableWidth, availableHeight}}
  198. */
  199. getVideoSize (containerWidth, containerHeight) {
  200. let { width, height } = this.getStreamSize();
  201. if (this.stream && this.isScreenSharing()) {
  202. return getDesktopVideoSize( width,
  203. height,
  204. containerWidth,
  205. containerHeight);
  206. } else {
  207. return getCameraVideoSize( width,
  208. height,
  209. containerWidth,
  210. containerHeight);
  211. }
  212. }
  213. /**
  214. * Calculate optimal video position (offset for top left corner)
  215. * for specified video size and container size.
  216. * @param {number} width video width
  217. * @param {number} height video height
  218. * @param {number} containerWidth container width
  219. * @param {number} containerHeight container height
  220. * @returns {{horizontalIndent, verticalIndent}}
  221. */
  222. getVideoPosition (width, height, containerWidth, containerHeight) {
  223. if (this.stream && this.isScreenSharing()) {
  224. return getDesktopVideoPosition( width,
  225. height,
  226. containerWidth,
  227. containerHeight);
  228. } else {
  229. return getCameraVideoPosition( width,
  230. height,
  231. containerWidth,
  232. containerHeight);
  233. }
  234. }
  235. /**
  236. * Update position of the remote connection message which describes that
  237. * the remote user is having connectivity issues.
  238. */
  239. positionRemoteConnectionMessage () {
  240. if (this.avatarDisplayed) {
  241. let $avatarImage = $("#dominantSpeakerAvatar");
  242. this.$remoteConnectionMessage.css(
  243. 'top',
  244. $avatarImage.offset().top + $avatarImage.height() + 10);
  245. } else {
  246. let height = this.$remoteConnectionMessage.height();
  247. let parentHeight = this.$remoteConnectionMessage.parent().height();
  248. this.$remoteConnectionMessage.css(
  249. 'top', (parentHeight/2) - (height/2));
  250. }
  251. let width = this.$remoteConnectionMessage.width();
  252. let parentWidth = this.$remoteConnectionMessage.parent().width();
  253. this.$remoteConnectionMessage.css(
  254. 'left', ((parentWidth/2) - (width/2)));
  255. }
  256. resize (containerWidth, containerHeight, animate = false) {
  257. let [width, height]
  258. = this.getVideoSize(containerWidth, containerHeight);
  259. let { horizontalIndent, verticalIndent }
  260. = this.getVideoPosition(width, height,
  261. containerWidth, containerHeight);
  262. // update avatar position
  263. let top = containerHeight / 2 - this.avatarHeight / 4 * 3;
  264. this.$avatar.css('top', top);
  265. this.positionRemoteConnectionMessage();
  266. this.$wrapper.animate({
  267. width: width,
  268. height: height,
  269. top: verticalIndent,
  270. bottom: verticalIndent,
  271. left: horizontalIndent,
  272. right: horizontalIndent
  273. }, {
  274. queue: false,
  275. duration: animate ? 500 : 0
  276. });
  277. }
  278. /**
  279. * Update video stream.
  280. * @param {JitsiTrack?} stream new stream
  281. * @param {string} videoType video type
  282. */
  283. setStream (stream, videoType) {
  284. if (this.stream === stream) {
  285. return;
  286. } else {
  287. // The stream has changed, so the image will be lost on detach
  288. this.wasVideoRendered = false;
  289. }
  290. // detach old stream
  291. if (this.stream) {
  292. this.stream.detach(this.$video[0]);
  293. }
  294. this.stream = stream;
  295. this.videoType = videoType;
  296. if (!stream) {
  297. return;
  298. }
  299. stream.attach(this.$video[0]);
  300. let flipX = stream.isLocal() && this.localFlipX;
  301. this.$video.css({
  302. transform: flipX ? 'scaleX(-1)' : 'none'
  303. });
  304. }
  305. /**
  306. * Changes the flipX state of the local video.
  307. * @param val {boolean} true if flipped.
  308. */
  309. setLocalFlipX(val) {
  310. this.localFlipX = val;
  311. if(!this.$video || !this.stream || !this.stream.isLocal())
  312. return;
  313. this.$video.css({
  314. transform: this.localFlipX ? 'scaleX(-1)' : 'none'
  315. });
  316. }
  317. /**
  318. * Check if current video stream is screen sharing.
  319. * @returns {boolean}
  320. */
  321. isScreenSharing () {
  322. return this.videoType === 'desktop';
  323. }
  324. /**
  325. * Show or hide user avatar.
  326. * @param {boolean} show
  327. */
  328. showAvatar (show) {
  329. // TO FIX: Video background need to be black, so that we don't have a
  330. // flickering effect when scrolling between videos and have the screen
  331. // move to grey before going back to video. Avatars though can have the
  332. // default background set.
  333. // In order to fix this code we need to introduce video background or
  334. // find a workaround for the video flickering.
  335. $("#largeVideoContainer").css("background",
  336. (show) ? interfaceConfig.DEFAULT_BACKGROUND : "#000");
  337. this.$avatar.css("visibility", show ? "visible" : "hidden");
  338. this.avatarDisplayed = show;
  339. this.emitter.emit(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE, show);
  340. }
  341. /**
  342. * Indicates that the remote user who is currently displayed by this video
  343. * container is having connectivity issues.
  344. *
  345. * @param {boolean} show <tt>true</tt> to show or <tt>false</tt> to hide
  346. * the indication.
  347. */
  348. showRemoteConnectionProblemIndicator (show) {
  349. this.$video.toggleClass("remoteVideoProblemFilter", show);
  350. this.$avatar.toggleClass("remoteVideoProblemFilter", show);
  351. }
  352. // We are doing fadeOut/fadeIn animations on parent div which wraps
  353. // largeVideo, because when Temasys plugin is in use it replaces
  354. // <video> elements with plugin <object> tag. In Safari jQuery is
  355. // unable to store values on this plugin object which breaks all
  356. // animation effects performed on it directly.
  357. show () {
  358. // its already visible
  359. if (this.isVisible) {
  360. return Promise.resolve();
  361. }
  362. return new Promise((resolve) => {
  363. this.$wrapper.css('visibility', 'visible').fadeTo(
  364. FADE_DURATION_MS,
  365. 1,
  366. () => {
  367. this.isVisible = true;
  368. resolve();
  369. }
  370. );
  371. });
  372. }
  373. hide () {
  374. // as the container is hidden/replaced by another container
  375. // hide its avatar
  376. this.showAvatar(false);
  377. // its already hidden
  378. if (!this.isVisible) {
  379. return Promise.resolve();
  380. }
  381. return new Promise((resolve) => {
  382. this.$wrapper.fadeTo(FADE_DURATION_MS, 0, () => {
  383. this.$wrapper.css('visibility', 'hidden');
  384. this.isVisible = false;
  385. resolve();
  386. });
  387. });
  388. }
  389. /**
  390. * @return {boolean} switch on dominant speaker event if on stage.
  391. */
  392. stayOnStage () {
  393. return false;
  394. }
  395. }