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

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