Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LargeVideo.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. const RTCBrowserType = require("../../RTC/RTCBrowserType");
  8. const avatarSize = interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE;
  9. function getStreamId(stream) {
  10. if (stream.isLocal()) {
  11. return APP.conference.localId;
  12. } else {
  13. return stream.getParticipantId();
  14. }
  15. }
  16. /**
  17. * Returns an array of the video dimensions, so that it keeps it's aspect
  18. * ratio and fits available area with it's larger dimension. This method
  19. * ensures that whole video will be visible and can leave empty areas.
  20. *
  21. * @return an array with 2 elements, the video width and the video height
  22. */
  23. function getDesktopVideoSize(videoWidth,
  24. videoHeight,
  25. videoSpaceWidth,
  26. videoSpaceHeight) {
  27. let aspectRatio = videoWidth / videoHeight;
  28. let availableWidth = Math.max(videoWidth, videoSpaceWidth);
  29. let availableHeight = Math.max(videoHeight, videoSpaceHeight);
  30. videoSpaceHeight -= BottomToolbar.getFilmStripHeight();
  31. if (availableWidth / aspectRatio >= videoSpaceHeight) {
  32. availableHeight = videoSpaceHeight;
  33. availableWidth = availableHeight * aspectRatio;
  34. }
  35. if (availableHeight * aspectRatio >= videoSpaceWidth) {
  36. availableWidth = videoSpaceWidth;
  37. availableHeight = availableWidth / aspectRatio;
  38. }
  39. return { availableWidth, availableHeight };
  40. }
  41. /**
  42. * Returns an array of the video dimensions. It respects the
  43. * VIDEO_LAYOUT_FIT config, to fit the video to the screen, by hiding some parts
  44. * of it, or to fit it to the height or width.
  45. *
  46. * @param videoWidth the original video width
  47. * @param videoHeight the original video height
  48. * @param videoSpaceWidth the width of the video space
  49. * @param videoSpaceHeight the height of the video space
  50. * @return an array with 2 elements, the video width and the video height
  51. */
  52. function getCameraVideoSize(videoWidth,
  53. videoHeight,
  54. videoSpaceWidth,
  55. videoSpaceHeight) {
  56. let aspectRatio = videoWidth / videoHeight;
  57. let availableWidth = videoWidth;
  58. let availableHeight = videoHeight;
  59. if (interfaceConfig.VIDEO_LAYOUT_FIT == 'height') {
  60. availableHeight = videoSpaceHeight;
  61. availableWidth = availableHeight*aspectRatio;
  62. }
  63. else if (interfaceConfig.VIDEO_LAYOUT_FIT == 'width') {
  64. availableWidth = videoSpaceWidth;
  65. availableHeight = availableWidth/aspectRatio;
  66. }
  67. else if (interfaceConfig.VIDEO_LAYOUT_FIT == 'both') {
  68. availableWidth = Math.max(videoWidth, videoSpaceWidth);
  69. availableHeight = Math.max(videoHeight, videoSpaceHeight);
  70. if (availableWidth / aspectRatio < videoSpaceHeight) {
  71. availableHeight = videoSpaceHeight;
  72. availableWidth = availableHeight * aspectRatio;
  73. }
  74. if (availableHeight * aspectRatio < videoSpaceWidth) {
  75. availableWidth = videoSpaceWidth;
  76. availableHeight = availableWidth / aspectRatio;
  77. }
  78. }
  79. return { availableWidth, availableHeight };
  80. }
  81. /**
  82. * Returns an array of the video horizontal and vertical indents,
  83. * so that if fits its parent.
  84. *
  85. * @return an array with 2 elements, the horizontal indent and the vertical
  86. * indent
  87. */
  88. function getCameraVideoPosition(videoWidth,
  89. videoHeight,
  90. videoSpaceWidth,
  91. videoSpaceHeight) {
  92. // Parent height isn't completely calculated when we position the video in
  93. // full screen mode and this is why we use the screen height in this case.
  94. // Need to think it further at some point and implement it properly.
  95. if (UIUtil.isFullScreen()) {
  96. videoSpaceHeight = window.innerHeight;
  97. }
  98. let horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  99. let verticalIndent = (videoSpaceHeight - videoHeight) / 2;
  100. return { horizontalIndent, verticalIndent };
  101. }
  102. /**
  103. * Returns an array of the video horizontal and vertical indents.
  104. * Centers horizontally and top aligns vertically.
  105. *
  106. * @return an array with 2 elements, the horizontal indent and the vertical
  107. * indent
  108. */
  109. function getDesktopVideoPosition(videoWidth,
  110. videoHeight,
  111. videoSpaceWidth,
  112. videoSpaceHeight) {
  113. let horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  114. let verticalIndent = 0;// Top aligned
  115. return { horizontalIndent, verticalIndent };
  116. }
  117. export const VideoContainerType = "video";
  118. class VideoContainer extends LargeContainer {
  119. // FIXME: With Temasys we have to re-select everytime
  120. get $video () {
  121. return $('#largeVideo');
  122. }
  123. get id () {
  124. if (this.stream) {
  125. return getStreamId(this.stream);
  126. }
  127. }
  128. constructor (onPlay) {
  129. super();
  130. this.stream = null;
  131. this.videoType = null;
  132. this.$avatar = $('#activeSpeaker');
  133. this.$wrapper = $('#largeVideoWrapper');
  134. if (!RTCBrowserType.isIExplorer()) {
  135. this.$video.volume = 0;
  136. }
  137. this.$video.on('play', onPlay);
  138. }
  139. getStreamSize () {
  140. let video = this.$video[0];
  141. return {
  142. width: video.videoWidth,
  143. height: video.videoHeight
  144. };
  145. }
  146. getVideoSize (containerWidth, containerHeight) {
  147. let { width, height } = this.getStreamSize();
  148. if (this.stream && this.isScreenSharing()) {
  149. return getDesktopVideoSize(width, height, containerWidth, containerHeight);
  150. } else {
  151. return getCameraVideoSize(width, height, containerWidth, containerHeight);
  152. }
  153. }
  154. getVideoPosition (width, height, containerWidth, containerHeight) {
  155. if (this.stream && this.isScreenSharing()) {
  156. return getDesktopVideoPosition(width, height, containerWidth, containerHeight);
  157. } else {
  158. return getCameraVideoPosition(width, height, containerWidth, containerHeight);
  159. }
  160. }
  161. resize (containerWidth, containerHeight, animate = false) {
  162. let { width, height } = this.getVideoSize(containerWidth, containerHeight);
  163. let { horizontalIndent, verticalIndent } = this.getVideoPosition(width, height, containerWidth, containerHeight);
  164. // update avatar position
  165. let top = this.containerHeight / 2 - avatarSize / 4 * 3;
  166. this.$avatar.css('top', top);
  167. this.$wrapper.animate({
  168. width,
  169. height,
  170. top: verticalIndent,
  171. bottom: verticalIndent,
  172. left: horizontalIndent,
  173. right: horizontalIndent
  174. }, {
  175. queue: false,
  176. duration: animate ? 500 : 0
  177. });
  178. }
  179. setStream (stream, videoType) {
  180. this.stream = stream;
  181. this.videoType = videoType;
  182. stream.attach(this.$video);
  183. let flipX = stream.isLocal() && !this.isScreenSharing();
  184. this.$video.css({
  185. transform: flipX ? 'scaleX(-1)' : 'none'
  186. });
  187. }
  188. isScreenSharing () {
  189. return this.videoType === 'desktop';
  190. }
  191. showAvatar (show) {
  192. this.$avatar.css("visibility", show ? "visible" : "hidden");
  193. }
  194. // We are doing fadeOut/fadeIn animations on parent div which wraps
  195. // largeVideo, because when Temasys plugin is in use it replaces
  196. // <video> elements with plugin <object> tag. In Safari jQuery is
  197. // unable to store values on this plugin object which breaks all
  198. // animation effects performed on it directly.
  199. show () {
  200. let $wrapper = this.$wrapper;
  201. return new Promise(resolve => {
  202. $wrapper.fadeIn(300, function () {
  203. $wrapper.css({visibility: 'visible'});
  204. $('.watermark').css({visibility: 'visible'});
  205. });
  206. resolve();
  207. });
  208. }
  209. hide () {
  210. this.showAvatar(false);
  211. let $wrapper = this.$wrapper;
  212. return new Promise(resolve => {
  213. $wrapper.fadeOut(300, function () {
  214. $wrapper.css({visibility: 'hidden'});
  215. $('.watermark').css({visibility: 'hidden'});
  216. resolve();
  217. });
  218. });
  219. }
  220. }
  221. export default class LargeVideoManager {
  222. constructor () {
  223. this.containers = {};
  224. this.state = VideoContainerType;
  225. this.videoContainer = new VideoContainer(() => this.resizeContainer(VideoContainerType));
  226. this.addContainer(VideoContainerType, this.videoContainer);
  227. this.width = 0;
  228. this.height = 0;
  229. this.$container = $('#largeVideoContainer');
  230. this.$container.css({
  231. display: 'inline-block'
  232. });
  233. if (interfaceConfig.SHOW_JITSI_WATERMARK) {
  234. let leftWatermarkDiv = this.$container.find("div.watermark.leftwatermark");
  235. leftWatermarkDiv.css({display: 'block'});
  236. leftWatermarkDiv.parent().attr('href', interfaceConfig.JITSI_WATERMARK_LINK);
  237. }
  238. if (interfaceConfig.SHOW_BRAND_WATERMARK) {
  239. let rightWatermarkDiv = this.$container.find("div.watermark.rightwatermark");
  240. rightWatermarkDiv.css({
  241. display: 'block',
  242. backgroundImage: 'url(images/rightwatermark.png)'
  243. });
  244. rightWatermarkDiv.parent().attr('href', interfaceConfig.BRAND_WATERMARK_LINK);
  245. }
  246. if (interfaceConfig.SHOW_POWERED_BY) {
  247. this.$container.children("a.poweredby").css({display: 'block'});
  248. }
  249. this.$container.hover(
  250. e => this.onHoverIn(e),
  251. e => this.onHoverOut(e)
  252. );
  253. }
  254. onHoverIn (e) {
  255. if (!this.state) {
  256. return;
  257. }
  258. let container = this.getContainer(this.state);
  259. container.onHoverIn(e);
  260. }
  261. onHoverOut (e) {
  262. if (!this.state) {
  263. return;
  264. }
  265. let container = this.getContainer(this.state);
  266. container.onHoverOut(e);
  267. }
  268. get id () {
  269. return this.videoContainer.id;
  270. }
  271. updateLargeVideo (stream, videoType) {
  272. let id = getStreamId(stream);
  273. let container = this.getContainer(this.state);
  274. container.hide().then(() => {
  275. console.info("hover in %s", id);
  276. this.state = VideoContainerType;
  277. this.videoContainer.setStream(stream, videoType);
  278. this.videoContainer.show();
  279. });
  280. }
  281. updateContainerSize (isSideBarVisible) {
  282. this.width = UIUtil.getAvailableVideoWidth(isSideBarVisible);
  283. this.height = window.innerHeight;
  284. }
  285. resizeContainer (type, animate = false) {
  286. let container = this.getContainer(type);
  287. container.resize(this.width, this.height, animate);
  288. }
  289. resize (animate) {
  290. // resize all containers
  291. Object.keys(this.containers).forEach(type => this.resizeContainer(type, animate));
  292. this.$container.animate({
  293. width: this.width,
  294. height: this.height
  295. }, {
  296. queue: false,
  297. duration: animate ? 500 : 0
  298. });
  299. }
  300. /**
  301. * Enables/disables the filter indicating a video problem to the user.
  302. *
  303. * @param enable <tt>true</tt> to enable, <tt>false</tt> to disable
  304. */
  305. enableVideoProblemFilter (enable) {
  306. this.videoContainer.$video.toggleClass("videoProblemFilter", enable);
  307. }
  308. /**
  309. * Updates the src of the active speaker avatar
  310. */
  311. updateAvatar (thumbUrl) {
  312. $("#activeSpeakerAvatar").attr('src', thumbUrl);
  313. }
  314. showAvatar (show) {
  315. this.videoContainer.showAvatar(show);
  316. }
  317. addContainer (type, container) {
  318. if (this.containers[type]) {
  319. throw new Error(`container of type ${type} already exist`);
  320. }
  321. this.containers[type] = container;
  322. this.resizeContainer(type);
  323. }
  324. getContainer (type) {
  325. let container = this.containers[type];
  326. if (!container) {
  327. throw new Error(`container of type ${type} doesn't exist`);
  328. }
  329. return container;
  330. }
  331. removeContainer (type) {
  332. if (!this.containers[type]) {
  333. throw new Error(`container of type ${type} doesn't exist`);
  334. }
  335. delete this.containers[type];
  336. }
  337. showContainer (type) {
  338. if (this.state === type) {
  339. return Promise.resolve();
  340. }
  341. let container = this.getContainer(type);
  342. if (this.state) {
  343. let oldContainer = this.containers[this.state];
  344. if (oldContainer) {
  345. oldContainer.hide();
  346. }
  347. }
  348. this.state = type;
  349. return container.show();
  350. }
  351. }