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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /* global $, APP, Strophe, interfaceConfig */
  2. var Avatar = require("../avatar/Avatar");
  3. var RTCBrowserType = require("../../RTC/RTCBrowserType");
  4. var UIUtil = require("../util/UIUtil");
  5. var UIEvents = require("../../../service/UI/UIEvents");
  6. var xmpp = require("../../xmpp/xmpp");
  7. var ToolbarToggler = require("../toolbars/ToolbarToggler");
  8. // FIXME: With Temasys we have to re-select everytime
  9. //var video = $('#largeVideo');
  10. var currentVideoWidth = null;
  11. var currentVideoHeight = null;
  12. // By default we use camera
  13. var getVideoSize = getCameraVideoSize;
  14. var getVideoPosition = getCameraVideoPosition;
  15. /**
  16. * The small video instance that is displayed in the large video
  17. * @type {SmallVideo}
  18. */
  19. var currentSmallVideo = null;
  20. /**
  21. * Indicates whether the large video is enabled.
  22. * @type {boolean}
  23. */
  24. var isEnabled = true;
  25. /**
  26. * Current large video state.
  27. * Possible values - video, prezi or etherpad.
  28. * @type {string}
  29. */
  30. var state = "video";
  31. /**
  32. * Returns the html element associated with the passed state of large video
  33. * @param state the state.
  34. * @returns {JQuery|*|jQuery|HTMLElement} the container.
  35. */
  36. function getContainerByState(state)
  37. {
  38. var selector = null;
  39. switch (state)
  40. {
  41. case "video":
  42. selector = "#largeVideo";
  43. break;
  44. case "etherpad":
  45. selector = "#etherpad>iframe";
  46. break;
  47. case "prezi":
  48. selector = "#presentation>iframe";
  49. break;
  50. }
  51. return (selector !== null)? $(selector) : null;
  52. }
  53. /**
  54. * Sets the size and position of the given video element.
  55. *
  56. * @param video the video element to position
  57. * @param width the desired video width
  58. * @param height the desired video height
  59. * @param horizontalIndent the left and right indent
  60. * @param verticalIndent the top and bottom indent
  61. */
  62. function positionVideo(video,
  63. width,
  64. height,
  65. horizontalIndent,
  66. verticalIndent,
  67. animate) {
  68. if (animate) {
  69. video.animate({
  70. width: width,
  71. height: height,
  72. top: verticalIndent,
  73. bottom: verticalIndent,
  74. left: horizontalIndent,
  75. right: horizontalIndent
  76. },
  77. {
  78. queue: false,
  79. duration: 500
  80. });
  81. } else {
  82. video.width(width);
  83. video.height(height);
  84. video.css({ top: verticalIndent + 'px',
  85. bottom: verticalIndent + 'px',
  86. left: horizontalIndent + 'px',
  87. right: horizontalIndent + 'px'});
  88. }
  89. }
  90. /**
  91. * Returns an array of the video dimensions, so that it keeps it's aspect
  92. * ratio and fits available area with it's larger dimension. This method
  93. * ensures that whole video will be visible and can leave empty areas.
  94. *
  95. * @return an array with 2 elements, the video width and the video height
  96. */
  97. function getDesktopVideoSize(videoWidth,
  98. videoHeight,
  99. videoSpaceWidth,
  100. videoSpaceHeight) {
  101. if (!videoWidth)
  102. videoWidth = currentVideoWidth;
  103. if (!videoHeight)
  104. videoHeight = currentVideoHeight;
  105. var aspectRatio = videoWidth / videoHeight;
  106. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  107. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  108. videoSpaceHeight -= $('#remoteVideos').outerHeight();
  109. if (availableWidth / aspectRatio >= videoSpaceHeight)
  110. {
  111. availableHeight = videoSpaceHeight;
  112. availableWidth = availableHeight * aspectRatio;
  113. }
  114. if (availableHeight * aspectRatio >= videoSpaceWidth)
  115. {
  116. availableWidth = videoSpaceWidth;
  117. availableHeight = availableWidth / aspectRatio;
  118. }
  119. return [availableWidth, availableHeight];
  120. }
  121. /**
  122. * Returns an array of the video horizontal and vertical indents,
  123. * so that if fits its parent.
  124. *
  125. * @return an array with 2 elements, the horizontal indent and the vertical
  126. * indent
  127. */
  128. function getCameraVideoPosition(videoWidth,
  129. videoHeight,
  130. videoSpaceWidth,
  131. videoSpaceHeight) {
  132. // Parent height isn't completely calculated when we position the video in
  133. // full screen mode and this is why we use the screen height in this case.
  134. // Need to think it further at some point and implement it properly.
  135. var isFullScreen = document.fullScreen ||
  136. document.mozFullScreen ||
  137. document.webkitIsFullScreen;
  138. if (isFullScreen)
  139. videoSpaceHeight = window.innerHeight;
  140. var horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  141. var verticalIndent = (videoSpaceHeight - videoHeight) / 2;
  142. return [horizontalIndent, verticalIndent];
  143. }
  144. /**
  145. * Returns an array of the video horizontal and vertical indents.
  146. * Centers horizontally and top aligns vertically.
  147. *
  148. * @return an array with 2 elements, the horizontal indent and the vertical
  149. * indent
  150. */
  151. function getDesktopVideoPosition(videoWidth,
  152. videoHeight,
  153. videoSpaceWidth,
  154. videoSpaceHeight) {
  155. var horizontalIndent = (videoSpaceWidth - videoWidth) / 2;
  156. var verticalIndent = 0;// Top aligned
  157. return [horizontalIndent, verticalIndent];
  158. }
  159. /**
  160. * Returns an array of the video dimensions, so that it covers the screen.
  161. * It leaves no empty areas, but some parts of the video might not be visible.
  162. *
  163. * @return an array with 2 elements, the video width and the video height
  164. */
  165. function getCameraVideoSize(videoWidth,
  166. videoHeight,
  167. videoSpaceWidth,
  168. videoSpaceHeight) {
  169. if (!videoWidth)
  170. videoWidth = currentVideoWidth;
  171. if (!videoHeight)
  172. videoHeight = currentVideoHeight;
  173. var aspectRatio = videoWidth / videoHeight;
  174. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  175. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  176. if (availableWidth / aspectRatio < videoSpaceHeight) {
  177. availableHeight = videoSpaceHeight;
  178. availableWidth = availableHeight * aspectRatio;
  179. }
  180. if (availableHeight * aspectRatio < videoSpaceWidth) {
  181. availableWidth = videoSpaceWidth;
  182. availableHeight = availableWidth / aspectRatio;
  183. }
  184. return [availableWidth, availableHeight];
  185. }
  186. /**
  187. * Updates the src of the active speaker avatar
  188. * @param jid of the current active speaker
  189. */
  190. function updateActiveSpeakerAvatarSrc() {
  191. var avatar = $("#activeSpeakerAvatar")[0];
  192. var jid = currentSmallVideo.peerJid;
  193. var url = Avatar.getActiveSpeakerUrl(jid);
  194. if (avatar.src === url)
  195. return;
  196. if (jid) {
  197. avatar.src = url;
  198. currentSmallVideo.showAvatar();
  199. }
  200. }
  201. /**
  202. * Change the video source of the large video.
  203. * @param isVisible
  204. */
  205. function changeVideo(isVisible) {
  206. if (!currentSmallVideo) {
  207. console.error("Unable to change large video - no 'currentSmallVideo'");
  208. return;
  209. }
  210. updateActiveSpeakerAvatarSrc();
  211. APP.RTC.setVideoSrc($('#largeVideo')[0], currentSmallVideo.getSrc());
  212. var videoTransform = document.getElementById('largeVideo')
  213. .style.webkitTransform;
  214. var flipX = currentSmallVideo.flipX;
  215. if (flipX && videoTransform !== 'scaleX(-1)') {
  216. document.getElementById('largeVideo').style.webkitTransform =
  217. "scaleX(-1)";
  218. } else if (!flipX && videoTransform === 'scaleX(-1)') {
  219. document.getElementById('largeVideo').style.webkitTransform =
  220. "none";
  221. }
  222. var isDesktop = APP.RTC.isVideoSrcDesktop(currentSmallVideo.peerJid);
  223. // Change the way we'll be measuring and positioning large video
  224. getVideoSize = isDesktop ? getDesktopVideoSize : getCameraVideoSize;
  225. getVideoPosition = isDesktop ? getDesktopVideoPosition :
  226. getCameraVideoPosition;
  227. // Only if the large video is currently visible.
  228. if (isVisible) {
  229. LargeVideo.VideoLayout.largeVideoUpdated(currentSmallVideo);
  230. $('#largeVideo').fadeIn(300);
  231. }
  232. }
  233. /**
  234. * Creates the html elements for the large video.
  235. */
  236. function createLargeVideoHTML()
  237. {
  238. var html = '<div id="largeVideoContainer" class="videocontainer">';
  239. html += '<div id="presentation"></div>' +
  240. '<div id="etherpad"></div>' +
  241. '<a target="_new"><div class="watermark leftwatermark"></div></a>' +
  242. '<a target="_new"><div class="watermark rightwatermark"></div></a>' +
  243. '<a class="poweredby" href="http://jitsi.org" target="_new" >' +
  244. '<span data-i18n="poweredby"></span> jitsi.org' +
  245. '</a>'+
  246. '<div id="activeSpeaker">' +
  247. '<img id="activeSpeakerAvatar" src=""/>' +
  248. '<canvas id="activeSpeakerAudioLevel"></canvas>' +
  249. '</div>' +
  250. '<video id="largeVideo" autoplay oncontextmenu="return false;"></video>';
  251. html += '</div>';
  252. $(html).prependTo("#videospace");
  253. if (interfaceConfig.SHOW_JITSI_WATERMARK) {
  254. var leftWatermarkDiv
  255. = $("#largeVideoContainer div[class='watermark leftwatermark']");
  256. leftWatermarkDiv.css({display: 'block'});
  257. leftWatermarkDiv.parent().get(0).href
  258. = interfaceConfig.JITSI_WATERMARK_LINK;
  259. }
  260. if (interfaceConfig.SHOW_BRAND_WATERMARK) {
  261. var rightWatermarkDiv
  262. = $("#largeVideoContainer div[class='watermark rightwatermark']");
  263. rightWatermarkDiv.css({display: 'block'});
  264. rightWatermarkDiv.parent().get(0).href
  265. = interfaceConfig.BRAND_WATERMARK_LINK;
  266. rightWatermarkDiv.get(0).style.backgroundImage
  267. = "url(images/rightwatermark.png)";
  268. }
  269. if (interfaceConfig.SHOW_POWERED_BY) {
  270. $("#largeVideoContainer>a[class='poweredby']").css({display: 'block'});
  271. }
  272. if (!RTCBrowserType.isIExplorer()) {
  273. $('#largeVideo').volume = 0;
  274. }
  275. }
  276. var LargeVideo = {
  277. init: function (VideoLayout, emitter) {
  278. if(!isEnabled)
  279. return;
  280. createLargeVideoHTML();
  281. this.VideoLayout = VideoLayout;
  282. this.eventEmitter = emitter;
  283. this.eventEmitter.emit(UIEvents.LARGEVIDEO_INIT);
  284. var self = this;
  285. // Listen for large video size updates
  286. var largeVideo = $('#largeVideo')[0];
  287. var onplaying = function (arg1, arg2, arg3) {
  288. // re-select
  289. if (RTCBrowserType.isTemasysPluginUsed())
  290. largeVideo = $('#largeVideo')[0];
  291. currentVideoWidth = largeVideo.videoWidth;
  292. currentVideoHeight = largeVideo.videoHeight;
  293. self.position(currentVideoWidth, currentVideoHeight);
  294. };
  295. largeVideo.onplaying = onplaying;
  296. },
  297. /**
  298. * Indicates if the large video is currently visible.
  299. *
  300. * @return <tt>true</tt> if visible, <tt>false</tt> - otherwise
  301. */
  302. isLargeVideoVisible: function() {
  303. return $('#largeVideo').is(':visible');
  304. },
  305. /**
  306. * Returns <tt>true</tt> if the user is currently displayed on large video.
  307. */
  308. isCurrentlyOnLarge: function (resourceJid) {
  309. return currentSmallVideo && resourceJid &&
  310. currentSmallVideo.getResourceJid() === resourceJid;
  311. },
  312. /**
  313. * Updates the large video with the given new video source.
  314. */
  315. updateLargeVideo: function (resourceJid, forceUpdate) {
  316. if(!isEnabled)
  317. return;
  318. var newSmallVideo = this.VideoLayout.getSmallVideo(resourceJid);
  319. console.log('hover in ' + resourceJid + ', video: ', newSmallVideo);
  320. if (!LargeVideo.isCurrentlyOnLarge(resourceJid) || forceUpdate) {
  321. $('#activeSpeaker').css('visibility', 'hidden');
  322. var oldSmallVideo = null;
  323. if (currentSmallVideo) {
  324. oldSmallVideo = currentSmallVideo;
  325. }
  326. currentSmallVideo = newSmallVideo;
  327. var oldJid = null;
  328. if (oldSmallVideo)
  329. oldJid = oldSmallVideo.peerJid;
  330. if (oldJid !== resourceJid) {
  331. // we want the notification to trigger even if userJid is undefined,
  332. // or null.
  333. this.eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, resourceJid);
  334. }
  335. if (RTCBrowserType.isSafari()) {
  336. // FIXME In Safari fadeOut works only for the first time
  337. changeVideo(this.isLargeVideoVisible());
  338. } else {
  339. $('#largeVideo').fadeOut(300,
  340. changeVideo.bind($('#largeVideo'), this.isLargeVideoVisible()));
  341. }
  342. } else {
  343. if (currentSmallVideo) {
  344. currentSmallVideo.showAvatar();
  345. }
  346. }
  347. },
  348. /**
  349. * Shows/hides the large video.
  350. */
  351. setLargeVideoVisible: function(isVisible) {
  352. if(!isEnabled)
  353. return;
  354. if (isVisible) {
  355. $('#largeVideo').css({visibility: 'visible'});
  356. $('.watermark').css({visibility: 'visible'});
  357. if(currentSmallVideo)
  358. currentSmallVideo.enableDominantSpeaker(true);
  359. }
  360. else {
  361. $('#largeVideo').css({visibility: 'hidden'});
  362. $('#activeSpeaker').css('visibility', 'hidden');
  363. $('.watermark').css({visibility: 'hidden'});
  364. if(currentSmallVideo)
  365. currentSmallVideo.enableDominantSpeaker(false);
  366. }
  367. },
  368. onVideoTypeChanged: function (jid) {
  369. if(!isEnabled)
  370. return;
  371. var resourceJid = Strophe.getResourceFromJid(jid);
  372. if (LargeVideo.isCurrentlyOnLarge(resourceJid))
  373. {
  374. var isDesktop = APP.RTC.isVideoSrcDesktop(jid);
  375. getVideoSize = isDesktop ? getDesktopVideoSize : getCameraVideoSize;
  376. getVideoPosition = isDesktop ? getDesktopVideoPosition
  377. : getCameraVideoPosition;
  378. this.position(null, null);
  379. }
  380. },
  381. /**
  382. * Positions the large video.
  383. *
  384. * @param videoWidth the stream video width
  385. * @param videoHeight the stream video height
  386. */
  387. position: function (videoWidth, videoHeight,
  388. videoSpaceWidth, videoSpaceHeight, animate) {
  389. if(!isEnabled)
  390. return;
  391. if(!videoSpaceWidth)
  392. videoSpaceWidth = $('#videospace').width();
  393. if(!videoSpaceHeight)
  394. videoSpaceHeight = window.innerHeight;
  395. var videoSize = getVideoSize(videoWidth,
  396. videoHeight,
  397. videoSpaceWidth,
  398. videoSpaceHeight);
  399. var largeVideoWidth = videoSize[0];
  400. var largeVideoHeight = videoSize[1];
  401. var videoPosition = getVideoPosition(largeVideoWidth,
  402. largeVideoHeight,
  403. videoSpaceWidth,
  404. videoSpaceHeight);
  405. var horizontalIndent = videoPosition[0];
  406. var verticalIndent = videoPosition[1];
  407. positionVideo($('#largeVideo'),
  408. largeVideoWidth,
  409. largeVideoHeight,
  410. horizontalIndent, verticalIndent, animate);
  411. },
  412. resize: function (animate, isVisible, completeFunction) {
  413. if(!isEnabled)
  414. return;
  415. var availableHeight = window.innerHeight;
  416. var availableWidth = UIUtil.getAvailableVideoWidth(isVisible);
  417. if (availableWidth < 0 || availableHeight < 0) return;
  418. var avatarSize = interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE;
  419. var top = availableHeight / 2 - avatarSize / 4 * 3;
  420. $('#activeSpeaker').css('top', top);
  421. if(animate) {
  422. $('#videospace').animate({
  423. right: window.innerWidth - availableWidth,
  424. width: availableWidth,
  425. height: availableHeight
  426. },
  427. {
  428. queue: false,
  429. duration: 500,
  430. complete: completeFunction
  431. });
  432. $('#largeVideoContainer').animate({
  433. width: availableWidth,
  434. height: availableHeight
  435. },
  436. {
  437. queue: false,
  438. duration: 500
  439. });
  440. } else {
  441. $('#videospace').width(availableWidth);
  442. $('#videospace').height(availableHeight);
  443. $('#largeVideoContainer').width(availableWidth);
  444. $('#largeVideoContainer').height(availableHeight);
  445. }
  446. return [availableWidth, availableHeight];
  447. },
  448. resizeVideoAreaAnimated: function (isVisible, completeFunction) {
  449. if(!isEnabled)
  450. return;
  451. var size = this.resize(true, isVisible, completeFunction);
  452. this.position(null, null, size[0], size[1], true);
  453. },
  454. getResourceJid: function () {
  455. return currentSmallVideo ? currentSmallVideo.getResourceJid() : null;
  456. },
  457. updateAvatar: function (resourceJid) {
  458. if(!isEnabled)
  459. return;
  460. if (resourceJid === this.getResourceJid()) {
  461. updateActiveSpeakerAvatarSrc();
  462. }
  463. },
  464. showAvatar: function (resourceJid, show) {
  465. if(!isEnabled)
  466. return;
  467. if(this.getResourceJid() === resourceJid && state === "video") {
  468. $("#largeVideo").css("visibility", show ? "hidden" : "visible");
  469. $('#activeSpeaker').css("visibility", show ? "visible" : "hidden");
  470. return true;
  471. }
  472. return false;
  473. },
  474. /**
  475. * Disables the large video
  476. */
  477. disable: function () {
  478. isEnabled = false;
  479. },
  480. /**
  481. * Enables the large video
  482. */
  483. enable: function () {
  484. isEnabled = true;
  485. },
  486. /**
  487. * Returns true if the video is enabled.
  488. */
  489. isEnabled: function () {
  490. return isEnabled;
  491. },
  492. /**
  493. * Creates the iframe used by the etherpad
  494. * @param src the value for src attribute
  495. * @param onloadHandler handler executed when the iframe loads it content
  496. * @returns {HTMLElement} the iframe
  497. */
  498. createEtherpadIframe: function (src, onloadHandler) {
  499. if(!isEnabled)
  500. return;
  501. var etherpadIFrame = document.createElement('iframe');
  502. etherpadIFrame.src = src;
  503. etherpadIFrame.frameBorder = 0;
  504. etherpadIFrame.scrolling = "no";
  505. etherpadIFrame.width = $('#largeVideoContainer').width() || 640;
  506. etherpadIFrame.height = $('#largeVideoContainer').height() || 480;
  507. etherpadIFrame.setAttribute('style', 'visibility: hidden;');
  508. document.getElementById('etherpad').appendChild(etherpadIFrame);
  509. etherpadIFrame.onload = onloadHandler;
  510. return etherpadIFrame;
  511. },
  512. /**
  513. * Changes the state of the large video.
  514. * Possible values - video, prezi, etherpad.
  515. * @param newState - the new state
  516. */
  517. setState: function (newState) {
  518. if(state === newState)
  519. return;
  520. var currentContainer = getContainerByState(state);
  521. if(!currentContainer)
  522. return;
  523. var self = this;
  524. var oldState = state;
  525. switch (newState)
  526. {
  527. case "etherpad":
  528. $('#activeSpeaker').css('visibility', 'hidden');
  529. currentContainer.fadeOut(300, function () {
  530. if (oldState === "prezi") {
  531. currentContainer.css({opacity: '0'});
  532. $('#reloadPresentation').css({display: 'none'});
  533. }
  534. else {
  535. self.setLargeVideoVisible(false);
  536. }
  537. });
  538. $('#etherpad>iframe').fadeIn(300, function () {
  539. document.body.style.background = '#eeeeee';
  540. $('#etherpad>iframe').css({visibility: 'visible'});
  541. $('#etherpad').css({zIndex: 2});
  542. });
  543. break;
  544. case "prezi":
  545. var prezi = $('#presentation>iframe');
  546. currentContainer.fadeOut(300, function() {
  547. document.body.style.background = 'black';
  548. });
  549. prezi.fadeIn(300, function() {
  550. prezi.css({opacity:'1'});
  551. ToolbarToggler.dockToolbar(true);//fix that
  552. self.setLargeVideoVisible(false);
  553. $('#etherpad>iframe').css({visibility: 'hidden'});
  554. $('#etherpad').css({zIndex: 0});
  555. });
  556. $('#activeSpeaker').css('visibility', 'hidden');
  557. break;
  558. case "video":
  559. currentContainer.fadeOut(300, function () {
  560. $('#presentation>iframe').css({opacity:'0'});
  561. $('#reloadPresentation').css({display:'none'});
  562. $('#etherpad>iframe').css({visibility: 'hidden'});
  563. $('#etherpad').css({zIndex: 0});
  564. document.body.style.background = 'black';
  565. ToolbarToggler.dockToolbar(false);//fix that
  566. });
  567. $('#largeVideo').fadeIn(300, function () {
  568. self.setLargeVideoVisible(true);
  569. });
  570. break;
  571. }
  572. state = newState;
  573. },
  574. /**
  575. * Returns the current state of the large video.
  576. * @returns {string} the current state - video, prezi or etherpad.
  577. */
  578. getState: function () {
  579. return state;
  580. },
  581. /**
  582. * Sets hover handlers for the large video container div.
  583. * @param inHandler
  584. * @param outHandler
  585. */
  586. setHover: function(inHandler, outHandler)
  587. {
  588. $('#largeVideoContainer').hover(inHandler, outHandler);
  589. }
  590. };
  591. module.exports = LargeVideo;