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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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. focusContainer.appendChild(indicatorSpan);
  408. createFocusIndicatorElement(indicatorSpan);
  409. }
  410. }
  411. };
  412. /**
  413. * Shows video muted indicator over small videos.
  414. */
  415. my.showVideoIndicator = function(videoSpanId, isMuted) {
  416. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  417. if (isMuted === 'false') {
  418. if (videoMutedSpan.length > 0) {
  419. videoMutedSpan.remove();
  420. }
  421. }
  422. else {
  423. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  424. videoMutedSpan = document.createElement('span');
  425. videoMutedSpan.className = 'videoMuted';
  426. if (audioMutedSpan) {
  427. videoMutedSpan.right = '30px';
  428. }
  429. $('#' + videoSpanId)[0].appendChild(videoMutedSpan);
  430. var mutedIndicator = document.createElement('i');
  431. mutedIndicator.className = 'icon-camera-disabled';
  432. Util.setTooltip(mutedIndicator,
  433. "Participant has<br/>stopped the camera.",
  434. "top");
  435. videoMutedSpan.appendChild(mutedIndicator);
  436. }
  437. };
  438. /**
  439. * Shows audio muted indicator over small videos.
  440. */
  441. my.showAudioIndicator = function(videoSpanId, isMuted) {
  442. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  443. if (isMuted === 'false') {
  444. if (audioMutedSpan.length > 0) {
  445. audioMutedSpan.remove();
  446. }
  447. }
  448. else {
  449. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  450. audioMutedSpan = document.createElement('span');
  451. audioMutedSpan.className = 'audioMuted';
  452. Util.setTooltip(audioMutedSpan,
  453. "Participant is muted",
  454. "top");
  455. if (videoMutedSpan) {
  456. audioMutedSpan.right = '30px';
  457. }
  458. $('#' + videoSpanId)[0].appendChild(audioMutedSpan);
  459. var mutedIndicator = document.createElement('i');
  460. mutedIndicator.className = 'icon-mic-disabled';
  461. audioMutedSpan.appendChild(mutedIndicator);
  462. }
  463. };
  464. /**
  465. * Resizes the large video container.
  466. */
  467. my.resizeLargeVideoContainer = function () {
  468. Chat.resizeChat();
  469. var availableHeight = window.innerHeight;
  470. var availableWidth = Util.getAvailableVideoWidth();
  471. if (availableWidth < 0 || availableHeight < 0) return;
  472. $('#videospace').width(availableWidth);
  473. $('#videospace').height(availableHeight);
  474. $('#largeVideoContainer').width(availableWidth);
  475. $('#largeVideoContainer').height(availableHeight);
  476. VideoLayout.resizeThumbnails();
  477. };
  478. /**
  479. * Resizes thumbnails.
  480. */
  481. my.resizeThumbnails = function() {
  482. var videoSpaceWidth = $('#remoteVideos').width();
  483. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  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. my.calculateThumbnailSize = function (videoSpaceWidth) {
  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 3px borders arround videos and border around the remote
  587. // videos area
  588. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 50;
  589. var availableWidth = availableWinWidth / numvids;
  590. var aspectRatio = 16.0 / 9.0;
  591. var maxHeight = Math.min(160, availableHeight);
  592. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  593. if (availableHeight < availableWidth / aspectRatio) {
  594. availableWidth = Math.floor(availableHeight * aspectRatio);
  595. }
  596. return [availableWidth, availableHeight];
  597. };
  598. /**
  599. * Returns an array of the video dimensions, so that it keeps it's aspect
  600. * ratio and fits available area with it's larger dimension. This method
  601. * ensures that whole video will be visible and can leave empty areas.
  602. *
  603. * @return an array with 2 elements, the video width and the video height
  604. */
  605. function getDesktopVideoSize(videoWidth,
  606. videoHeight,
  607. videoSpaceWidth,
  608. videoSpaceHeight) {
  609. if (!videoWidth)
  610. videoWidth = currentVideoWidth;
  611. if (!videoHeight)
  612. videoHeight = currentVideoHeight;
  613. var aspectRatio = videoWidth / videoHeight;
  614. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  615. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  616. videoSpaceHeight -= $('#remoteVideos').outerHeight();
  617. if (availableWidth / aspectRatio >= videoSpaceHeight)
  618. {
  619. availableHeight = videoSpaceHeight;
  620. availableWidth = availableHeight * aspectRatio;
  621. }
  622. if (availableHeight * aspectRatio >= videoSpaceWidth)
  623. {
  624. availableWidth = videoSpaceWidth;
  625. availableHeight = availableWidth / aspectRatio;
  626. }
  627. return [availableWidth, availableHeight];
  628. }
  629. /**
  630. * Creates the edit display name button.
  631. *
  632. * @returns the edit button
  633. */
  634. function createEditDisplayNameButton() {
  635. var editButton = document.createElement('a');
  636. editButton.className = 'displayname';
  637. Util.setTooltip(editButton,
  638. 'Click to edit your<br/>display name',
  639. "top");
  640. editButton.innerHTML = '<i class="fa fa-pencil"></i>';
  641. return editButton;
  642. }
  643. /**
  644. * Creates the element indicating the focus of the conference.
  645. *
  646. * @param parentElement the parent element where the focus indicator will
  647. * be added
  648. */
  649. function createFocusIndicatorElement(parentElement) {
  650. var focusIndicator = document.createElement('i');
  651. focusIndicator.className = 'fa fa-star';
  652. parentElement.appendChild(focusIndicator);
  653. Util.setTooltip(parentElement,
  654. "The owner of<br/>this conference",
  655. "top");
  656. }
  657. /**
  658. * Updates the remote video menu.
  659. *
  660. * @param jid the jid indicating the video for which we're adding a menu.
  661. * @param isMuted indicates the current mute state
  662. */
  663. my.updateRemoteVideoMenu = function(jid, isMuted) {
  664. var muteMenuItem
  665. = $('#remote_popupmenu_'
  666. + Strophe.getResourceFromJid(jid)
  667. + '>li>a.mutelink');
  668. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  669. if (muteMenuItem.length) {
  670. var muteLink = muteMenuItem.get(0);
  671. if (isMuted === 'true') {
  672. muteLink.innerHTML = mutedIndicator + ' Muted';
  673. muteLink.className = 'mutelink disabled';
  674. }
  675. else {
  676. muteLink.innerHTML = mutedIndicator + ' Mute';
  677. muteLink.className = 'mutelink';
  678. }
  679. }
  680. };
  681. /**
  682. * Returns the current active speaker resource jid.
  683. */
  684. my.getActiveSpeakerResourceJid = function () {
  685. return currentActiveSpeaker;
  686. };
  687. /**
  688. * Adds the remote video menu element for the given <tt>jid</tt> in the
  689. * given <tt>parentElement</tt>.
  690. *
  691. * @param jid the jid indicating the video for which we're adding a menu.
  692. * @param parentElement the parent element where this menu will be added
  693. */
  694. function addRemoteVideoMenu(jid, parentElement) {
  695. var spanElement = document.createElement('span');
  696. spanElement.className = 'remotevideomenu';
  697. parentElement.appendChild(spanElement);
  698. var menuElement = document.createElement('i');
  699. menuElement.className = 'fa fa-angle-down';
  700. menuElement.title = 'Remote user controls';
  701. spanElement.appendChild(menuElement);
  702. // <ul class="popupmenu">
  703. // <li><a href="#">Mute</a></li>
  704. // <li><a href="#">Eject</a></li>
  705. // </ul>
  706. var popupmenuElement = document.createElement('ul');
  707. popupmenuElement.className = 'popupmenu';
  708. popupmenuElement.id
  709. = 'remote_popupmenu_' + Strophe.getResourceFromJid(jid);
  710. spanElement.appendChild(popupmenuElement);
  711. var muteMenuItem = document.createElement('li');
  712. var muteLinkItem = document.createElement('a');
  713. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  714. if (!mutedAudios[jid]) {
  715. muteLinkItem.innerHTML = mutedIndicator + 'Mute';
  716. muteLinkItem.className = 'mutelink';
  717. }
  718. else {
  719. muteLinkItem.innerHTML = mutedIndicator + ' Muted';
  720. muteLinkItem.className = 'mutelink disabled';
  721. }
  722. muteLinkItem.onclick = function(){
  723. if ($(this).attr('disabled') != undefined) {
  724. event.preventDefault();
  725. }
  726. var isMute = !mutedAudios[jid];
  727. connection.moderate.setMute(jid, isMute);
  728. popupmenuElement.setAttribute('style', 'display:none;');
  729. if (isMute) {
  730. this.innerHTML = mutedIndicator + ' Muted';
  731. this.className = 'mutelink disabled';
  732. }
  733. else {
  734. this.innerHTML = mutedIndicator + ' Mute';
  735. this.className = 'mutelink';
  736. }
  737. };
  738. muteMenuItem.appendChild(muteLinkItem);
  739. popupmenuElement.appendChild(muteMenuItem);
  740. var ejectIndicator = "<i class='fa fa-eject'></i>";
  741. var ejectMenuItem = document.createElement('li');
  742. var ejectLinkItem = document.createElement('a');
  743. ejectLinkItem.innerHTML = ejectIndicator + ' Kick out';
  744. ejectLinkItem.onclick = function(){
  745. connection.moderate.eject(jid);
  746. popupmenuElement.setAttribute('style', 'display:none;');
  747. };
  748. ejectMenuItem.appendChild(ejectLinkItem);
  749. popupmenuElement.appendChild(ejectMenuItem);
  750. }
  751. /**
  752. * On audio muted event.
  753. */
  754. $(document).bind('audiomuted.muc', function (event, jid, isMuted) {
  755. var videoSpanId = null;
  756. if (jid === connection.emuc.myroomjid) {
  757. videoSpanId = 'localVideoContainer';
  758. } else {
  759. VideoLayout.ensurePeerContainerExists(jid);
  760. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  761. }
  762. if (focus) {
  763. mutedAudios[jid] = isMuted;
  764. VideoLayout.updateRemoteVideoMenu(jid, isMuted);
  765. }
  766. if (videoSpanId)
  767. VideoLayout.showAudioIndicator(videoSpanId, isMuted);
  768. });
  769. /**
  770. * On video muted event.
  771. */
  772. $(document).bind('videomuted.muc', function (event, jid, isMuted) {
  773. var videoSpanId = null;
  774. if (jid === connection.emuc.myroomjid) {
  775. videoSpanId = 'localVideoContainer';
  776. } else {
  777. VideoLayout.ensurePeerContainerExists(jid);
  778. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  779. }
  780. if (videoSpanId)
  781. VideoLayout.showVideoIndicator(videoSpanId, isMuted);
  782. });
  783. /**
  784. * On active speaker changed event.
  785. */
  786. $(document).bind('activespeakerchanged', function (event, resourceJid) {
  787. // We ignore local user events.
  788. if (resourceJid
  789. === Strophe.getResourceFromJid(connection.emuc.myroomjid))
  790. return;
  791. // Obtain container for new active speaker.
  792. var container = document.getElementById(
  793. 'participant_' + resourceJid);
  794. // Update the current active speaker.
  795. if (resourceJid !== currentActiveSpeaker)
  796. currentActiveSpeaker = resourceJid;
  797. else
  798. return;
  799. // Local video will not have container found, but that's ok
  800. // since we don't want to switch to local video.
  801. if (container && !focusedVideoSrc)
  802. {
  803. var video = container.getElementsByTagName("video");
  804. if (video.length)
  805. {
  806. VideoLayout.updateLargeVideo(video[0].src);
  807. }
  808. }
  809. });
  810. return my;
  811. }(VideoLayout || {}));