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