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

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