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

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