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

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