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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. var Avatar = require("../avatar/Avatar");
  2. var UIUtil = require("../util/UIUtil");
  3. var UIEvents = require("../../../service/UI/UIEvents");
  4. var xmpp = require("../../xmpp/xmpp");
  5. var video = $('#largeVideo');
  6. var currentVideoWidth = null;
  7. var currentVideoHeight = null;
  8. // By default we use camera
  9. var getVideoSize = getCameraVideoSize;
  10. var getVideoPosition = getCameraVideoPosition;
  11. var currentSmallVideo = null;
  12. var oldSmallVideo = null;
  13. /**
  14. * Sets the size and position of the given video element.
  15. *
  16. * @param video the video element to position
  17. * @param width the desired video width
  18. * @param height the desired video height
  19. * @param horizontalIndent the left and right indent
  20. * @param verticalIndent the top and bottom indent
  21. */
  22. function positionVideo(video,
  23. width,
  24. height,
  25. horizontalIndent,
  26. verticalIndent,
  27. animate) {
  28. if(animate)
  29. {
  30. video.animate({
  31. width: width,
  32. height: height,
  33. top: verticalIndent,
  34. bottom: verticalIndent,
  35. left: horizontalIndent,
  36. right: horizontalIndent
  37. },
  38. {
  39. queue: false,
  40. duration: 500
  41. });
  42. }
  43. else
  44. {
  45. video.width(width);
  46. video.height(height);
  47. video.css({ top: verticalIndent + 'px',
  48. bottom: verticalIndent + 'px',
  49. left: horizontalIndent + 'px',
  50. right: horizontalIndent + 'px'});
  51. }
  52. }
  53. /**
  54. * Returns an array of the video dimensions, so that it keeps it's aspect
  55. * ratio and fits available area with it's larger dimension. This method
  56. * ensures that whole video will be visible and can leave empty areas.
  57. *
  58. * @return an array with 2 elements, the video width and the video height
  59. */
  60. function getDesktopVideoSize(videoWidth,
  61. videoHeight,
  62. videoSpaceWidth,
  63. videoSpaceHeight) {
  64. if (!videoWidth)
  65. videoWidth = currentVideoWidth;
  66. if (!videoHeight)
  67. videoHeight = currentVideoHeight;
  68. var aspectRatio = videoWidth / videoHeight;
  69. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  70. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  71. videoSpaceHeight -= $('#remoteVideos').outerHeight();
  72. if (availableWidth / aspectRatio >= videoSpaceHeight)
  73. {
  74. availableHeight = videoSpaceHeight;
  75. availableWidth = availableHeight * aspectRatio;
  76. }
  77. if (availableHeight * aspectRatio >= videoSpaceWidth)
  78. {
  79. availableWidth = videoSpaceWidth;
  80. availableHeight = availableWidth / aspectRatio;
  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. var isFullScreen = document.fullScreen ||
  99. document.mozFullScreen ||
  100. document.webkitIsFullScreen;
  101. if (isFullScreen)
  102. videoSpaceHeight = window.innerHeight;
  103. var horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  104. var verticalIndent = (videoSpaceHeight - videoHeight) / 2;
  105. return [horizontalIndent, verticalIndent];
  106. }
  107. /**
  108. * Returns an array of the video horizontal and vertical indents.
  109. * Centers horizontally and top aligns vertically.
  110. *
  111. * @return an array with 2 elements, the horizontal indent and the vertical
  112. * indent
  113. */
  114. function getDesktopVideoPosition(videoWidth,
  115. videoHeight,
  116. videoSpaceWidth,
  117. videoSpaceHeight) {
  118. var horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  119. var verticalIndent = 0;// Top aligned
  120. return [horizontalIndent, verticalIndent];
  121. }
  122. /**
  123. * Returns an array of the video dimensions, so that it covers the screen.
  124. * It leaves no empty areas, but some parts of the video might not be visible.
  125. *
  126. * @return an array with 2 elements, the video width and the video height
  127. */
  128. function getCameraVideoSize(videoWidth,
  129. videoHeight,
  130. videoSpaceWidth,
  131. videoSpaceHeight) {
  132. if (!videoWidth)
  133. videoWidth = currentVideoWidth;
  134. if (!videoHeight)
  135. videoHeight = currentVideoHeight;
  136. var aspectRatio = videoWidth / videoHeight;
  137. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  138. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  139. if (availableWidth / aspectRatio < videoSpaceHeight) {
  140. availableHeight = videoSpaceHeight;
  141. availableWidth = availableHeight * aspectRatio;
  142. }
  143. if (availableHeight * aspectRatio < videoSpaceWidth) {
  144. availableWidth = videoSpaceWidth;
  145. availableHeight = availableWidth / aspectRatio;
  146. }
  147. return [availableWidth, availableHeight];
  148. }
  149. function changeVideo(isVisible) {
  150. Avatar.updateActiveSpeakerAvatarSrc(currentSmallVideo.peerJid);
  151. APP.RTC.setVideoSrc($('#largeVideo')[0], currentSmallVideo.getSrc());
  152. var videoTransform = document.getElementById('largeVideo')
  153. .style.webkitTransform;
  154. var flipX = currentSmallVideo.flipX;
  155. if (flipX && videoTransform !== 'scaleX(-1)') {
  156. document.getElementById('largeVideo').style.webkitTransform
  157. = "scaleX(-1)";
  158. }
  159. else if (!flipX && videoTransform === 'scaleX(-1)') {
  160. document.getElementById('largeVideo').style.webkitTransform
  161. = "none";
  162. }
  163. var isDesktop = APP.RTC.isVideoSrcDesktop(currentSmallVideo.peerJid);
  164. // Change the way we'll be measuring and positioning large video
  165. getVideoSize = isDesktop
  166. ? getDesktopVideoSize
  167. : getCameraVideoSize;
  168. getVideoPosition = isDesktop
  169. ? getDesktopVideoPosition
  170. : getCameraVideoPosition;
  171. // Only if the large video is currently visible.
  172. // Disable previous dominant speaker video.
  173. if (oldSmallVideo) {
  174. oldSmallVideo.enableDominantSpeaker(false);
  175. }
  176. // Enable new dominant speaker in the remote videos section.
  177. if (currentSmallVideo) {
  178. currentSmallVideo.enableDominantSpeaker(true);
  179. }
  180. if (isVisible) {
  181. // using "this" should be ok because we're called
  182. // from within the fadeOut event.
  183. $(this).fadeIn(300);
  184. }
  185. if(oldSmallVideo)
  186. Avatar.showUserAvatar(oldSmallVideo.peerJid);
  187. }
  188. var LargeVideo = {
  189. init: function (VideoLayout, emitter) {
  190. this.VideoLayout = VideoLayout;
  191. this.eventEmitter = emitter;
  192. var self = this;
  193. // Listen for large video size updates
  194. document.getElementById('largeVideo')
  195. .addEventListener('loadedmetadata', function (e) {
  196. currentVideoWidth = this.videoWidth;
  197. currentVideoHeight = this.videoHeight;
  198. self.position(currentVideoWidth, currentVideoHeight);
  199. });
  200. },
  201. /**
  202. * Indicates if the large video is currently visible.
  203. *
  204. * @return <tt>true</tt> if visible, <tt>false</tt> - otherwise
  205. */
  206. isLargeVideoVisible: function() {
  207. return video.is(':visible');
  208. },
  209. /**
  210. * Updates the large video with the given new video source.
  211. */
  212. updateLargeVideo: function(resourceJid, forceUpdate) {
  213. console.log('hover in', resourceJid);
  214. var newSmallVideo = this.VideoLayout.getSmallVideo(resourceJid);
  215. if ((currentSmallVideo && currentSmallVideo.resourceJid !== resourceJid)
  216. || forceUpdate) {
  217. $('#activeSpeaker').css('visibility', 'hidden');
  218. if(currentSmallVideo) {
  219. oldSmallVideo = currentSmallVideo;
  220. } else {
  221. oldSmallVideo = null;
  222. }
  223. currentSmallVideo = newSmallVideo;
  224. var oldJid = null;
  225. if(oldSmallVideo)
  226. oldJid = oldSmallVideo.peerJid;
  227. if (oldJid !== resourceJid) {
  228. // we want the notification to trigger even if userJid is undefined,
  229. // or null.
  230. this.eventEmitter.emit(UIEvents.SELECTED_ENDPOINT,
  231. resourceJid);
  232. }
  233. video.fadeOut(300, changeVideo.bind(video, this.isLargeVideoVisible()));
  234. } else {
  235. var jid = null;
  236. if(currentSmallVideo)
  237. jid = currentSmallVideo.peerJid;
  238. Avatar.showUserAvatar(jid);
  239. }
  240. },
  241. /**
  242. * Shows/hides the large video.
  243. */
  244. setLargeVideoVisible: function(isVisible) {
  245. if (isVisible) {
  246. $('#largeVideo').css({visibility: 'visible'});
  247. $('.watermark').css({visibility: 'visible'});
  248. if(currentSmallVideo)
  249. currentSmallVideo.enableDominantSpeaker(true);
  250. }
  251. else {
  252. $('#largeVideo').css({visibility: 'hidden'});
  253. $('#activeSpeaker').css('visibility', 'hidden');
  254. $('.watermark').css({visibility: 'hidden'});
  255. if(currentSmallVideo)
  256. currentSmallVideo.enableDominantSpeaker(false);
  257. }
  258. },
  259. onVideoTypeChanged: function (jid) {
  260. if(jid && currentSmallVideo && jid === currentSmallVideo.peerJid)
  261. {
  262. var isDesktop = APP.RTC.isVideoSrcDesktop(jid);
  263. getVideoSize = isDesktop
  264. ? getDesktopVideoSize
  265. : getCameraVideoSize;
  266. getVideoPosition = isDesktop
  267. ? getDesktopVideoPosition
  268. : getCameraVideoPosition;
  269. this.position(null, null);
  270. }
  271. },
  272. /**
  273. * Positions the large video.
  274. *
  275. * @param videoWidth the stream video width
  276. * @param videoHeight the stream video height
  277. */
  278. position: function (videoWidth, videoHeight,
  279. videoSpaceWidth, videoSpaceHeight, animate) {
  280. if(!videoSpaceWidth)
  281. videoSpaceWidth = $('#videospace').width();
  282. if(!videoSpaceHeight)
  283. videoSpaceHeight = window.innerHeight;
  284. var videoSize = getVideoSize(videoWidth,
  285. videoHeight,
  286. videoSpaceWidth,
  287. videoSpaceHeight);
  288. var largeVideoWidth = videoSize[0];
  289. var largeVideoHeight = videoSize[1];
  290. var videoPosition = getVideoPosition(largeVideoWidth,
  291. largeVideoHeight,
  292. videoSpaceWidth,
  293. videoSpaceHeight);
  294. var horizontalIndent = videoPosition[0];
  295. var verticalIndent = videoPosition[1];
  296. positionVideo($('#largeVideo'),
  297. largeVideoWidth,
  298. largeVideoHeight,
  299. horizontalIndent, verticalIndent, animate);
  300. },
  301. isLargeVideoOnTop: function () {
  302. var Etherpad = require("../etherpad/Etherpad");
  303. var Prezi = require("../prezi/Prezi");
  304. return !Prezi.isPresentationVisible() && !Etherpad.isVisible();
  305. },
  306. resize: function (animate, isVisible, completeFunction) {
  307. var availableHeight = window.innerHeight;
  308. var availableWidth = UIUtil.getAvailableVideoWidth(isVisible);
  309. if (availableWidth < 0 || availableHeight < 0) return;
  310. var avatarSize = interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE;
  311. var top = availableHeight / 2 - avatarSize / 4 * 3;
  312. $('#activeSpeaker').css('top', top);
  313. if(animate)
  314. {
  315. $('#videospace').animate({
  316. right: window.innerWidth - availableWidth,
  317. width: availableWidth,
  318. height: availableHeight
  319. },
  320. {
  321. queue: false,
  322. duration: 500,
  323. complete: completeFunction
  324. });
  325. $('#largeVideoContainer').animate({
  326. width: availableWidth,
  327. height: availableHeight
  328. },
  329. {
  330. queue: false,
  331. duration: 500
  332. });
  333. }
  334. else
  335. {
  336. $('#videospace').width(availableWidth);
  337. $('#videospace').height(availableHeight);
  338. $('#largeVideoContainer').width(availableWidth);
  339. $('#largeVideoContainer').height(availableHeight);
  340. }
  341. return [availableWidth, availableHeight];
  342. },
  343. resizeVideoAreaAnimated: function (isVisible, completeFunction) {
  344. var size = this.resize(true, isVisible, completeFunction);
  345. this.position(null, null, size[0], size[1], true);
  346. },
  347. getResourceJid: function () {
  348. if(!currentSmallVideo)
  349. return null;
  350. return currentSmallVideo.peerJid;
  351. }
  352. }
  353. module.exports = LargeVideo;