您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VideoContainer.js 12KB

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