您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JitsiConference.js 49KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. /* global Strophe, $, Promise */
  2. /* jshint -W101 */
  3. var logger = require("jitsi-meet-logger").getLogger(__filename);
  4. var RTC = require("./modules/RTC/RTC");
  5. var XMPPEvents = require("./service/xmpp/XMPPEvents");
  6. var AuthenticationEvents = require("./service/authentication/AuthenticationEvents");
  7. var RTCEvents = require("./service/RTC/RTCEvents");
  8. var EventEmitter = require("events");
  9. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  10. var JitsiConferenceErrors = require("./JitsiConferenceErrors");
  11. var JitsiParticipant = require("./JitsiParticipant");
  12. var Statistics = require("./modules/statistics/statistics");
  13. var JitsiDTMFManager = require('./modules/DTMF/JitsiDTMFManager');
  14. var JitsiTrackEvents = require("./JitsiTrackEvents");
  15. var JitsiTrackErrors = require("./JitsiTrackErrors");
  16. var JitsiTrackError = require("./JitsiTrackError");
  17. var Settings = require("./modules/settings/Settings");
  18. var ComponentsVersions = require("./modules/version/ComponentsVersions");
  19. var GlobalOnErrorHandler = require("./modules/util/GlobalOnErrorHandler");
  20. /**
  21. * Creates a JitsiConference object with the given name and properties.
  22. * Note: this constructor is not a part of the public API (objects should be
  23. * created using JitsiConnection.createConference).
  24. * @param options.config properties / settings related to the conference that will be created.
  25. * @param options.name the name of the conference
  26. * @param options.connection the JitsiConnection object for this JitsiConference.
  27. * @constructor
  28. */
  29. function JitsiConference(options) {
  30. if(!options.name || options.name.toLowerCase() !== options.name) {
  31. var errmsg
  32. = "Invalid conference name (no conference name passed or it "
  33. + "contains invalid characters like capital letters)!";
  34. logger.error(errmsg);
  35. throw new Error(errmsg);
  36. }
  37. this.options = options;
  38. this.connection = this.options.connection;
  39. this.xmpp = this.connection.xmpp;
  40. this.eventEmitter = new EventEmitter();
  41. var confID = this.options.name + '@' + this.xmpp.options.hosts.muc;
  42. this.settings = new Settings();
  43. this.room = this.xmpp.createRoom(this.options.name, this.options.config,
  44. this.settings);
  45. this.componentsVersions = new ComponentsVersions(this.room);
  46. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  47. this.rtc = new RTC(this.room, options);
  48. this.statistics = new Statistics(this.xmpp, {
  49. callStatsID: this.options.config.callStatsID,
  50. callStatsSecret: this.options.config.callStatsSecret,
  51. disableThirdPartyRequests:
  52. this.options.config.disableThirdPartyRequests,
  53. roomName: this.options.name
  54. });
  55. setupListeners(this);
  56. this.participants = {};
  57. this.lastDominantSpeaker = null;
  58. this.dtmfManager = null;
  59. this.somebodySupportsDTMF = false;
  60. this.authEnabled = false;
  61. this.authIdentity;
  62. this.startAudioMuted = false;
  63. this.startVideoMuted = false;
  64. this.startMutedPolicy = {audio: false, video: false};
  65. this.availableDevices = {
  66. audio: undefined,
  67. video: undefined
  68. };
  69. this.isMutedByFocus = false;
  70. this.reportedAudioSSRCs = {};
  71. }
  72. /**
  73. * Joins the conference.
  74. * @param password {string} the password
  75. */
  76. JitsiConference.prototype.join = function (password) {
  77. if(this.room)
  78. this.room.join(password);
  79. };
  80. /**
  81. * Check if joined to the conference.
  82. */
  83. JitsiConference.prototype.isJoined = function () {
  84. return this.room && this.room.joined;
  85. };
  86. /**
  87. * Leaves the conference and calls onMemberLeft for every participant.
  88. */
  89. JitsiConference.prototype._leaveRoomAndRemoveParticipants = function () {
  90. // leave the conference
  91. if (this.room) {
  92. this.room.leave();
  93. }
  94. this.room = null;
  95. // remove all participants
  96. this.getParticipants().forEach(function (participant) {
  97. this.onMemberLeft(participant.getJid());
  98. }.bind(this));
  99. this.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  100. }
  101. /**
  102. * Leaves the conference.
  103. * @returns {Promise}
  104. */
  105. JitsiConference.prototype.leave = function () {
  106. var conference = this;
  107. return Promise.all(
  108. conference.getLocalTracks().map(function (track) {
  109. return conference.removeTrack(track);
  110. })
  111. ).then(this._leaveRoomAndRemoveParticipants.bind(this))
  112. .catch(function (error) {
  113. logger.error(error);
  114. GlobalOnErrorHandler.callUnhandledRejectionHandler(
  115. {promise: this, reason: error});
  116. // We are proceeding with leaving the conference because room.leave may
  117. // succeed.
  118. this._leaveRoomAndRemoveParticipants();
  119. return Promise.resolve();
  120. }.bind(this));
  121. };
  122. /**
  123. * Returns name of this conference.
  124. */
  125. JitsiConference.prototype.getName = function () {
  126. return this.options.name;
  127. };
  128. /**
  129. * Check if authentication is enabled for this conference.
  130. */
  131. JitsiConference.prototype.isAuthEnabled = function () {
  132. return this.authEnabled;
  133. };
  134. /**
  135. * Check if user is logged in.
  136. */
  137. JitsiConference.prototype.isLoggedIn = function () {
  138. return !!this.authIdentity;
  139. };
  140. /**
  141. * Get authorized login.
  142. */
  143. JitsiConference.prototype.getAuthLogin = function () {
  144. return this.authIdentity;
  145. };
  146. /**
  147. * Check if external authentication is enabled for this conference.
  148. */
  149. JitsiConference.prototype.isExternalAuthEnabled = function () {
  150. return this.room && this.room.moderator.isExternalAuthEnabled();
  151. };
  152. /**
  153. * Get url for external authentication.
  154. * @param {boolean} [urlForPopup] if true then return url for login popup,
  155. * else url of login page.
  156. * @returns {Promise}
  157. */
  158. JitsiConference.prototype.getExternalAuthUrl = function (urlForPopup) {
  159. return new Promise(function (resolve, reject) {
  160. if (!this.isExternalAuthEnabled()) {
  161. reject();
  162. return;
  163. }
  164. if (urlForPopup) {
  165. this.room.moderator.getPopupLoginUrl(resolve, reject);
  166. } else {
  167. this.room.moderator.getLoginUrl(resolve, reject);
  168. }
  169. }.bind(this));
  170. };
  171. /**
  172. * Returns the local tracks.
  173. */
  174. JitsiConference.prototype.getLocalTracks = function () {
  175. if (this.rtc) {
  176. return this.rtc.localTracks.slice();
  177. } else {
  178. return [];
  179. }
  180. };
  181. /**
  182. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  183. * in JitsiConferenceEvents.
  184. * @param eventId the event ID.
  185. * @param handler handler for the event.
  186. *
  187. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  188. */
  189. JitsiConference.prototype.on = function (eventId, handler) {
  190. if(this.eventEmitter)
  191. this.eventEmitter.on(eventId, handler);
  192. };
  193. /**
  194. * Removes event listener
  195. * @param eventId the event ID.
  196. * @param [handler] optional, the specific handler to unbind
  197. *
  198. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  199. */
  200. JitsiConference.prototype.off = function (eventId, handler) {
  201. if(this.eventEmitter)
  202. this.eventEmitter.removeListener(eventId, handler);
  203. };
  204. // Common aliases for event emitter
  205. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on;
  206. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off;
  207. /**
  208. * Receives notifications from other participants about commands / custom events
  209. * (sent by sendCommand or sendCommandOnce methods).
  210. * @param command {String} the name of the command
  211. * @param handler {Function} handler for the command
  212. */
  213. JitsiConference.prototype.addCommandListener = function (command, handler) {
  214. if(this.room)
  215. this.room.addPresenceListener(command, handler);
  216. };
  217. /**
  218. * Removes command listener
  219. * @param command {String} the name of the command
  220. */
  221. JitsiConference.prototype.removeCommandListener = function (command) {
  222. if(this.room)
  223. this.room.removePresenceListener(command);
  224. };
  225. /**
  226. * Sends text message to the other participants in the conference
  227. * @param message the text message.
  228. */
  229. JitsiConference.prototype.sendTextMessage = function (message) {
  230. if(this.room)
  231. this.room.sendMessage(message);
  232. };
  233. /**
  234. * Send presence command.
  235. * @param name {String} the name of the command.
  236. * @param values {Object} with keys and values that will be sent.
  237. **/
  238. JitsiConference.prototype.sendCommand = function (name, values) {
  239. if(this.room) {
  240. this.room.addToPresence(name, values);
  241. this.room.sendPresence();
  242. }
  243. };
  244. /**
  245. * Send presence command one time.
  246. * @param name {String} the name of the command.
  247. * @param values {Object} with keys and values that will be sent.
  248. **/
  249. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  250. this.sendCommand(name, values);
  251. this.removeCommand(name);
  252. };
  253. /**
  254. * Removes presence command.
  255. * @param name {String} the name of the command.
  256. **/
  257. JitsiConference.prototype.removeCommand = function (name) {
  258. if(this.room)
  259. this.room.removeFromPresence(name);
  260. };
  261. /**
  262. * Sets the display name for this conference.
  263. * @param name the display name to set
  264. */
  265. JitsiConference.prototype.setDisplayName = function(name) {
  266. if(this.room){
  267. // remove previously set nickname
  268. this.room.removeFromPresence("nick");
  269. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  270. this.room.sendPresence();
  271. }
  272. };
  273. /**
  274. * Set new subject for this conference. (available only for moderator)
  275. * @param {string} subject new subject
  276. */
  277. JitsiConference.prototype.setSubject = function (subject) {
  278. if (this.room && this.isModerator()) {
  279. this.room.setSubject(subject);
  280. }
  281. };
  282. /**
  283. * Adds JitsiLocalTrack object to the conference.
  284. * @param track the JitsiLocalTrack object.
  285. * @returns {Promise<JitsiLocalTrack>}
  286. * @throws {Error} if the specified track is a video track and there is already
  287. * another video track in the conference.
  288. */
  289. JitsiConference.prototype.addTrack = function (track) {
  290. if (track.disposed) {
  291. return Promise.reject(
  292. new JitsiTrackError(JitsiTrackErrors.TRACK_IS_DISPOSED));
  293. }
  294. if (track.isVideoTrack()) {
  295. // Ensure there's exactly 1 local video track in the conference.
  296. var localVideoTrack = this.rtc.getLocalVideoTrack();
  297. if (localVideoTrack) {
  298. // Don't be excessively harsh and severe if the API client happens
  299. // to attempt to add the same local video track twice.
  300. if (track === localVideoTrack) {
  301. return Promise.resolve(track);
  302. } else {
  303. return Promise.reject(new Error(
  304. "cannot add second video track to the conference"));
  305. }
  306. }
  307. }
  308. track.ssrcHandler = function (conference, ssrcMap) {
  309. if(ssrcMap[this.getMSID()]){
  310. this._setSSRC(ssrcMap[this.getMSID()]);
  311. conference.room.removeListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  312. this.ssrcHandler);
  313. }
  314. }.bind(track, this);
  315. this.room.addListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  316. track.ssrcHandler);
  317. if(track.isAudioTrack() || (track.isVideoTrack() &&
  318. track.videoType !== "desktop")) {
  319. // Report active device to statistics
  320. var devices = RTC.getCurrentlyAvailableMediaDevices();
  321. device = devices.find(function (d) {
  322. return d.kind === track.getTrack().kind + 'input'
  323. && d.label === track.getTrack().label;
  324. });
  325. if(device)
  326. Statistics.sendActiveDeviceListEvent(
  327. RTC.getEventDataForActiveDevice(device));
  328. }
  329. return new Promise(function (resolve, reject) {
  330. this.room.addStream(track.getOriginalStream(), function () {
  331. if (track.isVideoTrack()) {
  332. this.removeCommand("videoType");
  333. this.sendCommand("videoType", {
  334. value: track.videoType,
  335. attributes: {
  336. xmlns: 'http://jitsi.org/jitmeet/video'
  337. }
  338. });
  339. }
  340. this.rtc.addLocalTrack(track);
  341. if (track.startMuted) {
  342. track.mute();
  343. }
  344. // ensure that we're sharing proper "is muted" state
  345. if (track.isAudioTrack()) {
  346. this.room.setAudioMute(track.isMuted());
  347. } else {
  348. this.room.setVideoMute(track.isMuted());
  349. }
  350. track.muteHandler = this._fireMuteChangeEvent.bind(this, track);
  351. track.audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  352. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  353. track.muteHandler);
  354. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  355. track.audioLevelHandler);
  356. //FIXME: This dependacy is not necessary. This is quick fix.
  357. track._setConference(this);
  358. // send event for starting screen sharing
  359. // FIXME: we assume we have only one screen sharing track
  360. // if we change this we need to fix this check
  361. if (track.isVideoTrack() && track.videoType === "desktop")
  362. this.statistics.sendScreenSharingEvent(true);
  363. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  364. resolve(track);
  365. }.bind(this), function (error) {
  366. reject(error);
  367. });
  368. }.bind(this));
  369. };
  370. /**
  371. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  372. * @param audioLevel the audio level
  373. */
  374. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  375. this.eventEmitter.emit(
  376. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  377. this.myUserId(), audioLevel);
  378. };
  379. /**
  380. * Fires TRACK_MUTE_CHANGED change conference event.
  381. * @param track the JitsiTrack object related to the event.
  382. */
  383. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  384. // check if track was muted by focus and now is unmuted by user
  385. if (this.isMutedByFocus && track.isAudioTrack() && !track.isMuted()) {
  386. this.isMutedByFocus = false;
  387. // unmute local user on server
  388. this.room.muteParticipant(this.room.myroomjid, false);
  389. }
  390. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  391. };
  392. /**
  393. * Removes JitsiLocalTrack object to the conference.
  394. * @param track the JitsiLocalTrack object.
  395. * @returns {Promise}
  396. */
  397. JitsiConference.prototype.removeTrack = function (track) {
  398. if (track.disposed) {
  399. return Promise.reject(
  400. new JitsiTrackError(JitsiTrackErrors.TRACK_IS_DISPOSED));
  401. }
  402. if(!this.room){
  403. if(this.rtc) {
  404. this.rtc.removeLocalTrack(track);
  405. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  406. }
  407. return Promise.resolve();
  408. }
  409. return new Promise(function (resolve, reject) {
  410. this.room.removeStream(track.getOriginalStream(), function(){
  411. track._setSSRC(null);
  412. track._setConference(null);
  413. this.rtc.removeLocalTrack(track);
  414. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  415. track.muteHandler);
  416. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  417. track.audioLevelHandler);
  418. this.room.removeListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  419. track.ssrcHandler);
  420. // send event for stopping screen sharing
  421. // FIXME: we assume we have only one screen sharing track
  422. // if we change this we need to fix this check
  423. if (track.isVideoTrack() && track.videoType === "desktop")
  424. this.statistics.sendScreenSharingEvent(false);
  425. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  426. resolve();
  427. }.bind(this), function (error) {
  428. reject(error);
  429. }, {
  430. mtype: track.getType(),
  431. type: "remove",
  432. ssrc: track.ssrc});
  433. }.bind(this));
  434. };
  435. /**
  436. * Get role of the local user.
  437. * @returns {string} user role: 'moderator' or 'none'
  438. */
  439. JitsiConference.prototype.getRole = function () {
  440. return this.room.role;
  441. };
  442. /**
  443. * Check if local user is moderator.
  444. * @returns {boolean} true if local user is moderator, false otherwise.
  445. */
  446. JitsiConference.prototype.isModerator = function () {
  447. return this.room.isModerator();
  448. };
  449. /**
  450. * Set password for the room.
  451. * @param {string} password new password for the room.
  452. * @returns {Promise}
  453. */
  454. JitsiConference.prototype.lock = function (password) {
  455. if (!this.isModerator()) {
  456. return Promise.reject();
  457. }
  458. var conference = this;
  459. return new Promise(function (resolve, reject) {
  460. conference.room.lockRoom(password || "", function () {
  461. resolve();
  462. }, function (err) {
  463. reject(err);
  464. }, function () {
  465. reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED);
  466. });
  467. });
  468. };
  469. /**
  470. * Remove password from the room.
  471. * @returns {Promise}
  472. */
  473. JitsiConference.prototype.unlock = function () {
  474. return this.lock();
  475. };
  476. /**
  477. * Elects the participant with the given id to be the selected participant or the speaker.
  478. * @param id the identifier of the participant
  479. */
  480. JitsiConference.prototype.selectParticipant = function(participantId) {
  481. if (this.rtc) {
  482. this.rtc.selectedEndpoint(participantId);
  483. }
  484. };
  485. /**
  486. *
  487. * @param id the identifier of the participant
  488. */
  489. JitsiConference.prototype.pinParticipant = function(participantId) {
  490. if (this.rtc) {
  491. this.rtc.pinEndpoint(participantId);
  492. }
  493. };
  494. /**
  495. * Returns the list of participants for this conference.
  496. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  497. */
  498. JitsiConference.prototype.getParticipants = function() {
  499. return Object.keys(this.participants).map(function (key) {
  500. return this.participants[key];
  501. }, this);
  502. };
  503. /**
  504. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  505. * undefined if there isn't one).
  506. * @param id the id of the participant.
  507. */
  508. JitsiConference.prototype.getParticipantById = function(id) {
  509. return this.participants[id];
  510. };
  511. /**
  512. * Kick participant from this conference.
  513. * @param {string} id id of the participant to kick
  514. */
  515. JitsiConference.prototype.kickParticipant = function (id) {
  516. var participant = this.getParticipantById(id);
  517. if (!participant) {
  518. return;
  519. }
  520. this.room.kick(participant.getJid());
  521. };
  522. /**
  523. * Kick participant from this conference.
  524. * @param {string} id id of the participant to kick
  525. */
  526. JitsiConference.prototype.muteParticipant = function (id) {
  527. var participant = this.getParticipantById(id);
  528. if (!participant) {
  529. return;
  530. }
  531. this.room.muteParticipant(participant.getJid(), true);
  532. };
  533. /**
  534. * Indicates that a participant has joined the conference.
  535. *
  536. * @param jid the jid of the participant in the MUC
  537. * @param nick the display name of the participant
  538. * @param role the role of the participant in the MUC
  539. * @param isHidden indicates if this is a hidden participant (sysem participant,
  540. * for example a recorder).
  541. */
  542. JitsiConference.prototype.onMemberJoined
  543. = function (jid, nick, role, isHidden) {
  544. var id = Strophe.getResourceFromJid(jid);
  545. if (id === 'focus' || this.myUserId() === id) {
  546. return;
  547. }
  548. var participant = new JitsiParticipant(jid, this, nick, isHidden);
  549. participant._role = role;
  550. this.participants[id] = participant;
  551. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  552. // XXX Since disco is checked in multiple places (e.g.
  553. // modules/xmpp/strophe.jingle.js, modules/xmpp/strophe.rayo.js), check it
  554. // here as well.
  555. var disco = this.xmpp.connection.disco;
  556. if (disco) {
  557. disco.info(
  558. jid, "node", function(iq) {
  559. participant._supportsDTMF = $(iq).find(
  560. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  561. this.updateDTMFSupport();
  562. }.bind(this)
  563. );
  564. } else {
  565. // FIXME Should participant._supportsDTMF be assigned false here (and
  566. // this.updateDTMFSupport invoked)?
  567. }
  568. };
  569. JitsiConference.prototype.onMemberLeft = function (jid) {
  570. var id = Strophe.getResourceFromJid(jid);
  571. if (id === 'focus' || this.myUserId() === id) {
  572. return;
  573. }
  574. var participant = this.participants[id];
  575. delete this.participants[id];
  576. this.rtc.removeRemoteTracks(id);
  577. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  578. };
  579. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  580. var id = Strophe.getResourceFromJid(jid);
  581. var participant = this.getParticipantById(id);
  582. if (!participant) {
  583. return;
  584. }
  585. participant._role = role;
  586. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  587. };
  588. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  589. var id = Strophe.getResourceFromJid(jid);
  590. var participant = this.getParticipantById(id);
  591. if (!participant) {
  592. return;
  593. }
  594. if (participant._displayName === displayName)
  595. return;
  596. participant._displayName = displayName;
  597. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  598. };
  599. /**
  600. * Notifies this JitsiConference that a JitsiRemoteTrack was added (into the
  601. * ChatRoom of this JitsiConference).
  602. *
  603. * @param {JitsiRemoteTrack} track the JitsiRemoteTrack which was added to this
  604. * JitsiConference
  605. */
  606. JitsiConference.prototype.onTrackAdded = function (track) {
  607. var id = track.getParticipantId();
  608. var participant = this.getParticipantById(id);
  609. if (!participant) {
  610. return;
  611. }
  612. // Add track to JitsiParticipant.
  613. participant._tracks.push(track);
  614. var emitter = this.eventEmitter;
  615. track.addEventListener(
  616. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  617. function () {
  618. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  619. }
  620. );
  621. track.addEventListener(
  622. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  623. function (audioLevel) {
  624. emitter.emit(
  625. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  626. id,
  627. audioLevel);
  628. }
  629. );
  630. emitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  631. };
  632. JitsiConference.prototype.updateDTMFSupport = function () {
  633. var somebodySupportsDTMF = false;
  634. var participants = this.getParticipants();
  635. // check if at least 1 participant supports DTMF
  636. for (var i = 0; i < participants.length; i += 1) {
  637. if (participants[i].supportsDTMF()) {
  638. somebodySupportsDTMF = true;
  639. break;
  640. }
  641. }
  642. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  643. this.somebodySupportsDTMF = somebodySupportsDTMF;
  644. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  645. }
  646. };
  647. /**
  648. * Allows to check if there is at least one user in the conference
  649. * that supports DTMF.
  650. * @returns {boolean} true if somebody supports DTMF, false otherwise
  651. */
  652. JitsiConference.prototype.isDTMFSupported = function () {
  653. return this.somebodySupportsDTMF;
  654. };
  655. /**
  656. * Returns the local user's ID
  657. * @return {string} local user's ID
  658. */
  659. JitsiConference.prototype.myUserId = function () {
  660. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  661. };
  662. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  663. if (!this.dtmfManager) {
  664. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  665. if (!connection) {
  666. logger.warn("cannot sendTones: no conneciton");
  667. return;
  668. }
  669. var tracks = this.getLocalTracks().filter(function (track) {
  670. return track.isAudioTrack();
  671. });
  672. if (!tracks.length) {
  673. logger.warn("cannot sendTones: no local audio stream");
  674. return;
  675. }
  676. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  677. }
  678. this.dtmfManager.sendTones(tones, duration, pause);
  679. };
  680. /**
  681. * Returns true if the recording is supproted and false if not.
  682. */
  683. JitsiConference.prototype.isRecordingSupported = function () {
  684. if(this.room)
  685. return this.room.isRecordingSupported();
  686. return false;
  687. };
  688. /**
  689. * Returns null if the recording is not supported, "on" if the recording started
  690. * and "off" if the recording is not started.
  691. */
  692. JitsiConference.prototype.getRecordingState = function () {
  693. return (this.room) ? this.room.getRecordingState() : undefined;
  694. }
  695. /**
  696. * Returns the url of the recorded video.
  697. */
  698. JitsiConference.prototype.getRecordingURL = function () {
  699. return (this.room) ? this.room.getRecordingURL() : null;
  700. }
  701. /**
  702. * Starts/stops the recording
  703. */
  704. JitsiConference.prototype.toggleRecording = function (options) {
  705. if(this.room)
  706. return this.room.toggleRecording(options, function (status, error) {
  707. this.eventEmitter.emit(
  708. JitsiConferenceEvents.RECORDER_STATE_CHANGED, status, error);
  709. }.bind(this));
  710. this.eventEmitter.emit(
  711. JitsiConferenceEvents.RECORDER_STATE_CHANGED, "error",
  712. new Error("The conference is not created yet!"));
  713. }
  714. /**
  715. * Returns true if the SIP calls are supported and false otherwise
  716. */
  717. JitsiConference.prototype.isSIPCallingSupported = function () {
  718. if(this.room)
  719. return this.room.isSIPCallingSupported();
  720. return false;
  721. }
  722. /**
  723. * Dials a number.
  724. * @param number the number
  725. */
  726. JitsiConference.prototype.dial = function (number) {
  727. if(this.room)
  728. return this.room.dial(number);
  729. return new Promise(function(resolve, reject){
  730. reject(new Error("The conference is not created yet!"))});
  731. }
  732. /**
  733. * Hangup an existing call
  734. */
  735. JitsiConference.prototype.hangup = function () {
  736. if(this.room)
  737. return this.room.hangup();
  738. return new Promise(function(resolve, reject){
  739. reject(new Error("The conference is not created yet!"))});
  740. }
  741. /**
  742. * Returns the phone number for joining the conference.
  743. */
  744. JitsiConference.prototype.getPhoneNumber = function () {
  745. if(this.room)
  746. return this.room.getPhoneNumber();
  747. return null;
  748. }
  749. /**
  750. * Returns the pin for joining the conference with phone.
  751. */
  752. JitsiConference.prototype.getPhonePin = function () {
  753. if(this.room)
  754. return this.room.getPhonePin();
  755. return null;
  756. }
  757. /**
  758. * Returns the connection state for the current room. Its ice connection state
  759. * for its session.
  760. */
  761. JitsiConference.prototype.getConnectionState = function () {
  762. if(this.room)
  763. return this.room.getConnectionState();
  764. return null;
  765. }
  766. /**
  767. * Make all new participants mute their audio/video on join.
  768. * @param policy {Object} object with 2 boolean properties for video and audio:
  769. * @param {boolean} audio if audio should be muted.
  770. * @param {boolean} video if video should be muted.
  771. */
  772. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  773. if (!this.isModerator()) {
  774. return;
  775. }
  776. this.startMutedPolicy = policy;
  777. this.room.removeFromPresence("startmuted");
  778. this.room.addToPresence("startmuted", {
  779. attributes: {
  780. audio: policy.audio,
  781. video: policy.video,
  782. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  783. }
  784. });
  785. this.room.sendPresence();
  786. };
  787. /**
  788. * Returns current start muted policy
  789. * @returns {Object} with 2 proprties - audio and video.
  790. */
  791. JitsiConference.prototype.getStartMutedPolicy = function () {
  792. return this.startMutedPolicy;
  793. };
  794. /**
  795. * Check if audio is muted on join.
  796. */
  797. JitsiConference.prototype.isStartAudioMuted = function () {
  798. return this.startAudioMuted;
  799. };
  800. /**
  801. * Check if video is muted on join.
  802. */
  803. JitsiConference.prototype.isStartVideoMuted = function () {
  804. return this.startVideoMuted;
  805. };
  806. /**
  807. * Get object with internal logs.
  808. */
  809. JitsiConference.prototype.getLogs = function () {
  810. var data = this.xmpp.getJingleLog();
  811. var metadata = {};
  812. metadata.time = new Date();
  813. metadata.url = window.location.href;
  814. metadata.ua = navigator.userAgent;
  815. var log = this.xmpp.getXmppLog();
  816. if (log) {
  817. metadata.xmpp = log;
  818. }
  819. data.metadata = metadata;
  820. return data;
  821. };
  822. /**
  823. * Returns measured connectionTimes.
  824. */
  825. JitsiConference.prototype.getConnectionTimes = function () {
  826. return this.room.connectionTimes;
  827. };
  828. /**
  829. * Sets a property for the local participant.
  830. */
  831. JitsiConference.prototype.setLocalParticipantProperty = function(name, value) {
  832. this.sendCommand("jitsi_participant_" + name, {value: value});
  833. };
  834. /**
  835. * Sends the given feedback through CallStats if enabled.
  836. *
  837. * @param overallFeedback an integer between 1 and 5 indicating the
  838. * user feedback
  839. * @param detailedFeedback detailed feedback from the user. Not yet used
  840. */
  841. JitsiConference.prototype.sendFeedback =
  842. function(overallFeedback, detailedFeedback){
  843. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  844. }
  845. /**
  846. * Returns true if the callstats integration is enabled, otherwise returns
  847. * false.
  848. *
  849. * @returns true if the callstats integration is enabled, otherwise returns
  850. * false.
  851. */
  852. JitsiConference.prototype.isCallstatsEnabled = function () {
  853. return this.statistics.isCallstatsEnabled();
  854. }
  855. /**
  856. * Reports detected audio problem with the media stream related to the passed
  857. * ssrc.
  858. * @param ssrc {string} the ssrc
  859. */
  860. JitsiConference.prototype._reportAudioProblem = function (ssrc) {
  861. if(this.reportedAudioSSRCs[ssrc])
  862. return;
  863. var track = this.rtc.getRemoteTrackBySSRC(ssrc);
  864. if(!track || !track.isAudioTrack())
  865. return;
  866. this.reportedAudioSSRCs[ssrc] = true;
  867. var errorContent = {
  868. errMsg: "The audio is received but not played",
  869. ssrc: ssrc
  870. };
  871. var mstream = track.stream, mtrack = track.track;
  872. if(mstream) {
  873. errorContent.MediaStream = {
  874. active: mstream.active,
  875. id: mstream.id
  876. }
  877. }
  878. if(mtrack) {
  879. errorContent.MediaStreamTrack = {
  880. enabled: mtrack.enabled,
  881. id: mtrack.id,
  882. label: mtrack.label,
  883. muted: mtrack.muted
  884. }
  885. }
  886. if(track.containers) {
  887. errorContent.containers = [];
  888. track.containers.forEach(function (container) {
  889. errorContent.containers.push({
  890. autoplay: container.autoplay,
  891. muted: container.muted,
  892. src: container.src,
  893. volume: container.volume,
  894. id: container.id,
  895. ended: container.ended,
  896. paused: container.paused,
  897. readyState: container.readyState
  898. });
  899. });
  900. }
  901. this.statistics.sendDetectedAudioProblem(
  902. new Error(JSON.stringify(errorContent)));
  903. logger.error("Audio problem detected. The audio is received but not played",
  904. errorContent);
  905. }
  906. /**
  907. * Setups the listeners needed for the conference.
  908. * @param conference the conference
  909. */
  910. function setupListeners(conference) {
  911. conference.xmpp.addListener(
  912. XMPPEvents.CALL_INCOMING, function (jingleSession, jingleOffer, now) {
  913. if (conference.room.isFocus(jingleSession.peerjid)) {
  914. // Accept incoming call
  915. conference.room.setJingleSession(jingleSession);
  916. conference.room.connectionTimes["session.initiate"] = now;
  917. try{
  918. jingleSession.initialize(false /* initiator */,
  919. conference.room);
  920. } catch (error) {
  921. GlobalOnErrorHandler.callErrorHandler(error);
  922. };
  923. conference.rtc.onIncommingCall(jingleSession);
  924. jingleSession.acceptOffer(jingleOffer, null,
  925. function (error) {
  926. GlobalOnErrorHandler.callErrorHandler(error);
  927. logger.error(
  928. "Failed to accept incoming Jingle session", error);
  929. }
  930. );
  931. // Start callstats as soon as peerconnection is initialized,
  932. // do not wait for XMPPEvents.PEERCONNECTION_READY, as it may never
  933. // happen in case if user doesn't have or denied permission to
  934. // both camera and microphone.
  935. conference.statistics.startCallStats(jingleSession, conference.settings);
  936. conference.statistics.startRemoteStats(jingleSession.peerconnection);
  937. } else {
  938. // Error cause this should never happen unless something is wrong!
  939. var errmsg
  940. = "Rejecting session-initiate from non-focus user: "
  941. + jingleSession.peerjid;
  942. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  943. logger.error(errmsg);
  944. }
  945. });
  946. conference.room.addListener(XMPPEvents.ICE_RESTARTING, function () {
  947. // All data channels have to be closed, before ICE restart
  948. // otherwise Chrome will not trigger "opened" event for the channel
  949. // established with the new bridge
  950. conference.rtc.closeAllDataChannels();
  951. });
  952. conference.room.addListener(XMPPEvents.REMOTE_TRACK_ADDED,
  953. function (data) {
  954. var track = conference.rtc.createRemoteTrack(data);
  955. if (track) {
  956. conference.onTrackAdded(track);
  957. }
  958. }
  959. );
  960. conference.room.addListener(XMPPEvents.REMOTE_TRACK_REMOVED,
  961. function (streamId, trackId) {
  962. conference.getParticipants().forEach(function(participant) {
  963. var tracks = participant.getTracks();
  964. for(var i = 0; i < tracks.length; i++) {
  965. if(tracks[i]
  966. && tracks[i].getStreamId() == streamId
  967. && tracks[i].getTrackId() == trackId) {
  968. var track = participant._tracks.splice(i, 1)[0];
  969. conference.eventEmitter.emit(
  970. JitsiConferenceEvents.TRACK_REMOVED, track);
  971. return;
  972. }
  973. }
  974. });
  975. }
  976. );
  977. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  978. function (value) {
  979. // set isMutedByFocus when setAudioMute Promise ends
  980. conference.rtc.setAudioMute(value).then(
  981. function() {
  982. conference.isMutedByFocus = true;
  983. },
  984. function() {
  985. logger.warn(
  986. "Error while audio muting due to focus request");
  987. });
  988. }
  989. );
  990. conference.room.addListener(XMPPEvents.SUBJECT_CHANGED, function (subject) {
  991. conference.eventEmitter.emit(JitsiConferenceEvents.SUBJECT_CHANGED,
  992. subject);
  993. });
  994. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  995. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  996. });
  997. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  998. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  999. JitsiConferenceErrors.CONNECTION_ERROR, pres);
  1000. });
  1001. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  1002. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1003. JitsiConferenceErrors.CONNECTION_ERROR, pres);
  1004. });
  1005. conference.room.addListener(XMPPEvents.ROOM_MAX_USERS_ERROR,
  1006. function (pres) {
  1007. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1008. JitsiConferenceErrors.CONFERENCE_MAX_USERS, pres);
  1009. });
  1010. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  1011. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  1012. });
  1013. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  1014. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  1015. });
  1016. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  1017. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  1018. });
  1019. conference.room.addListener(XMPPEvents.RESERVATION_ERROR, function (code, msg) {
  1020. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.RESERVATION_ERROR, code, msg);
  1021. });
  1022. conference.room.addListener(XMPPEvents.GRACEFUL_SHUTDOWN, function () {
  1023. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  1024. });
  1025. conference.room.addListener(XMPPEvents.JINGLE_FATAL_ERROR, function (session, error) {
  1026. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.JINGLE_FATAL_ERROR, error);
  1027. });
  1028. conference.room.addListener(XMPPEvents.MUC_DESTROYED, function (reason) {
  1029. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_DESTROYED, reason);
  1030. });
  1031. conference.room.addListener(XMPPEvents.CHAT_ERROR_RECEIVED, function (err, msg) {
  1032. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_ERROR, JitsiConferenceErrors.CHAT_ERROR, err, msg);
  1033. });
  1034. conference.room.addListener(XMPPEvents.FOCUS_DISCONNECTED, function (focus, retrySec) {
  1035. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_DISCONNECTED, focus, retrySec);
  1036. });
  1037. conference.room.addListener(XMPPEvents.FOCUS_LEFT, function () {
  1038. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_LEFT);
  1039. });
  1040. conference.room.setParticipantPropertyListener(function (node, from) {
  1041. var participant = conference.getParticipantById(from);
  1042. if (!participant) {
  1043. return;
  1044. }
  1045. participant.setProperty(
  1046. node.tagName.substring("jitsi_participant_".length),
  1047. node.value);
  1048. });
  1049. conference.room.addListener(XMPPEvents.KICKED, function () {
  1050. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  1051. });
  1052. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  1053. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  1054. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  1055. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  1056. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  1057. // log all events for the recorder operated by the moderator
  1058. if (conference.statistics && conference.isModerator()) {
  1059. conference.on(JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  1060. function (status, error) {
  1061. Statistics.sendLog(
  1062. "[Recorder] status: " + status
  1063. + (error? " error: " + error : ""));
  1064. });
  1065. }
  1066. });
  1067. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  1068. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  1069. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  1070. });
  1071. conference.room.addListener(XMPPEvents.RECORDER_STATE_CHANGED,
  1072. function (state) {
  1073. conference.eventEmitter.emit(
  1074. JitsiConferenceEvents.RECORDER_STATE_CHANGED, state);
  1075. });
  1076. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  1077. conference.eventEmitter.emit(
  1078. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  1079. });
  1080. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  1081. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  1082. });
  1083. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED,
  1084. function (error) {
  1085. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1086. JitsiConferenceErrors.SETUP_FAILED, error);
  1087. });
  1088. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  1089. conference.authEnabled = authEnabled;
  1090. conference.authIdentity = authIdentity;
  1091. conference.eventEmitter.emit(JitsiConferenceEvents.AUTH_STATUS_CHANGED, authEnabled, authIdentity);
  1092. });
  1093. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  1094. var id = Strophe.getResourceFromJid(jid);
  1095. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  1096. });
  1097. conference.room.addListener(XMPPEvents.PRESENCE_STATUS, function (jid, status) {
  1098. var id = Strophe.getResourceFromJid(jid);
  1099. var participant = conference.getParticipantById(id);
  1100. if (!participant || participant._status === status) {
  1101. return;
  1102. }
  1103. participant._status = status;
  1104. conference.eventEmitter.emit(JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  1105. });
  1106. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  1107. if(conference.lastDominantSpeaker !== id && conference.room) {
  1108. conference.lastDominantSpeaker = id;
  1109. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  1110. }
  1111. if (conference.statistics && conference.myUserId() === id) {
  1112. // We are the new dominant speaker.
  1113. conference.statistics.sendDominantSpeakerEvent();
  1114. }
  1115. });
  1116. conference.rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, function () {
  1117. var now = window.performance.now();
  1118. logger.log("(TIME) data channel opened ", now);
  1119. conference.room.connectionTimes["data.channel.opened"] = now;
  1120. });
  1121. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  1122. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  1123. });
  1124. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  1125. function (lastNEndpoints, endpointsEnteringLastN) {
  1126. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  1127. lastNEndpoints, endpointsEnteringLastN);
  1128. });
  1129. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  1130. function (audioMuted, videoMuted) {
  1131. conference.startAudioMuted = audioMuted;
  1132. conference.startVideoMuted = videoMuted;
  1133. // mute existing local tracks because this is initial mute from
  1134. // Jicofo
  1135. conference.getLocalTracks().forEach(function (track) {
  1136. if (conference.startAudioMuted && track.isAudioTrack()) {
  1137. track.mute();
  1138. }
  1139. if (conference.startVideoMuted && track.isVideoTrack()) {
  1140. track.mute();
  1141. }
  1142. });
  1143. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  1144. });
  1145. conference.room.addPresenceListener("startmuted", function (data, from) {
  1146. var isModerator = false;
  1147. if (conference.myUserId() === from && conference.isModerator()) {
  1148. isModerator = true;
  1149. } else {
  1150. var participant = conference.getParticipantById(from);
  1151. if (participant && participant.isModerator()) {
  1152. isModerator = true;
  1153. }
  1154. }
  1155. if (!isModerator) {
  1156. return;
  1157. }
  1158. var startAudioMuted = data.attributes.audio === 'true';
  1159. var startVideoMuted = data.attributes.video === 'true';
  1160. var updated = false;
  1161. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  1162. conference.startMutedPolicy.audio = startAudioMuted;
  1163. updated = true;
  1164. }
  1165. if (startVideoMuted !== conference.startMutedPolicy.video) {
  1166. conference.startMutedPolicy.video = startVideoMuted;
  1167. updated = true;
  1168. }
  1169. if (updated) {
  1170. conference.eventEmitter.emit(
  1171. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  1172. conference.startMutedPolicy
  1173. );
  1174. }
  1175. });
  1176. conference.rtc.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  1177. conference.room.updateDeviceAvailability(devices);
  1178. });
  1179. conference.room.addPresenceListener("devices", function (data, from) {
  1180. var isAudioAvailable = false;
  1181. var isVideoAvailable = false;
  1182. data.children.forEach(function (config) {
  1183. if (config.tagName === 'audio') {
  1184. isAudioAvailable = config.value === 'true';
  1185. }
  1186. if (config.tagName === 'video') {
  1187. isVideoAvailable = config.value === 'true';
  1188. }
  1189. });
  1190. var availableDevices;
  1191. if (conference.myUserId() === from) {
  1192. availableDevices = conference.availableDevices;
  1193. } else {
  1194. var participant = conference.getParticipantById(from);
  1195. if (!participant) {
  1196. return;
  1197. }
  1198. availableDevices = participant._availableDevices;
  1199. }
  1200. var updated = false;
  1201. if (availableDevices.audio !== isAudioAvailable) {
  1202. updated = true;
  1203. availableDevices.audio = isAudioAvailable;
  1204. }
  1205. if (availableDevices.video !== isVideoAvailable) {
  1206. updated = true;
  1207. availableDevices.video = isVideoAvailable;
  1208. }
  1209. if (updated) {
  1210. conference.eventEmitter.emit(
  1211. JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED,
  1212. from, availableDevices);
  1213. }
  1214. });
  1215. if(conference.statistics) {
  1216. //FIXME: Maybe remove event should not be associated with the conference.
  1217. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  1218. var userId = null;
  1219. var resource = conference.rtc.getResourceBySSRC(ssrc);
  1220. if (!resource)
  1221. return;
  1222. conference.rtc.setAudioLevel(resource, level);
  1223. });
  1224. conference.statistics.addAudioProblemListener(function (ssrc) {
  1225. conference._reportAudioProblem(ssrc)
  1226. });
  1227. conference.statistics.addConnectionStatsListener(function (stats) {
  1228. var ssrc2resolution = stats.resolution;
  1229. var id2resolution = {};
  1230. // preprocess resolutions: group by user id, skip incorrect
  1231. // resolutions etc.
  1232. Object.keys(ssrc2resolution).forEach(function (ssrc) {
  1233. var resolution = ssrc2resolution[ssrc];
  1234. if (!resolution.width || !resolution.height ||
  1235. resolution.width == -1 || resolution.height == -1) {
  1236. return;
  1237. }
  1238. var id = conference.rtc.getResourceBySSRC(ssrc);
  1239. if (!id) {
  1240. return;
  1241. }
  1242. // ssrc to resolution map for user id
  1243. var idResolutions = id2resolution[id] || {};
  1244. idResolutions[ssrc] = resolution;
  1245. id2resolution[id] = idResolutions;
  1246. });
  1247. stats.resolution = id2resolution;
  1248. conference.eventEmitter.emit(
  1249. JitsiConferenceEvents.CONNECTION_STATS, stats);
  1250. });
  1251. conference.room.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  1252. function () {
  1253. conference.statistics.dispose();
  1254. });
  1255. conference.room.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  1256. function (pc) {
  1257. conference.statistics.sendIceConnectionFailedEvent(pc);
  1258. conference.room.eventEmitter.emit(
  1259. XMPPEvents.CONFERENCE_SETUP_FAILED,
  1260. new Error("ICE fail"));
  1261. });
  1262. conference.rtc.addListener(RTCEvents.TRACK_ATTACHED,
  1263. function(track, container) {
  1264. var ssrc = track.getSSRC();
  1265. if (!container.id || !ssrc) {
  1266. return;
  1267. }
  1268. conference.statistics.associateStreamWithVideoTag(
  1269. ssrc, track.isLocal(), track.getUsageLabel(), container.id);
  1270. });
  1271. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  1272. function (track) {
  1273. if(!track.isLocal())
  1274. return;
  1275. var type = (track.getType() === "audio")? "audio" : "video";
  1276. conference.statistics.sendMuteEvent(track.isMuted(), type);
  1277. });
  1278. conference.room.addListener(XMPPEvents.CREATE_OFFER_FAILED, function (e, pc) {
  1279. conference.statistics.sendCreateOfferFailed(e, pc);
  1280. });
  1281. conference.room.addListener(XMPPEvents.CREATE_ANSWER_FAILED, function (e, pc) {
  1282. conference.statistics.sendCreateAnswerFailed(e, pc);
  1283. });
  1284. conference.room.addListener(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  1285. function (e, pc) {
  1286. conference.statistics.sendSetLocalDescFailed(e, pc);
  1287. }
  1288. );
  1289. conference.room.addListener(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  1290. function (e, pc) {
  1291. conference.statistics.sendSetRemoteDescFailed(e, pc);
  1292. }
  1293. );
  1294. conference.room.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  1295. function (e, pc) {
  1296. conference.statistics.sendAddIceCandidateFailed(e, pc);
  1297. }
  1298. );
  1299. }
  1300. }
  1301. module.exports = JitsiConference;