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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. /**
  150. * Updates the src of the active speaker avatar
  151. * @param jid of the current active speaker
  152. */
  153. function updateActiveSpeakerAvatarSrc() {
  154. var avatar = $("#activeSpeakerAvatar")[0];
  155. var jid = currentSmallVideo.peerJid;
  156. var url = Avatar.getGravatarUrl(jid);
  157. if(avatar.src === url)
  158. return;
  159. var isMuted = null;
  160. if(!LargeVideo.VideoLayout.isInLastN(currentSmallVideo.resourceJid)) {
  161. isMuted = true;
  162. }
  163. else
  164. {
  165. isMuted = APP.RTC.isVideoMuted(jid);
  166. }
  167. if (jid && isMuted !== null) {
  168. avatar.src = url;
  169. $("#largeVideo").css("visibility", isMuted ? "hidden" : "visible");
  170. currentSmallVideo.showAvatar(isMuted);
  171. }
  172. }
  173. function changeVideo(isVisible) {
  174. updateActiveSpeakerAvatarSrc();
  175. APP.RTC.setVideoSrc($('#largeVideo')[0], currentSmallVideo.getSrc());
  176. var videoTransform = document.getElementById('largeVideo')
  177. .style.webkitTransform;
  178. var flipX = currentSmallVideo.flipX;
  179. if (flipX && videoTransform !== 'scaleX(-1)') {
  180. document.getElementById('largeVideo').style.webkitTransform
  181. = "scaleX(-1)";
  182. }
  183. else if (!flipX && videoTransform === 'scaleX(-1)') {
  184. document.getElementById('largeVideo').style.webkitTransform
  185. = "none";
  186. }
  187. var isDesktop = APP.RTC.isVideoSrcDesktop(currentSmallVideo.peerJid);
  188. // Change the way we'll be measuring and positioning large video
  189. getVideoSize = isDesktop
  190. ? getDesktopVideoSize
  191. : getCameraVideoSize;
  192. getVideoPosition = isDesktop
  193. ? getDesktopVideoPosition
  194. : getCameraVideoPosition;
  195. // Only if the large video is currently visible.
  196. // Disable previous dominant speaker video.
  197. if (oldSmallVideo) {
  198. oldSmallVideo.enableDominantSpeaker(false);
  199. }
  200. // Enable new dominant speaker in the remote videos section.
  201. if (currentSmallVideo) {
  202. currentSmallVideo.enableDominantSpeaker(true);
  203. }
  204. if (isVisible) {
  205. // using "this" should be ok because we're called
  206. // from within the fadeOut event.
  207. $(this).fadeIn(300);
  208. }
  209. if(oldSmallVideo)
  210. oldSmallVideo.showAvatar();
  211. }
  212. var LargeVideo = {
  213. init: function (VideoLayout, emitter) {
  214. this.VideoLayout = VideoLayout;
  215. this.eventEmitter = emitter;
  216. var self = this;
  217. // Listen for large video size updates
  218. document.getElementById('largeVideo')
  219. .addEventListener('loadedmetadata', function (e) {
  220. currentVideoWidth = this.videoWidth;
  221. currentVideoHeight = this.videoHeight;
  222. self.position(currentVideoWidth, currentVideoHeight);
  223. });
  224. },
  225. /**
  226. * Indicates if the large video is currently visible.
  227. *
  228. * @return <tt>true</tt> if visible, <tt>false</tt> - otherwise
  229. */
  230. isLargeVideoVisible: function() {
  231. return video.is(':visible');
  232. },
  233. /**
  234. * Updates the large video with the given new video source.
  235. */
  236. updateLargeVideo: function(resourceJid, forceUpdate) {
  237. console.log('hover in', resourceJid);
  238. var newSmallVideo = this.VideoLayout.getSmallVideo(resourceJid);
  239. if ((currentSmallVideo && currentSmallVideo.resourceJid !== resourceJid)
  240. || forceUpdate) {
  241. $('#activeSpeaker').css('visibility', 'hidden');
  242. if(currentSmallVideo) {
  243. oldSmallVideo = currentSmallVideo;
  244. } else {
  245. oldSmallVideo = null;
  246. }
  247. currentSmallVideo = newSmallVideo;
  248. var oldJid = null;
  249. if(oldSmallVideo)
  250. oldJid = oldSmallVideo.peerJid;
  251. if (oldJid !== resourceJid) {
  252. // we want the notification to trigger even if userJid is undefined,
  253. // or null.
  254. this.eventEmitter.emit(UIEvents.SELECTED_ENDPOINT,
  255. resourceJid);
  256. }
  257. video.fadeOut(300, changeVideo.bind(video, this.isLargeVideoVisible()));
  258. } else {
  259. if(currentSmallVideo) {
  260. currentSmallVideo.showAvatar();
  261. }
  262. }
  263. },
  264. /**
  265. * Shows/hides the large video.
  266. */
  267. setLargeVideoVisible: function(isVisible) {
  268. if (isVisible) {
  269. $('#largeVideo').css({visibility: 'visible'});
  270. $('.watermark').css({visibility: 'visible'});
  271. if(currentSmallVideo)
  272. currentSmallVideo.enableDominantSpeaker(true);
  273. }
  274. else {
  275. $('#largeVideo').css({visibility: 'hidden'});
  276. $('#activeSpeaker').css('visibility', 'hidden');
  277. $('.watermark').css({visibility: 'hidden'});
  278. if(currentSmallVideo)
  279. currentSmallVideo.enableDominantSpeaker(false);
  280. }
  281. },
  282. onVideoTypeChanged: function (jid) {
  283. if(jid && currentSmallVideo && jid === currentSmallVideo.peerJid)
  284. {
  285. var isDesktop = APP.RTC.isVideoSrcDesktop(jid);
  286. getVideoSize = isDesktop
  287. ? getDesktopVideoSize
  288. : getCameraVideoSize;
  289. getVideoPosition = isDesktop
  290. ? getDesktopVideoPosition
  291. : getCameraVideoPosition;
  292. this.position(null, null);
  293. }
  294. },
  295. /**
  296. * Positions the large video.
  297. *
  298. * @param videoWidth the stream video width
  299. * @param videoHeight the stream video height
  300. */
  301. position: function (videoWidth, videoHeight,
  302. videoSpaceWidth, videoSpaceHeight, animate) {
  303. if(!videoSpaceWidth)
  304. videoSpaceWidth = $('#videospace').width();
  305. if(!videoSpaceHeight)
  306. videoSpaceHeight = window.innerHeight;
  307. var videoSize = getVideoSize(videoWidth,
  308. videoHeight,
  309. videoSpaceWidth,
  310. videoSpaceHeight);
  311. var largeVideoWidth = videoSize[0];
  312. var largeVideoHeight = videoSize[1];
  313. var videoPosition = getVideoPosition(largeVideoWidth,
  314. largeVideoHeight,
  315. videoSpaceWidth,
  316. videoSpaceHeight);
  317. var horizontalIndent = videoPosition[0];
  318. var verticalIndent = videoPosition[1];
  319. positionVideo($('#largeVideo'),
  320. largeVideoWidth,
  321. largeVideoHeight,
  322. horizontalIndent, verticalIndent, animate);
  323. },
  324. isLargeVideoOnTop: function () {
  325. var Etherpad = require("../etherpad/Etherpad");
  326. var Prezi = require("../prezi/Prezi");
  327. return !Prezi.isPresentationVisible() && !Etherpad.isVisible();
  328. },
  329. resize: function (animate, isVisible, completeFunction) {
  330. var availableHeight = window.innerHeight;
  331. var availableWidth = UIUtil.getAvailableVideoWidth(isVisible);
  332. if (availableWidth < 0 || availableHeight < 0) return;
  333. var avatarSize = interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE;
  334. var top = availableHeight / 2 - avatarSize / 4 * 3;
  335. $('#activeSpeaker').css('top', top);
  336. if(animate)
  337. {
  338. $('#videospace').animate({
  339. right: window.innerWidth - availableWidth,
  340. width: availableWidth,
  341. height: availableHeight
  342. },
  343. {
  344. queue: false,
  345. duration: 500,
  346. complete: completeFunction
  347. });
  348. $('#largeVideoContainer').animate({
  349. width: availableWidth,
  350. height: availableHeight
  351. },
  352. {
  353. queue: false,
  354. duration: 500
  355. });
  356. }
  357. else
  358. {
  359. $('#videospace').width(availableWidth);
  360. $('#videospace').height(availableHeight);
  361. $('#largeVideoContainer').width(availableWidth);
  362. $('#largeVideoContainer').height(availableHeight);
  363. }
  364. return [availableWidth, availableHeight];
  365. },
  366. resizeVideoAreaAnimated: function (isVisible, completeFunction) {
  367. var size = this.resize(true, isVisible, completeFunction);
  368. this.position(null, null, size[0], size[1], true);
  369. },
  370. getResourceJid: function () {
  371. if(!currentSmallVideo)
  372. return null;
  373. return currentSmallVideo.resourceJid;
  374. },
  375. updateAvatar: function (resourceJid) {
  376. if (resourceJid === this.getResourceJid()) {
  377. updateActiveSpeakerAvatarSrc();
  378. }
  379. },
  380. showAvatar: function (resourceJid, show) {
  381. if(this.getResourceJid() === resourceJid
  382. && LargeVideo.isLargeVideoOnTop())
  383. {
  384. $("#largeVideo").css("visibility", show ? "hidden" : "visible");
  385. $('#activeSpeaker').css("visibility", show ? "visible" : "hidden");
  386. return true;
  387. }
  388. return false;
  389. }
  390. }
  391. module.exports = LargeVideo;