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

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