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 14KB

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