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

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