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

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