You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

videolayout.js 84KB

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