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.

videolayout.js 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. var VideoLayout = (function (my) {
  2. var preMuted = false;
  3. var currentActiveSpeaker = null;
  4. my.changeLocalAudio = function(stream) {
  5. connection.jingle.localAudio = stream;
  6. RTC.attachMediaStream($('#localAudio'), stream);
  7. document.getElementById('localAudio').autoplay = true;
  8. document.getElementById('localAudio').volume = 0;
  9. if (preMuted) {
  10. toggleAudio();
  11. preMuted = false;
  12. }
  13. };
  14. my.changeLocalVideo = function(stream, flipX) {
  15. connection.jingle.localVideo = stream;
  16. var localVideo = document.createElement('video');
  17. localVideo.id = 'localVideo_' + stream.id;
  18. localVideo.autoplay = true;
  19. localVideo.volume = 0; // is it required if audio is separated ?
  20. localVideo.oncontextmenu = function () { return false; };
  21. var localVideoContainer = document.getElementById('localVideoWrapper');
  22. localVideoContainer.appendChild(localVideo);
  23. var localVideoSelector = $('#' + localVideo.id);
  24. // Add click handler
  25. localVideoSelector.click(function () {
  26. VideoLayout.handleVideoThumbClicked(localVideo.src);
  27. });
  28. // Add hover handler
  29. $('#localVideoContainer').hover(
  30. function() {
  31. VideoLayout.showDisplayName('localVideoContainer', true);
  32. },
  33. function() {
  34. if (focusedVideoSrc !== localVideo.src)
  35. VideoLayout.showDisplayName('localVideoContainer', false);
  36. }
  37. );
  38. // Add stream ended handler
  39. stream.onended = function () {
  40. localVideoContainer.removeChild(localVideo);
  41. VideoLayout.checkChangeLargeVideo(localVideo.src);
  42. };
  43. // Flip video x axis if needed
  44. flipXLocalVideo = flipX;
  45. if (flipX) {
  46. localVideoSelector.addClass("flipVideoX");
  47. }
  48. // Attach WebRTC stream
  49. RTC.attachMediaStream(localVideoSelector, stream);
  50. localVideoSrc = localVideo.src;
  51. VideoLayout.updateLargeVideo(localVideoSrc, 0);
  52. };
  53. /**
  54. * Checks if removed video is currently displayed and tries to display another one instead.
  55. * @param removedVideoSrc src stream identifier of the video.
  56. */
  57. my.checkChangeLargeVideo = function(removedVideoSrc) {
  58. if (removedVideoSrc === $('#largeVideo').attr('src')) {
  59. // this is currently displayed as large
  60. // pick the last visible video in the row
  61. // if nobody else is left, this picks the local video
  62. var pick = $('#remoteVideos>span[id!="mixedstream"]:visible:last>video').get(0);
  63. if (!pick) {
  64. console.info("Last visible video no longer exists");
  65. pick = $('#remoteVideos>span[id!="mixedstream"]>video').get(0);
  66. if (!pick) {
  67. // Try local video
  68. console.info("Fallback to local video...");
  69. pick = $('#remoteVideos>span>span>video').get(0);
  70. }
  71. }
  72. // mute if localvideo
  73. if (pick) {
  74. VideoLayout.updateLargeVideo(pick.src, pick.volume);
  75. } else {
  76. console.warn("Failed to elect large video");
  77. }
  78. }
  79. };
  80. /**
  81. * Updates the large video with the given new video source.
  82. */
  83. my.updateLargeVideo = function(newSrc, vol) {
  84. console.log('hover in', newSrc);
  85. if ($('#largeVideo').attr('src') != newSrc) {
  86. var isVisible = $('#largeVideo').is(':visible');
  87. $('#largeVideo').fadeOut(300, function () {
  88. $(this).attr('src', newSrc);
  89. // Screen stream is already rotated
  90. var flipX = (newSrc === localVideoSrc) && flipXLocalVideo;
  91. var videoTransform = document.getElementById('largeVideo')
  92. .style.webkitTransform;
  93. if (flipX && videoTransform !== 'scaleX(-1)') {
  94. document.getElementById('largeVideo').style.webkitTransform
  95. = "scaleX(-1)";
  96. }
  97. else if (!flipX && videoTransform === 'scaleX(-1)') {
  98. document.getElementById('largeVideo').style.webkitTransform
  99. = "none";
  100. }
  101. // Change the way we'll be measuring and positioning large video
  102. var isDesktop = isVideoSrcDesktop(newSrc);
  103. getVideoSize = isDesktop
  104. ? getDesktopVideoSize
  105. : getCameraVideoSize;
  106. getVideoPosition = isDesktop
  107. ? getDesktopVideoPosition
  108. : getCameraVideoPosition;
  109. if (isVisible)
  110. $(this).fadeIn(300);
  111. });
  112. }
  113. };
  114. my.handleVideoThumbClicked = function(videoSrc) {
  115. // Restore style for previously focused video
  116. var focusJid = getJidFromVideoSrc(focusedVideoSrc);
  117. var oldContainer =
  118. getParticipantContainer(focusJid);
  119. if (oldContainer) {
  120. oldContainer.removeClass("videoContainerFocused");
  121. VideoLayout.enableActiveSpeaker(
  122. Strophe.getResourceFromJid(focusJid), false);
  123. }
  124. // Unlock
  125. if (focusedVideoSrc === videoSrc)
  126. {
  127. focusedVideoSrc = null;
  128. return;
  129. }
  130. // Lock new video
  131. focusedVideoSrc = videoSrc;
  132. var userJid = getJidFromVideoSrc(videoSrc);
  133. if (userJid)
  134. {
  135. var container = getParticipantContainer(userJid);
  136. container.addClass("videoContainerFocused");
  137. var resourceJid = Strophe.getResourceFromJid(userJid);
  138. VideoLayout.enableActiveSpeaker(resourceJid, true);
  139. }
  140. $(document).trigger("video.selected", [false]);
  141. VideoLayout.updateLargeVideo(videoSrc, 1);
  142. $('audio').each(function (idx, el) {
  143. if (el.id.indexOf('mixedmslabel') !== -1) {
  144. el.volume = 0;
  145. el.volume = 1;
  146. }
  147. });
  148. };
  149. /**
  150. * Positions the large video.
  151. *
  152. * @param videoWidth the stream video width
  153. * @param videoHeight the stream video height
  154. */
  155. my.positionLarge = function (videoWidth, videoHeight) {
  156. var videoSpaceWidth = $('#videospace').width();
  157. var videoSpaceHeight = window.innerHeight;
  158. var videoSize = getVideoSize(videoWidth,
  159. videoHeight,
  160. videoSpaceWidth,
  161. videoSpaceHeight);
  162. var largeVideoWidth = videoSize[0];
  163. var largeVideoHeight = videoSize[1];
  164. var videoPosition = getVideoPosition(largeVideoWidth,
  165. largeVideoHeight,
  166. videoSpaceWidth,
  167. videoSpaceHeight);
  168. var horizontalIndent = videoPosition[0];
  169. var verticalIndent = videoPosition[1];
  170. positionVideo($('#largeVideo'),
  171. largeVideoWidth,
  172. largeVideoHeight,
  173. horizontalIndent, verticalIndent);
  174. };
  175. /**
  176. * Shows/hides the large video.
  177. */
  178. my.setLargeVideoVisible = function(isVisible) {
  179. if (isVisible) {
  180. $('#largeVideo').css({visibility: 'visible'});
  181. $('.watermark').css({visibility: 'visible'});
  182. }
  183. else {
  184. $('#largeVideo').css({visibility: 'hidden'});
  185. $('.watermark').css({visibility: 'hidden'});
  186. }
  187. };
  188. /**
  189. * Checks if container for participant identified by given peerJid exists
  190. * in the document and creates it eventually.
  191. *
  192. * @param peerJid peer Jid to check.
  193. */
  194. my.ensurePeerContainerExists = function(peerJid) {
  195. var peerResource = Strophe.getResourceFromJid(peerJid);
  196. var videoSpanId = 'participant_' + peerResource;
  197. if ($('#' + videoSpanId).length > 0) {
  198. // If there's been a focus change, make sure we add focus related
  199. // interface!!
  200. if (focus && $('#remote_popupmenu_' + peerResource).length <= 0)
  201. addRemoteVideoMenu( peerJid,
  202. document.getElementById(videoSpanId));
  203. return;
  204. }
  205. var container
  206. = VideoLayout.addRemoteVideoContainer(peerJid, videoSpanId);
  207. var nickfield = document.createElement('span');
  208. nickfield.className = "nick";
  209. nickfield.appendChild(document.createTextNode(peerResource));
  210. container.appendChild(nickfield);
  211. VideoLayout.resizeThumbnails();
  212. };
  213. my.addRemoteVideoContainer = function(peerJid, spanId) {
  214. var container = document.createElement('span');
  215. container.id = spanId;
  216. container.className = 'videocontainer';
  217. var remotes = document.getElementById('remoteVideos');
  218. // If the peerJid is null then this video span couldn't be directly
  219. // associated with a participant (this could happen in the case of prezi).
  220. if (focus && peerJid != null)
  221. addRemoteVideoMenu(peerJid, container);
  222. remotes.appendChild(container);
  223. return container;
  224. };
  225. /**
  226. * Shows the display name for the given video.
  227. */
  228. my.setDisplayName = function(videoSpanId, displayName) {
  229. var nameSpan = $('#' + videoSpanId + '>span.displayname');
  230. // If we already have a display name for this video.
  231. if (nameSpan.length > 0) {
  232. var nameSpanElement = nameSpan.get(0);
  233. if (nameSpanElement.id === 'localDisplayName' &&
  234. $('#localDisplayName').text() !== displayName) {
  235. $('#localDisplayName').text(displayName);
  236. } else {
  237. $('#' + videoSpanId + '_name').text(displayName);
  238. }
  239. } else {
  240. var editButton = null;
  241. if (videoSpanId === 'localVideoContainer') {
  242. editButton = createEditDisplayNameButton();
  243. }
  244. if (displayName.length) {
  245. nameSpan = document.createElement('span');
  246. nameSpan.className = 'displayname';
  247. nameSpan.innerText = displayName;
  248. $('#' + videoSpanId)[0].appendChild(nameSpan);
  249. }
  250. if (!editButton) {
  251. nameSpan.id = videoSpanId + '_name';
  252. } else {
  253. nameSpan.id = 'localDisplayName';
  254. $('#' + videoSpanId)[0].appendChild(editButton);
  255. var editableText = document.createElement('input');
  256. editableText.className = 'displayname';
  257. editableText.id = 'editDisplayName';
  258. if (displayName.length) {
  259. editableText.value
  260. = displayName.substring(0, displayName.indexOf(' (me)'));
  261. }
  262. editableText.setAttribute('style', 'display:none;');
  263. editableText.setAttribute('placeholder', 'ex. Jane Pink');
  264. $('#' + videoSpanId)[0].appendChild(editableText);
  265. $('#localVideoContainer .displayname').bind("click", function (e) {
  266. e.preventDefault();
  267. $('#localDisplayName').hide();
  268. $('#editDisplayName').show();
  269. $('#editDisplayName').focus();
  270. $('#editDisplayName').select();
  271. var inputDisplayNameHandler = function (name) {
  272. if (nickname !== name) {
  273. nickname = name;
  274. window.localStorage.displayname = nickname;
  275. connection.emuc.addDisplayNameToPresence(nickname);
  276. connection.emuc.sendPresence();
  277. Chat.setChatConversationMode(true);
  278. }
  279. if (!$('#localDisplayName').is(":visible")) {
  280. if (nickname) {
  281. $('#localDisplayName').text(nickname + " (me)");
  282. $('#localDisplayName').show();
  283. }
  284. else {
  285. $('#localDisplayName').text(nickname);
  286. }
  287. $('#editDisplayName').hide();
  288. }
  289. };
  290. $('#editDisplayName').one("focusout", function (e) {
  291. inputDisplayNameHandler(this.value);
  292. });
  293. $('#editDisplayName').on('keydown', function (e) {
  294. if (e.keyCode === 13) {
  295. e.preventDefault();
  296. inputDisplayNameHandler(this.value);
  297. }
  298. });
  299. });
  300. }
  301. }
  302. };
  303. /**
  304. * Shows/hides the display name on the remote video.
  305. * @param videoSpanId the identifier of the video span element
  306. * @param isShow indicates if the display name should be shown or hidden
  307. */
  308. my.showDisplayName = function(videoSpanId, isShow) {
  309. var nameSpan = $('#' + videoSpanId + '>span.displayname').get(0);
  310. if (isShow) {
  311. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  312. nameSpan.setAttribute("style", "display:inline-block;");
  313. }
  314. else {
  315. if (nameSpan)
  316. nameSpan.setAttribute("style", "display:none;");
  317. }
  318. };
  319. /**
  320. * Shows a visual indicator for the focus of the conference.
  321. * Currently if we're not the owner of the conference we obtain the focus
  322. * from the connection.jingle.sessions.
  323. */
  324. my.showFocusIndicator = function() {
  325. if (focus !== null) {
  326. var indicatorSpan = $('#localVideoContainer .focusindicator');
  327. if (indicatorSpan.children().length === 0)
  328. {
  329. createFocusIndicatorElement(indicatorSpan[0]);
  330. }
  331. }
  332. else if (Object.keys(connection.jingle.sessions).length > 0) {
  333. // If we're only a participant the focus will be the only session we have.
  334. var session
  335. = connection.jingle.sessions
  336. [Object.keys(connection.jingle.sessions)[0]];
  337. var focusId
  338. = 'participant_' + Strophe.getResourceFromJid(session.peerjid);
  339. var focusContainer = document.getElementById(focusId);
  340. if (!focusContainer) {
  341. console.error("No focus container!");
  342. return;
  343. }
  344. var indicatorSpan = $('#' + focusId + ' .focusindicator');
  345. if (!indicatorSpan || indicatorSpan.length === 0) {
  346. indicatorSpan = document.createElement('span');
  347. indicatorSpan.className = 'focusindicator';
  348. Util.setTooltip(indicatorSpan,
  349. "The owner of<br/>this conference",
  350. "top");
  351. focusContainer.appendChild(indicatorSpan);
  352. createFocusIndicatorElement(indicatorSpan);
  353. }
  354. }
  355. };
  356. /**
  357. * Shows video muted indicator over small videos.
  358. */
  359. my.showVideoIndicator = function(videoSpanId, isMuted) {
  360. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  361. if (isMuted === 'false') {
  362. if (videoMutedSpan.length > 0) {
  363. videoMutedSpan.remove();
  364. }
  365. }
  366. else {
  367. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  368. videoMutedSpan = document.createElement('span');
  369. videoMutedSpan.className = 'videoMuted';
  370. if (audioMutedSpan) {
  371. videoMutedSpan.right = '30px';
  372. }
  373. $('#' + videoSpanId)[0].appendChild(videoMutedSpan);
  374. var mutedIndicator = document.createElement('i');
  375. mutedIndicator.className = 'icon-camera-disabled';
  376. Util.setTooltip(mutedIndicator,
  377. "Participant has<br/>stopped the camera.",
  378. "top");
  379. videoMutedSpan.appendChild(mutedIndicator);
  380. }
  381. };
  382. /**
  383. * Shows audio muted indicator over small videos.
  384. */
  385. my.showAudioIndicator = function(videoSpanId, isMuted) {
  386. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  387. if (isMuted === 'false') {
  388. if (audioMutedSpan.length > 0) {
  389. audioMutedSpan.remove();
  390. }
  391. }
  392. else {
  393. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  394. audioMutedSpan = document.createElement('span');
  395. audioMutedSpan.className = 'audioMuted';
  396. Util.setTooltip(audioMutedSpan,
  397. "Participant is muted",
  398. "top");
  399. if (videoMutedSpan) {
  400. audioMutedSpan.right = '30px';
  401. }
  402. $('#' + videoSpanId)[0].appendChild(audioMutedSpan);
  403. var mutedIndicator = document.createElement('i');
  404. mutedIndicator.className = 'icon-mic-disabled';
  405. audioMutedSpan.appendChild(mutedIndicator);
  406. }
  407. };
  408. /**
  409. * Resizes the large video container.
  410. */
  411. my.resizeLargeVideoContainer = function () {
  412. Chat.resizeChat();
  413. var availableHeight = window.innerHeight;
  414. var availableWidth = Util.getAvailableVideoWidth();
  415. if (availableWidth < 0 || availableHeight < 0) return;
  416. $('#videospace').width(availableWidth);
  417. $('#videospace').height(availableHeight);
  418. $('#largeVideoContainer').width(availableWidth);
  419. $('#largeVideoContainer').height(availableHeight);
  420. VideoLayout.resizeThumbnails();
  421. };
  422. /**
  423. * Resizes thumbnails.
  424. */
  425. my.resizeThumbnails = function() {
  426. var thumbnailSize = calculateThumbnailSize();
  427. var width = thumbnailSize[0];
  428. var height = thumbnailSize[1];
  429. // size videos so that while keeping AR and max height, we have a
  430. // nice fit
  431. $('#remoteVideos').height(height);
  432. $('#remoteVideos>span').width(width);
  433. $('#remoteVideos>span').height(height);
  434. };
  435. /**
  436. * Enables the active speaker UI.
  437. *
  438. * @param resourceJid the jid indicating the video element to
  439. * activate/deactivate
  440. * @param isEnable indicates if the active speaker should be enabled or
  441. * disabled
  442. */
  443. my.enableActiveSpeaker = function(resourceJid, isEnable) {
  444. var videoSpanId = null;
  445. if (resourceJid
  446. === Strophe.getResourceFromJid(connection.emuc.myroomjid))
  447. videoSpanId = 'localVideoWrapper';
  448. else
  449. videoSpanId = 'participant_' + resourceJid;
  450. videoSpan = document.getElementById(videoSpanId);
  451. if (!videoSpan) {
  452. console.error("No video element for jid", resourceJid);
  453. return;
  454. }
  455. // If there's an active speaker (automatically) selected we have to
  456. // disable this state and update the current active speaker.
  457. if (isEnable) {
  458. if (currentActiveSpeaker) {
  459. var oldSpeaker = currentActiveSpeaker;
  460. setTimeout(function () {
  461. VideoLayout.enableActiveSpeaker(oldSpeaker, false);
  462. }, 200);
  463. }
  464. currentActiveSpeaker = resourceJid;
  465. }
  466. else if (resourceJid === currentActiveSpeaker)
  467. currentActiveSpeaker = null;
  468. var activeSpeakerCanvas = $('#' + videoSpanId + '>canvas');
  469. var videoElement = $('#' + videoSpanId + '>video');
  470. var canvasSize = calculateThumbnailSize();
  471. if (isEnable && (!activeSpeakerCanvas
  472. || activeSpeakerCanvas.length === 0)) {
  473. activeSpeakerCanvas = document.createElement('canvas');
  474. activeSpeakerCanvas.width = canvasSize[0];
  475. activeSpeakerCanvas.height = canvasSize[1];
  476. // We flip the canvas image if this is the local video.
  477. if (videoSpanId === 'localVideoWrapper')
  478. activeSpeakerCanvas.className += " flipVideoX";
  479. videoSpan.appendChild(activeSpeakerCanvas);
  480. activeSpeakerCanvas.addEventListener(
  481. 'click',
  482. function() {
  483. VideoLayout.handleVideoThumbClicked(
  484. videoElement.get(0).src);
  485. }, false);
  486. }
  487. else {
  488. activeSpeakerCanvas = activeSpeakerCanvas.get(0);
  489. }
  490. if (videoElement && videoElement.length > 0) {
  491. var video = document.getElementById(videoElement.get(0).id);
  492. if (isEnable) {
  493. var context = activeSpeakerCanvas.getContext('2d');
  494. context.fillRect(0, 0, canvasSize[0], canvasSize[1]);
  495. context.drawImage(video, 0, 0, canvasSize[0], canvasSize[1]);
  496. Util.imageToGrayScale(activeSpeakerCanvas);
  497. VideoLayout
  498. .showDisplayName(videoSpanId, true);
  499. activeSpeakerCanvas
  500. .setAttribute('style', 'display:block !important;');
  501. video.setAttribute('style', 'display:none !important;');
  502. }
  503. else {
  504. VideoLayout
  505. .showDisplayName(videoSpanId, false);
  506. video.setAttribute('style', 'display:block !important;');
  507. activeSpeakerCanvas
  508. .setAttribute('style', 'display:none !important;');
  509. }
  510. }
  511. };
  512. /**
  513. * Gets the selector of video thumbnail container for the user identified by
  514. * given <tt>userJid</tt>
  515. * @param userJid user's Jid for whom we want to get the video container.
  516. */
  517. function getParticipantContainer(userJid)
  518. {
  519. if (!userJid)
  520. return null;
  521. if (userJid === connection.emuc.myroomjid)
  522. return $("#localVideoContainer");
  523. else
  524. return $("#participant_" + Strophe.getResourceFromJid(userJid));
  525. }
  526. /**
  527. * Sets the size and position of the given video element.
  528. *
  529. * @param video the video element to position
  530. * @param width the desired video width
  531. * @param height the desired video height
  532. * @param horizontalIndent the left and right indent
  533. * @param verticalIndent the top and bottom indent
  534. */
  535. function positionVideo(video,
  536. width,
  537. height,
  538. horizontalIndent,
  539. verticalIndent) {
  540. video.width(width);
  541. video.height(height);
  542. video.css({ top: verticalIndent + 'px',
  543. bottom: verticalIndent + 'px',
  544. left: horizontalIndent + 'px',
  545. right: horizontalIndent + 'px'});
  546. }
  547. /**
  548. * Calculates the thumbnail size.
  549. */
  550. var calculateThumbnailSize = function () {
  551. // Calculate the available height, which is the inner window height minus
  552. // 39px for the header minus 2px for the delimiter lines on the top and
  553. // bottom of the large video, minus the 36px space inside the remoteVideos
  554. // container used for highlighting shadow.
  555. var availableHeight = 100;
  556. var numvids = $('#remoteVideos>span:visible').length;
  557. // Remove the 1px borders arround videos and the chat width.
  558. var availableWinWidth = $('#remoteVideos').width() - 2 * numvids - 50;
  559. var availableWidth = availableWinWidth / numvids;
  560. var aspectRatio = 16.0 / 9.0;
  561. var maxHeight = Math.min(160, availableHeight);
  562. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  563. if (availableHeight < availableWidth / aspectRatio) {
  564. availableWidth = Math.floor(availableHeight * aspectRatio);
  565. }
  566. return [availableWidth, availableHeight];
  567. };
  568. /**
  569. * Returns an array of the video dimensions, so that it keeps it's aspect
  570. * ratio and fits available area with it's larger dimension. This method
  571. * ensures that whole video will be visible and can leave empty areas.
  572. *
  573. * @return an array with 2 elements, the video width and the video height
  574. */
  575. function getDesktopVideoSize(videoWidth,
  576. videoHeight,
  577. videoSpaceWidth,
  578. videoSpaceHeight) {
  579. if (!videoWidth)
  580. videoWidth = currentVideoWidth;
  581. if (!videoHeight)
  582. videoHeight = currentVideoHeight;
  583. var aspectRatio = videoWidth / videoHeight;
  584. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  585. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  586. videoSpaceHeight -= $('#remoteVideos').outerHeight();
  587. if (availableWidth / aspectRatio >= videoSpaceHeight)
  588. {
  589. availableHeight = videoSpaceHeight;
  590. availableWidth = availableHeight * aspectRatio;
  591. }
  592. if (availableHeight * aspectRatio >= videoSpaceWidth)
  593. {
  594. availableWidth = videoSpaceWidth;
  595. availableHeight = availableWidth / aspectRatio;
  596. }
  597. return [availableWidth, availableHeight];
  598. }
  599. /**
  600. * Creates the edit display name button.
  601. *
  602. * @returns the edit button
  603. */
  604. function createEditDisplayNameButton() {
  605. var editButton = document.createElement('a');
  606. editButton.className = 'displayname';
  607. Util.setTooltip(editButton,
  608. 'Click to edit your<br/>display name',
  609. "top");
  610. editButton.innerHTML = '<i class="fa fa-pencil"></i>';
  611. return editButton;
  612. }
  613. /**
  614. * Creates the element indicating the focus of the conference.
  615. *
  616. * @param parentElement the parent element where the focus indicator will
  617. * be added
  618. */
  619. function createFocusIndicatorElement(parentElement) {
  620. var focusIndicator = document.createElement('i');
  621. focusIndicator.className = 'fa fa-star';
  622. parentElement.appendChild(focusIndicator);
  623. }
  624. /**
  625. * Updates the remote video menu.
  626. *
  627. * @param jid the jid indicating the video for which we're adding a menu.
  628. * @param isMuted indicates the current mute state
  629. */
  630. my.updateRemoteVideoMenu = function(jid, isMuted) {
  631. var muteMenuItem
  632. = $('#remote_popupmenu_'
  633. + Strophe.getResourceFromJid(jid)
  634. + '>li>a.mutelink');
  635. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  636. if (muteMenuItem.length) {
  637. var muteLink = muteMenuItem.get(0);
  638. if (isMuted === 'true') {
  639. muteLink.innerHTML = mutedIndicator + ' Muted';
  640. muteLink.className = 'mutelink disabled';
  641. }
  642. else {
  643. muteLink.innerHTML = mutedIndicator + ' Mute';
  644. muteLink.className = 'mutelink';
  645. }
  646. }
  647. };
  648. /**
  649. * Adds the remote video menu element for the given <tt>jid</tt> in the
  650. * given <tt>parentElement</tt>.
  651. *
  652. * @param jid the jid indicating the video for which we're adding a menu.
  653. * @param parentElement the parent element where this menu will be added
  654. */
  655. function addRemoteVideoMenu(jid, parentElement) {
  656. var spanElement = document.createElement('span');
  657. spanElement.className = 'remotevideomenu';
  658. parentElement.appendChild(spanElement);
  659. var menuElement = document.createElement('i');
  660. menuElement.className = 'fa fa-angle-down';
  661. menuElement.title = 'Remote user controls';
  662. spanElement.appendChild(menuElement);
  663. // <ul class="popupmenu">
  664. // <li><a href="#">Mute</a></li>
  665. // <li><a href="#">Eject</a></li>
  666. // </ul>
  667. var popupmenuElement = document.createElement('ul');
  668. popupmenuElement.className = 'popupmenu';
  669. popupmenuElement.id
  670. = 'remote_popupmenu_' + Strophe.getResourceFromJid(jid);
  671. spanElement.appendChild(popupmenuElement);
  672. var muteMenuItem = document.createElement('li');
  673. var muteLinkItem = document.createElement('a');
  674. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  675. if (!mutedAudios[jid]) {
  676. muteLinkItem.innerHTML = mutedIndicator + 'Mute';
  677. muteLinkItem.className = 'mutelink';
  678. }
  679. else {
  680. muteLinkItem.innerHTML = mutedIndicator + ' Muted';
  681. muteLinkItem.className = 'mutelink disabled';
  682. }
  683. muteLinkItem.onclick = function(){
  684. if ($(this).attr('disabled') != undefined) {
  685. event.preventDefault();
  686. }
  687. var isMute = !mutedAudios[jid];
  688. connection.moderate.setMute(jid, isMute);
  689. popupmenuElement.setAttribute('style', 'display:none;');
  690. if (isMute) {
  691. this.innerHTML = mutedIndicator + ' Muted';
  692. this.className = 'mutelink disabled';
  693. }
  694. else {
  695. this.innerHTML = mutedIndicator + ' Mute';
  696. this.className = 'mutelink';
  697. }
  698. };
  699. muteMenuItem.appendChild(muteLinkItem);
  700. popupmenuElement.appendChild(muteMenuItem);
  701. var ejectIndicator = "<i class='fa fa-eject'></i>";
  702. var ejectMenuItem = document.createElement('li');
  703. var ejectLinkItem = document.createElement('a');
  704. ejectLinkItem.innerHTML = ejectIndicator + ' Kick out';
  705. ejectLinkItem.onclick = function(){
  706. connection.moderate.eject(jid);
  707. popupmenuElement.setAttribute('style', 'display:none;');
  708. };
  709. ejectMenuItem.appendChild(ejectLinkItem);
  710. popupmenuElement.appendChild(ejectMenuItem);
  711. }
  712. $(document).bind('audiomuted.muc', function (event, jid, isMuted) {
  713. var videoSpanId = null;
  714. if (jid === connection.emuc.myroomjid) {
  715. videoSpanId = 'localVideoContainer';
  716. } else {
  717. VideoLayout.ensurePeerContainerExists(jid);
  718. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  719. }
  720. if (focus) {
  721. mutedAudios[jid] = isMuted;
  722. VideoLayout.updateRemoteVideoMenu(jid, isMuted);
  723. }
  724. if (videoSpanId)
  725. VideoLayout.showAudioIndicator(videoSpanId, isMuted);
  726. });
  727. $(document).bind('videomuted.muc', function (event, jid, isMuted) {
  728. var videoSpanId = null;
  729. if (jid === connection.emuc.myroomjid) {
  730. videoSpanId = 'localVideoContainer';
  731. } else {
  732. VideoLayout.ensurePeerContainerExists(jid);
  733. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  734. }
  735. if (videoSpanId)
  736. VideoLayout.showVideoIndicator(videoSpanId, isMuted);
  737. });
  738. return my;
  739. }(VideoLayout || {}));