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.

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