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

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