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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  1. var VideoLayout = (function (my) {
  2. var currentDominantSpeaker = null;
  3. var lastNCount = config.channelLastN;
  4. var lastNEndpointsCache = [];
  5. var largeVideoState = {
  6. updateInProgress: false,
  7. newSrc: ''
  8. };
  9. my.connectionIndicators = {};
  10. my.changeLocalAudio = function(stream) {
  11. connection.jingle.localAudio = stream;
  12. RTC.attachMediaStream($('#localAudio'), stream);
  13. document.getElementById('localAudio').autoplay = true;
  14. document.getElementById('localAudio').volume = 0;
  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. // Set default display name.
  26. setDisplayName('localVideoContainer');
  27. if(!VideoLayout.connectionIndicators["localVideoContainer"]) {
  28. VideoLayout.connectionIndicators["localVideoContainer"]
  29. = new ConnectionIndicator($("#localVideoContainer")[0]);
  30. }
  31. AudioLevels.updateAudioLevelCanvas();
  32. var localVideoSelector = $('#' + localVideo.id);
  33. // Add click handler to both video and video wrapper elements in case
  34. // there's no video.
  35. localVideoSelector.click(function () {
  36. VideoLayout.handleVideoThumbClicked(localVideo.src);
  37. });
  38. $('#localVideoContainer').click(function () {
  39. VideoLayout.handleVideoThumbClicked(localVideo.src);
  40. });
  41. // Add hover handler
  42. $('#localVideoContainer').hover(
  43. function() {
  44. VideoLayout.showDisplayName('localVideoContainer', true);
  45. },
  46. function() {
  47. if (!VideoLayout.isLargeVideoVisible()
  48. || localVideo.src !== $('#largeVideo').attr('src'))
  49. VideoLayout.showDisplayName('localVideoContainer', false);
  50. }
  51. );
  52. // Add stream ended handler
  53. stream.onended = function () {
  54. localVideoContainer.removeChild(localVideo);
  55. VideoLayout.updateRemovedVideo(localVideo.src);
  56. };
  57. // Flip video x axis if needed
  58. flipXLocalVideo = flipX;
  59. if (flipX) {
  60. localVideoSelector.addClass("flipVideoX");
  61. }
  62. // Attach WebRTC stream
  63. var videoStream = simulcast.getLocalVideoStream();
  64. RTC.attachMediaStream(localVideoSelector, videoStream);
  65. localVideoSrc = localVideo.src;
  66. VideoLayout.updateLargeVideo(localVideoSrc, 0);
  67. };
  68. /**
  69. * Checks if removed video is currently displayed and tries to display
  70. * another one instead.
  71. * @param removedVideoSrc src stream identifier of the video.
  72. */
  73. my.updateRemovedVideo = function(removedVideoSrc) {
  74. if (removedVideoSrc === $('#largeVideo').attr('src')) {
  75. // this is currently displayed as large
  76. // pick the last visible video in the row
  77. // if nobody else is left, this picks the local video
  78. var pick
  79. = $('#remoteVideos>span[id!="mixedstream"]:visible:last>video')
  80. .get(0);
  81. if (!pick) {
  82. console.info("Last visible video no longer exists");
  83. pick = $('#remoteVideos>span[id!="mixedstream"]>video').get(0);
  84. if (!pick || !pick.src) {
  85. // Try local video
  86. console.info("Fallback to local video...");
  87. pick = $('#remoteVideos>span>span>video').get(0);
  88. }
  89. }
  90. // mute if localvideo
  91. if (pick) {
  92. VideoLayout.updateLargeVideo(pick.src, pick.volume);
  93. } else {
  94. console.warn("Failed to elect large video");
  95. }
  96. }
  97. };
  98. /**
  99. * Updates the large video with the given new video source.
  100. */
  101. my.updateLargeVideo = function(newSrc, vol) {
  102. console.log('hover in', newSrc);
  103. if ($('#largeVideo').attr('src') != newSrc) {
  104. // Due to the simulcast the localVideoSrc may have changed when the
  105. // fadeOut event triggers. In that case the getJidFromVideoSrc and
  106. // isVideoSrcDesktop methods will not function correctly.
  107. //
  108. // Also, again due to the simulcast, the updateLargeVideo method can
  109. // be called multiple times almost simultaneously. Therefore, we
  110. // store the state here and update only once.
  111. largeVideoState.newSrc = newSrc;
  112. largeVideoState.isVisible = $('#largeVideo').is(':visible');
  113. largeVideoState.isDesktop = isVideoSrcDesktop(newSrc);
  114. largeVideoState.userJid = getJidFromVideoSrc(newSrc);
  115. // Screen stream is already rotated
  116. largeVideoState.flipX = (newSrc === localVideoSrc) && flipXLocalVideo;
  117. var oldSrc = $('#largeVideo').attr('src');
  118. largeVideoState.oldJid = getJidFromVideoSrc(oldSrc);
  119. var fade = false;
  120. if (largeVideoState.oldJid != largeVideoState.userJid) {
  121. fade = true;
  122. // we want the notification to trigger even if userJid is undefined,
  123. // or null.
  124. $(document).trigger("selectedendpointchanged", [largeVideoState.userJid]);
  125. }
  126. if (!largeVideoState.updateInProgress) {
  127. largeVideoState.updateInProgress = true;
  128. var doUpdate = function () {
  129. $('#largeVideo').attr('src', largeVideoState.newSrc);
  130. var videoTransform = document.getElementById('largeVideo')
  131. .style.webkitTransform;
  132. if (largeVideoState.flipX && videoTransform !== 'scaleX(-1)') {
  133. document.getElementById('largeVideo').style.webkitTransform
  134. = "scaleX(-1)";
  135. }
  136. else if (!largeVideoState.flipX && videoTransform === 'scaleX(-1)') {
  137. document.getElementById('largeVideo').style.webkitTransform
  138. = "none";
  139. }
  140. // Change the way we'll be measuring and positioning large video
  141. getVideoSize = largeVideoState.isDesktop
  142. ? getDesktopVideoSize
  143. : getCameraVideoSize;
  144. getVideoPosition = largeVideoState.isDesktop
  145. ? getDesktopVideoPosition
  146. : getCameraVideoPosition;
  147. // Only if the large video is currently visible.
  148. // Disable previous dominant speaker video.
  149. if (largeVideoState.oldJid) {
  150. var oldResourceJid = Strophe.getResourceFromJid(largeVideoState.oldJid);
  151. VideoLayout.enableDominantSpeaker(oldResourceJid, false);
  152. if(VideoLayout.connectionIndicators) {
  153. var videoContainerId = null;
  154. if (oldResourceJid == Strophe.getResourceFromJid(connection.emuc.myroomjid)) {
  155. videoContainerId = 'localVideoContainer';
  156. }
  157. else {
  158. videoContainerId = 'participant_' + oldResourceJid;
  159. }
  160. if(VideoLayout.connectionIndicators[videoContainerId])
  161. VideoLayout.connectionIndicators[videoContainerId].setShowHQ(false);
  162. }
  163. }
  164. // Enable new dominant speaker in the remote videos section.
  165. if (largeVideoState.userJid) {
  166. var resourceJid = Strophe.getResourceFromJid(largeVideoState.userJid);
  167. VideoLayout.enableDominantSpeaker(resourceJid, true);
  168. if(VideoLayout.connectionIndicators)
  169. {
  170. var videoContainerId = null;
  171. if (resourceJid
  172. === Strophe.getResourceFromJid(connection.emuc.myroomjid)) {
  173. videoContainerId = 'localVideoContainer';
  174. }
  175. else {
  176. videoContainerId = 'participant_' + resourceJid;
  177. }
  178. if(VideoLayout.connectionIndicators[videoContainerId])
  179. VideoLayout.connectionIndicators[videoContainerId].setShowHQ(true);
  180. }
  181. }
  182. if (fade && largeVideoState.isVisible) {
  183. // using "this" should be ok because we're called
  184. // from within the fadeOut event.
  185. $(this).fadeIn(300);
  186. }
  187. largeVideoState.updateInProgress = false;
  188. };
  189. if (fade) {
  190. $('#largeVideo').fadeOut(300, doUpdate);
  191. } else {
  192. doUpdate();
  193. }
  194. }
  195. }
  196. };
  197. my.handleVideoThumbClicked = function(videoSrc) {
  198. // Restore style for previously focused video
  199. var focusJid = getJidFromVideoSrc(focusedVideoSrc);
  200. var oldContainer = getParticipantContainer(focusJid);
  201. if (oldContainer) {
  202. oldContainer.removeClass("videoContainerFocused");
  203. }
  204. // Unlock current focused.
  205. if (focusedVideoSrc === videoSrc)
  206. {
  207. focusedVideoSrc = null;
  208. var dominantSpeakerVideo = null;
  209. // Enable the currently set dominant speaker.
  210. if (currentDominantSpeaker) {
  211. dominantSpeakerVideo
  212. = $('#participant_' + currentDominantSpeaker + '>video')
  213. .get(0);
  214. if (dominantSpeakerVideo) {
  215. VideoLayout.updateLargeVideo(dominantSpeakerVideo.src, 1);
  216. }
  217. }
  218. return;
  219. }
  220. // Lock new video
  221. focusedVideoSrc = videoSrc;
  222. // Update focused/pinned interface.
  223. var userJid = getJidFromVideoSrc(videoSrc);
  224. if (userJid)
  225. {
  226. var container = getParticipantContainer(userJid);
  227. container.addClass("videoContainerFocused");
  228. }
  229. // Triggers a "video.selected" event. The "false" parameter indicates
  230. // this isn't a prezi.
  231. $(document).trigger("video.selected", [false]);
  232. VideoLayout.updateLargeVideo(videoSrc, 1);
  233. $('audio').each(function (idx, el) {
  234. if (el.id.indexOf('mixedmslabel') !== -1) {
  235. el.volume = 0;
  236. el.volume = 1;
  237. }
  238. });
  239. };
  240. /**
  241. * Positions the large video.
  242. *
  243. * @param videoWidth the stream video width
  244. * @param videoHeight the stream video height
  245. */
  246. my.positionLarge = function (videoWidth, videoHeight) {
  247. var videoSpaceWidth = $('#videospace').width();
  248. var videoSpaceHeight = window.innerHeight;
  249. var videoSize = getVideoSize(videoWidth,
  250. videoHeight,
  251. videoSpaceWidth,
  252. videoSpaceHeight);
  253. var largeVideoWidth = videoSize[0];
  254. var largeVideoHeight = videoSize[1];
  255. var videoPosition = getVideoPosition(largeVideoWidth,
  256. largeVideoHeight,
  257. videoSpaceWidth,
  258. videoSpaceHeight);
  259. var horizontalIndent = videoPosition[0];
  260. var verticalIndent = videoPosition[1];
  261. positionVideo($('#largeVideo'),
  262. largeVideoWidth,
  263. largeVideoHeight,
  264. horizontalIndent, verticalIndent);
  265. };
  266. /**
  267. * Shows/hides the large video.
  268. */
  269. my.setLargeVideoVisible = function(isVisible) {
  270. var largeVideoJid = getJidFromVideoSrc($('#largeVideo').attr('src'));
  271. var resourceJid = Strophe.getResourceFromJid(largeVideoJid);
  272. if (isVisible) {
  273. $('#largeVideo').css({visibility: 'visible'});
  274. $('.watermark').css({visibility: 'visible'});
  275. VideoLayout.enableDominantSpeaker(resourceJid, true);
  276. }
  277. else {
  278. $('#largeVideo').css({visibility: 'hidden'});
  279. $('.watermark').css({visibility: 'hidden'});
  280. VideoLayout.enableDominantSpeaker(resourceJid, false);
  281. }
  282. };
  283. /**
  284. * Indicates if the large video is currently visible.
  285. *
  286. * @return <tt>true</tt> if visible, <tt>false</tt> - otherwise
  287. */
  288. my.isLargeVideoVisible = function() {
  289. return $('#largeVideo').is(':visible');
  290. };
  291. /**
  292. * Checks if container for participant identified by given peerJid exists
  293. * in the document and creates it eventually.
  294. *
  295. * @param peerJid peer Jid to check.
  296. *
  297. * @return Returns <tt>true</tt> if the peer container exists,
  298. * <tt>false</tt> - otherwise
  299. */
  300. my.ensurePeerContainerExists = function(peerJid) {
  301. ContactList.ensureAddContact(peerJid);
  302. var resourceJid = Strophe.getResourceFromJid(peerJid);
  303. var videoSpanId = 'participant_' + resourceJid;
  304. if ($('#' + videoSpanId).length > 0) {
  305. // If there's been a focus change, make sure we add focus related
  306. // interface!!
  307. if (focus && $('#remote_popupmenu_' + resourceJid).length <= 0)
  308. addRemoteVideoMenu( peerJid,
  309. document.getElementById(videoSpanId));
  310. }
  311. else {
  312. var container
  313. = VideoLayout.addRemoteVideoContainer(peerJid, videoSpanId);
  314. // Set default display name.
  315. setDisplayName(videoSpanId);
  316. VideoLayout.connectionIndicators[videoSpanId] = new ConnectionIndicator(container);
  317. var nickfield = document.createElement('span');
  318. nickfield.className = "nick";
  319. nickfield.appendChild(document.createTextNode(resourceJid));
  320. container.appendChild(nickfield);
  321. // In case this is not currently in the last n we don't show it.
  322. if (lastNCount
  323. && lastNCount > 0
  324. && $('#remoteVideos>span').length >= lastNCount + 2) {
  325. showPeerContainer(resourceJid, false);
  326. }
  327. else
  328. VideoLayout.resizeThumbnails();
  329. }
  330. };
  331. my.addRemoteVideoContainer = function(peerJid, spanId) {
  332. var container = document.createElement('span');
  333. container.id = spanId;
  334. container.className = 'videocontainer';
  335. var remotes = document.getElementById('remoteVideos');
  336. // If the peerJid is null then this video span couldn't be directly
  337. // associated with a participant (this could happen in the case of prezi).
  338. if (focus && peerJid != null)
  339. addRemoteVideoMenu(peerJid, container);
  340. remotes.appendChild(container);
  341. AudioLevels.updateAudioLevelCanvas(peerJid);
  342. return container;
  343. };
  344. /**
  345. * Creates an audio or video stream element.
  346. */
  347. my.createStreamElement = function (sid, stream) {
  348. var isVideo = stream.getVideoTracks().length > 0;
  349. var element = isVideo
  350. ? document.createElement('video')
  351. : document.createElement('audio');
  352. var id = (isVideo ? 'remoteVideo_' : 'remoteAudio_')
  353. + sid + '_' + stream.id;
  354. element.id = id;
  355. element.autoplay = true;
  356. element.oncontextmenu = function () { return false; };
  357. return element;
  358. };
  359. my.addRemoteStreamElement
  360. = function (container, sid, stream, peerJid, thessrc) {
  361. var newElementId = null;
  362. var isVideo = stream.getVideoTracks().length > 0;
  363. if (container) {
  364. var streamElement = VideoLayout.createStreamElement(sid, stream);
  365. newElementId = streamElement.id;
  366. container.appendChild(streamElement);
  367. var sel = $('#' + newElementId);
  368. sel.hide();
  369. // If the container is currently visible we attach the stream.
  370. if (!isVideo
  371. || (container.offsetParent !== null && isVideo)) {
  372. var videoStream = simulcast.getReceivingVideoStream(stream);
  373. RTC.attachMediaStream(sel, videoStream);
  374. if (isVideo)
  375. waitForRemoteVideo(sel, thessrc, stream);
  376. }
  377. stream.onended = function () {
  378. console.log('stream ended', this);
  379. VideoLayout.removeRemoteStreamElement(
  380. stream, isVideo, container);
  381. if (peerJid)
  382. ContactList.removeContact(peerJid);
  383. };
  384. // Add click handler.
  385. container.onclick = function (event) {
  386. /*
  387. * FIXME It turns out that videoThumb may not exist (if there is
  388. * no actual video).
  389. */
  390. var videoThumb = $('#' + container.id + '>video').get(0);
  391. if (videoThumb)
  392. VideoLayout.handleVideoThumbClicked(videoThumb.src);
  393. event.preventDefault();
  394. return false;
  395. };
  396. // Add hover handler
  397. $(container).hover(
  398. function() {
  399. VideoLayout.showDisplayName(container.id, true);
  400. },
  401. function() {
  402. var videoSrc = null;
  403. if ($('#' + container.id + '>video')
  404. && $('#' + container.id + '>video').length > 0) {
  405. videoSrc = $('#' + container.id + '>video').get(0).src;
  406. }
  407. // If the video has been "pinned" by the user we want to
  408. // keep the display name on place.
  409. if (!VideoLayout.isLargeVideoVisible()
  410. || videoSrc !== $('#largeVideo').attr('src'))
  411. VideoLayout.showDisplayName(container.id, false);
  412. }
  413. );
  414. }
  415. return newElementId;
  416. };
  417. /**
  418. * Removes the remote stream element corresponding to the given stream and
  419. * parent container.
  420. *
  421. * @param stream the stream
  422. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  423. * @param container
  424. */
  425. my.removeRemoteStreamElement = function (stream, isVideo, container) {
  426. if (!container)
  427. return;
  428. var select = null;
  429. var removedVideoSrc = null;
  430. if (isVideo) {
  431. select = $('#' + container.id + '>video');
  432. removedVideoSrc = select.get(0).src;
  433. }
  434. else
  435. select = $('#' + container.id + '>audio');
  436. // Remove video source from the mapping.
  437. delete videoSrcToSsrc[removedVideoSrc];
  438. // Mark video as removed to cancel waiting loop(if video is removed
  439. // before has started)
  440. select.removed = true;
  441. select.remove();
  442. var audioCount = $('#' + container.id + '>audio').length;
  443. var videoCount = $('#' + container.id + '>video').length;
  444. if (!audioCount && !videoCount) {
  445. console.log("Remove whole user", container.id);
  446. if(VideoLayout.connectionIndicators[container.id])
  447. VideoLayout.connectionIndicators[container.id].remove();
  448. // Remove whole container
  449. container.remove();
  450. Util.playSoundNotification('userLeft');
  451. VideoLayout.resizeThumbnails();
  452. }
  453. if (removedVideoSrc)
  454. VideoLayout.updateRemovedVideo(removedVideoSrc);
  455. };
  456. /**
  457. * Show/hide peer container for the given resourceJid.
  458. */
  459. function showPeerContainer(resourceJid, isShow) {
  460. var peerContainer = $('#participant_' + resourceJid);
  461. if (!peerContainer)
  462. return;
  463. if (!peerContainer.is(':visible') && isShow)
  464. peerContainer.show();
  465. else if (peerContainer.is(':visible') && !isShow)
  466. {
  467. peerContainer.hide();
  468. if(VideoLayout.connectionIndicators['participant_' + resourceJid])
  469. VideoLayout.connectionIndicators['participant_' + resourceJid].hide();
  470. }
  471. VideoLayout.resizeThumbnails();
  472. ContactList.setClickable(resourceJid, isShow);
  473. };
  474. /**
  475. * Sets the display name for the given video span id.
  476. */
  477. function setDisplayName(videoSpanId, displayName) {
  478. var nameSpan = $('#' + videoSpanId + '>span.displayname');
  479. var defaultLocalDisplayName = "Me";
  480. // If we already have a display name for this video.
  481. if (nameSpan.length > 0) {
  482. var nameSpanElement = nameSpan.get(0);
  483. if (nameSpanElement.id === 'localDisplayName' &&
  484. $('#localDisplayName').text() !== displayName) {
  485. if (displayName && displayName.length > 0)
  486. $('#localDisplayName').text(displayName + ' (me)');
  487. else
  488. $('#localDisplayName').text(defaultLocalDisplayName);
  489. } else {
  490. if (displayName && displayName.length > 0)
  491. $('#' + videoSpanId + '_name').text(displayName);
  492. else
  493. $('#' + videoSpanId + '_name').text(interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  494. }
  495. } else {
  496. var editButton = null;
  497. nameSpan = document.createElement('span');
  498. nameSpan.className = 'displayname';
  499. $('#' + videoSpanId)[0].appendChild(nameSpan);
  500. if (videoSpanId === 'localVideoContainer') {
  501. editButton = createEditDisplayNameButton();
  502. nameSpan.innerText = defaultLocalDisplayName;
  503. }
  504. else {
  505. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  506. }
  507. if (displayName && displayName.length > 0) {
  508. nameSpan.innerText = displayName;
  509. }
  510. if (!editButton) {
  511. nameSpan.id = videoSpanId + '_name';
  512. } else {
  513. nameSpan.id = 'localDisplayName';
  514. $('#' + videoSpanId)[0].appendChild(editButton);
  515. var editableText = document.createElement('input');
  516. editableText.className = 'displayname';
  517. editableText.type = 'text';
  518. editableText.id = 'editDisplayName';
  519. if (displayName && displayName.length) {
  520. editableText.value
  521. = displayName.substring(0, displayName.indexOf(' (me)'));
  522. }
  523. editableText.setAttribute('style', 'display:none;');
  524. editableText.setAttribute('placeholder', 'ex. Jane Pink');
  525. $('#' + videoSpanId)[0].appendChild(editableText);
  526. $('#localVideoContainer .displayname')
  527. .bind("click", function (e) {
  528. e.preventDefault();
  529. $('#localDisplayName').hide();
  530. $('#editDisplayName').show();
  531. $('#editDisplayName').focus();
  532. $('#editDisplayName').select();
  533. var inputDisplayNameHandler = function (name) {
  534. if (nickname !== name) {
  535. nickname = name;
  536. window.localStorage.displayname = nickname;
  537. connection.emuc.addDisplayNameToPresence(nickname);
  538. connection.emuc.sendPresence();
  539. Chat.setChatConversationMode(true);
  540. }
  541. if (!$('#localDisplayName').is(":visible")) {
  542. if (nickname)
  543. $('#localDisplayName').text(nickname + " (me)");
  544. else
  545. $('#localDisplayName')
  546. .text(defaultLocalDisplayName);
  547. $('#localDisplayName').show();
  548. }
  549. $('#editDisplayName').hide();
  550. };
  551. $('#editDisplayName').one("focusout", function (e) {
  552. inputDisplayNameHandler(this.value);
  553. });
  554. $('#editDisplayName').on('keydown', function (e) {
  555. if (e.keyCode === 13) {
  556. e.preventDefault();
  557. inputDisplayNameHandler(this.value);
  558. }
  559. });
  560. });
  561. }
  562. }
  563. };
  564. /**
  565. * Shows/hides the display name on the remote video.
  566. * @param videoSpanId the identifier of the video span element
  567. * @param isShow indicates if the display name should be shown or hidden
  568. */
  569. my.showDisplayName = function(videoSpanId, isShow) {
  570. var nameSpan = $('#' + videoSpanId + '>span.displayname').get(0);
  571. if (isShow) {
  572. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  573. nameSpan.setAttribute("style", "display:inline-block;");
  574. }
  575. else {
  576. if (nameSpan)
  577. nameSpan.setAttribute("style", "display:none;");
  578. }
  579. };
  580. /**
  581. * Shows the presence status message for the given video.
  582. */
  583. my.setPresenceStatus = function (videoSpanId, statusMsg) {
  584. if (!$('#' + videoSpanId).length) {
  585. // No container
  586. return;
  587. }
  588. var statusSpan = $('#' + videoSpanId + '>span.status');
  589. if (!statusSpan.length) {
  590. //Add status span
  591. statusSpan = document.createElement('span');
  592. statusSpan.className = 'status';
  593. statusSpan.id = videoSpanId + '_status';
  594. $('#' + videoSpanId)[0].appendChild(statusSpan);
  595. statusSpan = $('#' + videoSpanId + '>span.status');
  596. }
  597. // Display status
  598. if (statusMsg && statusMsg.length) {
  599. $('#' + videoSpanId + '_status').text(statusMsg);
  600. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  601. }
  602. else {
  603. // Hide
  604. statusSpan.get(0).setAttribute("style", "display:none;");
  605. }
  606. };
  607. /**
  608. * Shows a visual indicator for the focus of the conference.
  609. * Currently if we're not the owner of the conference we obtain the focus
  610. * from the connection.jingle.sessions.
  611. */
  612. my.showFocusIndicator = function() {
  613. if (focus !== null) {
  614. var indicatorSpan = $('#localVideoContainer .focusindicator');
  615. if (indicatorSpan.children().length === 0)
  616. {
  617. createFocusIndicatorElement(indicatorSpan[0]);
  618. }
  619. }
  620. else if (Object.keys(connection.jingle.sessions).length > 0) {
  621. // If we're only a participant the focus will be the only session we have.
  622. var session
  623. = connection.jingle.sessions
  624. [Object.keys(connection.jingle.sessions)[0]];
  625. var focusId
  626. = 'participant_' + Strophe.getResourceFromJid(session.peerjid);
  627. var focusContainer = document.getElementById(focusId);
  628. if (!focusContainer) {
  629. console.error("No focus container!");
  630. return;
  631. }
  632. var indicatorSpan = $('#' + focusId + ' .focusindicator');
  633. if (!indicatorSpan || indicatorSpan.length === 0) {
  634. indicatorSpan = document.createElement('span');
  635. indicatorSpan.className = 'focusindicator';
  636. focusContainer.appendChild(indicatorSpan);
  637. createFocusIndicatorElement(indicatorSpan);
  638. }
  639. }
  640. };
  641. /**
  642. * Shows video muted indicator over small videos.
  643. */
  644. my.showVideoIndicator = function(videoSpanId, isMuted) {
  645. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  646. if (isMuted === 'false') {
  647. if (videoMutedSpan.length > 0) {
  648. videoMutedSpan.remove();
  649. }
  650. }
  651. else {
  652. if(videoMutedSpan.length == 0) {
  653. videoMutedSpan = document.createElement('span');
  654. videoMutedSpan.className = 'videoMuted';
  655. $('#' + videoSpanId)[0].appendChild(videoMutedSpan);
  656. var mutedIndicator = document.createElement('i');
  657. mutedIndicator.className = 'icon-camera-disabled';
  658. Util.setTooltip(mutedIndicator,
  659. "Participant has<br/>stopped the camera.",
  660. "top");
  661. videoMutedSpan.appendChild(mutedIndicator);
  662. }
  663. VideoLayout.updateMutePosition(videoSpanId);
  664. }
  665. };
  666. my.updateMutePosition = function (videoSpanId) {
  667. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  668. var connectionIndicator = $('#' + videoSpanId + '>div.connectionindicator');
  669. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  670. if(connectionIndicator.length > 0
  671. && connectionIndicator[0].style.display != "none") {
  672. audioMutedSpan.css({right: "23px"});
  673. videoMutedSpan.css({right: ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  674. }
  675. else
  676. {
  677. audioMutedSpan.css({right: "0px"});
  678. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  679. }
  680. }
  681. /**
  682. * Shows audio muted indicator over small videos.
  683. * @param {string} isMuted
  684. */
  685. my.showAudioIndicator = function(videoSpanId, isMuted) {
  686. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  687. if (isMuted === 'false') {
  688. if (audioMutedSpan.length > 0) {
  689. audioMutedSpan.popover('hide');
  690. audioMutedSpan.remove();
  691. }
  692. }
  693. else {
  694. if(audioMutedSpan.length == 0 ) {
  695. audioMutedSpan = document.createElement('span');
  696. audioMutedSpan.className = 'audioMuted';
  697. Util.setTooltip(audioMutedSpan,
  698. "Participant is muted",
  699. "top");
  700. $('#' + videoSpanId)[0].appendChild(audioMutedSpan);
  701. var mutedIndicator = document.createElement('i');
  702. mutedIndicator.className = 'icon-mic-disabled';
  703. audioMutedSpan.appendChild(mutedIndicator);
  704. }
  705. VideoLayout.updateMutePosition(videoSpanId);
  706. }
  707. };
  708. /*
  709. * Shows or hides the audio muted indicator over the local thumbnail video.
  710. * @param {boolean} isMuted
  711. */
  712. my.showLocalAudioIndicator = function(isMuted) {
  713. VideoLayout.showAudioIndicator('localVideoContainer', isMuted.toString());
  714. };
  715. /**
  716. * Resizes the large video container.
  717. */
  718. my.resizeLargeVideoContainer = function () {
  719. Chat.resizeChat();
  720. var availableHeight = window.innerHeight;
  721. var availableWidth = Util.getAvailableVideoWidth();
  722. if (availableWidth < 0 || availableHeight < 0) return;
  723. $('#videospace').width(availableWidth);
  724. $('#videospace').height(availableHeight);
  725. $('#largeVideoContainer').width(availableWidth);
  726. $('#largeVideoContainer').height(availableHeight);
  727. VideoLayout.resizeThumbnails();
  728. };
  729. /**
  730. * Resizes thumbnails.
  731. */
  732. my.resizeThumbnails = function() {
  733. var videoSpaceWidth = $('#remoteVideos').width();
  734. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  735. var width = thumbnailSize[0];
  736. var height = thumbnailSize[1];
  737. // size videos so that while keeping AR and max height, we have a
  738. // nice fit
  739. $('#remoteVideos').height(height);
  740. $('#remoteVideos>span').width(width);
  741. $('#remoteVideos>span').height(height);
  742. $(document).trigger("remotevideo.resized", [width, height]);
  743. };
  744. /**
  745. * Enables the dominant speaker UI.
  746. *
  747. * @param resourceJid the jid indicating the video element to
  748. * activate/deactivate
  749. * @param isEnable indicates if the dominant speaker should be enabled or
  750. * disabled
  751. */
  752. my.enableDominantSpeaker = function(resourceJid, isEnable) {
  753. var videoSpanId = null;
  754. var videoContainerId = null;
  755. if (resourceJid
  756. === Strophe.getResourceFromJid(connection.emuc.myroomjid)) {
  757. videoSpanId = 'localVideoWrapper';
  758. videoContainerId = 'localVideoContainer';
  759. }
  760. else {
  761. videoSpanId = 'participant_' + resourceJid;
  762. videoContainerId = videoSpanId;
  763. }
  764. var displayName = resourceJid;
  765. var nameSpan = $('#' + videoContainerId + '>span.displayname');
  766. if (nameSpan.length > 0)
  767. displayName = nameSpan.text();
  768. console.log("UI enable dominant speaker",
  769. displayName,
  770. resourceJid,
  771. isEnable);
  772. videoSpan = document.getElementById(videoContainerId);
  773. if (!videoSpan) {
  774. console.error("No video element for jid", resourceJid);
  775. return;
  776. }
  777. var video = $('#' + videoSpanId + '>video');
  778. if (video && video.length > 0) {
  779. if (isEnable) {
  780. VideoLayout.showDisplayName(videoContainerId, true);
  781. if (!videoSpan.classList.contains("dominantspeaker"))
  782. videoSpan.classList.add("dominantspeaker");
  783. video.css({visibility: 'hidden'});
  784. }
  785. else {
  786. VideoLayout.showDisplayName(videoContainerId, false);
  787. if (videoSpan.classList.contains("dominantspeaker"))
  788. videoSpan.classList.remove("dominantspeaker");
  789. video.css({visibility: 'visible'});
  790. }
  791. }
  792. };
  793. /**
  794. * Gets the selector of video thumbnail container for the user identified by
  795. * given <tt>userJid</tt>
  796. * @param userJid user's Jid for whom we want to get the video container.
  797. */
  798. function getParticipantContainer(userJid)
  799. {
  800. if (!userJid)
  801. return null;
  802. if (userJid === connection.emuc.myroomjid)
  803. return $("#localVideoContainer");
  804. else
  805. return $("#participant_" + Strophe.getResourceFromJid(userJid));
  806. }
  807. /**
  808. * Sets the size and position of the given video element.
  809. *
  810. * @param video the video element to position
  811. * @param width the desired video width
  812. * @param height the desired video height
  813. * @param horizontalIndent the left and right indent
  814. * @param verticalIndent the top and bottom indent
  815. */
  816. function positionVideo(video,
  817. width,
  818. height,
  819. horizontalIndent,
  820. verticalIndent) {
  821. video.width(width);
  822. video.height(height);
  823. video.css({ top: verticalIndent + 'px',
  824. bottom: verticalIndent + 'px',
  825. left: horizontalIndent + 'px',
  826. right: horizontalIndent + 'px'});
  827. }
  828. /**
  829. * Calculates the thumbnail size.
  830. *
  831. * @param videoSpaceWidth the width of the video space
  832. */
  833. my.calculateThumbnailSize = function (videoSpaceWidth) {
  834. // Calculate the available height, which is the inner window height minus
  835. // 39px for the header minus 2px for the delimiter lines on the top and
  836. // bottom of the large video, minus the 36px space inside the remoteVideos
  837. // container used for highlighting shadow.
  838. var availableHeight = 100;
  839. var numvids = $('#remoteVideos>span:visible').length;
  840. if (lastNCount && lastNCount > 0) {
  841. numvids = Math.min(lastNCount + 1, numvids);
  842. }
  843. // Remove the 3px borders arround videos and border around the remote
  844. // videos area and the 4 pixels between the local video and the others
  845. //TODO: Find out where the 4 pixels come from and remove them
  846. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70 - 4;
  847. var availableWidth = availableWinWidth / numvids;
  848. var aspectRatio = 16.0 / 9.0;
  849. var maxHeight = Math.min(160, availableHeight);
  850. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  851. if (availableHeight < availableWidth / aspectRatio) {
  852. availableWidth = Math.floor(availableHeight * aspectRatio);
  853. }
  854. return [availableWidth, availableHeight];
  855. };
  856. /**
  857. * Returns an array of the video dimensions, so that it keeps it's aspect
  858. * ratio and fits available area with it's larger dimension. This method
  859. * ensures that whole video will be visible and can leave empty areas.
  860. *
  861. * @return an array with 2 elements, the video width and the video height
  862. */
  863. function getDesktopVideoSize(videoWidth,
  864. videoHeight,
  865. videoSpaceWidth,
  866. videoSpaceHeight) {
  867. if (!videoWidth)
  868. videoWidth = currentVideoWidth;
  869. if (!videoHeight)
  870. videoHeight = currentVideoHeight;
  871. var aspectRatio = videoWidth / videoHeight;
  872. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  873. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  874. videoSpaceHeight -= $('#remoteVideos').outerHeight();
  875. if (availableWidth / aspectRatio >= videoSpaceHeight)
  876. {
  877. availableHeight = videoSpaceHeight;
  878. availableWidth = availableHeight * aspectRatio;
  879. }
  880. if (availableHeight * aspectRatio >= videoSpaceWidth)
  881. {
  882. availableWidth = videoSpaceWidth;
  883. availableHeight = availableWidth / aspectRatio;
  884. }
  885. return [availableWidth, availableHeight];
  886. }
  887. /**
  888. * Creates the edit display name button.
  889. *
  890. * @returns the edit button
  891. */
  892. function createEditDisplayNameButton() {
  893. var editButton = document.createElement('a');
  894. editButton.className = 'displayname';
  895. Util.setTooltip(editButton,
  896. 'Click to edit your<br/>display name',
  897. "top");
  898. editButton.innerHTML = '<i class="fa fa-pencil"></i>';
  899. return editButton;
  900. }
  901. /**
  902. * Creates the element indicating the focus of the conference.
  903. *
  904. * @param parentElement the parent element where the focus indicator will
  905. * be added
  906. */
  907. function createFocusIndicatorElement(parentElement) {
  908. var focusIndicator = document.createElement('i');
  909. focusIndicator.className = 'fa fa-star';
  910. parentElement.appendChild(focusIndicator);
  911. Util.setTooltip(parentElement,
  912. "The owner of<br/>this conference",
  913. "top");
  914. }
  915. /**
  916. * Updates the remote video menu.
  917. *
  918. * @param jid the jid indicating the video for which we're adding a menu.
  919. * @param isMuted indicates the current mute state
  920. */
  921. my.updateRemoteVideoMenu = function(jid, isMuted) {
  922. var muteMenuItem
  923. = $('#remote_popupmenu_'
  924. + Strophe.getResourceFromJid(jid)
  925. + '>li>a.mutelink');
  926. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  927. if (muteMenuItem.length) {
  928. var muteLink = muteMenuItem.get(0);
  929. if (isMuted === 'true') {
  930. muteLink.innerHTML = mutedIndicator + ' Muted';
  931. muteLink.className = 'mutelink disabled';
  932. }
  933. else {
  934. muteLink.innerHTML = mutedIndicator + ' Mute';
  935. muteLink.className = 'mutelink';
  936. }
  937. }
  938. };
  939. /**
  940. * Returns the current dominant speaker resource jid.
  941. */
  942. my.getDominantSpeakerResourceJid = function () {
  943. return currentDominantSpeaker;
  944. };
  945. /**
  946. * Returns the corresponding resource jid to the given peer container
  947. * DOM element.
  948. *
  949. * @return the corresponding resource jid to the given peer container
  950. * DOM element
  951. */
  952. my.getPeerContainerResourceJid = function (containerElement) {
  953. var i = containerElement.id.indexOf('participant_');
  954. if (i >= 0)
  955. return containerElement.id.substring(i + 12);
  956. };
  957. /**
  958. * Adds the remote video menu element for the given <tt>jid</tt> in the
  959. * given <tt>parentElement</tt>.
  960. *
  961. * @param jid the jid indicating the video for which we're adding a menu.
  962. * @param parentElement the parent element where this menu will be added
  963. */
  964. function addRemoteVideoMenu(jid, parentElement) {
  965. var spanElement = document.createElement('span');
  966. spanElement.className = 'remotevideomenu';
  967. parentElement.appendChild(spanElement);
  968. var menuElement = document.createElement('i');
  969. menuElement.className = 'fa fa-angle-down';
  970. menuElement.title = 'Remote user controls';
  971. spanElement.appendChild(menuElement);
  972. // <ul class="popupmenu">
  973. // <li><a href="#">Mute</a></li>
  974. // <li><a href="#">Eject</a></li>
  975. // </ul>
  976. var popupmenuElement = document.createElement('ul');
  977. popupmenuElement.className = 'popupmenu';
  978. popupmenuElement.id
  979. = 'remote_popupmenu_' + Strophe.getResourceFromJid(jid);
  980. spanElement.appendChild(popupmenuElement);
  981. var muteMenuItem = document.createElement('li');
  982. var muteLinkItem = document.createElement('a');
  983. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  984. if (!mutedAudios[jid]) {
  985. muteLinkItem.innerHTML = mutedIndicator + 'Mute';
  986. muteLinkItem.className = 'mutelink';
  987. }
  988. else {
  989. muteLinkItem.innerHTML = mutedIndicator + ' Muted';
  990. muteLinkItem.className = 'mutelink disabled';
  991. }
  992. muteLinkItem.onclick = function(){
  993. if ($(this).attr('disabled') != undefined) {
  994. event.preventDefault();
  995. }
  996. var isMute = !mutedAudios[jid];
  997. connection.moderate.setMute(jid, isMute);
  998. popupmenuElement.setAttribute('style', 'display:none;');
  999. if (isMute) {
  1000. this.innerHTML = mutedIndicator + ' Muted';
  1001. this.className = 'mutelink disabled';
  1002. }
  1003. else {
  1004. this.innerHTML = mutedIndicator + ' Mute';
  1005. this.className = 'mutelink';
  1006. }
  1007. };
  1008. muteMenuItem.appendChild(muteLinkItem);
  1009. popupmenuElement.appendChild(muteMenuItem);
  1010. var ejectIndicator = "<i class='fa fa-eject'></i>";
  1011. var ejectMenuItem = document.createElement('li');
  1012. var ejectLinkItem = document.createElement('a');
  1013. ejectLinkItem.innerHTML = ejectIndicator + ' Kick out';
  1014. ejectLinkItem.onclick = function(){
  1015. connection.moderate.eject(jid);
  1016. popupmenuElement.setAttribute('style', 'display:none;');
  1017. };
  1018. ejectMenuItem.appendChild(ejectLinkItem);
  1019. popupmenuElement.appendChild(ejectMenuItem);
  1020. var paddingSpan = document.createElement('span');
  1021. paddingSpan.className = 'popupmenuPadding';
  1022. popupmenuElement.appendChild(paddingSpan);
  1023. }
  1024. /**
  1025. * On audio muted event.
  1026. */
  1027. $(document).bind('audiomuted.muc', function (event, jid, isMuted) {
  1028. if (jid === connection.emuc.myroomjid) {
  1029. // The local mute indicator is controlled locally
  1030. return;
  1031. }
  1032. VideoLayout.ensurePeerContainerExists(jid);
  1033. if (focus) {
  1034. mutedAudios[jid] = isMuted;
  1035. VideoLayout.updateRemoteVideoMenu(jid, isMuted);
  1036. }
  1037. var videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  1038. if (videoSpanId)
  1039. VideoLayout.showAudioIndicator(videoSpanId, isMuted);
  1040. });
  1041. /**
  1042. * On video muted event.
  1043. */
  1044. $(document).bind('videomuted.muc', function (event, jid, isMuted) {
  1045. var videoSpanId = null;
  1046. if (jid === connection.emuc.myroomjid) {
  1047. videoSpanId = 'localVideoContainer';
  1048. } else {
  1049. VideoLayout.ensurePeerContainerExists(jid);
  1050. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  1051. }
  1052. if (videoSpanId)
  1053. VideoLayout.showVideoIndicator(videoSpanId, isMuted);
  1054. });
  1055. /**
  1056. * Display name changed.
  1057. */
  1058. $(document).bind('displaynamechanged',
  1059. function (event, jid, displayName, status) {
  1060. if (jid === 'localVideoContainer'
  1061. || jid === connection.emuc.myroomjid) {
  1062. setDisplayName('localVideoContainer',
  1063. displayName);
  1064. } else {
  1065. VideoLayout.ensurePeerContainerExists(jid);
  1066. setDisplayName(
  1067. 'participant_' + Strophe.getResourceFromJid(jid),
  1068. displayName,
  1069. status);
  1070. }
  1071. });
  1072. /**
  1073. * On dominant speaker changed event.
  1074. */
  1075. $(document).bind('dominantspeakerchanged', function (event, resourceJid) {
  1076. // We ignore local user events.
  1077. if (resourceJid
  1078. === Strophe.getResourceFromJid(connection.emuc.myroomjid))
  1079. return;
  1080. // Update the current dominant speaker.
  1081. if (resourceJid !== currentDominantSpeaker) {
  1082. var oldSpeakerVideoSpanId = "participant_" + currentDominantSpeaker,
  1083. newSpeakerVideoSpanId = "participant_" + resourceJid;
  1084. if($("#" + oldSpeakerVideoSpanId + ">span.displayname").text() ===
  1085. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME) {
  1086. setDisplayName(oldSpeakerVideoSpanId, null);
  1087. }
  1088. if($("#" + newSpeakerVideoSpanId + ">span.displayname").text() ===
  1089. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME) {
  1090. setDisplayName(newSpeakerVideoSpanId,
  1091. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  1092. }
  1093. currentDominantSpeaker = resourceJid;
  1094. } else {
  1095. return;
  1096. }
  1097. // Obtain container for new dominant speaker.
  1098. var container = document.getElementById(
  1099. 'participant_' + resourceJid);
  1100. // Local video will not have container found, but that's ok
  1101. // since we don't want to switch to local video.
  1102. if (container && !focusedVideoSrc)
  1103. {
  1104. var video = container.getElementsByTagName("video");
  1105. // Update the large video if the video source is already available,
  1106. // otherwise wait for the "videoactive.jingle" event.
  1107. if (video.length && video[0].currentTime > 0)
  1108. VideoLayout.updateLargeVideo(video[0].src);
  1109. }
  1110. });
  1111. /**
  1112. * On last N change event.
  1113. *
  1114. * @param event the event that notified us
  1115. * @param lastNEndpoints the list of last N endpoints
  1116. * @param endpointsEnteringLastN the list currently entering last N
  1117. * endpoints
  1118. */
  1119. $(document).bind('lastnchanged', function ( event,
  1120. lastNEndpoints,
  1121. endpointsEnteringLastN,
  1122. stream) {
  1123. if (lastNCount !== lastNEndpoints.length)
  1124. lastNCount = lastNEndpoints.length;
  1125. lastNEndpointsCache = lastNEndpoints;
  1126. $('#remoteVideos>span').each(function( index, element ) {
  1127. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  1128. if (resourceJid
  1129. && lastNEndpoints.indexOf(resourceJid) < 0) {
  1130. console.log("Remove from last N", resourceJid);
  1131. showPeerContainer(resourceJid, false);
  1132. }
  1133. });
  1134. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  1135. endpointsEnteringLastN = lastNEndpoints;
  1136. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  1137. endpointsEnteringLastN.forEach(function (resourceJid) {
  1138. if (!$('#participant_' + resourceJid).is(':visible')) {
  1139. console.log("Add to last N", resourceJid);
  1140. showPeerContainer(resourceJid, true);
  1141. mediaStreams.some(function (mediaStream) {
  1142. if (mediaStream.peerjid
  1143. && Strophe.getResourceFromJid(mediaStream.peerjid)
  1144. === resourceJid
  1145. && mediaStream.type === mediaStream.VIDEO_TYPE) {
  1146. var sel = $('#participant_' + resourceJid + '>video');
  1147. var videoStream = simulcast.getReceivingVideoStream(mediaStream.stream);
  1148. RTC.attachMediaStream(sel, videoStream);
  1149. waitForRemoteVideo(
  1150. sel,
  1151. mediaStream.ssrc,
  1152. mediaStream.stream);
  1153. return true;
  1154. }
  1155. });
  1156. }
  1157. });
  1158. }
  1159. });
  1160. $(document).bind('videoactive.jingle', function (event, videoelem) {
  1161. if (videoelem.attr('id').indexOf('mixedmslabel') === -1) {
  1162. // ignore mixedmslabela0 and v0
  1163. videoelem.show();
  1164. VideoLayout.resizeThumbnails();
  1165. var videoParent = videoelem.parent();
  1166. var parentResourceJid = null;
  1167. if (videoParent)
  1168. parentResourceJid
  1169. = VideoLayout.getPeerContainerResourceJid(videoParent[0]);
  1170. // Update the large video to the last added video only if there's no
  1171. // current dominant or focused speaker or update it to the current
  1172. // dominant speaker.
  1173. if ((!focusedVideoSrc && !VideoLayout.getDominantSpeakerResourceJid())
  1174. || (parentResourceJid
  1175. && VideoLayout.getDominantSpeakerResourceJid()
  1176. === parentResourceJid)) {
  1177. VideoLayout.updateLargeVideo(videoelem.attr('src'), 1);
  1178. }
  1179. VideoLayout.showFocusIndicator();
  1180. }
  1181. });
  1182. /**
  1183. * On simulcast layers changed event.
  1184. */
  1185. $(document).bind('simulcastlayerschanged', function (event, endpointSimulcastLayers) {
  1186. endpointSimulcastLayers.forEach(function (esl) {
  1187. var primarySSRC = esl.simulcastLayer.primarySSRC;
  1188. var msid = simulcast.getRemoteVideoStreamIdBySSRC(primarySSRC);
  1189. // Get session and stream from msid.
  1190. var session, electedStream;
  1191. var i, j, k;
  1192. if (connection.jingle) {
  1193. var keys = Object.keys(connection.jingle.sessions);
  1194. for (i = 0; i < keys.length; i++) {
  1195. var sid = keys[i];
  1196. if (electedStream) {
  1197. // stream found, stop.
  1198. break;
  1199. }
  1200. session = connection.jingle.sessions[sid];
  1201. if (session.remoteStreams) {
  1202. for (j = 0; j < session.remoteStreams.length; j++) {
  1203. var remoteStream = session.remoteStreams[j];
  1204. if (electedStream) {
  1205. // stream found, stop.
  1206. break;
  1207. }
  1208. var tracks = remoteStream.getVideoTracks();
  1209. if (tracks) {
  1210. for (k = 0; k < tracks.length; k++) {
  1211. var track = tracks[k];
  1212. if (msid === [remoteStream.id, track.id].join(' ')) {
  1213. electedStream = new webkitMediaStream([track]);
  1214. // stream found, stop.
  1215. break;
  1216. }
  1217. }
  1218. }
  1219. }
  1220. }
  1221. }
  1222. }
  1223. if (session && electedStream) {
  1224. console.info('Switching simulcast substream.');
  1225. console.info([esl, primarySSRC, msid, session, electedStream]);
  1226. var msidParts = msid.split(' ');
  1227. var selRemoteVideo = $(['#', 'remoteVideo_', session.sid, '_', msidParts[0]].join(''));
  1228. var updateLargeVideo = (ssrc2jid[videoSrcToSsrc[selRemoteVideo.attr('src')]]
  1229. == ssrc2jid[videoSrcToSsrc[largeVideoState.newSrc]]);
  1230. var updateFocusedVideoSrc = (selRemoteVideo.attr('src') == focusedVideoSrc);
  1231. var electedStreamUrl = webkitURL.createObjectURL(electedStream);
  1232. selRemoteVideo.attr('src', electedStreamUrl);
  1233. videoSrcToSsrc[selRemoteVideo.attr('src')] = primarySSRC;
  1234. if (updateLargeVideo) {
  1235. VideoLayout.updateLargeVideo(electedStreamUrl);
  1236. }
  1237. if (updateFocusedVideoSrc) {
  1238. focusedVideoSrc = electedStreamUrl;
  1239. }
  1240. } else {
  1241. console.error('Could not find a stream or a session.', session, electedStream);
  1242. }
  1243. });
  1244. });
  1245. /**
  1246. * Constructs new connection indicator.
  1247. * @param videoContainer the video container associated with the indicator.
  1248. * @constructor
  1249. */
  1250. function ConnectionIndicator(videoContainer)
  1251. {
  1252. this.videoContainer = videoContainer;
  1253. this.bandwidth = null;
  1254. this.packetLoss = null;
  1255. this.bitrate = null;
  1256. this.showMoreValue = false;
  1257. this.resolution = null;
  1258. this.transport = [];
  1259. this.popover = null;
  1260. this.showHQ = false;
  1261. this.create();
  1262. }
  1263. /**
  1264. * Values for the connection quality
  1265. * @type {{98: string, 81: string, 64: string, 47: string, 30: string, 0: string}}
  1266. */
  1267. ConnectionIndicator.connectionQualityValues = {
  1268. 98: "18px", //full
  1269. 81: "15px",//4 bars
  1270. 64: "11px",//3 bars
  1271. 47: "7px",//2 bars
  1272. 30: "3px",//1 bar
  1273. 0: "0px"//empty
  1274. };
  1275. /**
  1276. * Sets the value of the property that indicates whether the displayed resolution si the
  1277. * resolution of High Quality stream or Low Quality
  1278. * @param value boolean.
  1279. */
  1280. ConnectionIndicator.prototype.setShowHQ = function (value) {
  1281. this.showHQ = value;
  1282. this.updatePopoverData();
  1283. };
  1284. /**
  1285. * Generates the html content.
  1286. * @returns {string} the html content.
  1287. */
  1288. ConnectionIndicator.prototype.generateText = function () {
  1289. var downloadBitrate, uploadBitrate, packetLoss, resolution;
  1290. if(this.bitrate === null)
  1291. {
  1292. downloadBitrate = "N/A";
  1293. uploadBitrate = "N/A";
  1294. }
  1295. else
  1296. {
  1297. downloadBitrate = this.bitrate.download? this.bitrate.download + " Kbps" : "N/A";
  1298. uploadBitrate = this.bitrate.upload? this.bitrate.upload + " Kbps" : "N/A";
  1299. }
  1300. if(this.packetLoss === null)
  1301. {
  1302. packetLoss = "N/A";
  1303. }
  1304. else
  1305. {
  1306. packetLoss = "<span class='jitsipopover_green'>&darr;</span>" +
  1307. (this.packetLoss.download != null? this.packetLoss.download : "N/A") +
  1308. "% <span class='jitsipopover_orange'>&uarr;</span>" +
  1309. (this.packetLoss.upload != null? this.packetLoss.upload : "N/A") + "%";
  1310. }
  1311. var resolutionValue = null;
  1312. if(this.resolution)
  1313. {
  1314. if(this.showHQ && this.resolution.hq)
  1315. {
  1316. resolutionValue = this.resolution.hq;
  1317. }
  1318. else if(!this.showHQ && this.resolution.lq)
  1319. {
  1320. resolutionValue = this.resolution.lq;
  1321. }
  1322. }
  1323. if(!resolutionValue ||
  1324. !resolutionValue.height ||
  1325. !resolutionValue.width)
  1326. {
  1327. resolution = "N/A";
  1328. }
  1329. else
  1330. {
  1331. resolution = resolutionValue.width + "x" + resolutionValue.height;
  1332. }
  1333. var result = "<table style='width:100%'><tr><td><span class='jitsipopover_blue'>Bitrate:</span></td><td><span class='jitsipopover_green'>&darr;</span>" +
  1334. downloadBitrate + " <span class='jitsipopover_orange'>&uarr;</span>" +
  1335. uploadBitrate + "</td></tr><tr><td>" +
  1336. "<tr><td><span class='jitsipopover_blue'>Packet loss: </span></td><td>" + packetLoss + "</td></tr><tr><td>" +
  1337. "<span class='jitsipopover_blue'>Resolution:</span></td><td>" + resolution + "</td></tr></table>";
  1338. if(this.videoContainer.id == "localVideoContainer")
  1339. result += "<div class=\"jitsipopover_showmore\" onclick = \"VideoLayout.connectionIndicators['" +
  1340. this.videoContainer.id + "'].showMore()\">" + (this.showMoreValue? "Show less" : "Show More") + "</div><br />";
  1341. if(this.showMoreValue)
  1342. {
  1343. var downloadBandwidth, uploadBandwidth, transport;
  1344. if(this.bandwidth === null)
  1345. {
  1346. downloadBandwidth = "N/A";
  1347. uploadBandwidth = "N/A";
  1348. }
  1349. else
  1350. {
  1351. downloadBandwidth = this.bandwidth.download? this.bandwidth.download + " Kbps" : "N/A";
  1352. uploadBandwidth = this.bandwidth.upload? this.bandwidth.upload + " Kbps" : "N/A";
  1353. }
  1354. if(!this.transport || this.transport.length === 0)
  1355. {
  1356. transport = "<tr><td><span class='jitsipopover_blue'>Address:</span></td><td> N/A</td></tr>";
  1357. }
  1358. else
  1359. {
  1360. var localTransport = "<tr><td><span class='jitsipopover_blue'>Local address: </span></td><td> " +
  1361. this.transport[0].localip.substring(0,this.transport[0].localip.indexOf(":")) + "</td></tr>"
  1362. transport = "<tr><td><span class='jitsipopover_blue'>Remote address:</span></td><td> " +
  1363. this.transport[0].ip.substring(0,this.transport[0].ip.indexOf(":")) + "</td></tr>";
  1364. if(this.transport.length > 1)
  1365. {
  1366. transport += "<tr><td><span class='jitsipopover_blue'>Remote ports:</span></td><td>";
  1367. localTransport += "<tr><td><span class='jitsipopover_blue'>Local ports:</span></td><td>";
  1368. }
  1369. else
  1370. {
  1371. transport += "<tr><td><span class='jitsipopover_blue'>Remote port:</span></td><td>";
  1372. localTransport += "<tr><td><span class='jitsipopover_blue'>Local port:</span></td><td>";
  1373. }
  1374. for(var i = 0; i < this.transport.length; i++)
  1375. {
  1376. transport += ((i !== 0)? ", " : "") +
  1377. this.transport[i].ip.substring(this.transport[i].ip.indexOf(":")+1,
  1378. this.transport[i].ip.length);
  1379. localTransport += ((i !== 0)? ", " : "") +
  1380. this.transport[i].localip.substring(this.transport[i].localip.indexOf(":")+1,
  1381. this.transport[i].localip.length);
  1382. }
  1383. transport += "</td></tr>";
  1384. transport += localTransport + "</td></tr>";
  1385. transport +="<tr><td><span class='jitsipopover_blue'>Transport:</span></td><td>" + this.transport[0].type + "</td></tr>";
  1386. }
  1387. result += "<table style='width:100%'><tr><td><span class='jitsipopover_blue'>Estimated bandwidth:</span> </td><td>" +
  1388. "<span class='jitsipopover_green'>&darr;</span>" + downloadBandwidth +
  1389. " <span class='jitsipopover_orange'>&uarr;</span>" +
  1390. uploadBandwidth + "</td></tr>";
  1391. result += transport + "</table>";
  1392. }
  1393. return result;
  1394. };
  1395. /**
  1396. * Shows or hide the additional information.
  1397. */
  1398. ConnectionIndicator.prototype.showMore = function () {
  1399. this.showMoreValue = !this.showMoreValue;
  1400. this.updatePopoverData();
  1401. };
  1402. /**
  1403. * Creates the indicator
  1404. */
  1405. ConnectionIndicator.prototype.create = function () {
  1406. this.connectionIndicatorContainer = document.createElement("div");
  1407. this.connectionIndicatorContainer.className = "connectionindicator";
  1408. this.connectionIndicatorContainer.style.display = "none";
  1409. this.videoContainer.appendChild(this.connectionIndicatorContainer);
  1410. this.popover = new JitsiPopover($("#" + this.videoContainer.id + " > .connectionindicator"),
  1411. {content: "<div class=\"connection_info\">Come back here for " +
  1412. "connection information once the conference starts</div>", skin: "black"});
  1413. function createIcon(classes)
  1414. {
  1415. var icon = document.createElement("span");
  1416. for(var i in classes)
  1417. {
  1418. icon.classList.add(classes[i]);
  1419. }
  1420. icon.appendChild(document.createElement("i")).classList.add("icon-connection");
  1421. return icon;
  1422. }
  1423. this.emptyIcon = this.connectionIndicatorContainer.appendChild(
  1424. createIcon(["connection", "connection_empty"]));
  1425. this.fullIcon = this.connectionIndicatorContainer.appendChild(
  1426. createIcon(["connection", "connection_full"]));
  1427. };
  1428. /**
  1429. * Removes the indicator
  1430. */
  1431. ConnectionIndicator.prototype.remove = function()
  1432. {
  1433. this.connectionIndicatorContainer.remove();
  1434. this.popover.forceHide();
  1435. };
  1436. /**
  1437. * Updates the data of the indicator
  1438. * @param percent the percent of connection quality
  1439. * @param object the statistics data.
  1440. */
  1441. ConnectionIndicator.prototype.updateConnectionQuality = function (percent, object) {
  1442. if(percent === null)
  1443. {
  1444. this.connectionIndicatorContainer.style.display = "none";
  1445. this.popover.forceHide();
  1446. return;
  1447. }
  1448. else
  1449. {
  1450. if(this.connectionIndicatorContainer.style.display == "none") {
  1451. this.connectionIndicatorContainer.style.display = "block";
  1452. VideoLayout.updateMutePosition(this.videoContainer.id);
  1453. }
  1454. }
  1455. this.bandwidth = object.bandwidth;
  1456. this.bitrate = object.bitrate;
  1457. this.packetLoss = object.packetLoss;
  1458. this.transport = object.transport;
  1459. if(object.resolution)
  1460. {
  1461. this.resolution = object.resolution;
  1462. }
  1463. for(var quality in ConnectionIndicator.connectionQualityValues)
  1464. {
  1465. if(percent >= quality)
  1466. {
  1467. this.fullIcon.style.width = ConnectionIndicator.connectionQualityValues[quality];
  1468. }
  1469. }
  1470. this.updatePopoverData();
  1471. };
  1472. /**
  1473. * Updates the resolution
  1474. * @param resolution the new resolution
  1475. */
  1476. ConnectionIndicator.prototype.updateResolution = function (resolution) {
  1477. this.resolution = resolution;
  1478. this.updatePopoverData();
  1479. };
  1480. /**
  1481. * Updates the content of the popover
  1482. */
  1483. ConnectionIndicator.prototype.updatePopoverData = function () {
  1484. this.popover.updateContent("<div class=\"connection_info\">" + this.generateText() + "</div>");
  1485. };
  1486. /**
  1487. * Hides the popover
  1488. */
  1489. ConnectionIndicator.prototype.hide = function () {
  1490. this.popover.forceHide();
  1491. };
  1492. /**
  1493. * Hides the indicator
  1494. */
  1495. ConnectionIndicator.prototype.hideIndicator = function () {
  1496. this.connectionIndicatorContainer.style.display = "none";
  1497. if(this.popover)
  1498. this.popover.forceHide();
  1499. };
  1500. /**
  1501. * Updates the data for the indicator
  1502. * @param id the id of the indicator
  1503. * @param percent the percent for connection quality
  1504. * @param object the data
  1505. */
  1506. function updateStatsIndicator(id, percent, object) {
  1507. if(VideoLayout.connectionIndicators[id])
  1508. VideoLayout.connectionIndicators[id].updateConnectionQuality(percent, object);
  1509. }
  1510. /**
  1511. * Updates local stats
  1512. * @param percent
  1513. * @param object
  1514. */
  1515. my.updateLocalConnectionStats = function (percent, object) {
  1516. var resolution = null;
  1517. if(object.resolution !== null)
  1518. {
  1519. resolution = object.resolution;
  1520. object.resolution = resolution[connection.emuc.myroomjid];
  1521. delete resolution[connection.emuc.myroomjid];
  1522. }
  1523. updateStatsIndicator("localVideoContainer", percent, object);
  1524. for(var jid in resolution)
  1525. {
  1526. if(resolution[jid] === null)
  1527. continue;
  1528. var id = 'participant_' + Strophe.getResourceFromJid(jid);
  1529. if(VideoLayout.connectionIndicators[id])
  1530. {
  1531. VideoLayout.connectionIndicators[id].updateResolution(resolution[jid]);
  1532. }
  1533. }
  1534. };
  1535. /**
  1536. * Updates remote stats.
  1537. * @param jid the jid associated with the stats
  1538. * @param percent the connection quality percent
  1539. * @param object the stats data
  1540. */
  1541. my.updateConnectionStats = function (jid, percent, object) {
  1542. var resourceJid = Strophe.getResourceFromJid(jid);
  1543. var videoSpanId = 'participant_' + resourceJid;
  1544. updateStatsIndicator(videoSpanId, percent, object);
  1545. };
  1546. /**
  1547. * Removes the connection
  1548. * @param jid
  1549. */
  1550. my.removeConnectionIndicator = function (jid) {
  1551. if(VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)])
  1552. VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)].remove();
  1553. };
  1554. /**
  1555. * Hides the connection indicator
  1556. * @param jid
  1557. */
  1558. my.hideConnectionIndicator = function (jid) {
  1559. if(VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)])
  1560. VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)].hide();
  1561. };
  1562. /**
  1563. * Hides all the indicators
  1564. */
  1565. my.onStatsStop = function () {
  1566. for(var indicator in VideoLayout.connectionIndicators)
  1567. {
  1568. VideoLayout.connectionIndicators[indicator].hideIndicator();
  1569. }
  1570. };
  1571. return my;
  1572. }(VideoLayout || {}));