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.

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