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.

LargeVideo.js 13KB

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