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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. '<span id="videoConnectionMessage"></span>';
  252. html += '</div>';
  253. $(html).prependTo("#videospace");
  254. if (interfaceConfig.SHOW_JITSI_WATERMARK) {
  255. var leftWatermarkDiv
  256. = $("#largeVideoContainer div[class='watermark leftwatermark']");
  257. leftWatermarkDiv.css({display: 'block'});
  258. leftWatermarkDiv.parent().get(0).href
  259. = interfaceConfig.JITSI_WATERMARK_LINK;
  260. }
  261. if (interfaceConfig.SHOW_BRAND_WATERMARK) {
  262. var rightWatermarkDiv
  263. = $("#largeVideoContainer div[class='watermark rightwatermark']");
  264. rightWatermarkDiv.css({display: 'block'});
  265. rightWatermarkDiv.parent().get(0).href
  266. = interfaceConfig.BRAND_WATERMARK_LINK;
  267. rightWatermarkDiv.get(0).style.backgroundImage
  268. = "url(images/rightwatermark.png)";
  269. }
  270. if (interfaceConfig.SHOW_POWERED_BY) {
  271. $("#largeVideoContainer>a[class='poweredby']").css({display: 'block'});
  272. }
  273. if (!RTCBrowserType.isIExplorer()) {
  274. $('#largeVideo').volume = 0;
  275. }
  276. }
  277. var LargeVideo = {
  278. init: function (VideoLayout, emitter) {
  279. if(!isEnabled)
  280. return;
  281. createLargeVideoHTML();
  282. this.VideoLayout = VideoLayout;
  283. this.eventEmitter = emitter;
  284. this.eventEmitter.emit(UIEvents.LARGEVIDEO_INIT);
  285. var self = this;
  286. // Listen for large video size updates
  287. var largeVideo = $('#largeVideo')[0];
  288. var onplaying = function (arg1, arg2, arg3) {
  289. // re-select
  290. if (RTCBrowserType.isTemasysPluginUsed())
  291. largeVideo = $('#largeVideo')[0];
  292. currentVideoWidth = largeVideo.videoWidth;
  293. currentVideoHeight = largeVideo.videoHeight;
  294. self.position(currentVideoWidth, currentVideoHeight);
  295. };
  296. largeVideo.onplaying = onplaying;
  297. },
  298. /**
  299. * Indicates if the large video is currently visible.
  300. *
  301. * @return <tt>true</tt> if visible, <tt>false</tt> - otherwise
  302. */
  303. isLargeVideoVisible: function() {
  304. return $('#largeVideo').is(':visible');
  305. },
  306. /**
  307. * Returns <tt>true</tt> if the user is currently displayed on large video.
  308. */
  309. isCurrentlyOnLarge: function (resourceJid) {
  310. return currentSmallVideo && resourceJid &&
  311. currentSmallVideo.getResourceJid() === resourceJid;
  312. },
  313. /**
  314. * Updates the large video with the given new video source.
  315. */
  316. updateLargeVideo: function (resourceJid, forceUpdate) {
  317. if(!isEnabled)
  318. return;
  319. var newSmallVideo = this.VideoLayout.getSmallVideo(resourceJid);
  320. console.log('hover in ' + resourceJid + ', video: ', newSmallVideo);
  321. if (!LargeVideo.isCurrentlyOnLarge(resourceJid) || forceUpdate) {
  322. $('#activeSpeaker').css('visibility', 'hidden');
  323. var oldSmallVideo = null;
  324. if (currentSmallVideo) {
  325. oldSmallVideo = currentSmallVideo;
  326. }
  327. currentSmallVideo = newSmallVideo;
  328. var oldJid = null;
  329. if (oldSmallVideo)
  330. oldJid = oldSmallVideo.peerJid;
  331. if (oldJid !== resourceJid) {
  332. // we want the notification to trigger even if userJid is undefined,
  333. // or null.
  334. this.eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, resourceJid);
  335. }
  336. if (RTCBrowserType.isSafari()) {
  337. // FIXME In Safari fadeOut works only for the first time
  338. changeVideo(this.isLargeVideoVisible());
  339. } else {
  340. $('#largeVideo').fadeOut(300,
  341. changeVideo.bind($('#largeVideo'), this.isLargeVideoVisible()));
  342. }
  343. } else {
  344. if (currentSmallVideo) {
  345. currentSmallVideo.showAvatar();
  346. }
  347. }
  348. },
  349. /**
  350. * Shows/hides the large video.
  351. */
  352. setLargeVideoVisible: function(isVisible) {
  353. if(!isEnabled)
  354. return;
  355. if (isVisible) {
  356. $('#largeVideo').css({visibility: 'visible'});
  357. $('.watermark').css({visibility: 'visible'});
  358. if(currentSmallVideo)
  359. currentSmallVideo.enableDominantSpeaker(true);
  360. }
  361. else {
  362. $('#largeVideo').css({visibility: 'hidden'});
  363. $('#activeSpeaker').css('visibility', 'hidden');
  364. $('.watermark').css({visibility: 'hidden'});
  365. if(currentSmallVideo)
  366. currentSmallVideo.enableDominantSpeaker(false);
  367. }
  368. },
  369. onVideoTypeChanged: function (jid) {
  370. if(!isEnabled)
  371. return;
  372. var resourceJid = Strophe.getResourceFromJid(jid);
  373. if (LargeVideo.isCurrentlyOnLarge(resourceJid))
  374. {
  375. var isDesktop = APP.RTC.isVideoSrcDesktop(jid);
  376. getVideoSize = isDesktop ? getDesktopVideoSize : getCameraVideoSize;
  377. getVideoPosition = isDesktop ? getDesktopVideoPosition
  378. : getCameraVideoPosition;
  379. this.position(null, null);
  380. }
  381. },
  382. /**
  383. * Positions the large video.
  384. *
  385. * @param videoWidth the stream video width
  386. * @param videoHeight the stream video height
  387. */
  388. position: function (videoWidth, videoHeight,
  389. videoSpaceWidth, videoSpaceHeight, animate) {
  390. if(!isEnabled)
  391. return;
  392. if(!videoSpaceWidth)
  393. videoSpaceWidth = $('#videospace').width();
  394. if(!videoSpaceHeight)
  395. videoSpaceHeight = window.innerHeight;
  396. var videoSize = getVideoSize(videoWidth,
  397. videoHeight,
  398. videoSpaceWidth,
  399. videoSpaceHeight);
  400. var largeVideoWidth = videoSize[0];
  401. var largeVideoHeight = videoSize[1];
  402. var videoPosition = getVideoPosition(largeVideoWidth,
  403. largeVideoHeight,
  404. videoSpaceWidth,
  405. videoSpaceHeight);
  406. var horizontalIndent = videoPosition[0];
  407. var verticalIndent = videoPosition[1];
  408. positionVideo($('#largeVideo'),
  409. largeVideoWidth,
  410. largeVideoHeight,
  411. horizontalIndent, verticalIndent, animate);
  412. },
  413. resize: function (animate, isVisible, completeFunction) {
  414. if(!isEnabled)
  415. return;
  416. var availableHeight = window.innerHeight;
  417. var availableWidth = UIUtil.getAvailableVideoWidth(isVisible);
  418. if (availableWidth < 0 || availableHeight < 0) return;
  419. var avatarSize = interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE;
  420. var top = availableHeight / 2 - avatarSize / 4 * 3;
  421. $('#activeSpeaker').css('top', top);
  422. if(animate) {
  423. $('#videospace').animate({
  424. right: window.innerWidth - availableWidth,
  425. width: availableWidth,
  426. height: availableHeight
  427. },
  428. {
  429. queue: false,
  430. duration: 500,
  431. complete: completeFunction
  432. });
  433. $('#largeVideoContainer').animate({
  434. width: availableWidth,
  435. height: availableHeight
  436. },
  437. {
  438. queue: false,
  439. duration: 500
  440. });
  441. } else {
  442. $('#videospace').width(availableWidth);
  443. $('#videospace').height(availableHeight);
  444. $('#largeVideoContainer').width(availableWidth);
  445. $('#largeVideoContainer').height(availableHeight);
  446. }
  447. return [availableWidth, availableHeight];
  448. },
  449. resizeVideoAreaAnimated: function (isVisible, completeFunction) {
  450. if(!isEnabled)
  451. return;
  452. var size = this.resize(true, isVisible, completeFunction);
  453. this.position(null, null, size[0], size[1], true);
  454. },
  455. getResourceJid: function () {
  456. return currentSmallVideo ? currentSmallVideo.getResourceJid() : null;
  457. },
  458. updateAvatar: function (resourceJid) {
  459. if(!isEnabled)
  460. return;
  461. if (resourceJid === this.getResourceJid()) {
  462. updateActiveSpeakerAvatarSrc();
  463. }
  464. },
  465. showAvatar: function (resourceJid, show) {
  466. if(!isEnabled)
  467. return;
  468. if(this.getResourceJid() === resourceJid && state === "video") {
  469. $("#largeVideo").css("visibility", show ? "hidden" : "visible");
  470. $('#activeSpeaker').css("visibility", show ? "visible" : "hidden");
  471. return true;
  472. }
  473. return false;
  474. },
  475. /**
  476. * Disables the large video
  477. */
  478. disable: function () {
  479. isEnabled = false;
  480. },
  481. /**
  482. * Enables the large video
  483. */
  484. enable: function () {
  485. isEnabled = true;
  486. },
  487. /**
  488. * Returns true if the video is enabled.
  489. */
  490. isEnabled: function () {
  491. return isEnabled;
  492. },
  493. /**
  494. * Creates the iframe used by the etherpad
  495. * @param src the value for src attribute
  496. * @param onloadHandler handler executed when the iframe loads it content
  497. * @returns {HTMLElement} the iframe
  498. */
  499. createEtherpadIframe: function (src, onloadHandler) {
  500. if(!isEnabled)
  501. return;
  502. var etherpadIFrame = document.createElement('iframe');
  503. etherpadIFrame.src = src;
  504. etherpadIFrame.frameBorder = 0;
  505. etherpadIFrame.scrolling = "no";
  506. etherpadIFrame.width = $('#largeVideoContainer').width() || 640;
  507. etherpadIFrame.height = $('#largeVideoContainer').height() || 480;
  508. etherpadIFrame.setAttribute('style', 'visibility: hidden;');
  509. document.getElementById('etherpad').appendChild(etherpadIFrame);
  510. etherpadIFrame.onload = onloadHandler;
  511. return etherpadIFrame;
  512. },
  513. /**
  514. * Changes the state of the large video.
  515. * Possible values - video, prezi, etherpad.
  516. * @param newState - the new state
  517. */
  518. setState: function (newState) {
  519. if(state === newState)
  520. return;
  521. var currentContainer = getContainerByState(state);
  522. if(!currentContainer)
  523. return;
  524. var self = this;
  525. var oldState = state;
  526. switch (newState)
  527. {
  528. case "etherpad":
  529. $('#activeSpeaker').css('visibility', 'hidden');
  530. currentContainer.fadeOut(300, function () {
  531. if (oldState === "prezi") {
  532. currentContainer.css({opacity: '0'});
  533. $('#reloadPresentation').css({display: 'none'});
  534. }
  535. else {
  536. self.setLargeVideoVisible(false);
  537. }
  538. });
  539. $('#etherpad>iframe').fadeIn(300, function () {
  540. document.body.style.background = '#eeeeee';
  541. $('#etherpad>iframe').css({visibility: 'visible'});
  542. $('#etherpad').css({zIndex: 2});
  543. });
  544. break;
  545. case "prezi":
  546. var prezi = $('#presentation>iframe');
  547. currentContainer.fadeOut(300, function() {
  548. document.body.style.background = 'black';
  549. });
  550. prezi.fadeIn(300, function() {
  551. prezi.css({opacity:'1'});
  552. ToolbarToggler.dockToolbar(true);//fix that
  553. self.setLargeVideoVisible(false);
  554. $('#etherpad>iframe').css({visibility: 'hidden'});
  555. $('#etherpad').css({zIndex: 0});
  556. });
  557. $('#activeSpeaker').css('visibility', 'hidden');
  558. break;
  559. case "video":
  560. currentContainer.fadeOut(300, function () {
  561. $('#presentation>iframe').css({opacity:'0'});
  562. $('#reloadPresentation').css({display:'none'});
  563. $('#etherpad>iframe').css({visibility: 'hidden'});
  564. $('#etherpad').css({zIndex: 0});
  565. document.body.style.background = 'black';
  566. ToolbarToggler.dockToolbar(false);//fix that
  567. });
  568. $('#largeVideo').fadeIn(300, function () {
  569. self.setLargeVideoVisible(true);
  570. });
  571. break;
  572. }
  573. state = newState;
  574. },
  575. /**
  576. * Returns the current state of the large video.
  577. * @returns {string} the current state - video, prezi or etherpad.
  578. */
  579. getState: function () {
  580. return state;
  581. },
  582. /**
  583. * Sets hover handlers for the large video container div.
  584. * @param inHandler
  585. * @param outHandler
  586. */
  587. setHover: function(inHandler, outHandler)
  588. {
  589. $('#largeVideoContainer').hover(inHandler, outHandler);
  590. },
  591. enableVideoProblemFilter: function (enable) {
  592. $("#largeVideo").toggleClass("videoProblemFilter", enable);
  593. }
  594. };
  595. module.exports = LargeVideo;