Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

videolayout.js 79KB

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