您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

videolayout.js 44KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. var VideoLayout = (function (my) {
  2. var preMuted = false;
  3. var currentDominantSpeaker = null;
  4. var lastNCount = config.channelLastN;
  5. var lastNEndpointsCache = [];
  6. my.changeLocalAudio = function(stream) {
  7. connection.jingle.localAudio = stream;
  8. RTC.attachMediaStream($('#localAudio'), stream);
  9. document.getElementById('localAudio').autoplay = true;
  10. document.getElementById('localAudio').volume = 0;
  11. if (preMuted) {
  12. toggleAudio();
  13. preMuted = false;
  14. }
  15. };
  16. my.changeLocalVideo = function(stream, flipX) {
  17. connection.jingle.localVideo = stream;
  18. var localVideo = document.createElement('video');
  19. localVideo.id = 'localVideo_' + stream.id;
  20. localVideo.autoplay = true;
  21. localVideo.volume = 0; // is it required if audio is separated ?
  22. localVideo.oncontextmenu = function () { return false; };
  23. var localVideoContainer = document.getElementById('localVideoWrapper');
  24. localVideoContainer.appendChild(localVideo);
  25. AudioLevels.updateAudioLevelCanvas();
  26. var localVideoSelector = $('#' + localVideo.id);
  27. // Add click handler to both video and video wrapper elements in case
  28. // there's no video.
  29. localVideoSelector.click(function () {
  30. VideoLayout.handleVideoThumbClicked(localVideo.src);
  31. });
  32. $('#localVideoContainer').click(function () {
  33. VideoLayout.handleVideoThumbClicked(localVideo.src);
  34. });
  35. // Add hover handler
  36. $('#localVideoContainer').hover(
  37. function() {
  38. VideoLayout.showDisplayName('localVideoContainer', true);
  39. },
  40. function() {
  41. if (!VideoLayout.isLargeVideoVisible()
  42. || localVideo.src !== $('#largeVideo').attr('src'))
  43. VideoLayout.showDisplayName('localVideoContainer', false);
  44. }
  45. );
  46. // Add stream ended handler
  47. stream.onended = function () {
  48. localVideoContainer.removeChild(localVideo);
  49. VideoLayout.updateRemovedVideo(localVideo.src);
  50. };
  51. // Flip video x axis if needed
  52. flipXLocalVideo = flipX;
  53. if (flipX) {
  54. localVideoSelector.addClass("flipVideoX");
  55. }
  56. // Attach WebRTC stream
  57. RTC.attachMediaStream(localVideoSelector, stream);
  58. localVideoSrc = localVideo.src;
  59. VideoLayout.updateLargeVideo(localVideoSrc, 0);
  60. };
  61. /**
  62. * Checks if removed video is currently displayed and tries to display
  63. * another one instead.
  64. * @param removedVideoSrc src stream identifier of the video.
  65. */
  66. my.updateRemovedVideo = function(removedVideoSrc) {
  67. if (removedVideoSrc === $('#largeVideo').attr('src')) {
  68. // this is currently displayed as large
  69. // pick the last visible video in the row
  70. // if nobody else is left, this picks the local video
  71. var pick
  72. = $('#remoteVideos>span[id!="mixedstream"]:visible:last>video')
  73. .get(0);
  74. if (!pick) {
  75. console.info("Last visible video no longer exists");
  76. pick = $('#remoteVideos>span[id!="mixedstream"]>video').get(0);
  77. if (!pick || !pick.src) {
  78. // Try local video
  79. console.info("Fallback to local video...");
  80. pick = $('#remoteVideos>span>span>video').get(0);
  81. }
  82. }
  83. // mute if localvideo
  84. if (pick) {
  85. VideoLayout.updateLargeVideo(pick.src, pick.volume);
  86. } else {
  87. console.warn("Failed to elect large video");
  88. }
  89. }
  90. };
  91. /**
  92. * Updates the large video with the given new video source.
  93. */
  94. my.updateLargeVideo = function(newSrc, vol) {
  95. console.log('hover in', newSrc);
  96. if ($('#largeVideo').attr('src') != newSrc) {
  97. var isVisible = $('#largeVideo').is(':visible');
  98. $('#largeVideo').fadeOut(300, function () {
  99. var oldSrc = $(this).attr('src');
  100. $(this).attr('src', newSrc);
  101. // Screen stream is already rotated
  102. var flipX = (newSrc === localVideoSrc) && flipXLocalVideo;
  103. var videoTransform = document.getElementById('largeVideo')
  104. .style.webkitTransform;
  105. if (flipX && videoTransform !== 'scaleX(-1)') {
  106. document.getElementById('largeVideo').style.webkitTransform
  107. = "scaleX(-1)";
  108. }
  109. else if (!flipX && videoTransform === 'scaleX(-1)') {
  110. document.getElementById('largeVideo').style.webkitTransform
  111. = "none";
  112. }
  113. // Change the way we'll be measuring and positioning large video
  114. var isDesktop = isVideoSrcDesktop(newSrc);
  115. getVideoSize = isDesktop
  116. ? getDesktopVideoSize
  117. : getCameraVideoSize;
  118. getVideoPosition = isDesktop
  119. ? getDesktopVideoPosition
  120. : getCameraVideoPosition;
  121. if (isVisible) {
  122. // Only if the large video is currently visible.
  123. // Disable previous dominant speaker video.
  124. var oldJid = getJidFromVideoSrc(oldSrc);
  125. if (oldJid) {
  126. var oldResourceJid = Strophe.getResourceFromJid(oldJid);
  127. VideoLayout.enableDominantSpeaker(oldResourceJid, false);
  128. }
  129. // Enable new dominant speaker in the remote videos section.
  130. var userJid = getJidFromVideoSrc(newSrc);
  131. if (userJid)
  132. {
  133. var resourceJid = Strophe.getResourceFromJid(userJid);
  134. VideoLayout.enableDominantSpeaker(resourceJid, true);
  135. }
  136. $(this).fadeIn(300);
  137. }
  138. });
  139. }
  140. };
  141. my.handleVideoThumbClicked = function(videoSrc) {
  142. // Restore style for previously focused video
  143. var focusJid = getJidFromVideoSrc(focusedVideoSrc);
  144. var oldContainer = getParticipantContainer(focusJid);
  145. if (oldContainer) {
  146. oldContainer.removeClass("videoContainerFocused");
  147. }
  148. // Unlock current focused.
  149. if (focusedVideoSrc === videoSrc)
  150. {
  151. focusedVideoSrc = null;
  152. var dominantSpeakerVideo = null;
  153. // Enable the currently set dominant speaker.
  154. if (currentDominantSpeaker) {
  155. dominantSpeakerVideo
  156. = $('#participant_' + currentDominantSpeaker + '>video')
  157. .get(0);
  158. if (dominantSpeakerVideo) {
  159. VideoLayout.updateLargeVideo(dominantSpeakerVideo.src, 1);
  160. }
  161. }
  162. return;
  163. }
  164. // Lock new video
  165. focusedVideoSrc = videoSrc;
  166. // Update focused/pinned interface.
  167. var userJid = getJidFromVideoSrc(videoSrc);
  168. if (userJid)
  169. {
  170. var container = getParticipantContainer(userJid);
  171. container.addClass("videoContainerFocused");
  172. }
  173. // Triggers a "video.selected" event. The "false" parameter indicates
  174. // this isn't a prezi.
  175. $(document).trigger("video.selected", [false]);
  176. VideoLayout.updateLargeVideo(videoSrc, 1);
  177. $('audio').each(function (idx, el) {
  178. if (el.id.indexOf('mixedmslabel') !== -1) {
  179. el.volume = 0;
  180. el.volume = 1;
  181. }
  182. });
  183. };
  184. /**
  185. * Positions the large video.
  186. *
  187. * @param videoWidth the stream video width
  188. * @param videoHeight the stream video height
  189. */
  190. my.positionLarge = function (videoWidth, videoHeight) {
  191. var videoSpaceWidth = $('#videospace').width();
  192. var videoSpaceHeight = window.innerHeight;
  193. var videoSize = getVideoSize(videoWidth,
  194. videoHeight,
  195. videoSpaceWidth,
  196. videoSpaceHeight);
  197. var largeVideoWidth = videoSize[0];
  198. var largeVideoHeight = videoSize[1];
  199. var videoPosition = getVideoPosition(largeVideoWidth,
  200. largeVideoHeight,
  201. videoSpaceWidth,
  202. videoSpaceHeight);
  203. var horizontalIndent = videoPosition[0];
  204. var verticalIndent = videoPosition[1];
  205. positionVideo($('#largeVideo'),
  206. largeVideoWidth,
  207. largeVideoHeight,
  208. horizontalIndent, verticalIndent);
  209. };
  210. /**
  211. * Shows/hides the large video.
  212. */
  213. my.setLargeVideoVisible = function(isVisible) {
  214. var largeVideoJid = getJidFromVideoSrc($('#largeVideo').attr('src'));
  215. var resourceJid = Strophe.getResourceFromJid(largeVideoJid);
  216. if (isVisible) {
  217. $('#largeVideo').css({visibility: 'visible'});
  218. $('.watermark').css({visibility: 'visible'});
  219. VideoLayout.enableDominantSpeaker(resourceJid, true);
  220. }
  221. else {
  222. $('#largeVideo').css({visibility: 'hidden'});
  223. $('.watermark').css({visibility: 'hidden'});
  224. VideoLayout.enableDominantSpeaker(resourceJid, false);
  225. }
  226. };
  227. /**
  228. * Indicates if the large video is currently visible.
  229. *
  230. * @return <tt>true</tt> if visible, <tt>false</tt> - otherwise
  231. */
  232. my.isLargeVideoVisible = function() {
  233. return $('#largeVideo').is(':visible');
  234. };
  235. /**
  236. * Checks if container for participant identified by given peerJid exists
  237. * in the document and creates it eventually.
  238. *
  239. * @param peerJid peer Jid to check.
  240. *
  241. * @return Returns <tt>true</tt> if the peer container exists,
  242. * <tt>false</tt> - otherwise
  243. */
  244. my.ensurePeerContainerExists = function(peerJid) {
  245. ContactList.ensureAddContact(peerJid);
  246. var resourceJid = Strophe.getResourceFromJid(peerJid);
  247. var videoSpanId = 'participant_' + resourceJid;
  248. if ($('#' + videoSpanId).length > 0) {
  249. // If there's been a focus change, make sure we add focus related
  250. // interface!!
  251. if (focus && $('#remote_popupmenu_' + resourceJid).length <= 0)
  252. addRemoteVideoMenu( peerJid,
  253. document.getElementById(videoSpanId));
  254. }
  255. else {
  256. var container
  257. = VideoLayout.addRemoteVideoContainer(peerJid, videoSpanId);
  258. var nickfield = document.createElement('span');
  259. nickfield.className = "nick";
  260. nickfield.appendChild(document.createTextNode(resourceJid));
  261. container.appendChild(nickfield);
  262. // In case this is not currently in the last n we don't show it.
  263. if (lastNCount
  264. && lastNCount > 0
  265. && $('#remoteVideos>span').length >= lastNCount + 2) {
  266. showPeerContainer(resourceJid, false);
  267. }
  268. else
  269. VideoLayout.resizeThumbnails();
  270. }
  271. };
  272. my.addRemoteVideoContainer = function(peerJid, spanId) {
  273. var container = document.createElement('span');
  274. container.id = spanId;
  275. container.className = 'videocontainer';
  276. var remotes = document.getElementById('remoteVideos');
  277. // If the peerJid is null then this video span couldn't be directly
  278. // associated with a participant (this could happen in the case of prezi).
  279. if (focus && peerJid != null)
  280. addRemoteVideoMenu(peerJid, container);
  281. remotes.appendChild(container);
  282. AudioLevels.updateAudioLevelCanvas(peerJid);
  283. return container;
  284. };
  285. /**
  286. * Creates an audio or video stream element.
  287. */
  288. my.createStreamElement = function (sid, stream) {
  289. var isVideo = stream.getVideoTracks().length > 0;
  290. var element = isVideo
  291. ? document.createElement('video')
  292. : document.createElement('audio');
  293. var id = (isVideo ? 'remoteVideo_' : 'remoteAudio_')
  294. + sid + '_' + stream.id;
  295. element.id = id;
  296. element.autoplay = true;
  297. element.oncontextmenu = function () { return false; };
  298. return element;
  299. };
  300. my.addRemoteStreamElement
  301. = function (container, sid, stream, peerJid, thessrc) {
  302. var newElementId = null;
  303. var isVideo = stream.getVideoTracks().length > 0;
  304. if (container) {
  305. var streamElement = VideoLayout.createStreamElement(sid, stream);
  306. newElementId = streamElement.id;
  307. container.appendChild(streamElement);
  308. var sel = $('#' + newElementId);
  309. sel.hide();
  310. // If the container is currently visible we attach the stream.
  311. if (!isVideo
  312. || (container.offsetParent !== null && isVideo)) {
  313. RTC.attachMediaStream(sel, stream);
  314. if (isVideo)
  315. waitForRemoteVideo(sel, thessrc, stream);
  316. }
  317. stream.onended = function () {
  318. console.log('stream ended', this);
  319. VideoLayout.removeRemoteStreamElement(stream, container);
  320. if (peerJid)
  321. ContactList.removeContact(peerJid);
  322. };
  323. // Add click handler.
  324. container.onclick = function (event) {
  325. /*
  326. * FIXME It turns out that videoThumb may not exist (if there is
  327. * no actual video).
  328. */
  329. var videoThumb = $('#' + container.id + '>video').get(0);
  330. if (videoThumb)
  331. VideoLayout.handleVideoThumbClicked(videoThumb.src);
  332. event.preventDefault();
  333. return false;
  334. };
  335. // Add hover handler
  336. $(container).hover(
  337. function() {
  338. VideoLayout.showDisplayName(container.id, true);
  339. },
  340. function() {
  341. var videoSrc = null;
  342. if ($('#' + container.id + '>video')
  343. && $('#' + container.id + '>video').length > 0) {
  344. videoSrc = $('#' + container.id + '>video').get(0).src;
  345. }
  346. // If the video has been "pinned" by the user we want to
  347. // keep the display name on place.
  348. if (!VideoLayout.isLargeVideoVisible()
  349. || videoSrc !== $('#largeVideo').attr('src'))
  350. VideoLayout.showDisplayName(container.id, false);
  351. }
  352. );
  353. }
  354. return newElementId;
  355. };
  356. /**
  357. * Removes the remote stream element corresponding to the given stream and
  358. * parent container.
  359. *
  360. * @param stream the stream
  361. * @param container
  362. */
  363. my.removeRemoteStreamElement = function (stream, container) {
  364. if (!container)
  365. return;
  366. var select = null;
  367. var removedVideoSrc = null;
  368. if (stream.getVideoTracks().length > 0) {
  369. select = $('#' + container.id + '>video');
  370. removedVideoSrc = select.get(0).src;
  371. }
  372. else
  373. select = $('#' + container.id + '>audio');
  374. // Remove video source from the mapping.
  375. delete videoSrcToSsrc[removedVideoSrc];
  376. // Mark video as removed to cancel waiting loop(if video is removed
  377. // before has started)
  378. select.removed = true;
  379. select.remove();
  380. var audioCount = $('#' + container.id + '>audio').length;
  381. var videoCount = $('#' + container.id + '>video').length;
  382. if (!audioCount && !videoCount) {
  383. console.log("Remove whole user", container.id);
  384. // Remove whole container
  385. container.remove();
  386. Util.playSoundNotification('userLeft');
  387. VideoLayout.resizeThumbnails();
  388. }
  389. if (removedVideoSrc)
  390. VideoLayout.updateRemovedVideo(removedVideoSrc);
  391. };
  392. /**
  393. * Show/hide peer container for the given resourceJid.
  394. */
  395. function showPeerContainer(resourceJid, isShow) {
  396. var peerContainer = $('#participant_' + resourceJid);
  397. if (!peerContainer)
  398. return;
  399. if (!peerContainer.is(':visible') && isShow)
  400. peerContainer.show();
  401. else if (peerContainer.is(':visible') && !isShow)
  402. peerContainer.hide();
  403. };
  404. /**
  405. * Sets the display name for the given video span id.
  406. */
  407. function setDisplayName(videoSpanId, displayName) {
  408. var nameSpan = $('#' + videoSpanId + '>span.displayname');
  409. var defaultLocalDisplayName = "Me";
  410. var defaultRemoteDisplayName = "Speaker";
  411. // If we already have a display name for this video.
  412. if (nameSpan.length > 0) {
  413. var nameSpanElement = nameSpan.get(0);
  414. if (nameSpanElement.id === 'localDisplayName' &&
  415. $('#localDisplayName').text() !== displayName) {
  416. if (displayName && displayName.length > 0)
  417. $('#localDisplayName').text(displayName + ' (me)');
  418. else
  419. $('#localDisplayName').text(defaultLocalDisplayName);
  420. } else {
  421. if (displayName && displayName.length > 0)
  422. $('#' + videoSpanId + '_name').text(displayName);
  423. else
  424. $('#' + videoSpanId + '_name').text(defaultRemoteDisplayName);
  425. }
  426. } else {
  427. var editButton = null;
  428. nameSpan = document.createElement('span');
  429. nameSpan.className = 'displayname';
  430. $('#' + videoSpanId)[0].appendChild(nameSpan);
  431. if (videoSpanId === 'localVideoContainer') {
  432. editButton = createEditDisplayNameButton();
  433. nameSpan.innerText = defaultLocalDisplayName;
  434. }
  435. else {
  436. nameSpan.innerText = defaultRemoteDisplayName;
  437. }
  438. if (displayName && displayName.length > 0) {
  439. nameSpan.innerText = displayName;
  440. }
  441. if (!editButton) {
  442. nameSpan.id = videoSpanId + '_name';
  443. } else {
  444. nameSpan.id = 'localDisplayName';
  445. $('#' + videoSpanId)[0].appendChild(editButton);
  446. var editableText = document.createElement('input');
  447. editableText.className = 'displayname';
  448. editableText.id = 'editDisplayName';
  449. if (displayName.length) {
  450. editableText.value
  451. = displayName.substring(0, displayName.indexOf(' (me)'));
  452. }
  453. editableText.setAttribute('style', 'display:none;');
  454. editableText.setAttribute('placeholder', 'ex. Jane Pink');
  455. $('#' + videoSpanId)[0].appendChild(editableText);
  456. $('#localVideoContainer .displayname')
  457. .bind("click", function (e) {
  458. e.preventDefault();
  459. $('#localDisplayName').hide();
  460. $('#editDisplayName').show();
  461. $('#editDisplayName').focus();
  462. $('#editDisplayName').select();
  463. var inputDisplayNameHandler = function (name) {
  464. if (nickname !== name) {
  465. nickname = name;
  466. window.localStorage.displayname = nickname;
  467. connection.emuc.addDisplayNameToPresence(nickname);
  468. connection.emuc.sendPresence();
  469. Chat.setChatConversationMode(true);
  470. }
  471. if (!$('#localDisplayName').is(":visible")) {
  472. if (nickname)
  473. $('#localDisplayName').text(nickname + " (me)");
  474. else
  475. $('#localDisplayName')
  476. .text(defaultLocalDisplayName);
  477. $('#localDisplayName').show();
  478. }
  479. $('#editDisplayName').hide();
  480. };
  481. $('#editDisplayName').one("focusout", function (e) {
  482. inputDisplayNameHandler(this.value);
  483. });
  484. $('#editDisplayName').on('keydown', function (e) {
  485. if (e.keyCode === 13) {
  486. e.preventDefault();
  487. inputDisplayNameHandler(this.value);
  488. }
  489. });
  490. });
  491. }
  492. }
  493. };
  494. /**
  495. * Shows/hides the display name on the remote video.
  496. * @param videoSpanId the identifier of the video span element
  497. * @param isShow indicates if the display name should be shown or hidden
  498. */
  499. my.showDisplayName = function(videoSpanId, isShow) {
  500. var nameSpan = $('#' + videoSpanId + '>span.displayname').get(0);
  501. if (isShow) {
  502. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  503. nameSpan.setAttribute("style", "display:inline-block;");
  504. }
  505. else {
  506. if (nameSpan)
  507. nameSpan.setAttribute("style", "display:none;");
  508. }
  509. };
  510. /**
  511. * Shows the presence status message for the given video.
  512. */
  513. my.setPresenceStatus = function (videoSpanId, statusMsg) {
  514. if (!$('#' + videoSpanId).length) {
  515. // No container
  516. return;
  517. }
  518. var statusSpan = $('#' + videoSpanId + '>span.status');
  519. if (!statusSpan.length) {
  520. //Add status span
  521. statusSpan = document.createElement('span');
  522. statusSpan.className = 'status';
  523. statusSpan.id = videoSpanId + '_status';
  524. $('#' + videoSpanId)[0].appendChild(statusSpan);
  525. statusSpan = $('#' + videoSpanId + '>span.status');
  526. }
  527. // Display status
  528. if (statusMsg && statusMsg.length) {
  529. $('#' + videoSpanId + '_status').text(statusMsg);
  530. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  531. }
  532. else {
  533. // Hide
  534. statusSpan.get(0).setAttribute("style", "display:none;");
  535. }
  536. };
  537. /**
  538. * Shows a visual indicator for the focus of the conference.
  539. * Currently if we're not the owner of the conference we obtain the focus
  540. * from the connection.jingle.sessions.
  541. */
  542. my.showFocusIndicator = function() {
  543. if (focus !== null) {
  544. var indicatorSpan = $('#localVideoContainer .focusindicator');
  545. if (indicatorSpan.children().length === 0)
  546. {
  547. createFocusIndicatorElement(indicatorSpan[0]);
  548. }
  549. }
  550. else if (Object.keys(connection.jingle.sessions).length > 0) {
  551. // If we're only a participant the focus will be the only session we have.
  552. var session
  553. = connection.jingle.sessions
  554. [Object.keys(connection.jingle.sessions)[0]];
  555. var focusId
  556. = 'participant_' + Strophe.getResourceFromJid(session.peerjid);
  557. var focusContainer = document.getElementById(focusId);
  558. if (!focusContainer) {
  559. console.error("No focus container!");
  560. return;
  561. }
  562. var indicatorSpan = $('#' + focusId + ' .focusindicator');
  563. if (!indicatorSpan || indicatorSpan.length === 0) {
  564. indicatorSpan = document.createElement('span');
  565. indicatorSpan.className = 'focusindicator';
  566. focusContainer.appendChild(indicatorSpan);
  567. createFocusIndicatorElement(indicatorSpan);
  568. }
  569. }
  570. };
  571. /**
  572. * Shows video muted indicator over small videos.
  573. */
  574. my.showVideoIndicator = function(videoSpanId, isMuted) {
  575. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  576. if (isMuted === 'false') {
  577. if (videoMutedSpan.length > 0) {
  578. videoMutedSpan.remove();
  579. }
  580. }
  581. else {
  582. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  583. videoMutedSpan = document.createElement('span');
  584. videoMutedSpan.className = 'videoMuted';
  585. if (audioMutedSpan) {
  586. videoMutedSpan.right = '30px';
  587. }
  588. $('#' + videoSpanId)[0].appendChild(videoMutedSpan);
  589. var mutedIndicator = document.createElement('i');
  590. mutedIndicator.className = 'icon-camera-disabled';
  591. Util.setTooltip(mutedIndicator,
  592. "Participant has<br/>stopped the camera.",
  593. "top");
  594. videoMutedSpan.appendChild(mutedIndicator);
  595. }
  596. };
  597. /**
  598. * Shows audio muted indicator over small videos.
  599. */
  600. my.showAudioIndicator = function(videoSpanId, isMuted) {
  601. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  602. if (isMuted === 'false') {
  603. if (audioMutedSpan.length > 0) {
  604. audioMutedSpan.remove();
  605. }
  606. }
  607. else {
  608. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  609. audioMutedSpan = document.createElement('span');
  610. audioMutedSpan.className = 'audioMuted';
  611. Util.setTooltip(audioMutedSpan,
  612. "Participant is muted",
  613. "top");
  614. if (videoMutedSpan) {
  615. audioMutedSpan.right = '30px';
  616. }
  617. $('#' + videoSpanId)[0].appendChild(audioMutedSpan);
  618. var mutedIndicator = document.createElement('i');
  619. mutedIndicator.className = 'icon-mic-disabled';
  620. audioMutedSpan.appendChild(mutedIndicator);
  621. }
  622. };
  623. /**
  624. * Resizes the large video container.
  625. */
  626. my.resizeLargeVideoContainer = function () {
  627. Chat.resizeChat();
  628. var availableHeight = window.innerHeight;
  629. var availableWidth = Util.getAvailableVideoWidth();
  630. if (availableWidth < 0 || availableHeight < 0) return;
  631. $('#videospace').width(availableWidth);
  632. $('#videospace').height(availableHeight);
  633. $('#largeVideoContainer').width(availableWidth);
  634. $('#largeVideoContainer').height(availableHeight);
  635. VideoLayout.resizeThumbnails();
  636. };
  637. /**
  638. * Resizes thumbnails.
  639. */
  640. my.resizeThumbnails = function() {
  641. var videoSpaceWidth = $('#remoteVideos').width();
  642. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  643. var width = thumbnailSize[0];
  644. var height = thumbnailSize[1];
  645. // size videos so that while keeping AR and max height, we have a
  646. // nice fit
  647. $('#remoteVideos').height(height);
  648. $('#remoteVideos>span').width(width);
  649. $('#remoteVideos>span').height(height);
  650. $(document).trigger("remotevideo.resized", [width, height]);
  651. };
  652. /**
  653. * Enables the dominant speaker UI.
  654. *
  655. * @param resourceJid the jid indicating the video element to
  656. * activate/deactivate
  657. * @param isEnable indicates if the dominant speaker should be enabled or
  658. * disabled
  659. */
  660. my.enableDominantSpeaker = function(resourceJid, isEnable) {
  661. var displayName = resourceJid;
  662. var nameSpan = $('#participant_' + resourceJid + '>span.displayname');
  663. if (nameSpan.length > 0)
  664. displayName = nameSpan.text();
  665. console.log("UI enable dominant speaker",
  666. displayName,
  667. resourceJid,
  668. isEnable);
  669. var videoSpanId = null;
  670. var videoContainerId = null;
  671. if (resourceJid
  672. === Strophe.getResourceFromJid(connection.emuc.myroomjid)) {
  673. videoSpanId = 'localVideoWrapper';
  674. videoContainerId = 'localVideoContainer';
  675. }
  676. else {
  677. videoSpanId = 'participant_' + resourceJid;
  678. videoContainerId = videoSpanId;
  679. }
  680. videoSpan = document.getElementById(videoContainerId);
  681. if (!videoSpan) {
  682. console.error("No video element for jid", resourceJid);
  683. return;
  684. }
  685. var video = $('#' + videoSpanId + '>video');
  686. if (video && video.length > 0) {
  687. if (isEnable) {
  688. VideoLayout.showDisplayName(videoContainerId, true);
  689. if (!videoSpan.classList.contains("dominantspeaker"))
  690. videoSpan.classList.add("dominantspeaker");
  691. video.css({visibility: 'hidden'});
  692. }
  693. else {
  694. VideoLayout.showDisplayName(videoContainerId, false);
  695. if (videoSpan.classList.contains("dominantspeaker"))
  696. videoSpan.classList.remove("dominantspeaker");
  697. video.css({visibility: 'visible'});
  698. }
  699. }
  700. };
  701. /**
  702. * Gets the selector of video thumbnail container for the user identified by
  703. * given <tt>userJid</tt>
  704. * @param userJid user's Jid for whom we want to get the video container.
  705. */
  706. function getParticipantContainer(userJid)
  707. {
  708. if (!userJid)
  709. return null;
  710. if (userJid === connection.emuc.myroomjid)
  711. return $("#localVideoContainer");
  712. else
  713. return $("#participant_" + Strophe.getResourceFromJid(userJid));
  714. }
  715. /**
  716. * Sets the size and position of the given video element.
  717. *
  718. * @param video the video element to position
  719. * @param width the desired video width
  720. * @param height the desired video height
  721. * @param horizontalIndent the left and right indent
  722. * @param verticalIndent the top and bottom indent
  723. */
  724. function positionVideo(video,
  725. width,
  726. height,
  727. horizontalIndent,
  728. verticalIndent) {
  729. video.width(width);
  730. video.height(height);
  731. video.css({ top: verticalIndent + 'px',
  732. bottom: verticalIndent + 'px',
  733. left: horizontalIndent + 'px',
  734. right: horizontalIndent + 'px'});
  735. }
  736. /**
  737. * Calculates the thumbnail size.
  738. *
  739. * @param videoSpaceWidth the width of the video space
  740. */
  741. my.calculateThumbnailSize = function (videoSpaceWidth) {
  742. // Calculate the available height, which is the inner window height minus
  743. // 39px for the header minus 2px for the delimiter lines on the top and
  744. // bottom of the large video, minus the 36px space inside the remoteVideos
  745. // container used for highlighting shadow.
  746. var availableHeight = 100;
  747. var numvids = 0;
  748. if (lastNCount && lastNCount > 0)
  749. numvids = lastNCount + 1;
  750. else
  751. numvids = $('#remoteVideos>span:visible').length;
  752. // Remove the 3px borders arround videos and border around the remote
  753. // videos area
  754. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70;
  755. var availableWidth = availableWinWidth / numvids;
  756. var aspectRatio = 16.0 / 9.0;
  757. var maxHeight = Math.min(160, availableHeight);
  758. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  759. if (availableHeight < availableWidth / aspectRatio) {
  760. availableWidth = Math.floor(availableHeight * aspectRatio);
  761. }
  762. return [availableWidth, availableHeight];
  763. };
  764. /**
  765. * Returns an array of the video dimensions, so that it keeps it's aspect
  766. * ratio and fits available area with it's larger dimension. This method
  767. * ensures that whole video will be visible and can leave empty areas.
  768. *
  769. * @return an array with 2 elements, the video width and the video height
  770. */
  771. function getDesktopVideoSize(videoWidth,
  772. videoHeight,
  773. videoSpaceWidth,
  774. videoSpaceHeight) {
  775. if (!videoWidth)
  776. videoWidth = currentVideoWidth;
  777. if (!videoHeight)
  778. videoHeight = currentVideoHeight;
  779. var aspectRatio = videoWidth / videoHeight;
  780. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  781. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  782. videoSpaceHeight -= $('#remoteVideos').outerHeight();
  783. if (availableWidth / aspectRatio >= videoSpaceHeight)
  784. {
  785. availableHeight = videoSpaceHeight;
  786. availableWidth = availableHeight * aspectRatio;
  787. }
  788. if (availableHeight * aspectRatio >= videoSpaceWidth)
  789. {
  790. availableWidth = videoSpaceWidth;
  791. availableHeight = availableWidth / aspectRatio;
  792. }
  793. return [availableWidth, availableHeight];
  794. }
  795. /**
  796. * Creates the edit display name button.
  797. *
  798. * @returns the edit button
  799. */
  800. function createEditDisplayNameButton() {
  801. var editButton = document.createElement('a');
  802. editButton.className = 'displayname';
  803. Util.setTooltip(editButton,
  804. 'Click to edit your<br/>display name',
  805. "top");
  806. editButton.innerHTML = '<i class="fa fa-pencil"></i>';
  807. return editButton;
  808. }
  809. /**
  810. * Creates the element indicating the focus of the conference.
  811. *
  812. * @param parentElement the parent element where the focus indicator will
  813. * be added
  814. */
  815. function createFocusIndicatorElement(parentElement) {
  816. var focusIndicator = document.createElement('i');
  817. focusIndicator.className = 'fa fa-star';
  818. parentElement.appendChild(focusIndicator);
  819. Util.setTooltip(parentElement,
  820. "The owner of<br/>this conference",
  821. "top");
  822. }
  823. /**
  824. * Updates the remote video menu.
  825. *
  826. * @param jid the jid indicating the video for which we're adding a menu.
  827. * @param isMuted indicates the current mute state
  828. */
  829. my.updateRemoteVideoMenu = function(jid, isMuted) {
  830. var muteMenuItem
  831. = $('#remote_popupmenu_'
  832. + Strophe.getResourceFromJid(jid)
  833. + '>li>a.mutelink');
  834. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  835. if (muteMenuItem.length) {
  836. var muteLink = muteMenuItem.get(0);
  837. if (isMuted === 'true') {
  838. muteLink.innerHTML = mutedIndicator + ' Muted';
  839. muteLink.className = 'mutelink disabled';
  840. }
  841. else {
  842. muteLink.innerHTML = mutedIndicator + ' Mute';
  843. muteLink.className = 'mutelink';
  844. }
  845. }
  846. };
  847. /**
  848. * Returns the current dominant speaker resource jid.
  849. */
  850. my.getDominantSpeakerResourceJid = function () {
  851. return currentDominantSpeaker;
  852. };
  853. /**
  854. * Returns the corresponding resource jid to the given peer container
  855. * DOM element.
  856. *
  857. * @return the corresponding resource jid to the given peer container
  858. * DOM element
  859. */
  860. my.getPeerContainerResourceJid = function (containerElement) {
  861. var i = containerElement.id.indexOf('participant_');
  862. if (i >= 0)
  863. return containerElement.id.substring(i + 12);
  864. };
  865. /**
  866. * Adds the remote video menu element for the given <tt>jid</tt> in the
  867. * given <tt>parentElement</tt>.
  868. *
  869. * @param jid the jid indicating the video for which we're adding a menu.
  870. * @param parentElement the parent element where this menu will be added
  871. */
  872. function addRemoteVideoMenu(jid, parentElement) {
  873. var spanElement = document.createElement('span');
  874. spanElement.className = 'remotevideomenu';
  875. parentElement.appendChild(spanElement);
  876. var menuElement = document.createElement('i');
  877. menuElement.className = 'fa fa-angle-down';
  878. menuElement.title = 'Remote user controls';
  879. spanElement.appendChild(menuElement);
  880. // <ul class="popupmenu">
  881. // <li><a href="#">Mute</a></li>
  882. // <li><a href="#">Eject</a></li>
  883. // </ul>
  884. var popupmenuElement = document.createElement('ul');
  885. popupmenuElement.className = 'popupmenu';
  886. popupmenuElement.id
  887. = 'remote_popupmenu_' + Strophe.getResourceFromJid(jid);
  888. spanElement.appendChild(popupmenuElement);
  889. var muteMenuItem = document.createElement('li');
  890. var muteLinkItem = document.createElement('a');
  891. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  892. if (!mutedAudios[jid]) {
  893. muteLinkItem.innerHTML = mutedIndicator + 'Mute';
  894. muteLinkItem.className = 'mutelink';
  895. }
  896. else {
  897. muteLinkItem.innerHTML = mutedIndicator + ' Muted';
  898. muteLinkItem.className = 'mutelink disabled';
  899. }
  900. muteLinkItem.onclick = function(){
  901. if ($(this).attr('disabled') != undefined) {
  902. event.preventDefault();
  903. }
  904. var isMute = !mutedAudios[jid];
  905. connection.moderate.setMute(jid, isMute);
  906. popupmenuElement.setAttribute('style', 'display:none;');
  907. if (isMute) {
  908. this.innerHTML = mutedIndicator + ' Muted';
  909. this.className = 'mutelink disabled';
  910. }
  911. else {
  912. this.innerHTML = mutedIndicator + ' Mute';
  913. this.className = 'mutelink';
  914. }
  915. };
  916. muteMenuItem.appendChild(muteLinkItem);
  917. popupmenuElement.appendChild(muteMenuItem);
  918. var ejectIndicator = "<i class='fa fa-eject'></i>";
  919. var ejectMenuItem = document.createElement('li');
  920. var ejectLinkItem = document.createElement('a');
  921. ejectLinkItem.innerHTML = ejectIndicator + ' Kick out';
  922. ejectLinkItem.onclick = function(){
  923. connection.moderate.eject(jid);
  924. popupmenuElement.setAttribute('style', 'display:none;');
  925. };
  926. ejectMenuItem.appendChild(ejectLinkItem);
  927. popupmenuElement.appendChild(ejectMenuItem);
  928. }
  929. /**
  930. * On audio muted event.
  931. */
  932. $(document).bind('audiomuted.muc', function (event, jid, isMuted) {
  933. var videoSpanId = null;
  934. if (jid === connection.emuc.myroomjid) {
  935. videoSpanId = 'localVideoContainer';
  936. } else {
  937. VideoLayout.ensurePeerContainerExists(jid);
  938. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  939. }
  940. if (focus) {
  941. mutedAudios[jid] = isMuted;
  942. VideoLayout.updateRemoteVideoMenu(jid, isMuted);
  943. }
  944. if (videoSpanId)
  945. VideoLayout.showAudioIndicator(videoSpanId, isMuted);
  946. });
  947. /**
  948. * On video muted event.
  949. */
  950. $(document).bind('videomuted.muc', function (event, jid, isMuted) {
  951. var videoSpanId = null;
  952. if (jid === connection.emuc.myroomjid) {
  953. videoSpanId = 'localVideoContainer';
  954. } else {
  955. VideoLayout.ensurePeerContainerExists(jid);
  956. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  957. }
  958. if (videoSpanId)
  959. VideoLayout.showVideoIndicator(videoSpanId, isMuted);
  960. });
  961. /**
  962. * Display name changed.
  963. */
  964. $(document).bind('displaynamechanged',
  965. function (event, jid, displayName, status) {
  966. if (jid === 'localVideoContainer'
  967. || jid === connection.emuc.myroomjid) {
  968. setDisplayName('localVideoContainer',
  969. displayName);
  970. } else {
  971. VideoLayout.ensurePeerContainerExists(jid);
  972. setDisplayName(
  973. 'participant_' + Strophe.getResourceFromJid(jid),
  974. displayName,
  975. status);
  976. }
  977. });
  978. /**
  979. * On dominant speaker changed event.
  980. */
  981. $(document).bind('dominantspeakerchanged', function (event, resourceJid) {
  982. // We ignore local user events.
  983. if (resourceJid
  984. === Strophe.getResourceFromJid(connection.emuc.myroomjid))
  985. return;
  986. // Update the current dominant speaker.
  987. if (resourceJid !== currentDominantSpeaker)
  988. currentDominantSpeaker = resourceJid;
  989. else
  990. return;
  991. // Obtain container for new dominant speaker.
  992. var container = document.getElementById(
  993. 'participant_' + resourceJid);
  994. // Local video will not have container found, but that's ok
  995. // since we don't want to switch to local video.
  996. if (container && !focusedVideoSrc)
  997. {
  998. var video = container.getElementsByTagName("video");
  999. // Update the large video if the video source is already available,
  1000. // otherwise wait for the "videoactive.jingle" event.
  1001. if (video.length && video[0].currentTime > 0)
  1002. VideoLayout.updateLargeVideo(video[0].src);
  1003. }
  1004. });
  1005. /**
  1006. * On last N change event.
  1007. *
  1008. * @param event the event that notified us
  1009. * @param lastNEndpoints the list of last N endpoints
  1010. * @param endpointsEnteringLastN the list currently entering last N
  1011. * endpoints
  1012. */
  1013. $(document).bind('lastnchanged', function ( event,
  1014. lastNEndpoints,
  1015. endpointsEnteringLastN,
  1016. stream) {
  1017. if (lastNCount !== lastNEndpoints.length)
  1018. lastNCount = lastNEndpoints.length;
  1019. lastNEndpointsCache = lastNEndpoints;
  1020. $('#remoteVideos>span').each(function( index, element ) {
  1021. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  1022. if (resourceJid
  1023. && lastNEndpoints.length > 0
  1024. && lastNEndpoints.indexOf(resourceJid) < 0) {
  1025. console.log("Remove from last N", resourceJid);
  1026. showPeerContainer(resourceJid, false);
  1027. }
  1028. });
  1029. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  1030. endpointsEnteringLastN = lastNEndpoints;
  1031. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  1032. endpointsEnteringLastN.forEach(function (resourceJid) {
  1033. if (!$('#participant_' + resourceJid).is(':visible')) {
  1034. console.log("Add to last N", resourceJid);
  1035. showPeerContainer(resourceJid, true);
  1036. mediaStreams.some(function (mediaStream) {
  1037. if (mediaStream.peerjid
  1038. && Strophe.getResourceFromJid(mediaStream.peerjid)
  1039. === resourceJid
  1040. && mediaStream.type === mediaStream.VIDEO_TYPE) {
  1041. var sel = $('#participant_' + resourceJid + '>video');
  1042. RTC.attachMediaStream(sel, mediaStream.stream);
  1043. waitForRemoteVideo(
  1044. sel,
  1045. mediaStream.ssrc,
  1046. mediaStream.stream);
  1047. return true;
  1048. }
  1049. });
  1050. }
  1051. });
  1052. }
  1053. });
  1054. $(document).bind('videoactive.jingle', function (event, videoelem) {
  1055. if (videoelem.attr('id').indexOf('mixedmslabel') === -1) {
  1056. // ignore mixedmslabela0 and v0
  1057. videoelem.show();
  1058. VideoLayout.resizeThumbnails();
  1059. var videoParent = videoelem.parent();
  1060. var parentResourceJid = null;
  1061. if (videoParent)
  1062. parentResourceJid
  1063. = VideoLayout.getPeerContainerResourceJid(videoParent[0]);
  1064. // Update the large video to the last added video only if there's no
  1065. // current dominant or focused speaker or update it to the current
  1066. // dominant speaker.
  1067. if ((!focusedVideoSrc && !VideoLayout.getDominantSpeakerResourceJid())
  1068. || (parentResourceJid
  1069. && VideoLayout.getDominantSpeakerResourceJid()
  1070. === parentResourceJid)) {
  1071. VideoLayout.updateLargeVideo(videoelem.attr('src'), 1);
  1072. }
  1073. VideoLayout.showFocusIndicator();
  1074. }
  1075. });
  1076. return my;
  1077. }(VideoLayout || {}));