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

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