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

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