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.

JitsiConference.js 138KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131
  1. import { getLogger } from '@jitsi/logger';
  2. import $ from 'jquery';
  3. import isEqual from 'lodash.isequal';
  4. import { Strophe } from 'strophe.js';
  5. import * as JitsiConferenceErrors from './JitsiConferenceErrors';
  6. import JitsiConferenceEventManager from './JitsiConferenceEventManager';
  7. import * as JitsiConferenceEvents from './JitsiConferenceEvents';
  8. import JitsiParticipant from './JitsiParticipant';
  9. import JitsiTrackError from './JitsiTrackError';
  10. import * as JitsiTrackErrors from './JitsiTrackErrors';
  11. import * as JitsiTrackEvents from './JitsiTrackEvents';
  12. import authenticateAndUpgradeRole from './authenticateAndUpgradeRole';
  13. import { CodecSelection } from './modules/RTC/CodecSelection';
  14. import RTC from './modules/RTC/RTC';
  15. import { SS_DEFAULT_FRAME_RATE } from './modules/RTC/ScreenObtainer';
  16. import browser from './modules/browser';
  17. import ConnectionQuality from './modules/connectivity/ConnectionQuality';
  18. import IceFailedHandling from './modules/connectivity/IceFailedHandling';
  19. import * as DetectionEvents from './modules/detection/DetectionEvents';
  20. import NoAudioSignalDetection from './modules/detection/NoAudioSignalDetection';
  21. import P2PDominantSpeakerDetection from './modules/detection/P2PDominantSpeakerDetection';
  22. import VADAudioAnalyser from './modules/detection/VADAudioAnalyser';
  23. import VADNoiseDetection from './modules/detection/VADNoiseDetection';
  24. import VADTalkMutedDetection from './modules/detection/VADTalkMutedDetection';
  25. import { E2EEncryption } from './modules/e2ee/E2EEncryption';
  26. import E2ePing from './modules/e2eping/e2eping';
  27. import Jvb121EventGenerator from './modules/event/Jvb121EventGenerator';
  28. import FeatureFlags from './modules/flags/FeatureFlags';
  29. import { LiteModeContext } from './modules/litemode/LiteModeContext';
  30. import ReceiveVideoController from './modules/qualitycontrol/ReceiveVideoController';
  31. import SendVideoController from './modules/qualitycontrol/SendVideoController';
  32. import RecordingManager from './modules/recording/RecordingManager';
  33. import Settings from './modules/settings/Settings';
  34. import AudioOutputProblemDetector from './modules/statistics/AudioOutputProblemDetector';
  35. import AvgRTPStatsReporter from './modules/statistics/AvgRTPStatsReporter';
  36. import LocalStatsCollector from './modules/statistics/LocalStatsCollector';
  37. import SpeakerStatsCollector from './modules/statistics/SpeakerStatsCollector';
  38. import Statistics from './modules/statistics/statistics';
  39. import EventEmitter from './modules/util/EventEmitter';
  40. import { safeSubtract } from './modules/util/MathUtil';
  41. import RandomUtil from './modules/util/RandomUtil';
  42. import ComponentsVersions from './modules/version/ComponentsVersions';
  43. import VideoSIPGW from './modules/videosipgw/VideoSIPGW';
  44. import * as VideoSIPGWConstants from './modules/videosipgw/VideoSIPGWConstants';
  45. import SignalingLayerImpl from './modules/xmpp/SignalingLayerImpl';
  46. import {
  47. FEATURE_E2EE,
  48. FEATURE_JIGASI,
  49. JITSI_MEET_MUC_TYPE
  50. } from './modules/xmpp/xmpp';
  51. import { BridgeVideoType } from './service/RTC/BridgeVideoType';
  52. import { CodecMimeType } from './service/RTC/CodecMimeType';
  53. import { MediaType } from './service/RTC/MediaType';
  54. import RTCEvents from './service/RTC/RTCEvents';
  55. import { SignalingEvents } from './service/RTC/SignalingEvents';
  56. import { getMediaTypeFromSourceName, getSourceNameForJitsiTrack } from './service/RTC/SignalingLayer';
  57. import { VideoType } from './service/RTC/VideoType';
  58. import {
  59. ACTION_JINGLE_RESTART,
  60. ACTION_JINGLE_SI_RECEIVED,
  61. ACTION_JINGLE_SI_TIMEOUT,
  62. ACTION_JINGLE_TERMINATE,
  63. ACTION_P2P_DECLINED,
  64. ACTION_P2P_ESTABLISHED,
  65. ACTION_P2P_FAILED,
  66. ACTION_P2P_SWITCH_TO_JVB,
  67. ICE_ESTABLISHMENT_DURATION_DIFF,
  68. createConferenceEvent,
  69. createJingleEvent,
  70. createP2PEvent
  71. } from './service/statistics/AnalyticsEvents';
  72. import { XMPPEvents } from './service/xmpp/XMPPEvents';
  73. const logger = getLogger(__filename);
  74. /**
  75. * How long since Jicofo is supposed to send a session-initiate, before
  76. * {@link ACTION_JINGLE_SI_TIMEOUT} analytics event is sent (in ms).
  77. * @type {number}
  78. */
  79. const JINGLE_SI_TIMEOUT = 5000;
  80. /**
  81. * Checks if a given string is a valid video codec mime type.
  82. *
  83. * @param {string} codec the codec string that needs to be validated.
  84. * @returns {CodecMimeType|null} mime type if valid, null otherwise.
  85. * @private
  86. */
  87. function _getCodecMimeType(codec) {
  88. if (typeof codec === 'string') {
  89. return Object.values(CodecMimeType).find(value => value === codec.toLowerCase());
  90. }
  91. return null;
  92. }
  93. /**
  94. * Creates a JitsiConference object with the given name and properties.
  95. * Note: this constructor is not a part of the public API (objects should be
  96. * created using JitsiConnection.createConference).
  97. * @param options.config properties / settings related to the conference that
  98. * will be created.
  99. * @param options.name the name of the conference
  100. * @param options.connection the JitsiConnection object for this
  101. * JitsiConference.
  102. * @param {number} [options.config.avgRtpStatsN=15] how many samples are to be
  103. * collected by {@link AvgRTPStatsReporter}, before arithmetic mean is
  104. * calculated and submitted to the analytics module.
  105. * @param {boolean} [options.config.enableIceRestart=false] - enables the ICE
  106. * restart logic.
  107. * @param {boolean} [options.config.p2p.enabled] when set to <tt>true</tt>
  108. * the peer to peer mode will be enabled. It means that when there are only 2
  109. * participants in the conference an attempt to make direct connection will be
  110. * made. If the connection succeeds the conference will stop sending data
  111. * through the JVB connection and will use the direct one instead.
  112. * @param {number} [options.config.p2p.backToP2PDelay=5] a delay given in
  113. * seconds, before the conference switches back to P2P, after the 3rd
  114. * participant has left the room.
  115. * @param {number} [options.config.channelLastN=-1] The requested amount of
  116. * videos are going to be delivered after the value is in effect. Set to -1 for
  117. * unlimited or all available videos.
  118. * @constructor
  119. *
  120. * FIXME Make all methods which are called from lib-internal classes
  121. * to non-public (use _). To name a few:
  122. * {@link JitsiConference.onLocalRoleChanged}
  123. * {@link JitsiConference.onUserRoleChanged}
  124. * {@link JitsiConference.onMemberLeft}
  125. * and so on...
  126. */
  127. export default function JitsiConference(options) {
  128. if (!options.name || options.name.toLowerCase() !== options.name.toString()) {
  129. const errmsg
  130. = 'Invalid conference name (no conference name passed or it '
  131. + 'contains invalid characters like capital letters)!';
  132. const additionalLogMsg = options.name
  133. ? `roomName=${options.name}; condition - ${options.name.toLowerCase()}!==${options.name.toString()}`
  134. : 'No room name passed!';
  135. logger.error(`${errmsg} ${additionalLogMsg}`);
  136. throw new Error(errmsg);
  137. }
  138. this.connection = options.connection;
  139. this.xmpp = this.connection?.xmpp;
  140. if (this.xmpp.isRoomCreated(options.name, options.customDomain)) {
  141. const errmsg = 'A conference with the same name has already been created!';
  142. delete this.connection;
  143. delete this.xmpp;
  144. logger.error(errmsg);
  145. throw new Error(errmsg);
  146. }
  147. this.eventEmitter = new EventEmitter();
  148. this.options = options;
  149. this.eventManager = new JitsiConferenceEventManager(this);
  150. /**
  151. * List of all the participants in the conference.
  152. * @type {Map<string, JitsiParticipant>};
  153. */
  154. this.participants = new Map();
  155. /**
  156. * The signaling layer instance.
  157. * @type {SignalingLayerImpl}
  158. * @private
  159. */
  160. this._signalingLayer = new SignalingLayerImpl();
  161. this._init(options);
  162. this.componentsVersions = new ComponentsVersions(this);
  163. /**
  164. * Jingle session instance for the JVB connection.
  165. * @type {JingleSessionPC}
  166. */
  167. this.jvbJingleSession = null;
  168. this.lastDominantSpeaker = null;
  169. this.dtmfManager = null;
  170. this.somebodySupportsDTMF = false;
  171. this.authEnabled = false;
  172. this.startAudioMuted = false;
  173. this.startVideoMuted = false;
  174. this.startMutedPolicy = {
  175. audio: false,
  176. video: false
  177. };
  178. this.isMutedByFocus = false;
  179. // when muted by focus we receive the jid of the initiator of the mute
  180. this.mutedByFocusActor = null;
  181. this.isVideoMutedByFocus = false;
  182. // when video muted by focus we receive the jid of the initiator of the mute
  183. this.mutedVideoByFocusActor = null;
  184. // Flag indicates if the 'onCallEnded' method was ever called on this
  185. // instance. Used to log extra analytics event for debugging purpose.
  186. // We need to know if the potential issue happened before or after
  187. // the restart.
  188. this.wasStopped = false;
  189. // Conference properties, maintained by jicofo.
  190. this.properties = {};
  191. /**
  192. * The object which monitors local and remote connection statistics (e.g.
  193. * sending bitrate) and calculates a number which represents the connection
  194. * quality.
  195. */
  196. this.connectionQuality
  197. = new ConnectionQuality(this, this.eventEmitter, options);
  198. /**
  199. * Reports average RTP statistics to the analytics module.
  200. * @type {AvgRTPStatsReporter}
  201. */
  202. this.avgRtpStatsReporter
  203. = new AvgRTPStatsReporter(this, options.config.avgRtpStatsN || 15);
  204. /**
  205. * Detects issues with the audio of remote participants.
  206. * @type {AudioOutputProblemDetector}
  207. */
  208. if (!options.config.disableAudioLevels) {
  209. this._audioOutputProblemDetector = new AudioOutputProblemDetector(this);
  210. }
  211. /**
  212. * Indicates whether the connection is interrupted or not.
  213. */
  214. this.isJvbConnectionInterrupted = false;
  215. /**
  216. * The object which tracks active speaker times
  217. */
  218. this.speakerStatsCollector = new SpeakerStatsCollector(this);
  219. /* P2P related fields below: */
  220. /**
  221. * Stores reference to deferred start P2P task. It's created when 3rd
  222. * participant leaves the room in order to avoid ping pong effect (it
  223. * could be just a page reload).
  224. * @type {number|null}
  225. */
  226. this.deferredStartP2PTask = null;
  227. const delay
  228. = parseInt(options.config.p2p && options.config.p2p.backToP2PDelay, 10);
  229. /**
  230. * A delay given in seconds, before the conference switches back to P2P
  231. * after the 3rd participant has left.
  232. * @type {number}
  233. */
  234. this.backToP2PDelay = isNaN(delay) ? 5 : delay;
  235. logger.info(`backToP2PDelay: ${this.backToP2PDelay}`);
  236. /**
  237. * If set to <tt>true</tt> it means the P2P ICE is no longer connected.
  238. * When <tt>false</tt> it means that P2P ICE (media) connection is up
  239. * and running.
  240. * @type {boolean}
  241. */
  242. this.isP2PConnectionInterrupted = false;
  243. /**
  244. * Flag set to <tt>true</tt> when P2P session has been established
  245. * (ICE has been connected) and this conference is currently in the peer to
  246. * peer mode (P2P connection is the active one).
  247. * @type {boolean}
  248. */
  249. this.p2p = false;
  250. /**
  251. * A JingleSession for the direct peer to peer connection.
  252. * @type {JingleSessionPC}
  253. */
  254. this.p2pJingleSession = null;
  255. this.videoSIPGWHandler = new VideoSIPGW(this.room);
  256. this.recordingManager = new RecordingManager(this.room);
  257. /**
  258. * If the conference.joined event has been sent this will store the timestamp when it happened.
  259. *
  260. * @type {undefined|number}
  261. * @private
  262. */
  263. this._conferenceJoinAnalyticsEventSent = undefined;
  264. /**
  265. * End-to-End Encryption. Make it available if supported.
  266. */
  267. if (this.isE2EESupported()) {
  268. logger.info('End-to-End Encryption is supported');
  269. this._e2eEncryption = new E2EEncryption(this);
  270. }
  271. if (FeatureFlags.isRunInLiteModeEnabled()) {
  272. logger.info('Lite mode enabled');
  273. this._liteModeContext = new LiteModeContext(this);
  274. }
  275. /**
  276. * Flag set to <tt>true</tt> when Jicofo sends a presence message indicating that the max audio sender limit has
  277. * been reached for the call. Once this is set, unmuting audio will be disabled from the client until it gets reset
  278. * again by Jicofo.
  279. */
  280. this._audioSenderLimitReached = undefined;
  281. /**
  282. * Flag set to <tt>true</tt> when Jicofo sends a presence message indicating that the max video sender limit has
  283. * been reached for the call. Once this is set, unmuting video will be disabled from the client until it gets reset
  284. * again by Jicofo.
  285. */
  286. this._videoSenderLimitReached = undefined;
  287. this._firefoxP2pEnabled = browser.isVersionGreaterThan(109)
  288. && (this.options.config.testing?.enableFirefoxP2p ?? true);
  289. }
  290. // FIXME convert JitsiConference to ES6 - ASAP !
  291. JitsiConference.prototype.constructor = JitsiConference;
  292. /**
  293. * Create a resource for the a jid. We use the room nickname (the resource part
  294. * of the occupant JID, see XEP-0045) as the endpoint ID in colibri. We require
  295. * endpoint IDs to be 8 hex digits because in some cases they get serialized
  296. * into a 32bit field.
  297. *
  298. * @param {string} jid - The id set onto the XMPP connection.
  299. * @param {boolean} isAuthenticatedUser - Whether or not the user has connected
  300. * to the XMPP service with a password.
  301. * @returns {string}
  302. * @static
  303. */
  304. JitsiConference.resourceCreator = function(jid, isAuthenticatedUser) {
  305. let mucNickname;
  306. if (isAuthenticatedUser) {
  307. // For authenticated users generate a random ID.
  308. mucNickname = RandomUtil.randomHexString(8).toLowerCase();
  309. } else {
  310. // We try to use the first part of the node (which for anonymous users
  311. // on prosody is a UUID) to match the previous behavior (and maybe make
  312. // debugging easier).
  313. mucNickname = Strophe.getNodeFromJid(jid)?.substr(0, 8)
  314. .toLowerCase();
  315. // But if this doesn't have the required format we just generate a new
  316. // random nickname.
  317. const re = /[0-9a-f]{8}/g;
  318. if (!mucNickname || !re.test(mucNickname)) {
  319. mucNickname = RandomUtil.randomHexString(8).toLowerCase();
  320. }
  321. }
  322. return mucNickname;
  323. };
  324. /**
  325. * Initializes the conference object properties
  326. * @param options {object}
  327. * @param options.connection {JitsiConnection} overrides this.connection
  328. */
  329. JitsiConference.prototype._init = function(options = {}) {
  330. this.eventManager.setupXMPPListeners();
  331. const { config } = this.options;
  332. // Get the codec preference settings from config.js.
  333. const codecSettings = {
  334. jvb: {
  335. preferenceOrder: browser.isMobileDevice() && config.videoQuality?.mobileCodecPreferenceOrder
  336. ? config.videoQuality.mobileCodecPreferenceOrder
  337. : config.videoQuality?.codecPreferenceOrder,
  338. disabledCodec: _getCodecMimeType(config.videoQuality?.disabledCodec),
  339. preferredCodec: _getCodecMimeType(config.videoQuality?.preferredCodec)
  340. },
  341. p2p: {
  342. preferenceOrder: browser.isMobileDevice() && config.p2p?.mobileCodecPreferenceOrder
  343. ? config.p2p.mobileCodecPreferenceOrder
  344. : config.p2p?.codecPreferenceOrder,
  345. disabledCodec: _getCodecMimeType(config.p2p?.disabledCodec),
  346. preferredCodec: _getCodecMimeType(config.p2p?.preferredCodec)
  347. }
  348. };
  349. this.codecSelection = new CodecSelection(this, codecSettings);
  350. this._statsCurrentId = config.statisticsId ? config.statisticsId : Settings.callStatsUserName;
  351. this.room = this.xmpp.createRoom(
  352. this.options.name, {
  353. ...config,
  354. statsId: this._statsCurrentId
  355. },
  356. JitsiConference.resourceCreator
  357. );
  358. this._signalingLayer.setChatRoom(this.room);
  359. this._signalingLayer.on(
  360. SignalingEvents.SOURCE_UPDATED,
  361. (sourceName, endpointId, muted, videoType) => {
  362. const participant = this.participants.get(endpointId);
  363. const mediaType = getMediaTypeFromSourceName(sourceName);
  364. if (participant) {
  365. participant._setSources(mediaType, muted, sourceName, videoType);
  366. this.eventEmitter.emit(JitsiConferenceEvents.PARTICIPANT_SOURCE_UPDATED, participant);
  367. }
  368. });
  369. // Connection interrupted/restored listeners
  370. this._onIceConnectionInterrupted
  371. = this._onIceConnectionInterrupted.bind(this);
  372. this.room.addListener(
  373. XMPPEvents.CONNECTION_INTERRUPTED, this._onIceConnectionInterrupted);
  374. this._onIceConnectionRestored = this._onIceConnectionRestored.bind(this);
  375. this.room.addListener(
  376. XMPPEvents.CONNECTION_RESTORED, this._onIceConnectionRestored);
  377. this._onIceConnectionEstablished
  378. = this._onIceConnectionEstablished.bind(this);
  379. this.room.addListener(
  380. XMPPEvents.CONNECTION_ESTABLISHED, this._onIceConnectionEstablished);
  381. this._updateProperties = this._updateProperties.bind(this);
  382. this.room.addListener(XMPPEvents.CONFERENCE_PROPERTIES_CHANGED,
  383. this._updateProperties);
  384. this._sendConferenceJoinAnalyticsEvent = this._sendConferenceJoinAnalyticsEvent.bind(this);
  385. this.room.addListener(XMPPEvents.MEETING_ID_SET, this._sendConferenceJoinAnalyticsEvent);
  386. this._removeLocalSourceOnReject = this._removeLocalSourceOnReject.bind(this);
  387. this._updateRoomPresence = this._updateRoomPresence.bind(this);
  388. this.room.addListener(XMPPEvents.SESSION_ACCEPT, this._updateRoomPresence);
  389. this.room.addListener(XMPPEvents.SOURCE_ADD, this._updateRoomPresence);
  390. this.room.addListener(XMPPEvents.SOURCE_ADD_ERROR, this._removeLocalSourceOnReject);
  391. this.room.addListener(XMPPEvents.SOURCE_REMOVE, this._updateRoomPresence);
  392. if (config.e2eping?.enabled) {
  393. this.e2eping = new E2ePing(
  394. this,
  395. config,
  396. (message, to) => {
  397. try {
  398. this.sendMessage(message, to, true /* sendThroughVideobridge */);
  399. } catch (error) {
  400. logger.warn('Failed to send E2E ping request or response.', error && error.msg);
  401. }
  402. });
  403. }
  404. if (!this.rtc) {
  405. this.rtc = new RTC(this, options);
  406. this.eventManager.setupRTCListeners();
  407. this._registerRtcListeners(this.rtc);
  408. }
  409. this.receiveVideoController = new ReceiveVideoController(this, this.rtc);
  410. this.sendVideoController = new SendVideoController(this, this.rtc);
  411. if (!this.statistics) {
  412. this.statistics = new Statistics(this, {
  413. aliasName: this._statsCurrentId,
  414. userName: config.statisticsDisplayName ? config.statisticsDisplayName : this.myUserId(),
  415. confID: config.confID || `${this.connection.options.hosts.domain}/${this.options.name}`,
  416. roomName: this.options.name,
  417. applicationName: config.applicationName
  418. });
  419. Statistics.analytics.addPermanentProperties({
  420. 'callstats_name': this._statsCurrentId
  421. });
  422. // Start performance observer for monitoring long tasks
  423. if (config.longTasksStatsInterval) {
  424. this.statistics.attachLongTasksStats();
  425. }
  426. }
  427. this.eventManager.setupChatRoomListeners();
  428. // Always add listeners because on reload we are executing leave and the
  429. // listeners are removed from statistics module.
  430. this.eventManager.setupStatisticsListeners();
  431. // Disable VAD processing on Safari since it causes audio input to
  432. // fail on some of the mobile devices.
  433. if (config.enableTalkWhileMuted && browser.supportsVADDetection()) {
  434. // If VAD processor factory method is provided uses VAD based detection, otherwise fallback to audio level
  435. // based detection.
  436. if (config.createVADProcessor) {
  437. logger.info('Using VAD detection for generating talk while muted events');
  438. if (!this._audioAnalyser) {
  439. this._audioAnalyser = new VADAudioAnalyser(this, config.createVADProcessor);
  440. }
  441. const vadTalkMutedDetection = new VADTalkMutedDetection();
  442. vadTalkMutedDetection.on(DetectionEvents.VAD_TALK_WHILE_MUTED, () =>
  443. this.eventEmitter.emit(JitsiConferenceEvents.TALK_WHILE_MUTED));
  444. this._audioAnalyser.addVADDetectionService(vadTalkMutedDetection);
  445. } else {
  446. logger.warn('No VAD Processor was provided. Talk while muted detection service was not initialized!');
  447. }
  448. }
  449. // Disable noisy mic detection on safari since it causes the audio input to
  450. // fail on Safari on iPadOS.
  451. if (config.enableNoisyMicDetection && browser.supportsVADDetection()) {
  452. if (config.createVADProcessor) {
  453. if (!this._audioAnalyser) {
  454. this._audioAnalyser = new VADAudioAnalyser(this, config.createVADProcessor);
  455. }
  456. const vadNoiseDetection = new VADNoiseDetection();
  457. vadNoiseDetection.on(DetectionEvents.VAD_NOISY_DEVICE, () =>
  458. this.eventEmitter.emit(JitsiConferenceEvents.NOISY_MIC));
  459. this._audioAnalyser.addVADDetectionService(vadNoiseDetection);
  460. } else {
  461. logger.warn('No VAD Processor was provided. Noisy microphone detection service was not initialized!');
  462. }
  463. }
  464. // Generates events based on no audio input detector.
  465. if (config.enableNoAudioDetection && !config.disableAudioLevels && LocalStatsCollector.isLocalStatsSupported()) {
  466. this._noAudioSignalDetection = new NoAudioSignalDetection(this);
  467. this._noAudioSignalDetection.on(DetectionEvents.NO_AUDIO_INPUT, () => {
  468. this.eventEmitter.emit(JitsiConferenceEvents.NO_AUDIO_INPUT);
  469. });
  470. this._noAudioSignalDetection.on(DetectionEvents.AUDIO_INPUT_STATE_CHANGE, hasAudioSignal => {
  471. this.eventEmitter.emit(JitsiConferenceEvents.AUDIO_INPUT_STATE_CHANGE, hasAudioSignal);
  472. });
  473. }
  474. if ('channelLastN' in config) {
  475. this.setLastN(config.channelLastN);
  476. }
  477. /**
  478. * Emits {@link JitsiConferenceEvents.JVB121_STATUS}.
  479. * @type {Jvb121EventGenerator}
  480. */
  481. this.jvb121Status = new Jvb121EventGenerator(this);
  482. // creates dominant speaker detection that works only in p2p mode
  483. this.p2pDominantSpeakerDetection = new P2PDominantSpeakerDetection(this);
  484. if (config && config.deploymentInfo && config.deploymentInfo.userRegion) {
  485. this.setLocalParticipantProperty(
  486. 'region', config.deploymentInfo.userRegion);
  487. }
  488. // Publish the codec preference to presence.
  489. this.setLocalParticipantProperty('codecList', this.codecSelection.getCodecPreferenceList('jvb'));
  490. // Set transcription language presence extension.
  491. // In case the language config is undefined or has the default value that the transcriber uses
  492. // (in our case Jigasi uses 'en-US'), don't set the participant property in order to avoid
  493. // needlessly polluting the presence stanza.
  494. if (config && config.transcriptionLanguage && config.transcriptionLanguage !== 'en-US') {
  495. this.setLocalParticipantProperty('transcription_language', config.transcriptionLanguage);
  496. }
  497. };
  498. /**
  499. * Joins the conference.
  500. * @param password {string} the password
  501. * @param replaceParticipant {boolean} whether the current join replaces
  502. * an existing participant with same jwt from the meeting.
  503. */
  504. JitsiConference.prototype.join = function(password, replaceParticipant = false) {
  505. if (this.room) {
  506. this.room.join(password, replaceParticipant).then(() => this._maybeSetSITimeout());
  507. }
  508. };
  509. /**
  510. * Authenticates and upgrades the role of the local participant/user.
  511. *
  512. * @returns {Object} A <tt>thenable</tt> which (1) settles when the process of
  513. * authenticating and upgrading the role of the local participant/user finishes
  514. * and (2) has a <tt>cancel</tt> method that allows the caller to interrupt the
  515. * process.
  516. */
  517. JitsiConference.prototype.authenticateAndUpgradeRole = function(options) {
  518. return authenticateAndUpgradeRole.call(this, {
  519. ...options,
  520. onCreateResource: JitsiConference.resourceCreator
  521. });
  522. };
  523. /**
  524. * Check if joined to the conference.
  525. */
  526. JitsiConference.prototype.isJoined = function() {
  527. return this.room && this.room.joined;
  528. };
  529. /**
  530. * Tells whether or not the P2P mode is enabled in the configuration.
  531. * @return {boolean}
  532. */
  533. JitsiConference.prototype.isP2PEnabled = function() {
  534. return Boolean(this.options.config.p2p && this.options.config.p2p.enabled)
  535. // FIXME: remove once we have a default config template. -saghul
  536. || typeof this.options.config.p2p === 'undefined';
  537. };
  538. /**
  539. * When in P2P test mode, the conference will not automatically switch to P2P
  540. * when there 2 participants.
  541. * @return {boolean}
  542. */
  543. JitsiConference.prototype.isP2PTestModeEnabled = function() {
  544. return Boolean(this.options.config.testing
  545. && this.options.config.testing.p2pTestMode);
  546. };
  547. /**
  548. * Leaves the conference.
  549. * @param reason {string|undefined} The reason for leaving the conference.
  550. * @returns {Promise}
  551. */
  552. JitsiConference.prototype.leave = async function(reason) {
  553. if (this.avgRtpStatsReporter) {
  554. this.avgRtpStatsReporter.dispose();
  555. this.avgRtpStatsReporter = null;
  556. }
  557. if (this._audioOutputProblemDetector) {
  558. this._audioOutputProblemDetector.dispose();
  559. this._audioOutputProblemDetector = null;
  560. }
  561. if (this.e2eping) {
  562. this.e2eping.stop();
  563. this.e2eping = null;
  564. }
  565. this.getLocalTracks().forEach(track => this.onLocalTrackRemoved(track));
  566. this.rtc.closeBridgeChannel();
  567. this._sendConferenceLeftAnalyticsEvent();
  568. if (this.statistics) {
  569. this.statistics.dispose();
  570. }
  571. this._delayedIceFailed && this._delayedIceFailed.cancel();
  572. this._maybeClearSITimeout();
  573. // Close both JVb and P2P JingleSessions
  574. if (this.jvbJingleSession) {
  575. this.jvbJingleSession.close();
  576. this.jvbJingleSession = null;
  577. }
  578. if (this.p2pJingleSession) {
  579. this.p2pJingleSession.close();
  580. this.p2pJingleSession = null;
  581. }
  582. // Leave the conference. If this.room == null we are calling second time leave().
  583. if (!this.room) {
  584. throw new Error('You have already left the conference');
  585. }
  586. const room = this.room;
  587. // Unregister connection state listeners
  588. room.removeListener(
  589. XMPPEvents.CONNECTION_INTERRUPTED,
  590. this._onIceConnectionInterrupted);
  591. room.removeListener(
  592. XMPPEvents.CONNECTION_RESTORED,
  593. this._onIceConnectionRestored);
  594. room.removeListener(
  595. XMPPEvents.CONNECTION_ESTABLISHED,
  596. this._onIceConnectionEstablished);
  597. room.removeListener(
  598. XMPPEvents.CONFERENCE_PROPERTIES_CHANGED,
  599. this._updateProperties);
  600. room.removeListener(XMPPEvents.MEETING_ID_SET, this._sendConferenceJoinAnalyticsEvent);
  601. room.removeListener(XMPPEvents.SESSION_ACCEPT, this._updateRoomPresence);
  602. room.removeListener(XMPPEvents.SOURCE_ADD, this._updateRoomPresence);
  603. room.removeListener(XMPPEvents.SOURCE_ADD_ERROR, this._removeLocalSourceOnReject);
  604. room.removeListener(XMPPEvents.SOURCE_REMOVE, this._updateRoomPresence);
  605. this.eventManager.removeXMPPListeners();
  606. this._signalingLayer.setChatRoom(null);
  607. this.room = null;
  608. let leaveError;
  609. try {
  610. await room.leave(reason);
  611. } catch (err) {
  612. leaveError = err;
  613. // Remove all participants because currently the conference
  614. // won't be usable anyway. This is done on success automatically
  615. // by the ChatRoom instance.
  616. this.getParticipants().forEach(
  617. participant => this.onMemberLeft(participant.getJid()));
  618. }
  619. if (this.rtc) {
  620. this.rtc.destroy();
  621. }
  622. if (leaveError) {
  623. throw leaveError;
  624. }
  625. };
  626. /**
  627. * Returns <tt>true</tt> if end conference support is enabled in the backend.
  628. *
  629. * @returns {boolean} whether end conference is supported in the backend.
  630. */
  631. JitsiConference.prototype.isEndConferenceSupported = function() {
  632. return Boolean(this.room && this.room.xmpp.endConferenceComponentAddress);
  633. };
  634. /**
  635. * Ends the conference.
  636. */
  637. JitsiConference.prototype.end = function() {
  638. if (!this.isEndConferenceSupported()) {
  639. logger.warn('Cannot end conference: is not supported.');
  640. return;
  641. }
  642. if (!this.room) {
  643. throw new Error('You have already left the conference');
  644. }
  645. this.room.end();
  646. };
  647. /**
  648. * Returns the currently active media session if any.
  649. *
  650. * @returns {JingleSessionPC|undefined}
  651. */
  652. JitsiConference.prototype.getActiveMediaSession = function() {
  653. return this.isP2PActive() ? this.p2pJingleSession : this.jvbJingleSession;
  654. };
  655. /**
  656. * Returns an array containing all media sessions existing in this conference.
  657. *
  658. * @returns {Array<JingleSessionPC>}
  659. */
  660. JitsiConference.prototype.getMediaSessions = function() {
  661. const sessions = [];
  662. this.jvbJingleSession && sessions.push(this.jvbJingleSession);
  663. this.p2pJingleSession && sessions.push(this.p2pJingleSession);
  664. return sessions;
  665. };
  666. /**
  667. * Registers event listeners on the RTC instance.
  668. * @param {RTC} rtc - the RTC module instance used by this conference.
  669. * @private
  670. * @returns {void}
  671. */
  672. JitsiConference.prototype._registerRtcListeners = function(rtc) {
  673. rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, () => {
  674. for (const localTrack of this.rtc.localTracks) {
  675. localTrack.isVideoTrack() && this._sendBridgeVideoTypeMessage(localTrack);
  676. }
  677. });
  678. };
  679. /**
  680. * Sends the 'VideoTypeMessage' to the bridge on the bridge channel so that the bridge can make bitrate allocation
  681. * decisions based on the video type of the local source.
  682. *
  683. * @param {JitsiLocalTrack} localtrack - The track associated with the local source signaled to the bridge.
  684. * @returns {void}
  685. * @private
  686. */
  687. JitsiConference.prototype._sendBridgeVideoTypeMessage = function(localtrack) {
  688. let videoType = !localtrack || localtrack.isMuted() ? BridgeVideoType.NONE : localtrack.getVideoType();
  689. if (videoType === BridgeVideoType.DESKTOP && this._desktopSharingFrameRate > SS_DEFAULT_FRAME_RATE) {
  690. videoType = BridgeVideoType.DESKTOP_HIGH_FPS;
  691. }
  692. localtrack && this.rtc.sendSourceVideoType(localtrack.getSourceName(), videoType);
  693. };
  694. /**
  695. * Returns name of this conference.
  696. */
  697. JitsiConference.prototype.getName = function() {
  698. return this.options.name.toString();
  699. };
  700. /**
  701. * Returns the {@link JitsiConnection} used by this this conference.
  702. */
  703. JitsiConference.prototype.getConnection = function() {
  704. return this.connection;
  705. };
  706. /**
  707. * Check if authentication is enabled for this conference.
  708. */
  709. JitsiConference.prototype.isAuthEnabled = function() {
  710. return this.authEnabled;
  711. };
  712. /**
  713. * Check if user is logged in.
  714. */
  715. JitsiConference.prototype.isLoggedIn = function() {
  716. return Boolean(this.authIdentity);
  717. };
  718. /**
  719. * Get authorized login.
  720. */
  721. JitsiConference.prototype.getAuthLogin = function() {
  722. return this.authIdentity;
  723. };
  724. /**
  725. * Returns the local tracks of the given media type, or all local tracks if no
  726. * specific type is given.
  727. * @param {MediaType} [mediaType] Optional media type (audio or video).
  728. */
  729. JitsiConference.prototype.getLocalTracks = function(mediaType) {
  730. let tracks = [];
  731. if (this.rtc) {
  732. tracks = this.rtc.getLocalTracks(mediaType);
  733. }
  734. return tracks;
  735. };
  736. /**
  737. * Obtains local audio track.
  738. * @return {JitsiLocalTrack|null}
  739. */
  740. JitsiConference.prototype.getLocalAudioTrack = function() {
  741. return this.rtc ? this.rtc.getLocalAudioTrack() : null;
  742. };
  743. /**
  744. * Obtains local video track.
  745. * @return {JitsiLocalTrack|null}
  746. */
  747. JitsiConference.prototype.getLocalVideoTrack = function() {
  748. return this.rtc ? this.rtc.getLocalVideoTrack() : null;
  749. };
  750. /**
  751. * Returns all the local video tracks.
  752. * @returns {Array<JitsiLocalTrack>}
  753. */
  754. JitsiConference.prototype.getLocalVideoTracks = function() {
  755. return this.rtc ? this.rtc.getLocalVideoTracks() : null;
  756. };
  757. /**
  758. * Obtains the performance statistics.
  759. * @returns {Object|null}
  760. */
  761. JitsiConference.prototype.getPerformanceStats = function() {
  762. return {
  763. longTasksStats: this.statistics.getLongTasksStats()
  764. };
  765. };
  766. /**
  767. * Attaches a handler for events(For example - "participant joined".) in the
  768. * conference. All possible event are defined in JitsiConferenceEvents.
  769. * @param eventId the event ID.
  770. * @param handler handler for the event.
  771. *
  772. * Note: consider adding eventing functionality by extending an EventEmitter
  773. * impl, instead of rolling ourselves
  774. */
  775. JitsiConference.prototype.on = function(eventId, handler) {
  776. if (this.eventEmitter) {
  777. this.eventEmitter.on(eventId, handler);
  778. }
  779. };
  780. /**
  781. * Adds a one-time`listener` function for the event.
  782. * @param eventId the event ID.
  783. * @param handler handler for the event.
  784. *
  785. */
  786. JitsiConference.prototype.once = function(eventId, handler) {
  787. if (this.eventEmitter) {
  788. this.eventEmitter.once(eventId, handler);
  789. }
  790. };
  791. /**
  792. * Removes event listener
  793. * @param eventId the event ID.
  794. * @param [handler] optional, the specific handler to unbind
  795. *
  796. * Note: consider adding eventing functionality by extending an EventEmitter
  797. * impl, instead of rolling ourselves
  798. */
  799. JitsiConference.prototype.off = function(eventId, handler) {
  800. if (this.eventEmitter) {
  801. this.eventEmitter.removeListener(eventId, handler);
  802. }
  803. };
  804. // Common aliases for event emitter
  805. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on;
  806. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off;
  807. /**
  808. * Receives notifications from other participants about commands / custom events
  809. * (sent by sendCommand or sendCommandOnce methods).
  810. * @param command {String} the name of the command
  811. * @param handler {Function} handler for the command
  812. */
  813. JitsiConference.prototype.addCommandListener = function(command, handler) {
  814. if (this.room) {
  815. this.room.addPresenceListener(command, handler);
  816. }
  817. };
  818. /**
  819. * Removes command listener
  820. * @param command {String} the name of the command
  821. * @param handler {Function} handler to remove for the command
  822. */
  823. JitsiConference.prototype.removeCommandListener = function(command, handler) {
  824. if (this.room) {
  825. this.room.removePresenceListener(command, handler);
  826. }
  827. };
  828. /**
  829. * Sends text message to the other participants in the conference
  830. * @param message the text message.
  831. * @param elementName the element name to encapsulate the message.
  832. * @deprecated Use 'sendMessage' instead. TODO: this should be private.
  833. */
  834. JitsiConference.prototype.sendTextMessage = function(message, elementName = 'body') {
  835. if (this.room) {
  836. this.room.sendMessage(message, elementName);
  837. }
  838. };
  839. /**
  840. * Send private text message to another participant of the conference
  841. * @param id the id of the participant to send a private message.
  842. * @param message the text message.
  843. * @param elementName the element name to encapsulate the message.
  844. * @deprecated Use 'sendMessage' instead. TODO: this should be private.
  845. */
  846. JitsiConference.prototype.sendPrivateTextMessage = function(id, message, elementName = 'body') {
  847. if (this.room) {
  848. this.room.sendPrivateMessage(id, message, elementName);
  849. }
  850. };
  851. /**
  852. * Send presence command.
  853. * @param name {String} the name of the command.
  854. * @param values {Object} with keys and values that will be sent.
  855. **/
  856. JitsiConference.prototype.sendCommand = function(name, values) {
  857. if (this.room) {
  858. this.room.addOrReplaceInPresence(name, values) && this.room.sendPresence();
  859. } else {
  860. logger.warn('Not sending a command, room not initialized.');
  861. }
  862. };
  863. /**
  864. * Send presence command one time.
  865. * @param name {String} the name of the command.
  866. * @param values {Object} with keys and values that will be sent.
  867. **/
  868. JitsiConference.prototype.sendCommandOnce = function(name, values) {
  869. this.sendCommand(name, values);
  870. this.removeCommand(name);
  871. };
  872. /**
  873. * Removes presence command.
  874. * @param name {String} the name of the command.
  875. **/
  876. JitsiConference.prototype.removeCommand = function(name) {
  877. if (this.room) {
  878. this.room.removeFromPresence(name);
  879. }
  880. };
  881. /**
  882. * Sets the display name for this conference.
  883. * @param name the display name to set
  884. */
  885. JitsiConference.prototype.setDisplayName = function(name) {
  886. if (this.room) {
  887. const nickKey = 'nick';
  888. if (name) {
  889. this.room.addOrReplaceInPresence(nickKey, {
  890. attributes: { xmlns: 'http://jabber.org/protocol/nick' },
  891. value: name
  892. }) && this.room.sendPresence();
  893. } else if (this.room.getFromPresence(nickKey)) {
  894. this.room.removeFromPresence(nickKey);
  895. this.room.sendPresence();
  896. }
  897. }
  898. };
  899. /**
  900. * Set new subject for this conference. (available only for moderator)
  901. * @param {string} subject new subject
  902. */
  903. JitsiConference.prototype.setSubject = function(subject) {
  904. if (this.room && this.isModerator()) {
  905. this.room.setSubject(subject);
  906. } else {
  907. logger.warn(`Failed to set subject, ${this.room ? '' : 'not in a room, '}${
  908. this.isModerator() ? '' : 'participant is not a moderator'}`);
  909. }
  910. };
  911. /**
  912. * Returns the transcription status.
  913. *
  914. * @returns {String} "on" or "off".
  915. */
  916. JitsiConference.prototype.getTranscriptionStatus = function() {
  917. return this.room.transcriptionStatus;
  918. };
  919. /**
  920. * Adds JitsiLocalTrack object to the conference.
  921. * @param {JitsiLocalTrack} track the JitsiLocalTrack object.
  922. * @returns {Promise<JitsiLocalTrack>}
  923. * @throws {Error} if the specified track is a video track and there is already
  924. * another video track in the conference.
  925. */
  926. JitsiConference.prototype.addTrack = function(track) {
  927. if (!track) {
  928. throw new Error('addTrack - a track is required');
  929. }
  930. const mediaType = track.getType();
  931. const localTracks = this.rtc.getLocalTracks(mediaType);
  932. // Ensure there's exactly 1 local track of each media type in the conference.
  933. if (localTracks.length > 0) {
  934. // Don't be excessively harsh and severe if the API client happens to attempt to add the same local track twice.
  935. if (track === localTracks[0]) {
  936. return Promise.resolve(track);
  937. }
  938. // Currently, only adding multiple video streams of different video types is supported.
  939. // TODO - remove this limitation once issues with jitsi-meet trying to add multiple camera streams is fixed.
  940. if (mediaType === MediaType.VIDEO
  941. && !localTracks.find(t => t.getVideoType() === track.getVideoType())) {
  942. const sourceName = getSourceNameForJitsiTrack(
  943. this.myUserId(),
  944. mediaType,
  945. this.getLocalTracks(mediaType)?.length);
  946. track.setSourceName(sourceName);
  947. const addTrackPromises = [];
  948. this.p2pJingleSession && addTrackPromises.push(this.p2pJingleSession.addTracks([ track ]));
  949. this.jvbJingleSession && addTrackPromises.push(this.jvbJingleSession.addTracks([ track ]));
  950. return Promise.all(addTrackPromises)
  951. .then(() => {
  952. this._setupNewTrack(track);
  953. this._sendBridgeVideoTypeMessage(track);
  954. this._updateRoomPresence(this.getActiveMediaSession());
  955. if (this.isMutedByFocus || this.isVideoMutedByFocus) {
  956. this._fireMuteChangeEvent(track);
  957. }
  958. });
  959. }
  960. return Promise.reject(new Error(`Cannot add second ${mediaType} track to the conference`));
  961. }
  962. return this.replaceTrack(null, track)
  963. .then(() => {
  964. // Presence needs to be sent here for desktop track since we need the presence to reach the remote peer
  965. // before signaling so that a fake participant tile is created for screenshare. Otherwise, presence will
  966. // only be sent after a session-accept or source-add is ack'ed.
  967. if (track.getVideoType() === VideoType.DESKTOP) {
  968. this._updateRoomPresence(this.getActiveMediaSession());
  969. }
  970. });
  971. };
  972. /**
  973. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event (for local tracks).
  974. * @param {number} audioLevel the audio level
  975. * @param {TraceablePeerConnection} [tpc]
  976. */
  977. JitsiConference.prototype._fireAudioLevelChangeEvent = function(audioLevel, tpc) {
  978. const activeTpc = this.getActivePeerConnection();
  979. // There will be no TraceablePeerConnection if audio levels do not come from
  980. // a peerconnection. LocalStatsCollector.js measures audio levels using Web
  981. // Audio Analyser API and emits local audio levels events through
  982. // JitsiTrack.setAudioLevel, but does not provide TPC instance which is
  983. // optional.
  984. if (!tpc || activeTpc === tpc) {
  985. this.eventEmitter.emit(
  986. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  987. this.myUserId(), audioLevel);
  988. }
  989. };
  990. /**
  991. * Fires TRACK_MUTE_CHANGED change conference event.
  992. * @param track the JitsiTrack object related to the event.
  993. */
  994. JitsiConference.prototype._fireMuteChangeEvent = function(track) {
  995. // check if track was muted by focus and now is unmuted by user
  996. if (this.isMutedByFocus && track.isAudioTrack() && !track.isMuted()) {
  997. this.isMutedByFocus = false;
  998. // unmute local user on server
  999. this.room.muteParticipant(this.room.myroomjid, false, MediaType.AUDIO);
  1000. } else if (this.isVideoMutedByFocus && track.isVideoTrack() && !track.isMuted()) {
  1001. this.isVideoMutedByFocus = false;
  1002. // unmute local user on server
  1003. this.room.muteParticipant(this.room.myroomjid, false, MediaType.VIDEO);
  1004. }
  1005. let actorParticipant;
  1006. if (this.mutedByFocusActor && track.isAudioTrack()) {
  1007. const actorId = Strophe.getResourceFromJid(this.mutedByFocusActor);
  1008. actorParticipant = this.participants.get(actorId);
  1009. } else if (this.mutedVideoByFocusActor && track.isVideoTrack()) {
  1010. const actorId = Strophe.getResourceFromJid(this.mutedVideoByFocusActor);
  1011. actorParticipant = this.participants.get(actorId);
  1012. }
  1013. // Send the video type message to the bridge if the track is not removed/added to the pc as part of
  1014. // the mute/unmute operation.
  1015. // In React Native we mute the camera by setting track.enabled but that doesn't
  1016. // work for screen-share tracks, so do the remove-as-mute for those.
  1017. const doesVideoMuteByStreamRemove
  1018. = browser.isReactNative() ? track.videoType === VideoType.DESKTOP : browser.doesVideoMuteByStreamRemove();
  1019. if (track.isVideoTrack() && !doesVideoMuteByStreamRemove) {
  1020. this._sendBridgeVideoTypeMessage(track);
  1021. }
  1022. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track, actorParticipant);
  1023. };
  1024. /**
  1025. * Returns the list of local tracks that need to be added to the peerconnection on join.
  1026. * This takes the startAudioMuted/startVideoMuted flags into consideration since we do not
  1027. * want to add the tracks if the user joins the call audio/video muted. The tracks will be
  1028. * added when the user unmutes for the first time.
  1029. * @returns {Array<JitsiLocalTrack>} - list of local tracks that are unmuted.
  1030. */
  1031. JitsiConference.prototype._getInitialLocalTracks = function() {
  1032. // Always add the audio track on certain platforms:
  1033. // * Safari / WebKit: because of a known issue where audio playout doesn't happen
  1034. // if the user joins audio and video muted.
  1035. // * React Native: after iOS 15, if a user joins muted they won't be able to unmute.
  1036. return this.getLocalTracks()
  1037. .filter(track => {
  1038. const trackType = track.getType();
  1039. if (trackType === MediaType.AUDIO
  1040. && (!(this.isStartAudioMuted() || this.startMutedPolicy.audio)
  1041. || browser.isWebKitBased()
  1042. || browser.isReactNative())) {
  1043. return true;
  1044. } else if (trackType === MediaType.VIDEO && !this.isStartVideoMuted() && !this.startMutedPolicy.video) {
  1045. return true;
  1046. }
  1047. // Remove the track from the conference.
  1048. this.onLocalTrackRemoved(track);
  1049. return false;
  1050. });
  1051. };
  1052. /**
  1053. * Clear JitsiLocalTrack properties and listeners.
  1054. * @param track the JitsiLocalTrack object.
  1055. */
  1056. JitsiConference.prototype.onLocalTrackRemoved = function(track) {
  1057. track.setConference(null);
  1058. this.rtc.removeLocalTrack(track);
  1059. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, track.muteHandler);
  1060. if (track.isAudioTrack()) {
  1061. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, track.audioLevelHandler);
  1062. }
  1063. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  1064. };
  1065. /**
  1066. * Removes JitsiLocalTrack from the conference and performs
  1067. * a new offer/answer cycle.
  1068. * @param {JitsiLocalTrack} track
  1069. * @returns {Promise}
  1070. */
  1071. JitsiConference.prototype.removeTrack = function(track) {
  1072. return this.replaceTrack(track, null);
  1073. };
  1074. /**
  1075. * Replaces oldTrack with newTrack and performs a single offer/answer
  1076. * cycle after both operations are done. Either oldTrack or newTrack
  1077. * can be null; replacing a valid 'oldTrack' with a null 'newTrack'
  1078. * effectively just removes 'oldTrack'
  1079. * @param {JitsiLocalTrack} oldTrack the current stream in use to be replaced
  1080. * @param {JitsiLocalTrack} newTrack the new stream to use
  1081. * @returns {Promise} resolves when the replacement is finished
  1082. */
  1083. JitsiConference.prototype.replaceTrack = function(oldTrack, newTrack) {
  1084. const oldVideoType = oldTrack?.getVideoType();
  1085. const mediaType = oldTrack?.getType() || newTrack?.getType();
  1086. const newVideoType = newTrack?.getVideoType();
  1087. if (oldTrack && newTrack && oldVideoType !== newVideoType) {
  1088. throw new Error(`Replacing a track of videoType=${oldVideoType} with a track of videoType=${newVideoType} is`
  1089. + ' not supported in this mode.');
  1090. }
  1091. if (newTrack) {
  1092. const sourceName = oldTrack
  1093. ? oldTrack.getSourceName()
  1094. : getSourceNameForJitsiTrack(
  1095. this.myUserId(),
  1096. mediaType,
  1097. this.getLocalTracks(mediaType)?.length);
  1098. newTrack.setSourceName(sourceName);
  1099. }
  1100. const oldTrackBelongsToConference = this === oldTrack?.conference;
  1101. if (oldTrackBelongsToConference && oldTrack.disposed) {
  1102. return Promise.reject(new JitsiTrackError(JitsiTrackErrors.TRACK_IS_DISPOSED));
  1103. }
  1104. if (newTrack?.disposed) {
  1105. return Promise.reject(new JitsiTrackError(JitsiTrackErrors.TRACK_IS_DISPOSED));
  1106. }
  1107. if (oldTrack && !oldTrackBelongsToConference) {
  1108. logger.warn(`JitsiConference.replaceTrack oldTrack (${oldTrack} does not belong to this conference`);
  1109. }
  1110. // Now replace the stream at the lower levels
  1111. return this._doReplaceTrack(oldTrackBelongsToConference ? oldTrack : null, newTrack)
  1112. .then(() => {
  1113. if (oldTrackBelongsToConference && !oldTrack.isMuted() && !newTrack) {
  1114. oldTrack._sendMuteStatus(true);
  1115. }
  1116. oldTrackBelongsToConference && this.onLocalTrackRemoved(oldTrack);
  1117. newTrack && this._setupNewTrack(newTrack);
  1118. // Send 'VideoTypeMessage' on the bridge channel when a video track is added/removed.
  1119. if ((oldTrackBelongsToConference && oldTrack?.isVideoTrack()) || newTrack?.isVideoTrack()) {
  1120. this._sendBridgeVideoTypeMessage(newTrack);
  1121. }
  1122. this._updateRoomPresence(this.getActiveMediaSession());
  1123. if (newTrack !== null && (this.isMutedByFocus || this.isVideoMutedByFocus)) {
  1124. this._fireMuteChangeEvent(newTrack);
  1125. }
  1126. return Promise.resolve();
  1127. })
  1128. .catch(error => {
  1129. logger.error(`replaceTrack failed: ${error?.stack}`);
  1130. return Promise.reject(error);
  1131. });
  1132. };
  1133. /**
  1134. * Replaces the tracks at the lower level by going through the Jingle session
  1135. * and WebRTC peer connection. The method will resolve immediately if there is
  1136. * currently no JingleSession started.
  1137. * @param {JitsiLocalTrack|null} oldTrack the track to be removed during
  1138. * the process or <tt>null</t> if the method should act as "add track"
  1139. * @param {JitsiLocalTrack|null} newTrack the new track to be added or
  1140. * <tt>null</tt> if the method should act as "remove track"
  1141. * @return {Promise} resolved when the process is done or rejected with a string
  1142. * which describes the error.
  1143. * @private
  1144. */
  1145. JitsiConference.prototype._doReplaceTrack = function(oldTrack, newTrack) {
  1146. const replaceTrackPromises = [];
  1147. if (this.jvbJingleSession) {
  1148. replaceTrackPromises.push(this.jvbJingleSession.replaceTrack(oldTrack, newTrack));
  1149. } else {
  1150. logger.info('_doReplaceTrack - no JVB JingleSession');
  1151. }
  1152. if (this.p2pJingleSession) {
  1153. replaceTrackPromises.push(this.p2pJingleSession.replaceTrack(oldTrack, newTrack));
  1154. } else {
  1155. logger.info('_doReplaceTrack - no P2P JingleSession');
  1156. }
  1157. return Promise.all(replaceTrackPromises);
  1158. };
  1159. /**
  1160. * Handler for when a source-add for a local source is rejected by Jicofo.
  1161. *
  1162. * @param {JingleSessionPC} jingleSession - The media session.
  1163. * @param {Error} error - The error message.
  1164. * @param {MediaType} mediaType - The media type of the track associated with the source that was rejected.
  1165. * @returns {void}
  1166. */
  1167. JitsiConference.prototype._removeLocalSourceOnReject = function(jingleSession, error, mediaType) {
  1168. if (!jingleSession) {
  1169. return;
  1170. }
  1171. logger.warn(`Source-add rejected on ${jingleSession}, reason="${error?.reason}", message="${error?.msg}"`);
  1172. const track = this.getLocalTracks(mediaType)[0];
  1173. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_UNMUTE_REJECTED, track);
  1174. };
  1175. /**
  1176. * Operations related to creating a new track
  1177. * @param {JitsiLocalTrack} newTrack the new track being created
  1178. */
  1179. JitsiConference.prototype._setupNewTrack = function(newTrack) {
  1180. const mediaType = newTrack.getType();
  1181. // Create a source name for this track if it doesn't exist.
  1182. if (!newTrack.getSourceName()) {
  1183. const sourceName = getSourceNameForJitsiTrack(
  1184. this.myUserId(),
  1185. mediaType,
  1186. this.getLocalTracks(mediaType)?.length);
  1187. newTrack.setSourceName(sourceName);
  1188. }
  1189. this.rtc.addLocalTrack(newTrack);
  1190. newTrack.setConference(this);
  1191. // Add event handlers.
  1192. newTrack.muteHandler = this._fireMuteChangeEvent.bind(this, newTrack);
  1193. newTrack.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, newTrack.muteHandler);
  1194. if (newTrack.isAudioTrack()) {
  1195. newTrack.audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  1196. newTrack.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, newTrack.audioLevelHandler);
  1197. }
  1198. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, newTrack);
  1199. };
  1200. /**
  1201. * Sets the video type.
  1202. * @param track
  1203. * @return <tt>true</tt> if video type was changed in presence.
  1204. * @private
  1205. */
  1206. JitsiConference.prototype._setNewVideoType = function(track) {
  1207. let videoTypeChanged = false;
  1208. if (track) {
  1209. videoTypeChanged = this._signalingLayer.setTrackVideoType(track.getSourceName(), track.videoType);
  1210. }
  1211. return videoTypeChanged;
  1212. };
  1213. /**
  1214. * Sets mute status.
  1215. * @param mediaType
  1216. * @param localTrack
  1217. * @param isMuted
  1218. * @param <tt>true</tt> when presence was changed, <tt>false</tt> otherwise.
  1219. * @private
  1220. */
  1221. JitsiConference.prototype._setTrackMuteStatus = function(mediaType, localTrack, isMuted) {
  1222. let presenceChanged = false;
  1223. if (localTrack) {
  1224. presenceChanged = this._signalingLayer.setTrackMuteStatus(localTrack.getSourceName(), isMuted);
  1225. presenceChanged && logger.debug(`Mute state of ${localTrack} changed to muted=${isMuted}`);
  1226. }
  1227. return presenceChanged;
  1228. };
  1229. /**
  1230. * Method called by the {@link JitsiLocalTrack} in order to add the underlying MediaStream to the RTCPeerConnection.
  1231. *
  1232. * @param {JitsiLocalTrack} track the local track that will be added to the pc.
  1233. * @return {Promise} resolved when the process is done or rejected with a string which describes the error.
  1234. */
  1235. JitsiConference.prototype._addLocalTrackToPc = function(track) {
  1236. const addPromises = [];
  1237. if (track.conference === this) {
  1238. if (this.jvbJingleSession) {
  1239. addPromises.push(this.jvbJingleSession.addTrackToPc(track));
  1240. } else {
  1241. logger.debug('Add local MediaStream - no JVB Jingle session started yet');
  1242. }
  1243. if (this.p2pJingleSession) {
  1244. addPromises.push(this.p2pJingleSession.addTrackToPc(track));
  1245. } else {
  1246. logger.debug('Add local MediaStream - no P2P Jingle session started yet');
  1247. }
  1248. } else {
  1249. // If the track hasn't been added to the conference yet because of start muted by focus, add it to the
  1250. // conference instead of adding it only to the media sessions.
  1251. addPromises.push(this.addTrack(track));
  1252. }
  1253. return Promise.allSettled(addPromises);
  1254. };
  1255. /**
  1256. * Method called by the {@link JitsiLocalTrack} in order to remove the underlying MediaStream from the
  1257. * RTCPeerConnection.
  1258. *
  1259. * @param {JitsiLocalTrack} track the local track that will be removed.
  1260. * @return {Promise} resolved when the process is done or rejected with a string which describes the error.
  1261. */
  1262. JitsiConference.prototype._removeLocalTrackFromPc = function(track) {
  1263. const removePromises = [];
  1264. if (track.conference === this) {
  1265. if (this.jvbJingleSession) {
  1266. removePromises.push(this.jvbJingleSession.removeTrackFromPc(track));
  1267. } else {
  1268. logger.debug('Remove local MediaStream - no JVB JingleSession started yet');
  1269. }
  1270. if (this.p2pJingleSession) {
  1271. removePromises.push(this.p2pJingleSession.removeTrackFromPc(track));
  1272. } else {
  1273. logger.debug('Remove local MediaStream - no P2P JingleSession started yet');
  1274. }
  1275. }
  1276. return Promise.allSettled(removePromises);
  1277. };
  1278. /**
  1279. * Get role of the local user.
  1280. * @returns {string} user role: 'moderator' or 'none'
  1281. */
  1282. JitsiConference.prototype.getRole = function() {
  1283. return this.room.role;
  1284. };
  1285. /**
  1286. * Returns whether or not the current conference has been joined as a hidden
  1287. * user.
  1288. *
  1289. * @returns {boolean|null} True if hidden, false otherwise. Will return null if
  1290. * no connection is active.
  1291. */
  1292. JitsiConference.prototype.isHidden = function() {
  1293. if (!this.connection) {
  1294. return null;
  1295. }
  1296. return Strophe.getDomainFromJid(this.connection.getJid())
  1297. === this.options.config.hiddenDomain;
  1298. };
  1299. /**
  1300. * Check if local user is moderator.
  1301. * @returns {boolean|null} true if local user is moderator, false otherwise. If
  1302. * we're no longer in the conference room then <tt>null</tt> is returned.
  1303. */
  1304. JitsiConference.prototype.isModerator = function() {
  1305. return this.room ? this.room.isModerator() : null;
  1306. };
  1307. /**
  1308. * Set password for the room.
  1309. * @param {string} password new password for the room.
  1310. * @returns {Promise}
  1311. */
  1312. JitsiConference.prototype.lock = function(password) {
  1313. if (!this.isModerator()) {
  1314. return Promise.reject(new Error('You are not moderator.'));
  1315. }
  1316. return new Promise((resolve, reject) => {
  1317. this.room.lockRoom(
  1318. password || '',
  1319. () => resolve(),
  1320. err => reject(err),
  1321. () => reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED));
  1322. });
  1323. };
  1324. /**
  1325. * Remove password from the room.
  1326. * @returns {Promise}
  1327. */
  1328. JitsiConference.prototype.unlock = function() {
  1329. return this.lock();
  1330. };
  1331. /**
  1332. * Obtains the current value for "lastN". See {@link setLastN} for more info.
  1333. * @returns {number}
  1334. */
  1335. JitsiConference.prototype.getLastN = function() {
  1336. return this.receiveVideoController.getLastN();
  1337. };
  1338. /**
  1339. * Obtains the forwarded sources list in this conference.
  1340. * @return {Array<string>|null}
  1341. */
  1342. JitsiConference.prototype.getForwardedSources = function() {
  1343. return this.rtc.getForwardedSources();
  1344. };
  1345. /**
  1346. * Selects a new value for "lastN". The requested amount of videos are going
  1347. * to be delivered after the value is in effect. Set to -1 for unlimited or
  1348. * all available videos.
  1349. * @param lastN the new number of videos the user would like to receive.
  1350. * @throws Error or RangeError if the given value is not a number or is smaller
  1351. * than -1.
  1352. */
  1353. JitsiConference.prototype.setLastN = function(lastN) {
  1354. if (!Number.isInteger(lastN) && !Number.parseInt(lastN, 10)) {
  1355. throw new Error(`Invalid value for lastN: ${lastN}`);
  1356. }
  1357. const n = Number(lastN);
  1358. if (n < -1) {
  1359. throw new RangeError('lastN cannot be smaller than -1');
  1360. }
  1361. this.receiveVideoController.setLastN(n);
  1362. // If the P2P session is not fully established yet, we wait until it gets established.
  1363. if (this.p2pJingleSession) {
  1364. const isVideoActive = n !== 0;
  1365. this.p2pJingleSession
  1366. .setP2pVideoTransferActive(isVideoActive)
  1367. .catch(error => {
  1368. logger.error(`Failed to adjust video transfer status (${isVideoActive})`, error);
  1369. });
  1370. }
  1371. };
  1372. /**
  1373. * @return Array<JitsiParticipant> an array of all participants in this conference.
  1374. */
  1375. JitsiConference.prototype.getParticipants = function() {
  1376. return Array.from(this.participants.values());
  1377. };
  1378. /**
  1379. * Returns the number of participants in the conference, including the local
  1380. * participant.
  1381. * @param countHidden {boolean} Whether or not to include hidden participants
  1382. * in the count. Default: false.
  1383. **/
  1384. JitsiConference.prototype.getParticipantCount = function(countHidden = false) {
  1385. let participants = this.getParticipants();
  1386. if (!countHidden) {
  1387. participants = participants.filter(p => !p.isHidden());
  1388. }
  1389. // Add one for the local participant.
  1390. return participants.length + 1;
  1391. };
  1392. /**
  1393. * @returns {JitsiParticipant} the participant in this conference with the
  1394. * specified id (or undefined if there isn't one).
  1395. * @param id the id of the participant.
  1396. */
  1397. JitsiConference.prototype.getParticipantById = function(id) {
  1398. return this.participants.get(id);
  1399. };
  1400. /**
  1401. * Grant owner rights to the participant.
  1402. * @param {string} id id of the participant to grant owner rights to.
  1403. */
  1404. JitsiConference.prototype.grantOwner = function(id) {
  1405. const participant = this.getParticipantById(id);
  1406. if (!participant) {
  1407. return;
  1408. }
  1409. this.room.setAffiliation(participant.getConnectionJid(), 'owner');
  1410. };
  1411. /**
  1412. * Revoke owner rights to the participant or local Participant as
  1413. * the user might want to refuse to be a moderator.
  1414. * @param {string} id id of the participant to revoke owner rights to.
  1415. */
  1416. JitsiConference.prototype.revokeOwner = function(id) {
  1417. const participant = this.getParticipantById(id);
  1418. const isMyself = this.myUserId() === id;
  1419. const role = this.isMembersOnly() ? 'member' : 'none';
  1420. if (isMyself) {
  1421. this.room.setAffiliation(this.connection.getJid(), role);
  1422. } else if (participant) {
  1423. this.room.setAffiliation(participant.getConnectionJid(), role);
  1424. }
  1425. };
  1426. /**
  1427. * Kick participant from this conference.
  1428. * @param {string} id id of the participant to kick
  1429. * @param {string} reason reason of the participant to kick
  1430. */
  1431. JitsiConference.prototype.kickParticipant = function(id, reason) {
  1432. const participant = this.getParticipantById(id);
  1433. if (!participant) {
  1434. return;
  1435. }
  1436. this.room.kick(participant.getJid(), reason);
  1437. };
  1438. /**
  1439. * Maybe clears the timeout which emits {@link ACTION_JINGLE_SI_TIMEOUT}
  1440. * analytics event.
  1441. * @private
  1442. */
  1443. JitsiConference.prototype._maybeClearSITimeout = function() {
  1444. if (this._sessionInitiateTimeout
  1445. && (this.jvbJingleSession || this.getParticipantCount() < 2)) {
  1446. window.clearTimeout(this._sessionInitiateTimeout);
  1447. this._sessionInitiateTimeout = null;
  1448. }
  1449. };
  1450. /**
  1451. * Sets a timeout which will emit {@link ACTION_JINGLE_SI_TIMEOUT} analytics
  1452. * event.
  1453. * @private
  1454. */
  1455. JitsiConference.prototype._maybeSetSITimeout = function() {
  1456. // Jicofo is supposed to invite if there are at least 2 participants
  1457. if (!this.jvbJingleSession
  1458. && this.getParticipantCount() >= 2
  1459. && !this._sessionInitiateTimeout) {
  1460. this._sessionInitiateTimeout = window.setTimeout(() => {
  1461. this._sessionInitiateTimeout = null;
  1462. Statistics.sendAnalytics(createJingleEvent(
  1463. ACTION_JINGLE_SI_TIMEOUT,
  1464. {
  1465. p2p: false,
  1466. value: JINGLE_SI_TIMEOUT
  1467. }));
  1468. }, JINGLE_SI_TIMEOUT);
  1469. }
  1470. };
  1471. /**
  1472. * Mutes a participant.
  1473. * @param {string} id The id of the participant to mute.
  1474. */
  1475. JitsiConference.prototype.muteParticipant = function(id, mediaType) {
  1476. const muteMediaType = mediaType ? mediaType : MediaType.AUDIO;
  1477. if (muteMediaType !== MediaType.AUDIO && muteMediaType !== MediaType.VIDEO) {
  1478. logger.error(`Unsupported media type: ${muteMediaType}`);
  1479. return;
  1480. }
  1481. const participant = this.getParticipantById(id);
  1482. if (!participant) {
  1483. return;
  1484. }
  1485. this.room.muteParticipant(participant.getJid(), true, muteMediaType);
  1486. };
  1487. /* eslint-disable max-params */
  1488. /**
  1489. * Notifies this JitsiConference that a new member has joined its chat room.
  1490. *
  1491. * FIXME This should NOT be exposed!
  1492. *
  1493. * @param jid the jid of the participant in the MUC
  1494. * @param nick the display name of the participant
  1495. * @param role the role of the participant in the MUC
  1496. * @param isHidden indicates if this is a hidden participant (system
  1497. * participant for example a recorder).
  1498. * @param statsID the participant statsID (optional)
  1499. * @param status the initial status if any
  1500. * @param identity the member identity, if any
  1501. * @param botType the member botType, if any
  1502. * @param fullJid the member full jid, if any
  1503. * @param features the member botType, if any
  1504. * @param isReplaceParticipant whether this join replaces a participant with
  1505. * the same jwt.
  1506. */
  1507. JitsiConference.prototype.onMemberJoined = function(
  1508. jid, nick, role, isHidden, statsID, status, identity, botType, fullJid, features, isReplaceParticipant) {
  1509. const id = Strophe.getResourceFromJid(jid);
  1510. if (id === 'focus' || this.myUserId() === id) {
  1511. return;
  1512. }
  1513. const participant = new JitsiParticipant(jid, this, nick, isHidden, statsID, status, identity);
  1514. participant.setConnectionJid(fullJid);
  1515. participant.setRole(role);
  1516. participant.setBotType(botType);
  1517. participant.setFeatures(features);
  1518. participant.setIsReplacing(isReplaceParticipant);
  1519. // Set remote tracks on the participant if source signaling was received before presence.
  1520. const remoteTracks = this.isP2PActive()
  1521. ? this.p2pJingleSession?.peerconnection.getRemoteTracks(id) ?? []
  1522. : this.jvbJingleSession?.peerconnection.getRemoteTracks(id) ?? [];
  1523. for (const track of remoteTracks) {
  1524. participant._tracks.push(track);
  1525. }
  1526. this.participants.set(id, participant);
  1527. this.eventEmitter.emit(
  1528. JitsiConferenceEvents.USER_JOINED,
  1529. id,
  1530. participant);
  1531. this._updateFeatures(participant);
  1532. // maybeStart only if we had finished joining as then we will have information for the number of participants
  1533. if (this.isJoined()) {
  1534. this._maybeStartOrStopP2P();
  1535. }
  1536. this._maybeSetSITimeout();
  1537. };
  1538. /* eslint-enable max-params */
  1539. /**
  1540. * Get notified when we joined the room.
  1541. *
  1542. * @private
  1543. */
  1544. JitsiConference.prototype._onMucJoined = function() {
  1545. this._numberOfParticipantsOnJoin = this.getParticipantCount();
  1546. this._maybeStartOrStopP2P();
  1547. };
  1548. /**
  1549. * Updates features for a participant.
  1550. * @param {JitsiParticipant} participant - The participant to query for features.
  1551. * @returns {void}
  1552. * @private
  1553. */
  1554. JitsiConference.prototype._updateFeatures = function(participant) {
  1555. participant.getFeatures()
  1556. .then(features => {
  1557. participant._supportsDTMF = features.has('urn:xmpp:jingle:dtmf:0');
  1558. this.updateDTMFSupport();
  1559. if (features.has(FEATURE_JIGASI)) {
  1560. participant.setProperty('features_jigasi', true);
  1561. }
  1562. if (features.has(FEATURE_E2EE)) {
  1563. participant.setProperty('features_e2ee', true);
  1564. }
  1565. })
  1566. .catch(() => false);
  1567. };
  1568. /**
  1569. * Get notified when member bot type had changed.
  1570. * @param jid the member jid
  1571. * @param botType the new botType value
  1572. * @private
  1573. */
  1574. JitsiConference.prototype._onMemberBotTypeChanged = function(jid, botType) {
  1575. // find the participant and mark it as non bot, as the real one will join
  1576. // in a moment
  1577. const peers = this.getParticipants();
  1578. const botParticipant = peers.find(p => p.getJid() === jid);
  1579. if (botParticipant) {
  1580. botParticipant.setBotType(botType);
  1581. const id = Strophe.getResourceFromJid(jid);
  1582. this.eventEmitter.emit(
  1583. JitsiConferenceEvents.BOT_TYPE_CHANGED,
  1584. id,
  1585. botType);
  1586. }
  1587. // if botType changed to undefined, botType was removed, in case of
  1588. // poltergeist mode this is the moment when the poltergeist had exited and
  1589. // the real participant had already replaced it.
  1590. // In this case we can check and try p2p
  1591. if (!botParticipant.getBotType()) {
  1592. this._maybeStartOrStopP2P();
  1593. }
  1594. };
  1595. JitsiConference.prototype.onMemberLeft = function(jid, reason) {
  1596. const id = Strophe.getResourceFromJid(jid);
  1597. if (id === 'focus' || this.myUserId() === id) {
  1598. return;
  1599. }
  1600. const mediaSessions = this.getMediaSessions();
  1601. let tracksToBeRemoved = [];
  1602. for (const session of mediaSessions) {
  1603. const remoteTracks = session.peerconnection.getRemoteTracks(id);
  1604. remoteTracks && (tracksToBeRemoved = [ ...tracksToBeRemoved, ...remoteTracks ]);
  1605. // Update the SSRC owners list.
  1606. session._signalingLayer.updateSsrcOwnersOnLeave(id);
  1607. if (!FeatureFlags.isSsrcRewritingSupported()) {
  1608. // Remove the ssrcs from the remote description and renegotiate.
  1609. session.removeRemoteStreamsOnLeave(id);
  1610. }
  1611. }
  1612. tracksToBeRemoved.forEach(track => {
  1613. // Fire the event before renegotiation is done so that the thumbnails can be removed immediately.
  1614. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  1615. if (FeatureFlags.isSsrcRewritingSupported()) {
  1616. track.setSourceName(null);
  1617. track.setOwner(null);
  1618. }
  1619. });
  1620. const participant = this.participants.get(id);
  1621. if (participant) {
  1622. this.participants.delete(id);
  1623. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant, reason);
  1624. }
  1625. if (this.room !== null) { // Skip if we have left the room already.
  1626. this._maybeStartOrStopP2P(true /* triggered by user left event */);
  1627. this._maybeClearSITimeout();
  1628. }
  1629. };
  1630. /* eslint-disable max-params */
  1631. /**
  1632. * Designates an event indicating that we were kicked from the XMPP MUC.
  1633. * @param {boolean} isSelfPresence - whether it is for local participant
  1634. * or another participant.
  1635. * @param {string} actorId - the id of the participant who was initiator
  1636. * of the kick.
  1637. * @param {string?} kickedParticipantId - when it is not a kick for local participant,
  1638. * this is the id of the participant which was kicked.
  1639. * @param {string} reason - reason of the participant to kick
  1640. * @param {boolean?} isReplaceParticipant - whether this is a server initiated kick in order
  1641. * to replace it with a participant with same jwt.
  1642. */
  1643. JitsiConference.prototype.onMemberKicked = function(
  1644. isSelfPresence,
  1645. actorId,
  1646. kickedParticipantId,
  1647. reason,
  1648. isReplaceParticipant) {
  1649. // This check which be true when we kick someone else. With the introduction of lobby
  1650. // the ChatRoom KICKED event is now also emitted for ourselves (the kicker) so we want to
  1651. // avoid emitting an event where `undefined` kicked someone.
  1652. if (actorId === this.myUserId()) {
  1653. return;
  1654. }
  1655. const actorParticipant = this.participants.get(actorId);
  1656. if (isSelfPresence) {
  1657. this.leave().finally(() => this.xmpp.disconnect());
  1658. this.eventEmitter.emit(
  1659. JitsiConferenceEvents.KICKED, actorParticipant, reason, isReplaceParticipant);
  1660. return;
  1661. }
  1662. const kickedParticipant = this.participants.get(kickedParticipantId);
  1663. kickedParticipant.setIsReplaced(isReplaceParticipant);
  1664. this.eventEmitter.emit(
  1665. JitsiConferenceEvents.PARTICIPANT_KICKED, actorParticipant, kickedParticipant, reason);
  1666. };
  1667. /**
  1668. * Method called on local MUC role change.
  1669. * @param {string} role the name of new user's role as defined by XMPP MUC.
  1670. */
  1671. JitsiConference.prototype.onLocalRoleChanged = function(role) {
  1672. // Emit role changed for local JID
  1673. this.eventEmitter.emit(
  1674. JitsiConferenceEvents.USER_ROLE_CHANGED, this.myUserId(), role);
  1675. };
  1676. JitsiConference.prototype.onUserRoleChanged = function(jid, role) {
  1677. const id = Strophe.getResourceFromJid(jid);
  1678. const participant = this.getParticipantById(id);
  1679. if (!participant) {
  1680. return;
  1681. }
  1682. participant.setRole(role);
  1683. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  1684. };
  1685. JitsiConference.prototype.onDisplayNameChanged = function(jid, displayName) {
  1686. const id = Strophe.getResourceFromJid(jid);
  1687. const participant = this.getParticipantById(id);
  1688. if (!participant) {
  1689. return;
  1690. }
  1691. if (participant._displayName === displayName) {
  1692. return;
  1693. }
  1694. participant._displayName = displayName;
  1695. this.eventEmitter.emit(
  1696. JitsiConferenceEvents.DISPLAY_NAME_CHANGED,
  1697. id,
  1698. displayName);
  1699. };
  1700. /**
  1701. * Notifies this JitsiConference that a JitsiRemoteTrack was added to the conference.
  1702. *
  1703. * @param {JitsiRemoteTrack} track the JitsiRemoteTrack which was added to this JitsiConference.
  1704. */
  1705. JitsiConference.prototype.onRemoteTrackAdded = function(track) {
  1706. if (track.isP2P && !this.isP2PActive()) {
  1707. logger.info('Trying to add remote P2P track, when not in P2P - IGNORED');
  1708. return;
  1709. } else if (!track.isP2P && this.isP2PActive()) {
  1710. logger.info('Trying to add remote JVB track, when in P2P - IGNORED');
  1711. return;
  1712. }
  1713. const id = track.getParticipantId();
  1714. const participant = this.getParticipantById(id);
  1715. // Add track to JitsiParticipant.
  1716. if (participant) {
  1717. participant._tracks.push(track);
  1718. } else {
  1719. logger.info(`Source signaling received before presence for ${id}`);
  1720. }
  1721. const emitter = this.eventEmitter;
  1722. track.addEventListener(
  1723. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  1724. () => emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track));
  1725. track.isAudioTrack() && track.addEventListener(
  1726. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  1727. (audioLevel, tpc) => {
  1728. const activeTPC = this.getActivePeerConnection();
  1729. if (activeTPC === tpc) {
  1730. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  1731. }
  1732. }
  1733. );
  1734. emitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  1735. };
  1736. /**
  1737. * Callback called by the Jingle plugin when 'session-answer' is received.
  1738. * @param {JingleSessionPC} session the Jingle session for which an answer was
  1739. * received.
  1740. * @param {jQuery} answer a jQuery selector pointing to 'jingle' IQ element
  1741. */
  1742. // eslint-disable-next-line no-unused-vars
  1743. JitsiConference.prototype.onCallAccepted = function(session, answer) {
  1744. if (this.p2pJingleSession === session) {
  1745. logger.info('P2P setAnswer');
  1746. this.p2pJingleSession.setAnswer(answer);
  1747. this.eventEmitter.emit(JitsiConferenceEvents._MEDIA_SESSION_STARTED, this.p2pJingleSession);
  1748. }
  1749. };
  1750. /**
  1751. * Callback called by the Jingle plugin when 'transport-info' is received.
  1752. * @param {JingleSessionPC} session the Jingle session for which the IQ was
  1753. * received
  1754. * @param {jQuery} transportInfo a jQuery selector pointing to 'jingle' IQ
  1755. * element
  1756. */
  1757. // eslint-disable-next-line no-unused-vars
  1758. JitsiConference.prototype.onTransportInfo = function(session, transportInfo) {
  1759. if (this.p2pJingleSession === session) {
  1760. logger.info('P2P addIceCandidates');
  1761. this.p2pJingleSession.addIceCandidates(transportInfo);
  1762. }
  1763. };
  1764. /**
  1765. * Notifies this JitsiConference that a JitsiRemoteTrack was removed from
  1766. * the conference.
  1767. *
  1768. * @param {JitsiRemoteTrack} removedTrack
  1769. */
  1770. JitsiConference.prototype.onRemoteTrackRemoved = function(removedTrack) {
  1771. this.getParticipants().forEach(participant => {
  1772. const tracks = participant.getTracks();
  1773. for (let i = 0; i < tracks.length; i++) {
  1774. if (tracks[i] === removedTrack) {
  1775. // Since the tracks have been compared and are
  1776. // considered equal the result of splice can be ignored.
  1777. participant._tracks.splice(i, 1);
  1778. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, removedTrack);
  1779. break;
  1780. }
  1781. }
  1782. }, this);
  1783. };
  1784. /**
  1785. * Handles an incoming call event for the P2P jingle session.
  1786. */
  1787. JitsiConference.prototype._onIncomingCallP2P = function(jingleSession, jingleOffer) {
  1788. let rejectReason;
  1789. const contentName = jingleOffer.find('>content').attr('name');
  1790. const peerUsesUnifiedPlan = contentName === '0' || contentName === '1';
  1791. // Reject P2P between endpoints that are not running in the same mode w.r.t to SDPs (plan-b and unified plan).
  1792. if (!peerUsesUnifiedPlan) {
  1793. rejectReason = {
  1794. reason: 'decline',
  1795. reasonDescription: 'P2P disabled',
  1796. errorMsg: 'P2P across two endpoints in different SDP modes is disabled'
  1797. };
  1798. } else if ((!this.isP2PEnabled() && !this.isP2PTestModeEnabled())
  1799. || (browser.isFirefox() && !this._firefoxP2pEnabled)) {
  1800. rejectReason = {
  1801. reason: 'decline',
  1802. reasonDescription: 'P2P disabled',
  1803. errorMsg: 'P2P mode disabled in the configuration or browser unsupported'
  1804. };
  1805. } else if (this.p2pJingleSession) {
  1806. // Reject incoming P2P call (already in progress)
  1807. rejectReason = {
  1808. reason: 'busy',
  1809. reasonDescription: 'P2P already in progress',
  1810. errorMsg: 'Duplicated P2P "session-initiate"'
  1811. };
  1812. } else if (!this._shouldBeInP2PMode()) {
  1813. rejectReason = {
  1814. reason: 'decline',
  1815. reasonDescription: 'P2P requirements not met',
  1816. errorMsg: 'Received P2P "session-initiate" when should not be in P2P mode'
  1817. };
  1818. Statistics.sendAnalytics(createJingleEvent(ACTION_P2P_DECLINED));
  1819. }
  1820. if (rejectReason) {
  1821. this._rejectIncomingCall(jingleSession, rejectReason);
  1822. } else {
  1823. this._acceptP2PIncomingCall(jingleSession, jingleOffer);
  1824. }
  1825. };
  1826. /**
  1827. * Handles an incoming call event.
  1828. */
  1829. JitsiConference.prototype.onIncomingCall = function(jingleSession, jingleOffer, now) {
  1830. // Handle incoming P2P call
  1831. if (jingleSession.isP2P) {
  1832. this._onIncomingCallP2P(jingleSession, jingleOffer);
  1833. } else {
  1834. if (!this.isFocus(jingleSession.remoteJid)) {
  1835. const description = 'Rejecting session-initiate from non-focus.';
  1836. this._rejectIncomingCall(
  1837. jingleSession, {
  1838. reason: 'security-error',
  1839. reasonDescription: description,
  1840. errorMsg: description
  1841. });
  1842. return;
  1843. }
  1844. this._acceptJvbIncomingCall(jingleSession, jingleOffer, now);
  1845. }
  1846. };
  1847. /**
  1848. * Accepts an incoming call event for the JVB jingle session.
  1849. */
  1850. JitsiConference.prototype._acceptJvbIncomingCall = function(jingleSession, jingleOffer, now) {
  1851. // Accept incoming call
  1852. this.jvbJingleSession = jingleSession;
  1853. this.room.connectionTimes['session.initiate'] = now;
  1854. this._sendConferenceJoinAnalyticsEvent();
  1855. if (this.wasStopped) {
  1856. Statistics.sendAnalyticsAndLog(createJingleEvent(ACTION_JINGLE_RESTART, { p2p: false }));
  1857. }
  1858. const serverRegion
  1859. = $(jingleOffer)
  1860. .find('>bridge-session[xmlns="http://jitsi.org/protocol/focus"]')
  1861. .attr('region');
  1862. this.eventEmitter.emit(JitsiConferenceEvents.SERVER_REGION_CHANGED, serverRegion);
  1863. this._maybeClearSITimeout();
  1864. Statistics.sendAnalytics(createJingleEvent(
  1865. ACTION_JINGLE_SI_RECEIVED,
  1866. {
  1867. p2p: false,
  1868. value: now
  1869. }));
  1870. try {
  1871. jingleSession.initialize(
  1872. this.room,
  1873. this.rtc,
  1874. this._signalingLayer,
  1875. {
  1876. ...this.options.config,
  1877. codecSettings: {
  1878. mediaType: MediaType.VIDEO,
  1879. codecList: this.codecSelection.getCodecPreferenceList('jvb')
  1880. },
  1881. enableInsertableStreams: this.isE2EEEnabled() || FeatureFlags.isRunInLiteModeEnabled()
  1882. });
  1883. } catch (error) {
  1884. logger.error(error);
  1885. return;
  1886. }
  1887. // Open a channel with the videobridge.
  1888. this._setBridgeChannel(jingleOffer, jingleSession.peerconnection);
  1889. const localTracks = this._getInitialLocalTracks();
  1890. try {
  1891. jingleSession.acceptOffer(
  1892. jingleOffer,
  1893. () => {
  1894. // If for any reason invite for the JVB session arrived after
  1895. // the P2P has been established already the media transfer needs
  1896. // to be turned off here.
  1897. if (this.isP2PActive() && this.jvbJingleSession) {
  1898. this._suspendMediaTransferForJvbConnection();
  1899. }
  1900. this.eventEmitter.emit(JitsiConferenceEvents._MEDIA_SESSION_STARTED, jingleSession);
  1901. if (!this.isP2PActive()) {
  1902. this.eventEmitter.emit(JitsiConferenceEvents._MEDIA_SESSION_ACTIVE_CHANGED, jingleSession);
  1903. }
  1904. },
  1905. error => {
  1906. logger.error('Failed to accept incoming Jingle session', error);
  1907. },
  1908. localTracks
  1909. );
  1910. // Set the capture fps for screenshare if it is set through the UI.
  1911. this._desktopSharingFrameRate
  1912. && jingleSession.peerconnection.setDesktopSharingFrameRate(this._desktopSharingFrameRate);
  1913. this.statistics.startRemoteStats(this.jvbJingleSession.peerconnection);
  1914. } catch (e) {
  1915. logger.error(e);
  1916. }
  1917. };
  1918. /**
  1919. * Sets the BridgeChannel.
  1920. *
  1921. * @param {jQuery} offerIq a jQuery selector pointing to the jingle element of
  1922. * the offer IQ which may carry the WebSocket URL for the 'websocket'
  1923. * BridgeChannel mode.
  1924. * @param {TraceablePeerConnection} pc the peer connection which will be used
  1925. * to listen for new WebRTC Data Channels (in the 'datachannel' mode).
  1926. */
  1927. JitsiConference.prototype._setBridgeChannel = function(offerIq, pc) {
  1928. const ignoreDomain = this.connection?.options?.bridgeChannel?.ignoreDomain;
  1929. const preferSctp = this.connection?.options?.bridgeChannel?.preferSctp ?? false;
  1930. const sctpOffered = $(offerIq).find('>content[name="data"]')
  1931. .first().length === 1;
  1932. let wsUrl = null;
  1933. logger.info(`SCTP: offered=${sctpOffered}, prefered=${preferSctp}`);
  1934. if (!(sctpOffered && preferSctp)) {
  1935. $(offerIq).find('>content>transport>web-socket')
  1936. .toArray()
  1937. .map(e => e.getAttribute('url'))
  1938. .forEach(url => {
  1939. if (!wsUrl && (!ignoreDomain || ignoreDomain !== new URL(url).hostname)) {
  1940. wsUrl = url;
  1941. logger.info(`Using colibri-ws url ${url}`);
  1942. } else if (!wsUrl) {
  1943. logger.info(`Ignoring colibri-ws url with domain ${ignoreDomain}`);
  1944. }
  1945. });
  1946. if (!wsUrl) {
  1947. const firstWsUrl = $(offerIq).find('>content>transport>web-socket')
  1948. .first();
  1949. if (firstWsUrl.length === 1) {
  1950. wsUrl = firstWsUrl[0].getAttribute('url');
  1951. logger.info(`Falling back to ${wsUrl}`);
  1952. }
  1953. }
  1954. }
  1955. if (wsUrl && !(sctpOffered && preferSctp)) {
  1956. // If the offer contains a websocket and we don't prefer SCTP use it.
  1957. this.rtc.initializeBridgeChannel(null, wsUrl);
  1958. } else if (sctpOffered) {
  1959. // Otherwise, fall back to an attempt to use SCTP.
  1960. this.rtc.initializeBridgeChannel(pc, null);
  1961. } else {
  1962. logger.warn('Neither SCTP nor a websocket is available. Will not initialize bridge channel.');
  1963. }
  1964. };
  1965. /**
  1966. * Rejects incoming Jingle call.
  1967. * @param {JingleSessionPC} jingleSession the session instance to be rejected.
  1968. * @param {object} [options]
  1969. * @param {string} options.reason the name of the reason element as defined
  1970. * by Jingle
  1971. * @param {string} options.reasonDescription the reason description which will
  1972. * be included in Jingle 'session-terminate' message.
  1973. * @param {string} options.errorMsg an error message to be logged on global
  1974. * error handler
  1975. * @private
  1976. */
  1977. JitsiConference.prototype._rejectIncomingCall = function(jingleSession, options) {
  1978. if (options?.errorMsg) {
  1979. logger.warn(options.errorMsg);
  1980. }
  1981. // Terminate the jingle session with a reason
  1982. jingleSession.terminate(
  1983. null /* success callback => we don't care */,
  1984. error => {
  1985. logger.warn(
  1986. 'An error occurred while trying to terminate'
  1987. + ' invalid Jingle session', error);
  1988. }, {
  1989. reason: options && options.reason,
  1990. reasonDescription: options && options.reasonDescription,
  1991. sendSessionTerminate: true
  1992. });
  1993. };
  1994. /**
  1995. * Handles the call ended event.
  1996. * XXX is this due to the remote side terminating the Jingle session?
  1997. *
  1998. * @param {JingleSessionPC} jingleSession the jingle session which has been
  1999. * terminated.
  2000. * @param {String} reasonCondition the Jingle reason condition.
  2001. * @param {String|null} reasonText human readable reason text which may provide
  2002. * more details about why the call has been terminated.
  2003. */
  2004. JitsiConference.prototype.onCallEnded = function(jingleSession, reasonCondition, reasonText) {
  2005. logger.info(
  2006. `Call ended: ${reasonCondition} - ${reasonText} P2P ?${
  2007. jingleSession.isP2P}`);
  2008. if (jingleSession === this.jvbJingleSession) {
  2009. this.wasStopped = true;
  2010. Statistics.sendAnalytics(
  2011. createJingleEvent(ACTION_JINGLE_TERMINATE, { p2p: false }));
  2012. // Stop the stats
  2013. if (this.statistics) {
  2014. this.statistics.stopRemoteStats(this.jvbJingleSession.peerconnection);
  2015. }
  2016. // Current JVB JingleSession is no longer valid, so set it to null
  2017. this.jvbJingleSession = null;
  2018. // Let the RTC service do any cleanups
  2019. this.rtc.onCallEnded();
  2020. } else if (jingleSession === this.p2pJingleSession) {
  2021. const stopOptions = {};
  2022. if (reasonCondition === 'connectivity-error' && reasonText === 'ICE FAILED') {
  2023. // It can happen that the other peer detects ICE failed and terminates the session, before we get the event
  2024. // on our side. But we are able to parse the reason and mark it here.
  2025. Statistics.analytics.addPermanentProperties({ p2pFailed: true });
  2026. } else if (reasonCondition === 'success' && reasonText === 'restart') {
  2027. // When we are restarting media sessions we don't want to switch the tracks to the JVB just yet.
  2028. stopOptions.requestRestart = true;
  2029. }
  2030. this._stopP2PSession(stopOptions);
  2031. } else {
  2032. logger.error(
  2033. 'Received onCallEnded for invalid session',
  2034. jingleSession.sid,
  2035. jingleSession.remoteJid,
  2036. reasonCondition,
  2037. reasonText);
  2038. }
  2039. };
  2040. /**
  2041. * Handles the suspend detected event. Leaves the room and fires suspended.
  2042. * @param {JingleSessionPC} jingleSession
  2043. */
  2044. JitsiConference.prototype.onSuspendDetected = function(jingleSession) {
  2045. if (!jingleSession.isP2P) {
  2046. this.leave();
  2047. this.eventEmitter.emit(JitsiConferenceEvents.SUSPEND_DETECTED);
  2048. }
  2049. };
  2050. JitsiConference.prototype.updateDTMFSupport = function() {
  2051. let somebodySupportsDTMF = false;
  2052. const participants = this.getParticipants();
  2053. // check if at least 1 participant supports DTMF
  2054. for (let i = 0; i < participants.length; i += 1) {
  2055. if (participants[i].supportsDTMF()) {
  2056. somebodySupportsDTMF = true;
  2057. break;
  2058. }
  2059. }
  2060. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  2061. this.somebodySupportsDTMF = somebodySupportsDTMF;
  2062. this.eventEmitter.emit(
  2063. JitsiConferenceEvents.DTMF_SUPPORT_CHANGED,
  2064. somebodySupportsDTMF);
  2065. }
  2066. };
  2067. /**
  2068. * Allows to check if there is at least one user in the conference
  2069. * that supports DTMF.
  2070. * @returns {boolean} true if somebody supports DTMF, false otherwise
  2071. */
  2072. JitsiConference.prototype.isDTMFSupported = function() {
  2073. return this.somebodySupportsDTMF;
  2074. };
  2075. /**
  2076. * Returns the local user's ID
  2077. * @return {string} local user's ID
  2078. */
  2079. JitsiConference.prototype.myUserId = function() {
  2080. return (
  2081. this.room && this.room.myroomjid
  2082. ? Strophe.getResourceFromJid(this.room.myroomjid)
  2083. : null);
  2084. };
  2085. JitsiConference.prototype.sendTones = function(tones, duration, pause) {
  2086. const peerConnection = this.getActivePeerConnection();
  2087. if (peerConnection) {
  2088. peerConnection.sendTones(tones, duration, pause);
  2089. } else {
  2090. logger.warn('cannot sendTones: no peer connection');
  2091. }
  2092. };
  2093. /**
  2094. * Starts recording the current conference.
  2095. *
  2096. * @param {Object} options - Configuration for the recording. See
  2097. * {@link Chatroom#startRecording} for more info.
  2098. * @returns {Promise} See {@link Chatroom#startRecording} for more info.
  2099. */
  2100. JitsiConference.prototype.startRecording = function(options) {
  2101. if (this.room) {
  2102. return this.recordingManager.startRecording(options);
  2103. }
  2104. return Promise.reject(new Error('The conference is not created yet!'));
  2105. };
  2106. /**
  2107. * Stop a recording session.
  2108. *
  2109. * @param {string} sessionID - The ID of the recording session that
  2110. * should be stopped.
  2111. * @returns {Promise} See {@link Chatroom#stopRecording} for more info.
  2112. */
  2113. JitsiConference.prototype.stopRecording = function(sessionID) {
  2114. if (this.room) {
  2115. return this.recordingManager.stopRecording(sessionID);
  2116. }
  2117. return Promise.reject(new Error('The conference is not created yet!'));
  2118. };
  2119. /**
  2120. * Returns true if the SIP calls are supported and false otherwise
  2121. */
  2122. JitsiConference.prototype.isSIPCallingSupported = function() {
  2123. return this.room?.xmpp?.moderator?.isSipGatewayEnabled() ?? false;
  2124. };
  2125. /**
  2126. * Dials a number.
  2127. * @param number the number
  2128. */
  2129. JitsiConference.prototype.dial = function(number) {
  2130. if (this.room) {
  2131. return this.room.dial(number);
  2132. }
  2133. return Promise.reject(new Error('The conference is not created yet!'));
  2134. };
  2135. /**
  2136. * Hangup an existing call
  2137. */
  2138. JitsiConference.prototype.hangup = function() {
  2139. if (this.room) {
  2140. return this.room.hangup();
  2141. }
  2142. return Promise.resolve();
  2143. };
  2144. /**
  2145. * Returns the phone number for joining the conference.
  2146. */
  2147. JitsiConference.prototype.getPhoneNumber = function() {
  2148. if (this.room) {
  2149. return this.room.getPhoneNumber();
  2150. }
  2151. return null;
  2152. };
  2153. /**
  2154. * Returns the pin for joining the conference with phone.
  2155. */
  2156. JitsiConference.prototype.getPhonePin = function() {
  2157. if (this.room) {
  2158. return this.room.getPhonePin();
  2159. }
  2160. return null;
  2161. };
  2162. /**
  2163. * Returns the meeting unique ID if any.
  2164. *
  2165. * @returns {string|undefined}
  2166. */
  2167. JitsiConference.prototype.getMeetingUniqueId = function() {
  2168. if (this.room) {
  2169. return this.room.getMeetingId();
  2170. }
  2171. };
  2172. /**
  2173. * Will return P2P or JVB <tt>TraceablePeerConnection</tt> depending on
  2174. * which connection is currently active.
  2175. *
  2176. * @return {TraceablePeerConnection|null} null if there isn't any active
  2177. * <tt>TraceablePeerConnection</tt> currently available.
  2178. * @public (FIXME how to make package local ?)
  2179. */
  2180. JitsiConference.prototype.getActivePeerConnection = function() {
  2181. const session = this.isP2PActive() ? this.p2pJingleSession : this.jvbJingleSession;
  2182. return session ? session.peerconnection : null;
  2183. };
  2184. /**
  2185. * Returns the connection state for the current room. Its ice connection state
  2186. * for its session.
  2187. * NOTE that "completed" ICE state which can appear on the P2P connection will
  2188. * be converted to "connected".
  2189. * @return {string|null} ICE state name or <tt>null</tt> if there is no active
  2190. * peer connection at this time.
  2191. */
  2192. JitsiConference.prototype.getConnectionState = function() {
  2193. const peerConnection = this.getActivePeerConnection();
  2194. return peerConnection ? peerConnection.getConnectionState() : null;
  2195. };
  2196. /**
  2197. * Make all new participants mute their audio/video on join.
  2198. * @param policy {Object} object with 2 boolean properties for video and audio:
  2199. * @param {boolean} audio if audio should be muted.
  2200. * @param {boolean} video if video should be muted.
  2201. */
  2202. JitsiConference.prototype.setStartMutedPolicy = function(policy) {
  2203. if (!this.isModerator()) {
  2204. logger.warn(`Failed to set start muted policy, ${this.room ? '' : 'not in a room, '}${
  2205. this.isModerator() ? '' : 'participant is not a moderator'}`);
  2206. return;
  2207. }
  2208. // Do not apply the startMutedPolicy locally on the moderator, the moderator should join with available local
  2209. // sources and the policy needs to be applied only on users that join the call after.
  2210. // this.startMutedPolicy = policy;
  2211. this.room.addOrReplaceInPresence('startmuted', {
  2212. attributes: {
  2213. audio: policy.audio,
  2214. video: policy.video,
  2215. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  2216. }
  2217. }) && this.room.sendPresence();
  2218. };
  2219. /**
  2220. * Returns current start muted policy
  2221. * @returns {Object} with 2 properties - audio and video.
  2222. */
  2223. JitsiConference.prototype.getStartMutedPolicy = function() {
  2224. return this.startMutedPolicy;
  2225. };
  2226. /**
  2227. * Check if audio is muted on join.
  2228. */
  2229. JitsiConference.prototype.isStartAudioMuted = function() {
  2230. return this.startAudioMuted;
  2231. };
  2232. /**
  2233. * Check if video is muted on join.
  2234. */
  2235. JitsiConference.prototype.isStartVideoMuted = function() {
  2236. return this.startVideoMuted;
  2237. };
  2238. /**
  2239. * Returns measured connectionTimes.
  2240. */
  2241. JitsiConference.prototype.getConnectionTimes = function() {
  2242. return this.room.connectionTimes;
  2243. };
  2244. /**
  2245. * Sets a property for the local participant.
  2246. */
  2247. JitsiConference.prototype.setLocalParticipantProperty = function(name, value) {
  2248. this.sendCommand(`jitsi_participant_${name}`, { value });
  2249. };
  2250. /**
  2251. * Removes a property for the local participant and sends the updated presence.
  2252. */
  2253. JitsiConference.prototype.removeLocalParticipantProperty = function(name) {
  2254. this.removeCommand(`jitsi_participant_${name}`);
  2255. this.room.sendPresence();
  2256. };
  2257. /**
  2258. * Gets a local participant property.
  2259. *
  2260. * @return value of the local participant property if the tagName exists in the
  2261. * list of properties, otherwise returns undefined.
  2262. */
  2263. JitsiConference.prototype.getLocalParticipantProperty = function(name) {
  2264. const property = this.room.presMap.nodes.find(prop =>
  2265. prop.tagName === `jitsi_participant_${name}`
  2266. );
  2267. return property ? property.value : undefined;
  2268. };
  2269. /**
  2270. * Sends the given feedback if enabled.
  2271. *
  2272. * @param overallFeedback an integer between 1 and 5 indicating the
  2273. * user feedback
  2274. * @param detailedFeedback detailed feedback from the user. Not yet used
  2275. * @returns {Promise} Resolves if feedback is submitted successfully.
  2276. */
  2277. JitsiConference.prototype.sendFeedback = function(overallFeedback, detailedFeedback) {
  2278. return this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  2279. };
  2280. /**
  2281. * @returns false, since callstats in not supported anymore.
  2282. * @deprecated
  2283. */
  2284. JitsiConference.prototype.isCallstatsEnabled = function() {
  2285. logger.warn('Callstats is no longer supported');
  2286. return false;
  2287. };
  2288. /**
  2289. * Finds the SSRC of a given track
  2290. *
  2291. * @param track
  2292. * @returns {number|undefined} the SSRC of the specificed track, otherwise undefined.
  2293. */
  2294. JitsiConference.prototype.getSsrcByTrack = function(track) {
  2295. return track.isLocal() ? this.getActivePeerConnection()?.getLocalSSRC(track) : track.getSSRC();
  2296. };
  2297. /**
  2298. * This is a no-op since callstats is no longer supported.
  2299. */
  2300. // eslint-disable-next-line no-empty-function
  2301. JitsiConference.prototype.sendApplicationLog = function() { };
  2302. /**
  2303. * Checks if the user identified by given <tt>mucJid</tt> is the conference focus.
  2304. * @param mucJid the full MUC address of the user to be checked.
  2305. * @returns {boolean|null} <tt>true</tt> if MUC user is the conference focus,
  2306. * <tt>false</tt> when is not. <tt>null</tt> if we're not in the MUC anymore and
  2307. * are unable to figure out the status or if given <tt>mucJid</tt> is invalid.
  2308. */
  2309. JitsiConference.prototype.isFocus = function(mucJid) {
  2310. return this.room ? this.room.isFocus(mucJid) : null;
  2311. };
  2312. /**
  2313. * Fires CONFERENCE_FAILED event with INCOMPATIBLE_SERVER_VERSIONS parameter
  2314. */
  2315. JitsiConference.prototype._fireIncompatibleVersionsEvent = function() {
  2316. this.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  2317. JitsiConferenceErrors.INCOMPATIBLE_SERVER_VERSIONS);
  2318. };
  2319. /**
  2320. * Sends a message via the data channel.
  2321. * @param to {string} the id of the endpoint that should receive the message.
  2322. * If "" the message will be sent to all participants.
  2323. * @param payload {object} the payload of the message.
  2324. * @throws NetworkError or InvalidStateError or Error if the operation fails.
  2325. * @deprecated Use 'sendMessage' instead. TODO: this should be private.
  2326. */
  2327. JitsiConference.prototype.sendEndpointMessage = function(to, payload) {
  2328. this.rtc.sendChannelMessage(to, payload);
  2329. };
  2330. /**
  2331. * Sends local stats via the bridge channel which then forwards to other endpoints selectively.
  2332. * @param {Object} payload The payload of the message.
  2333. * @throws NetworkError/InvalidStateError/Error if the operation fails or if there is no data channel created.
  2334. */
  2335. JitsiConference.prototype.sendEndpointStatsMessage = function(payload) {
  2336. this.rtc.sendEndpointStatsMessage(payload);
  2337. };
  2338. /**
  2339. * Sends a broadcast message via the data channel.
  2340. * @param payload {object} the payload of the message.
  2341. * @throws NetworkError or InvalidStateError or Error if the operation fails.
  2342. * @deprecated Use 'sendMessage' instead. TODO: this should be private.
  2343. */
  2344. JitsiConference.prototype.broadcastEndpointMessage = function(payload) {
  2345. this.sendEndpointMessage('', payload);
  2346. };
  2347. /**
  2348. * Sends a message to a given endpoint (if 'to' is a non-empty string), or
  2349. * broadcasts it to all endpoints in the conference.
  2350. * @param {string} to The ID of the endpoint/participant which is to receive
  2351. * the message, or '' to broadcast the message to all endpoints in the
  2352. * conference.
  2353. * @param {string|object} message the message to send. If this is of type
  2354. * 'string' it will be sent as a chat message. If it is of type 'object', it
  2355. * will be encapsulated in a format recognized by jitsi-meet and converted to
  2356. * JSON before being sent.
  2357. * @param {boolean} sendThroughVideobridge Whether to send the message through
  2358. * jitsi-videobridge (via the COLIBRI data channel or web socket), or through
  2359. * the XMPP MUC. Currently only objects can be sent through jitsi-videobridge.
  2360. */
  2361. JitsiConference.prototype.sendMessage = function(message, to = '', sendThroughVideobridge = false) {
  2362. const messageType = typeof message;
  2363. // Through videobridge we support only objects. Through XMPP we support
  2364. // objects (encapsulated in a specific JSON format) and strings (i.e.
  2365. // regular chat messages).
  2366. if (messageType !== 'object'
  2367. && (sendThroughVideobridge || messageType !== 'string')) {
  2368. logger.error(`Can not send a message of type ${messageType}`);
  2369. return;
  2370. }
  2371. if (sendThroughVideobridge) {
  2372. this.sendEndpointMessage(to, message);
  2373. } else {
  2374. let messageToSend = message;
  2375. // Name of packet extension of message stanza to send the required
  2376. // message in.
  2377. let elementName = 'body';
  2378. if (messageType === 'object') {
  2379. elementName = 'json-message';
  2380. // Mark as valid JSON message if not already
  2381. if (!messageToSend.hasOwnProperty(JITSI_MEET_MUC_TYPE)) {
  2382. messageToSend[JITSI_MEET_MUC_TYPE] = '';
  2383. }
  2384. try {
  2385. messageToSend = JSON.stringify(messageToSend);
  2386. } catch (e) {
  2387. logger.error('Can not send a message, stringify failed: ', e);
  2388. return;
  2389. }
  2390. }
  2391. if (to) {
  2392. this.sendPrivateTextMessage(to, messageToSend, elementName);
  2393. } else {
  2394. // Broadcast
  2395. this.sendTextMessage(messageToSend, elementName);
  2396. }
  2397. }
  2398. };
  2399. JitsiConference.prototype.isConnectionInterrupted = function() {
  2400. return this.isP2PActive()
  2401. ? this.isP2PConnectionInterrupted : this.isJvbConnectionInterrupted;
  2402. };
  2403. /**
  2404. * Handles {@link XMPPEvents.CONNECTION_RESTARTED} event. This happens when the bridge goes down
  2405. * and Jicofo moves conferences away to a different bridge.
  2406. * @param {JingleSessionPC} session
  2407. * @private
  2408. */
  2409. JitsiConference.prototype._onConferenceRestarted = function(session) {
  2410. if (!session.isP2P && this.options.config.enableForcedReload) {
  2411. this.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_RESTARTED);
  2412. }
  2413. };
  2414. /**
  2415. * Handles {@link XMPPEvents.CONNECTION_INTERRUPTED}
  2416. * @param {JingleSessionPC} session
  2417. * @private
  2418. */
  2419. JitsiConference.prototype._onIceConnectionInterrupted = function(session) {
  2420. if (session.isP2P) {
  2421. this.isP2PConnectionInterrupted = true;
  2422. } else {
  2423. this.isJvbConnectionInterrupted = true;
  2424. }
  2425. if (session.isP2P === this.isP2PActive()) {
  2426. this.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  2427. }
  2428. };
  2429. /**
  2430. * Handles {@link XMPPEvents.CONNECTION_ICE_FAILED}
  2431. * @param {JingleSessionPC} session
  2432. * @private
  2433. */
  2434. JitsiConference.prototype._onIceConnectionFailed = function(session) {
  2435. // We do nothing for the JVB connection, because it's up to the Jicofo to
  2436. // eventually come up with the new offer (at least for the time being).
  2437. if (session.isP2P) {
  2438. // Add p2pFailed property to analytics to distinguish, between "good"
  2439. // and "bad" connection
  2440. Statistics.analytics.addPermanentProperties({ p2pFailed: true });
  2441. if (this.p2pJingleSession) {
  2442. Statistics.sendAnalyticsAndLog(
  2443. createP2PEvent(
  2444. ACTION_P2P_FAILED,
  2445. {
  2446. initiator: this.p2pJingleSession.isInitiator
  2447. }));
  2448. }
  2449. this._stopP2PSession({
  2450. reason: 'connectivity-error',
  2451. reasonDescription: 'ICE FAILED'
  2452. });
  2453. } else if (session && this.jvbJingleSession === session) {
  2454. this._delayedIceFailed = new IceFailedHandling(this);
  2455. this._delayedIceFailed.start(session);
  2456. }
  2457. };
  2458. /**
  2459. * Handles {@link XMPPEvents.CONNECTION_RESTORED}
  2460. * @param {JingleSessionPC} session
  2461. * @private
  2462. */
  2463. JitsiConference.prototype._onIceConnectionRestored = function(session) {
  2464. if (session.isP2P) {
  2465. this.isP2PConnectionInterrupted = false;
  2466. } else {
  2467. this.isJvbConnectionInterrupted = false;
  2468. this._delayedIceFailed && this._delayedIceFailed.cancel();
  2469. }
  2470. if (session.isP2P === this.isP2PActive()) {
  2471. this.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  2472. }
  2473. };
  2474. /**
  2475. * Accept incoming P2P Jingle call.
  2476. * @param {JingleSessionPC} jingleSession the session instance
  2477. * @param {jQuery} jingleOffer a jQuery selector pointing to 'jingle' IQ element
  2478. * @private
  2479. */
  2480. JitsiConference.prototype._acceptP2PIncomingCall = function(jingleSession, jingleOffer) {
  2481. this.isP2PConnectionInterrupted = false;
  2482. // Accept the offer
  2483. this.p2pJingleSession = jingleSession;
  2484. this._sendConferenceJoinAnalyticsEvent();
  2485. this.p2pJingleSession.initialize(
  2486. this.room,
  2487. this.rtc,
  2488. this._signalingLayer,
  2489. {
  2490. ...this.options.config,
  2491. codecSettings: {
  2492. mediaType: MediaType.VIDEO,
  2493. codecList: this.codecSelection.getCodecPreferenceList('p2p')
  2494. },
  2495. enableInsertableStreams: this.isE2EEEnabled() || FeatureFlags.isRunInLiteModeEnabled()
  2496. });
  2497. const localTracks = this.getLocalTracks();
  2498. this.p2pJingleSession.acceptOffer(
  2499. jingleOffer,
  2500. () => {
  2501. logger.debug('Got RESULT for P2P "session-accept"');
  2502. this.eventEmitter.emit(
  2503. JitsiConferenceEvents._MEDIA_SESSION_STARTED,
  2504. jingleSession);
  2505. },
  2506. error => {
  2507. logger.error(
  2508. 'Failed to accept incoming P2P Jingle session', error);
  2509. },
  2510. localTracks);
  2511. };
  2512. /**
  2513. * Adds remote tracks to the conference associated with the JVB session.
  2514. * @private
  2515. */
  2516. JitsiConference.prototype._addRemoteJVBTracks = function() {
  2517. this._addRemoteTracks('JVB', this.jvbJingleSession.peerconnection.getRemoteTracks());
  2518. };
  2519. /**
  2520. * Adds remote tracks to the conference associated with the P2P session.
  2521. * @private
  2522. */
  2523. JitsiConference.prototype._addRemoteP2PTracks = function() {
  2524. this._addRemoteTracks('P2P', this.p2pJingleSession.peerconnection.getRemoteTracks());
  2525. };
  2526. /**
  2527. * Generates fake "remote track added" events for given Jingle session.
  2528. * @param {string} logName the session's nickname which will appear in log messages.
  2529. * @param {Array<JitsiRemoteTrack>} remoteTracks the tracks that will be added
  2530. * @private
  2531. */
  2532. JitsiConference.prototype._addRemoteTracks = function(logName, remoteTracks) {
  2533. for (const track of remoteTracks) {
  2534. if (this.participants.has(track.ownerEndpointId)) {
  2535. logger.info(`Adding remote ${logName} track: ${track}`);
  2536. this.onRemoteTrackAdded(track);
  2537. }
  2538. }
  2539. };
  2540. /**
  2541. * Called when {@link XMPPEvents.CONNECTION_ESTABLISHED} event is
  2542. * triggered for a {@link JingleSessionPC}. Switches the conference to use
  2543. * the P2P connection if the event comes from the P2P session.
  2544. * @param {JingleSessionPC} jingleSession the session instance.
  2545. * @private
  2546. */
  2547. JitsiConference.prototype._onIceConnectionEstablished = function(jingleSession) {
  2548. if (this.p2pJingleSession !== null) {
  2549. // store the establishment time of the p2p session as a field of the
  2550. // JitsiConference because the p2pJingleSession might get disposed (thus
  2551. // the value is lost).
  2552. this.p2pEstablishmentDuration
  2553. = this.p2pJingleSession.establishmentDuration;
  2554. }
  2555. if (this.jvbJingleSession !== null) {
  2556. this.jvbEstablishmentDuration
  2557. = this.jvbJingleSession.establishmentDuration;
  2558. }
  2559. let done = false;
  2560. // We don't care about the JVB case, there's nothing to be done
  2561. if (!jingleSession.isP2P) {
  2562. done = true;
  2563. } else if (this.p2pJingleSession !== jingleSession) {
  2564. logger.error('CONNECTION_ESTABLISHED - wrong P2P session instance ?!');
  2565. done = true;
  2566. }
  2567. if (!isNaN(this.p2pEstablishmentDuration)
  2568. && !isNaN(this.jvbEstablishmentDuration)) {
  2569. const establishmentDurationDiff
  2570. = this.p2pEstablishmentDuration - this.jvbEstablishmentDuration;
  2571. Statistics.sendAnalytics(
  2572. ICE_ESTABLISHMENT_DURATION_DIFF,
  2573. { value: establishmentDurationDiff });
  2574. }
  2575. if (jingleSession.isP2P === this.isP2PActive()) {
  2576. this.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_ESTABLISHED);
  2577. }
  2578. if (done) {
  2579. return;
  2580. }
  2581. // Update P2P status and emit events
  2582. this._setP2PStatus(true);
  2583. // Remove remote tracks
  2584. if (this.jvbJingleSession) {
  2585. this._removeRemoteJVBTracks();
  2586. } else {
  2587. logger.info('Not removing remote JVB tracks - no session yet');
  2588. }
  2589. this._addRemoteP2PTracks();
  2590. // Stop media transfer over the JVB connection
  2591. if (this.jvbJingleSession) {
  2592. this._suspendMediaTransferForJvbConnection();
  2593. }
  2594. logger.info('Starting remote stats with p2p connection');
  2595. this.statistics.startRemoteStats(this.p2pJingleSession.peerconnection);
  2596. Statistics.sendAnalyticsAndLog(
  2597. createP2PEvent(
  2598. ACTION_P2P_ESTABLISHED,
  2599. {
  2600. initiator: this.p2pJingleSession.isInitiator
  2601. }));
  2602. };
  2603. /**
  2604. * Called when the chat room reads a new list of properties from jicofo's
  2605. * presence. The properties may have changed, but they don't have to.
  2606. *
  2607. * @param {Object} properties - The properties keyed by the property name
  2608. * ('key').
  2609. * @private
  2610. */
  2611. JitsiConference.prototype._updateProperties = function(properties = {}) {
  2612. const changed = !isEqual(properties, this.properties);
  2613. this.properties = properties;
  2614. if (changed) {
  2615. this.eventEmitter.emit(JitsiConferenceEvents.PROPERTIES_CHANGED, this.properties);
  2616. const audioLimitReached = this.properties['audio-limit-reached'] === 'true';
  2617. const videoLimitReached = this.properties['video-limit-reached'] === 'true';
  2618. if (this._audioSenderLimitReached !== audioLimitReached) {
  2619. this._audioSenderLimitReached = audioLimitReached;
  2620. this.eventEmitter.emit(JitsiConferenceEvents.AUDIO_UNMUTE_PERMISSIONS_CHANGED, audioLimitReached);
  2621. logger.info(`Audio unmute permissions set by Jicofo to ${audioLimitReached}`);
  2622. }
  2623. if (this._videoSenderLimitReached !== videoLimitReached) {
  2624. this._videoSenderLimitReached = videoLimitReached;
  2625. this.eventEmitter.emit(JitsiConferenceEvents.VIDEO_UNMUTE_PERMISSIONS_CHANGED, videoLimitReached);
  2626. logger.info(`Video unmute permissions set by Jicofo to ${videoLimitReached}`);
  2627. }
  2628. // Some of the properties need to be added to analytics events.
  2629. const analyticsKeys = [
  2630. // The number of jitsi-videobridge instances currently used for the
  2631. // conference.
  2632. 'bridge-count'
  2633. ];
  2634. analyticsKeys.forEach(key => {
  2635. if (properties[key] !== undefined) {
  2636. Statistics.analytics.addPermanentProperties({
  2637. [key.replace('-', '_')]: properties[key]
  2638. });
  2639. }
  2640. });
  2641. // Handle changes to aggregate list of visitor codecs.
  2642. let publishedCodecs = this.properties['visitor-codecs']?.split(',');
  2643. if (publishedCodecs?.length) {
  2644. publishedCodecs = publishedCodecs.filter(codec => typeof codec === 'string'
  2645. && codec.trim().length
  2646. && Object.values(CodecMimeType).find(val => val === codec));
  2647. }
  2648. if (this._visitorCodecs !== publishedCodecs) {
  2649. this._visitorCodecs = publishedCodecs;
  2650. this.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_VISITOR_CODECS_CHANGED, this._visitorCodecs);
  2651. }
  2652. const oldValue = this._hasVisitors;
  2653. this._hasVisitors = this.properties['visitor-count'] > 0;
  2654. oldValue !== this._hasVisitors && this._maybeStartOrStopP2P(true);
  2655. }
  2656. };
  2657. /**
  2658. * Gets a conference property with a given key.
  2659. *
  2660. * @param {string} key - The key.
  2661. * @returns {*} The value
  2662. */
  2663. JitsiConference.prototype.getProperty = function(key) {
  2664. return this.properties[key];
  2665. };
  2666. /**
  2667. * Clears the deferred start P2P task if it has been scheduled.
  2668. * @private
  2669. */
  2670. JitsiConference.prototype._maybeClearDeferredStartP2P = function() {
  2671. if (this.deferredStartP2PTask) {
  2672. logger.info('Cleared deferred start P2P task');
  2673. clearTimeout(this.deferredStartP2PTask);
  2674. this.deferredStartP2PTask = null;
  2675. }
  2676. };
  2677. /**
  2678. * Removes from the conference remote tracks associated with the JVB
  2679. * connection.
  2680. * @private
  2681. */
  2682. JitsiConference.prototype._removeRemoteJVBTracks = function() {
  2683. this._removeRemoteTracks(
  2684. 'JVB', this.jvbJingleSession.peerconnection.getRemoteTracks());
  2685. };
  2686. /**
  2687. * Removes from the conference remote tracks associated with the P2P
  2688. * connection.
  2689. * @private
  2690. */
  2691. JitsiConference.prototype._removeRemoteP2PTracks = function() {
  2692. this._removeRemoteTracks(
  2693. 'P2P', this.p2pJingleSession.peerconnection.getRemoteTracks());
  2694. };
  2695. /**
  2696. * Generates fake "remote track removed" events for given Jingle session.
  2697. * @param {string} sessionNickname the session's nickname which will appear in
  2698. * log messages.
  2699. * @param {Array<JitsiRemoteTrack>} remoteTracks the tracks that will be removed
  2700. * @private
  2701. */
  2702. JitsiConference.prototype._removeRemoteTracks = function(sessionNickname, remoteTracks) {
  2703. for (const track of remoteTracks) {
  2704. logger.info(`Removing remote ${sessionNickname} track: ${track}`);
  2705. this.onRemoteTrackRemoved(track);
  2706. }
  2707. };
  2708. /**
  2709. * Resumes media transfer over the JVB connection.
  2710. * @private
  2711. */
  2712. JitsiConference.prototype._resumeMediaTransferForJvbConnection = function() {
  2713. logger.info('Resuming media transfer over the JVB connection...');
  2714. this.jvbJingleSession.setMediaTransferActive(true)
  2715. .then(() => {
  2716. logger.info('Resumed media transfer over the JVB connection!');
  2717. })
  2718. .catch(error => {
  2719. logger.error('Failed to resume media transfer over the JVB connection:', error);
  2720. });
  2721. };
  2722. /**
  2723. * Sets new P2P status and updates some events/states hijacked from
  2724. * the <tt>JitsiConference</tt>.
  2725. * @param {boolean} newStatus the new P2P status value, <tt>true</tt> means that
  2726. * P2P is now in use, <tt>false</tt> means that the JVB connection is now in use
  2727. * @private
  2728. */
  2729. JitsiConference.prototype._setP2PStatus = function(newStatus) {
  2730. if (this.p2p === newStatus) {
  2731. logger.debug(`Called _setP2PStatus with the same status: ${newStatus}`);
  2732. return;
  2733. }
  2734. this.p2p = newStatus;
  2735. if (newStatus) {
  2736. logger.info('Peer to peer connection established!');
  2737. // When we end up in a valid P2P session need to reset the properties
  2738. // in case they have persisted, after session with another peer.
  2739. Statistics.analytics.addPermanentProperties({
  2740. p2pFailed: false
  2741. });
  2742. // Sync up video transfer active in case p2pJingleSession not existed
  2743. // when the lastN value was being adjusted.
  2744. const isVideoActive = this.getLastN() !== 0;
  2745. this.p2pJingleSession.setP2pVideoTransferActive(isVideoActive)
  2746. .catch(error => {
  2747. logger.error(`Failed to sync up P2P video transfer status (${isVideoActive}), ${error}`);
  2748. });
  2749. } else {
  2750. logger.info('Peer to peer connection closed!');
  2751. }
  2752. // Clear dtmfManager, so that it can be recreated with new connection
  2753. this.dtmfManager = null;
  2754. // Update P2P status
  2755. this.eventEmitter.emit(
  2756. JitsiConferenceEvents.P2P_STATUS,
  2757. this,
  2758. this.p2p);
  2759. this.eventEmitter.emit(JitsiConferenceEvents._MEDIA_SESSION_ACTIVE_CHANGED, this.getActiveMediaSession());
  2760. // Refresh connection interrupted/restored
  2761. this.eventEmitter.emit(
  2762. this.isConnectionInterrupted()
  2763. ? JitsiConferenceEvents.CONNECTION_INTERRUPTED
  2764. : JitsiConferenceEvents.CONNECTION_RESTORED);
  2765. };
  2766. /**
  2767. * Starts new P2P session.
  2768. * @param {string} remoteJid the JID of the remote participant
  2769. * @private
  2770. */
  2771. JitsiConference.prototype._startP2PSession = function(remoteJid) {
  2772. this._maybeClearDeferredStartP2P();
  2773. if (this.p2pJingleSession) {
  2774. logger.error('P2P session already started!');
  2775. return;
  2776. }
  2777. this.isP2PConnectionInterrupted = false;
  2778. this.p2pJingleSession
  2779. = this.xmpp.connection.jingle.newP2PJingleSession(
  2780. this.room.myroomjid,
  2781. remoteJid);
  2782. logger.info(
  2783. 'Created new P2P JingleSession', this.room.myroomjid, remoteJid);
  2784. this._sendConferenceJoinAnalyticsEvent();
  2785. this.p2pJingleSession.initialize(
  2786. this.room,
  2787. this.rtc,
  2788. this._signalingLayer,
  2789. {
  2790. ...this.options.config,
  2791. codecSettings: {
  2792. mediaType: MediaType.VIDEO,
  2793. codecList: this.codecSelection.getCodecPreferenceList('p2p')
  2794. },
  2795. enableInsertableStreams: this.isE2EEEnabled() || FeatureFlags.isRunInLiteModeEnabled()
  2796. });
  2797. const localTracks = this.getLocalTracks();
  2798. this.p2pJingleSession.invite(localTracks);
  2799. };
  2800. /**
  2801. * Suspends media transfer over the JVB connection.
  2802. * @private
  2803. */
  2804. JitsiConference.prototype._suspendMediaTransferForJvbConnection = function() {
  2805. logger.info('Suspending media transfer over the JVB connection...');
  2806. this.jvbJingleSession.setMediaTransferActive(false)
  2807. .then(() => {
  2808. logger.info('Suspended media transfer over the JVB connection !');
  2809. })
  2810. .catch(error => {
  2811. logger.error('Failed to suspend media transfer over the JVB connection:', error);
  2812. });
  2813. };
  2814. /**
  2815. * Method when called will decide whether it's the time to start or stop
  2816. * the P2P session.
  2817. * @param {boolean} userLeftEvent if <tt>true</tt> it means that the call
  2818. * originates from the user left event.
  2819. * @private
  2820. */
  2821. JitsiConference.prototype._maybeStartOrStopP2P = function(userLeftEvent) {
  2822. if (!this.isP2PEnabled()
  2823. || this.isP2PTestModeEnabled()
  2824. || (browser.isFirefox() && !this._firefoxP2pEnabled)
  2825. || this.isE2EEEnabled()) {
  2826. logger.info('Auto P2P disabled');
  2827. return;
  2828. }
  2829. const peers = this.getParticipants();
  2830. const peerCount = peers.length;
  2831. // FIXME 1 peer and it must *support* P2P switching
  2832. const shouldBeInP2P = this._shouldBeInP2PMode();
  2833. // Clear deferred "start P2P" task
  2834. if (!shouldBeInP2P && this.deferredStartP2PTask) {
  2835. this._maybeClearDeferredStartP2P();
  2836. }
  2837. // Start peer to peer session
  2838. if (!this.p2pJingleSession && shouldBeInP2P) {
  2839. const peer = peerCount && peers[0];
  2840. const myId = this.myUserId();
  2841. const peersId = peer.getId();
  2842. if (myId > peersId) {
  2843. logger.debug(
  2844. 'I\'m the bigger peersId - '
  2845. + 'the other peer should start P2P', myId, peersId);
  2846. return;
  2847. } else if (myId === peersId) {
  2848. logger.error('The same IDs ? ', myId, peersId);
  2849. return;
  2850. }
  2851. const jid = peer.getJid();
  2852. if (userLeftEvent) {
  2853. if (this.deferredStartP2PTask) {
  2854. logger.error('Deferred start P2P task\'s been set already!');
  2855. return;
  2856. }
  2857. logger.info(
  2858. `Will start P2P with: ${jid} after ${
  2859. this.backToP2PDelay} seconds...`);
  2860. this.deferredStartP2PTask = setTimeout(
  2861. this._startP2PSession.bind(this, jid),
  2862. this.backToP2PDelay * 1000);
  2863. } else {
  2864. logger.info(`Will start P2P with: ${jid}`);
  2865. this._startP2PSession(jid);
  2866. }
  2867. } else if (this.p2pJingleSession && !shouldBeInP2P) {
  2868. logger.info(`Will stop P2P with: ${this.p2pJingleSession.remoteJid}`);
  2869. // Log that there will be a switch back to the JVB connection
  2870. if (this.p2pJingleSession.isInitiator && peerCount > 1) {
  2871. Statistics.sendAnalyticsAndLog(
  2872. createP2PEvent(ACTION_P2P_SWITCH_TO_JVB));
  2873. }
  2874. this._stopP2PSession();
  2875. }
  2876. };
  2877. /**
  2878. * Tells whether or not this conference should be currently in the P2P mode.
  2879. *
  2880. * @private
  2881. * @returns {boolean}
  2882. */
  2883. JitsiConference.prototype._shouldBeInP2PMode = function() {
  2884. const peers = this.getParticipants();
  2885. const peerCount = peers.length;
  2886. const hasBotPeer = peers.find(p => p.getBotType() === 'poltergeist' || p.hasFeature(FEATURE_JIGASI)) !== undefined;
  2887. const shouldBeInP2P = peerCount === 1 && !hasBotPeer && !this._hasVisitors;
  2888. logger.debug(`P2P? peerCount: ${peerCount}, hasBotPeer: ${hasBotPeer} => ${shouldBeInP2P}`);
  2889. return shouldBeInP2P;
  2890. };
  2891. /**
  2892. * Stops the current JVB jingle session.
  2893. *
  2894. * @param {Object} options - options for stopping JVB session.
  2895. * @param {string} options.reason - One of the Jingle "reason" element
  2896. * names as defined by https://xmpp.org/extensions/xep-0166.html#def-reason
  2897. * @param {string} options.reasonDescription - Text description that will be included in the session terminate message.
  2898. * @param {boolean} options.requestRestart - Whether this is due to a session restart, in which case, session will be
  2899. * set to null.
  2900. * @param {boolean} options.sendSessionTerminate - Whether session-terminate needs to be sent to Jicofo.
  2901. */
  2902. JitsiConference.prototype._stopJvbSession = function(options = {}) {
  2903. const {
  2904. requestRestart = false,
  2905. sendSessionTerminate = false
  2906. } = options;
  2907. if (!this.jvbJingleSession) {
  2908. logger.error('No JVB session to be stopped');
  2909. return;
  2910. }
  2911. // Remove remote JVB tracks.
  2912. !this.isP2PActive() && this._removeRemoteJVBTracks();
  2913. logger.info('Stopping stats for jvb connection');
  2914. this.statistics.stopRemoteStats(this.jvbJingleSession.peerconnection);
  2915. this.jvbJingleSession.terminate(
  2916. () => {
  2917. if (requestRestart && sendSessionTerminate) {
  2918. logger.info('session-terminate for ice restart - done');
  2919. }
  2920. this.jvbJingleSession = null;
  2921. },
  2922. error => {
  2923. if (requestRestart && sendSessionTerminate) {
  2924. logger.error('session-terminate for ice restart failed: reloading the client');
  2925. // Initiate a client reload if Jicofo responds to the session-terminate with an error.
  2926. this.eventEmitter.emit(
  2927. JitsiConferenceEvents.CONFERENCE_FAILED,
  2928. JitsiConferenceErrors.ICE_FAILED);
  2929. }
  2930. logger.error(`An error occurred while trying to terminate the JVB session', reason=${error.reason},`
  2931. + `msg=${error.msg}`);
  2932. },
  2933. options);
  2934. };
  2935. /**
  2936. * Stops the current P2P session.
  2937. * @param {Object} options - Options for stopping P2P.
  2938. * @param {string} options.reason - One of the Jingle "reason" element
  2939. * names as defined by https://xmpp.org/extensions/xep-0166.html#def-reason
  2940. * @param {string} options.reasonDescription - Text description that will be included in the session terminate message.
  2941. * @param {boolean} requestRestart - Whether this is due to a session restart, in which case
  2942. * media will not be resumed on the JVB.
  2943. * @private
  2944. */
  2945. JitsiConference.prototype._stopP2PSession = function(options = {}) {
  2946. const {
  2947. reason = 'success',
  2948. reasonDescription = 'Turning off P2P session',
  2949. requestRestart = false
  2950. } = options;
  2951. if (!this.p2pJingleSession) {
  2952. logger.error('No P2P session to be stopped!');
  2953. return;
  2954. }
  2955. const wasP2PEstablished = this.isP2PActive();
  2956. // Swap remote tracks, but only if the P2P has been fully established
  2957. if (wasP2PEstablished) {
  2958. if (this.jvbJingleSession && !requestRestart) {
  2959. this._resumeMediaTransferForJvbConnection();
  2960. }
  2961. // Remove remote P2P tracks
  2962. this._removeRemoteP2PTracks();
  2963. }
  2964. // Stop P2P stats
  2965. logger.info('Stopping remote stats for P2P connection');
  2966. this.statistics.stopRemoteStats(this.p2pJingleSession.peerconnection);
  2967. this.p2pJingleSession.terminate(
  2968. () => {
  2969. logger.info('P2P session terminate RESULT');
  2970. this.p2pJingleSession = null;
  2971. },
  2972. error => {
  2973. // Because both initiator and responder are simultaneously
  2974. // terminating their JingleSessions in case of the 'to JVB switch'
  2975. // when 3rd participant joins, both will dispose their sessions and
  2976. // reply with 'item-not-found' (see strophe.jingle.js). We don't
  2977. // want to log this as an error since it's expected behaviour.
  2978. //
  2979. // We want them both to terminate, because in case of initiator's
  2980. // crash the responder would stay in P2P mode until ICE fails which
  2981. // could take up to 20 seconds.
  2982. //
  2983. // NOTE: whilst this is an error callback, 'success' as a reason is
  2984. // considered as graceful session terminate
  2985. // where both initiator and responder terminate their sessions
  2986. // simultaneously.
  2987. if (reason !== 'success') {
  2988. logger.error('An error occurred while trying to terminate P2P Jingle session', error);
  2989. }
  2990. }, {
  2991. reason,
  2992. reasonDescription,
  2993. sendSessionTerminate: this.room
  2994. && this.getParticipantById(
  2995. Strophe.getResourceFromJid(this.p2pJingleSession.remoteJid))
  2996. });
  2997. this.p2pJingleSession = null;
  2998. // Update P2P status and other affected events/states
  2999. this._setP2PStatus(false);
  3000. if (wasP2PEstablished) {
  3001. // Add back remote JVB tracks
  3002. if (this.jvbJingleSession && !requestRestart) {
  3003. this._addRemoteJVBTracks();
  3004. } else {
  3005. logger.info('Not adding remote JVB tracks - no session yet');
  3006. }
  3007. }
  3008. };
  3009. /**
  3010. * Updates room presence if needed and send the packet in case of a modification.
  3011. * @param {JingleSessionPC} jingleSession the session firing the event, contains the peer connection which
  3012. * tracks we will check.
  3013. * @param {Object|null} ctx a context object we can distinguish multiple calls of the same pass of updating tracks.
  3014. */
  3015. JitsiConference.prototype._updateRoomPresence = function(jingleSession, ctx) {
  3016. if (!jingleSession) {
  3017. return;
  3018. }
  3019. // skips sending presence twice for the same pass of updating ssrcs
  3020. if (ctx) {
  3021. if (ctx.skip) {
  3022. return;
  3023. }
  3024. ctx.skip = true;
  3025. }
  3026. let presenceChanged = false;
  3027. let muteStatusChanged, videoTypeChanged;
  3028. const localTracks = jingleSession.peerconnection.getLocalTracks();
  3029. // Set presence for all the available local tracks.
  3030. for (const track of localTracks) {
  3031. const muted = track.isMuted();
  3032. muteStatusChanged = this._setTrackMuteStatus(track.getType(), track, muted);
  3033. muteStatusChanged && logger.debug(`Updating mute state of ${track} in presence to muted=${muted}`);
  3034. if (track.getType() === MediaType.VIDEO) {
  3035. videoTypeChanged = this._setNewVideoType(track);
  3036. videoTypeChanged && logger.debug(`Updating videoType in presence to ${track.getVideoType()}`);
  3037. }
  3038. presenceChanged = presenceChanged || muteStatusChanged || videoTypeChanged;
  3039. }
  3040. presenceChanged && this.room.sendPresence();
  3041. };
  3042. /**
  3043. * Checks whether or not the conference is currently in the peer to peer mode.
  3044. * Being in peer to peer mode means that the direct connection has been
  3045. * established and the P2P connection is being used for media transmission.
  3046. * @return {boolean} <tt>true</tt> if in P2P mode or <tt>false</tt> otherwise.
  3047. */
  3048. JitsiConference.prototype.isP2PActive = function() {
  3049. return this.p2p;
  3050. };
  3051. /**
  3052. * Returns the current ICE state of the P2P connection.
  3053. * NOTE: method is used by the jitsi-meet-torture tests.
  3054. * @return {string|null} an ICE state or <tt>null</tt> if there's currently
  3055. * no P2P connection.
  3056. */
  3057. JitsiConference.prototype.getP2PConnectionState = function() {
  3058. if (this.isP2PActive()) {
  3059. return this.p2pJingleSession.peerconnection.getConnectionState();
  3060. }
  3061. return null;
  3062. };
  3063. /**
  3064. * Configures the peerconnection so that a given framre rate can be achieved for desktop share.
  3065. *
  3066. * @param {number} maxFps The capture framerate to be used for desktop tracks.
  3067. * @returns {boolean} true if the operation is successful, false otherwise.
  3068. */
  3069. JitsiConference.prototype.setDesktopSharingFrameRate = function(maxFps) {
  3070. if (typeof maxFps !== 'number' || isNaN(maxFps)) {
  3071. logger.error(`Invalid value ${maxFps} specified for desktop capture frame rate`);
  3072. return false;
  3073. }
  3074. this._desktopSharingFrameRate = maxFps;
  3075. // Set capture fps for screenshare.
  3076. this.jvbJingleSession && this.jvbJingleSession.peerconnection.setDesktopSharingFrameRate(maxFps);
  3077. // Set the capture rate for desktop sharing.
  3078. this.rtc.setDesktopSharingFrameRate(maxFps);
  3079. return true;
  3080. };
  3081. /**
  3082. * Manually starts new P2P session (should be used only in the tests).
  3083. */
  3084. JitsiConference.prototype.startP2PSession = function() {
  3085. const peers = this.getParticipants();
  3086. // Start peer to peer session
  3087. if (peers.length === 1) {
  3088. const peerJid = peers[0].getJid();
  3089. this._startP2PSession(peerJid);
  3090. } else {
  3091. throw new Error(
  3092. 'There must be exactly 1 participant to start the P2P session !');
  3093. }
  3094. };
  3095. /**
  3096. * Manually stops the current P2P session (should be used only in the tests).
  3097. */
  3098. JitsiConference.prototype.stopP2PSession = function(options) {
  3099. this._stopP2PSession(options);
  3100. };
  3101. /**
  3102. * Get a summary of how long current participants have been the dominant speaker
  3103. * @returns {object}
  3104. */
  3105. JitsiConference.prototype.getSpeakerStats = function() {
  3106. return this.speakerStatsCollector.getStats();
  3107. };
  3108. /**
  3109. * Sends a face landmarks object to the xmpp server.
  3110. * @param {Object} payload
  3111. */
  3112. JitsiConference.prototype.sendFaceLandmarks = function(payload) {
  3113. if (payload.faceExpression) {
  3114. this.xmpp.sendFaceLandmarksEvent(this.room.roomjid, payload);
  3115. }
  3116. };
  3117. /**
  3118. * Sets the constraints for the video that is requested from the bridge.
  3119. *
  3120. * @param {Object} videoConstraints The constraints which are specified in the following format. The message updates
  3121. * the fields that are present and leaves the rest unchanged on the bridge. Therefore, any field that is not applicable
  3122. * anymore should be cleared by passing an empty object or list (whatever is applicable).
  3123. * {
  3124. * 'lastN': 20,
  3125. * 'selectedSources': ['A', 'B', 'C'],
  3126. * 'onStageSources': ['A'],
  3127. * 'defaultConstraints': { 'maxHeight': 180 },
  3128. * 'constraints': {
  3129. * 'A': { 'maxHeight': 720 }
  3130. * }
  3131. * }
  3132. * Where A, B and C are source-names of the remote tracks that are being requested from the bridge.
  3133. */
  3134. JitsiConference.prototype.setReceiverConstraints = function(videoConstraints) {
  3135. this.receiveVideoController.setReceiverConstraints(videoConstraints);
  3136. };
  3137. /**
  3138. * Sets the assumed bandwidth bps for the video that is requested from the bridge.
  3139. *
  3140. * @param {Number} assumedBandwidthBps - The bandwidth value expressed in bits per second.
  3141. */
  3142. JitsiConference.prototype.setAssumedBandwidthBps = function(assumedBandwidthBps) {
  3143. this.receiveVideoController.setAssumedBandwidthBps(assumedBandwidthBps);
  3144. };
  3145. /**
  3146. * Sets the maximum video size the local participant should receive from remote
  3147. * participants.
  3148. *
  3149. * @param {number} maxFrameHeight - the maximum frame height, in pixels,
  3150. * this receiver is willing to receive.
  3151. * @returns {void}
  3152. */
  3153. JitsiConference.prototype.setReceiverVideoConstraint = function(maxFrameHeight) {
  3154. this.receiveVideoController.setPreferredReceiveMaxFrameHeight(maxFrameHeight);
  3155. };
  3156. /**
  3157. * Sets the maximum video size the local participant should send to remote
  3158. * participants.
  3159. * @param {number} maxFrameHeight - The user preferred max frame height.
  3160. * @returns {Promise} promise that will be resolved when the operation is
  3161. * successful and rejected otherwise.
  3162. */
  3163. JitsiConference.prototype.setSenderVideoConstraint = function(maxFrameHeight) {
  3164. return this.sendVideoController.setPreferredSendMaxFrameHeight(maxFrameHeight);
  3165. };
  3166. /**
  3167. * Creates a video SIP GW session and returns it if service is enabled. Before
  3168. * creating a session one need to check whether video SIP GW service is
  3169. * available in the system {@link JitsiConference.isVideoSIPGWAvailable}. Even
  3170. * if there are available nodes to serve this request, after creating the
  3171. * session those nodes can be taken and the request about using the
  3172. * created session can fail.
  3173. *
  3174. * @param {string} sipAddress - The sip address to be used.
  3175. * @param {string} displayName - The display name to be used for this session.
  3176. * @returns {JitsiVideoSIPGWSession|Error} Returns null if conference is not
  3177. * initialised and there is no room.
  3178. */
  3179. JitsiConference.prototype.createVideoSIPGWSession = function(sipAddress, displayName) {
  3180. if (!this.room) {
  3181. return new Error(VideoSIPGWConstants.ERROR_NO_CONNECTION);
  3182. }
  3183. return this.videoSIPGWHandler
  3184. .createVideoSIPGWSession(sipAddress, displayName);
  3185. };
  3186. /**
  3187. * Sends a conference.join analytics event.
  3188. *
  3189. * @returns {void}
  3190. */
  3191. JitsiConference.prototype._sendConferenceJoinAnalyticsEvent = function() {
  3192. const meetingId = this.getMeetingUniqueId();
  3193. if (this._conferenceJoinAnalyticsEventSent || !meetingId || this.getActivePeerConnection() === null) {
  3194. return;
  3195. }
  3196. const conferenceConnectionTimes = this.getConnectionTimes();
  3197. const xmppConnectionTimes = this.connection.getConnectionTimes();
  3198. const gumStart = window.connectionTimes['firstObtainPermissions.start'];
  3199. const gumEnd = window.connectionTimes['firstObtainPermissions.end'];
  3200. const globalNSConnectionTimes = window.JitsiMeetJS?.app?.connectionTimes ?? {};
  3201. const connectionTimes = {
  3202. ...conferenceConnectionTimes,
  3203. ...xmppConnectionTimes,
  3204. ...globalNSConnectionTimes,
  3205. gumDuration: safeSubtract(gumEnd, gumStart),
  3206. xmppConnectingTime: safeSubtract(xmppConnectionTimes.connected, xmppConnectionTimes.connecting),
  3207. connectedToMUCJoinedTime: safeSubtract(
  3208. conferenceConnectionTimes['muc.joined'], xmppConnectionTimes.connected),
  3209. connectingToMUCJoinedTime: safeSubtract(
  3210. conferenceConnectionTimes['muc.joined'], xmppConnectionTimes.connecting),
  3211. numberOfParticipantsOnJoin: this._numberOfParticipantsOnJoin
  3212. };
  3213. Statistics.sendAnalytics(createConferenceEvent('joined', {
  3214. ...connectionTimes,
  3215. meetingId,
  3216. participantId: `${meetingId}.${this._statsCurrentId}`
  3217. }));
  3218. this._conferenceJoinAnalyticsEventSent = Date.now();
  3219. };
  3220. /**
  3221. * Sends conference.left analytics event.
  3222. * @private
  3223. */
  3224. JitsiConference.prototype._sendConferenceLeftAnalyticsEvent = function() {
  3225. const meetingId = this.getMeetingUniqueId();
  3226. if (!meetingId || !this._conferenceJoinAnalyticsEventSent) {
  3227. return;
  3228. }
  3229. Statistics.sendAnalytics(createConferenceEvent('left', {
  3230. meetingId,
  3231. participantId: `${meetingId}.${this._statsCurrentId}`,
  3232. stats: {
  3233. duration: Math.floor((Date.now() - this._conferenceJoinAnalyticsEventSent) / 1000),
  3234. perf: this.getPerformanceStats()
  3235. }
  3236. }));
  3237. };
  3238. /**
  3239. * Restarts all active media sessions.
  3240. *
  3241. * @returns {void}
  3242. */
  3243. JitsiConference.prototype._restartMediaSessions = function() {
  3244. if (this.p2pJingleSession) {
  3245. this._stopP2PSession({
  3246. reasonDescription: 'restart',
  3247. requestRestart: true
  3248. });
  3249. }
  3250. if (this.jvbJingleSession) {
  3251. this._stopJvbSession({
  3252. reason: 'success',
  3253. reasonDescription: 'restart required',
  3254. requestRestart: true,
  3255. sendSessionTerminate: true
  3256. });
  3257. }
  3258. this._maybeStartOrStopP2P(false);
  3259. };
  3260. /**
  3261. * Returns whether End-To-End encryption is enabled.
  3262. *
  3263. * @returns {boolean}
  3264. */
  3265. JitsiConference.prototype.isE2EEEnabled = function() {
  3266. return Boolean(this._e2eEncryption && this._e2eEncryption.isEnabled());
  3267. };
  3268. /**
  3269. * Returns whether End-To-End encryption is supported. Note that not all participants
  3270. * in the conference may support it.
  3271. *
  3272. * @returns {boolean}
  3273. */
  3274. JitsiConference.prototype.isE2EESupported = function() {
  3275. return E2EEncryption.isSupported(this.options.config);
  3276. };
  3277. /**
  3278. * Enables / disables End-to-End encryption.
  3279. *
  3280. * @param {boolean} enabled whether to enable E2EE or not.
  3281. * @returns {void}
  3282. */
  3283. JitsiConference.prototype.toggleE2EE = function(enabled) {
  3284. if (!this.isE2EESupported()) {
  3285. logger.warn('Cannot enable / disable E2EE: platform is not supported.');
  3286. return;
  3287. }
  3288. this._e2eEncryption.setEnabled(enabled);
  3289. };
  3290. /**
  3291. * Sets the key and index for End-to-End encryption.
  3292. *
  3293. * @param {CryptoKey} [keyInfo.encryptionKey] - encryption key.
  3294. * @param {Number} [keyInfo.index] - the index of the encryption key.
  3295. * @returns {void}
  3296. */
  3297. JitsiConference.prototype.setMediaEncryptionKey = function(keyInfo) {
  3298. this._e2eEncryption.setEncryptionKey(keyInfo);
  3299. };
  3300. /**
  3301. * Starts the participant verification process.
  3302. *
  3303. * @param {string} participantId The participant which will be marked as verified.
  3304. * @returns {void}
  3305. */
  3306. JitsiConference.prototype.startVerification = function(participantId) {
  3307. const participant = this.getParticipantById(participantId);
  3308. if (!participant) {
  3309. return;
  3310. }
  3311. this._e2eEncryption.startVerification(participant);
  3312. };
  3313. /**
  3314. * Marks the given participant as verified. After this is done, MAC verification will
  3315. * be performed and an event will be emitted with the result.
  3316. *
  3317. * @param {string} participantId The participant which will be marked as verified.
  3318. * @param {boolean} isVerified - whether the verification was succesfull.
  3319. * @returns {void}
  3320. */
  3321. JitsiConference.prototype.markParticipantVerified = function(participantId, isVerified) {
  3322. const participant = this.getParticipantById(participantId);
  3323. if (!participant) {
  3324. return;
  3325. }
  3326. this._e2eEncryption.markParticipantVerified(participant, isVerified);
  3327. };
  3328. /**
  3329. * Returns <tt>true</tt> if lobby support is enabled in the backend.
  3330. *
  3331. * @returns {boolean} whether lobby is supported in the backend.
  3332. */
  3333. JitsiConference.prototype.isLobbySupported = function() {
  3334. return Boolean(this.room && this.room.getLobby().isSupported());
  3335. };
  3336. /**
  3337. * Returns <tt>true</tt> if the room has members only enabled.
  3338. *
  3339. * @returns {boolean} whether conference room is members only.
  3340. */
  3341. JitsiConference.prototype.isMembersOnly = function() {
  3342. return Boolean(this.room && this.room.membersOnlyEnabled);
  3343. };
  3344. /**
  3345. * Returns <tt>true</tt> if the room supports visitors feature.
  3346. *
  3347. * @returns {boolean} whether conference room has visitors support.
  3348. */
  3349. JitsiConference.prototype.isVisitorsSupported = function() {
  3350. return Boolean(this.room && this.room.visitorsSupported);
  3351. };
  3352. /**
  3353. * Enables lobby by moderators
  3354. *
  3355. * @returns {Promise} resolves when lobby room is joined or rejects with the error.
  3356. */
  3357. JitsiConference.prototype.enableLobby = function() {
  3358. if (this.room && this.isModerator()) {
  3359. return this.room.getLobby().enable();
  3360. }
  3361. return Promise.reject(
  3362. new Error('The conference not started or user is not moderator'));
  3363. };
  3364. /**
  3365. * Disabled lobby by moderators
  3366. *
  3367. * @returns {void}
  3368. */
  3369. JitsiConference.prototype.disableLobby = function() {
  3370. if (this.room && this.isModerator()) {
  3371. this.room.getLobby().disable();
  3372. } else {
  3373. logger.warn(`Failed to disable lobby, ${this.room ? '' : 'not in a room, '}${
  3374. this.isModerator() ? '' : 'participant is not a moderator'}`);
  3375. }
  3376. };
  3377. /**
  3378. * Joins the lobby room with display name and optional email or with a shared password to skip waiting.
  3379. *
  3380. * @param {string} displayName Display name should be set to show it to moderators.
  3381. * @param {string} email Optional email is used to present avatar to the moderator.
  3382. * @returns {Promise<never>}
  3383. */
  3384. JitsiConference.prototype.joinLobby = function(displayName, email) {
  3385. if (this.room) {
  3386. return this.room.getLobby().join(displayName, email);
  3387. }
  3388. return Promise.reject(new Error('The conference not started'));
  3389. };
  3390. /**
  3391. * Gets the local id for a participant in a lobby room.
  3392. * Returns undefined when current participant is not in the lobby room.
  3393. * This is used for lobby room private chat messages.
  3394. *
  3395. * @returns {string}
  3396. */
  3397. JitsiConference.prototype.myLobbyUserId = function() {
  3398. if (this.room) {
  3399. return this.room.getLobby().getLocalId();
  3400. }
  3401. };
  3402. /**
  3403. * Sends a message to a lobby room.
  3404. * When id is specified it sends a private message.
  3405. * Otherwise it sends the message to all moderators.
  3406. * @param {message} Object The message to send
  3407. * @param {string} id The participant id.
  3408. *
  3409. * @returns {void}
  3410. */
  3411. JitsiConference.prototype.sendLobbyMessage = function(message, id) {
  3412. if (this.room) {
  3413. if (id) {
  3414. return this.room.getLobby().sendPrivateMessage(id, message);
  3415. }
  3416. return this.room.getLobby().sendMessage(message);
  3417. }
  3418. };
  3419. /**
  3420. * Adds a message listener to the lobby room
  3421. * @param {Function} listener The listener function,
  3422. * called when a new message is received in the lobby room.
  3423. *
  3424. * @returns {Function} Handler returned to be able to remove it later.
  3425. */
  3426. JitsiConference.prototype.addLobbyMessageListener = function(listener) {
  3427. if (this.room) {
  3428. return this.room.getLobby().addMessageListener(listener);
  3429. }
  3430. };
  3431. /**
  3432. * Removes a message handler from the lobby room
  3433. * @param {Function} handler The handler function to remove.
  3434. *
  3435. * @returns {void}
  3436. */
  3437. JitsiConference.prototype.removeLobbyMessageHandler = function(handler) {
  3438. if (this.room) {
  3439. return this.room.getLobby().removeMessageHandler(handler);
  3440. }
  3441. };
  3442. /**
  3443. * Denies an occupant in the lobby room access to the conference.
  3444. * @param {string} id The participant id.
  3445. */
  3446. JitsiConference.prototype.lobbyDenyAccess = function(id) {
  3447. if (this.room) {
  3448. this.room.getLobby().denyAccess(id);
  3449. }
  3450. };
  3451. /**
  3452. * Approves the request to join the conference to a participant waiting in the lobby.
  3453. *
  3454. * @param {string|Array<string>} param The participant id or an array of ids.
  3455. */
  3456. JitsiConference.prototype.lobbyApproveAccess = function(param) {
  3457. if (this.room) {
  3458. this.room.getLobby().approveAccess(param);
  3459. }
  3460. };
  3461. /**
  3462. * Returns <tt>true</tt> if AV Moderation support is enabled in the backend.
  3463. *
  3464. * @returns {boolean} whether AV Moderation is supported in the backend.
  3465. */
  3466. JitsiConference.prototype.isAVModerationSupported = function() {
  3467. return Boolean(this.room && this.room.getAVModeration().isSupported());
  3468. };
  3469. /**
  3470. * Enables AV Moderation.
  3471. * @param {MediaType} mediaType "audio" or "video"
  3472. */
  3473. JitsiConference.prototype.enableAVModeration = function(mediaType) {
  3474. if (this.room && this.isModerator()
  3475. && (mediaType === MediaType.AUDIO || mediaType === MediaType.VIDEO)) {
  3476. this.room.getAVModeration().enable(true, mediaType);
  3477. } else {
  3478. logger.warn(`Failed to enable AV moderation, ${this.room ? '' : 'not in a room, '}${
  3479. this.isModerator() ? '' : 'participant is not a moderator, '}${
  3480. this.room && this.isModerator() ? 'wrong media type passed' : ''}`);
  3481. }
  3482. };
  3483. /**
  3484. * Disables AV Moderation.
  3485. * @param {MediaType} mediaType "audio" or "video"
  3486. */
  3487. JitsiConference.prototype.disableAVModeration = function(mediaType) {
  3488. if (this.room && this.isModerator()
  3489. && (mediaType === MediaType.AUDIO || mediaType === MediaType.VIDEO)) {
  3490. this.room.getAVModeration().enable(false, mediaType);
  3491. } else {
  3492. logger.warn(`Failed to disable AV moderation, ${this.room ? '' : 'not in a room, '}${
  3493. this.isModerator() ? '' : 'participant is not a moderator, '}${
  3494. this.room && this.isModerator() ? 'wrong media type passed' : ''}`);
  3495. }
  3496. };
  3497. /**
  3498. * Approve participant access to certain media, allows unmuting audio or video.
  3499. *
  3500. * @param {MediaType} mediaType "audio" or "video"
  3501. * @param id the id of the participant.
  3502. */
  3503. JitsiConference.prototype.avModerationApprove = function(mediaType, id) {
  3504. if (this.room && this.isModerator()
  3505. && (mediaType === MediaType.AUDIO || mediaType === MediaType.VIDEO)) {
  3506. const participant = this.getParticipantById(id);
  3507. if (!participant) {
  3508. return;
  3509. }
  3510. this.room.getAVModeration().approve(mediaType, participant.getJid());
  3511. } else {
  3512. logger.warn(`AV moderation approve skipped , ${this.room ? '' : 'not in a room, '}${
  3513. this.isModerator() ? '' : 'participant is not a moderator, '}${
  3514. this.room && this.isModerator() ? 'wrong media type passed' : ''}`);
  3515. }
  3516. };
  3517. /**
  3518. * Reject participant access to certain media, blocks unmuting audio or video.
  3519. *
  3520. * @param {MediaType} mediaType "audio" or "video"
  3521. * @param id the id of the participant.
  3522. */
  3523. JitsiConference.prototype.avModerationReject = function(mediaType, id) {
  3524. if (this.room && this.isModerator()
  3525. && (mediaType === MediaType.AUDIO || mediaType === MediaType.VIDEO)) {
  3526. const participant = this.getParticipantById(id);
  3527. if (!participant) {
  3528. return;
  3529. }
  3530. this.room.getAVModeration().reject(mediaType, participant.getJid());
  3531. } else {
  3532. logger.warn(`AV moderation reject skipped , ${this.room ? '' : 'not in a room, '}${
  3533. this.isModerator() ? '' : 'participant is not a moderator, '}${
  3534. this.room && this.isModerator() ? 'wrong media type passed' : ''}`);
  3535. }
  3536. };
  3537. /**
  3538. * Returns the breakout rooms manager object.
  3539. *
  3540. * @returns {Object} the breakout rooms manager.
  3541. */
  3542. JitsiConference.prototype.getBreakoutRooms = function() {
  3543. return this.room?.getBreakoutRooms();
  3544. };
  3545. /**
  3546. * Returns the metadata handler object.
  3547. *
  3548. * @returns {Object} the room metadata handler.
  3549. */
  3550. JitsiConference.prototype.getMetadataHandler = function() {
  3551. return this.room?.getMetadataHandler();
  3552. };