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

VideoContainer.js 14KB

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