Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JitsiConference.js 51KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  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.selectEndpoint(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. var removedTracks = this.rtc.removeRemoteTracks(id);
  577. removedTracks.forEach(function (track) {
  578. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  579. }.bind(this));
  580. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  581. };
  582. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  583. var id = Strophe.getResourceFromJid(jid);
  584. var participant = this.getParticipantById(id);
  585. if (!participant) {
  586. return;
  587. }
  588. participant._role = role;
  589. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  590. };
  591. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  592. var id = Strophe.getResourceFromJid(jid);
  593. var participant = this.getParticipantById(id);
  594. if (!participant) {
  595. return;
  596. }
  597. if (participant._displayName === displayName)
  598. return;
  599. participant._displayName = displayName;
  600. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  601. };
  602. /**
  603. * Notifies this JitsiConference that a JitsiRemoteTrack was added (into the
  604. * ChatRoom of this JitsiConference).
  605. *
  606. * @param {JitsiRemoteTrack} track the JitsiRemoteTrack which was added to this
  607. * JitsiConference
  608. */
  609. JitsiConference.prototype.onTrackAdded = function (track) {
  610. var id = track.getParticipantId();
  611. var participant = this.getParticipantById(id);
  612. if (!participant) {
  613. return;
  614. }
  615. // Add track to JitsiParticipant.
  616. participant._tracks.push(track);
  617. var emitter = this.eventEmitter;
  618. track.addEventListener(
  619. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  620. function () {
  621. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  622. }
  623. );
  624. track.addEventListener(
  625. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  626. function (audioLevel) {
  627. emitter.emit(
  628. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  629. id,
  630. audioLevel);
  631. }
  632. );
  633. emitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  634. };
  635. JitsiConference.prototype.updateDTMFSupport = function () {
  636. var somebodySupportsDTMF = false;
  637. var participants = this.getParticipants();
  638. // check if at least 1 participant supports DTMF
  639. for (var i = 0; i < participants.length; i += 1) {
  640. if (participants[i].supportsDTMF()) {
  641. somebodySupportsDTMF = true;
  642. break;
  643. }
  644. }
  645. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  646. this.somebodySupportsDTMF = somebodySupportsDTMF;
  647. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  648. }
  649. };
  650. /**
  651. * Allows to check if there is at least one user in the conference
  652. * that supports DTMF.
  653. * @returns {boolean} true if somebody supports DTMF, false otherwise
  654. */
  655. JitsiConference.prototype.isDTMFSupported = function () {
  656. return this.somebodySupportsDTMF;
  657. };
  658. /**
  659. * Returns the local user's ID
  660. * @return {string} local user's ID
  661. */
  662. JitsiConference.prototype.myUserId = function () {
  663. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  664. };
  665. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  666. if (!this.dtmfManager) {
  667. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  668. if (!connection) {
  669. logger.warn("cannot sendTones: no conneciton");
  670. return;
  671. }
  672. var tracks = this.getLocalTracks().filter(function (track) {
  673. return track.isAudioTrack();
  674. });
  675. if (!tracks.length) {
  676. logger.warn("cannot sendTones: no local audio stream");
  677. return;
  678. }
  679. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  680. }
  681. this.dtmfManager.sendTones(tones, duration, pause);
  682. };
  683. /**
  684. * Returns true if the recording is supproted and false if not.
  685. */
  686. JitsiConference.prototype.isRecordingSupported = function () {
  687. if(this.room)
  688. return this.room.isRecordingSupported();
  689. return false;
  690. };
  691. /**
  692. * Returns null if the recording is not supported, "on" if the recording started
  693. * and "off" if the recording is not started.
  694. */
  695. JitsiConference.prototype.getRecordingState = function () {
  696. return (this.room) ? this.room.getRecordingState() : undefined;
  697. }
  698. /**
  699. * Returns the url of the recorded video.
  700. */
  701. JitsiConference.prototype.getRecordingURL = function () {
  702. return (this.room) ? this.room.getRecordingURL() : null;
  703. }
  704. /**
  705. * Starts/stops the recording
  706. */
  707. JitsiConference.prototype.toggleRecording = function (options) {
  708. if(this.room)
  709. return this.room.toggleRecording(options, function (status, error) {
  710. this.eventEmitter.emit(
  711. JitsiConferenceEvents.RECORDER_STATE_CHANGED, status, error);
  712. }.bind(this));
  713. this.eventEmitter.emit(
  714. JitsiConferenceEvents.RECORDER_STATE_CHANGED, "error",
  715. new Error("The conference is not created yet!"));
  716. }
  717. /**
  718. * Returns true if the SIP calls are supported and false otherwise
  719. */
  720. JitsiConference.prototype.isSIPCallingSupported = function () {
  721. if(this.room)
  722. return this.room.isSIPCallingSupported();
  723. return false;
  724. }
  725. /**
  726. * Dials a number.
  727. * @param number the number
  728. */
  729. JitsiConference.prototype.dial = function (number) {
  730. if(this.room)
  731. return this.room.dial(number);
  732. return new Promise(function(resolve, reject){
  733. reject(new Error("The conference is not created yet!"))});
  734. }
  735. /**
  736. * Hangup an existing call
  737. */
  738. JitsiConference.prototype.hangup = function () {
  739. if(this.room)
  740. return this.room.hangup();
  741. return new Promise(function(resolve, reject){
  742. reject(new Error("The conference is not created yet!"))});
  743. }
  744. /**
  745. * Returns the phone number for joining the conference.
  746. */
  747. JitsiConference.prototype.getPhoneNumber = function () {
  748. if(this.room)
  749. return this.room.getPhoneNumber();
  750. return null;
  751. }
  752. /**
  753. * Returns the pin for joining the conference with phone.
  754. */
  755. JitsiConference.prototype.getPhonePin = function () {
  756. if(this.room)
  757. return this.room.getPhonePin();
  758. return null;
  759. }
  760. /**
  761. * Returns the connection state for the current room. Its ice connection state
  762. * for its session.
  763. */
  764. JitsiConference.prototype.getConnectionState = function () {
  765. if(this.room)
  766. return this.room.getConnectionState();
  767. return null;
  768. }
  769. /**
  770. * Make all new participants mute their audio/video on join.
  771. * @param policy {Object} object with 2 boolean properties for video and audio:
  772. * @param {boolean} audio if audio should be muted.
  773. * @param {boolean} video if video should be muted.
  774. */
  775. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  776. if (!this.isModerator()) {
  777. return;
  778. }
  779. this.startMutedPolicy = policy;
  780. this.room.removeFromPresence("startmuted");
  781. this.room.addToPresence("startmuted", {
  782. attributes: {
  783. audio: policy.audio,
  784. video: policy.video,
  785. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  786. }
  787. });
  788. this.room.sendPresence();
  789. };
  790. /**
  791. * Returns current start muted policy
  792. * @returns {Object} with 2 proprties - audio and video.
  793. */
  794. JitsiConference.prototype.getStartMutedPolicy = function () {
  795. return this.startMutedPolicy;
  796. };
  797. /**
  798. * Check if audio is muted on join.
  799. */
  800. JitsiConference.prototype.isStartAudioMuted = function () {
  801. return this.startAudioMuted;
  802. };
  803. /**
  804. * Check if video is muted on join.
  805. */
  806. JitsiConference.prototype.isStartVideoMuted = function () {
  807. return this.startVideoMuted;
  808. };
  809. /**
  810. * Get object with internal logs.
  811. */
  812. JitsiConference.prototype.getLogs = function () {
  813. var data = this.xmpp.getJingleLog();
  814. var metadata = {};
  815. metadata.time = new Date();
  816. metadata.url = window.location.href;
  817. metadata.ua = navigator.userAgent;
  818. var log = this.xmpp.getXmppLog();
  819. if (log) {
  820. metadata.xmpp = log;
  821. }
  822. data.metadata = metadata;
  823. return data;
  824. };
  825. /**
  826. * Returns measured connectionTimes.
  827. */
  828. JitsiConference.prototype.getConnectionTimes = function () {
  829. return this.room.connectionTimes;
  830. };
  831. /**
  832. * Sets a property for the local participant.
  833. */
  834. JitsiConference.prototype.setLocalParticipantProperty = function(name, value) {
  835. this.sendCommand("jitsi_participant_" + name, {value: value});
  836. };
  837. /**
  838. * Sends the given feedback through CallStats if enabled.
  839. *
  840. * @param overallFeedback an integer between 1 and 5 indicating the
  841. * user feedback
  842. * @param detailedFeedback detailed feedback from the user. Not yet used
  843. */
  844. JitsiConference.prototype.sendFeedback =
  845. function(overallFeedback, detailedFeedback){
  846. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  847. }
  848. /**
  849. * Returns true if the callstats integration is enabled, otherwise returns
  850. * false.
  851. *
  852. * @returns true if the callstats integration is enabled, otherwise returns
  853. * false.
  854. */
  855. JitsiConference.prototype.isCallstatsEnabled = function () {
  856. return this.statistics.isCallstatsEnabled();
  857. }
  858. /**
  859. * Reports detected audio problem with the media stream related to the passed
  860. * ssrc.
  861. * @param ssrc {string} the ssrc
  862. * NOTE: all logger.log calls are there only to be able to see the info in
  863. * torture
  864. */
  865. JitsiConference.prototype._reportAudioProblem = function (ssrc) {
  866. if(this.reportedAudioSSRCs[ssrc])
  867. return;
  868. var track = this.rtc.getRemoteTrackBySSRC(ssrc);
  869. if(!track || !track.isAudioTrack())
  870. return;
  871. var id = track.getParticipantId();
  872. var displayName = null;
  873. if(id) {
  874. var participant = this.getParticipantById(id);
  875. if(participant) {
  876. displayName = participant.getDisplayName();
  877. }
  878. }
  879. this.reportedAudioSSRCs[ssrc] = true;
  880. var errorContent = {
  881. errMsg: "The audio is received but not played",
  882. ssrc: ssrc,
  883. jid: id,
  884. displayName: displayName
  885. };
  886. logger.log("=================The audio is received but not played" +
  887. "======================");
  888. logger.log("ssrc: ", ssrc);
  889. logger.log("jid: ", id);
  890. logger.log("displayName: ", displayName);
  891. var mstream = track.stream, mtrack = track.track;
  892. if(mstream) {
  893. logger.log("MediaStream:");
  894. errorContent.MediaStream = {
  895. active: mstream.active,
  896. id: mstream.id
  897. };
  898. logger.log("active: ", mstream.active);
  899. logger.log("id: ", mstream.id);
  900. }
  901. if(mtrack) {
  902. logger.log("MediaStreamTrack:");
  903. errorContent.MediaStreamTrack = {
  904. enabled: mtrack.enabled,
  905. id: mtrack.id,
  906. label: mtrack.label,
  907. muted: mtrack.muted
  908. }
  909. logger.log("enabled: ", mtrack.enabled);
  910. logger.log("id: ", mtrack.id);
  911. logger.log("label: ", mtrack.label);
  912. logger.log("muted: ", mtrack.muted);
  913. }
  914. if(track.containers) {
  915. errorContent.containers = [];
  916. logger.log("Containers:");
  917. track.containers.forEach(function (container) {
  918. logger.log("Container:");
  919. errorContent.containers.push({
  920. autoplay: container.autoplay,
  921. muted: container.muted,
  922. src: container.src,
  923. volume: container.volume,
  924. id: container.id,
  925. ended: container.ended,
  926. paused: container.paused,
  927. readyState: container.readyState
  928. });
  929. logger.log("autoplay: ", container.autoplay);
  930. logger.log("muted: ", container.muted);
  931. logger.log("src: ", container.src);
  932. logger.log("volume: ", container.volume);
  933. logger.log("id: ", container.id);
  934. logger.log("ended: ", container.ended);
  935. logger.log("paused: ", container.paused);
  936. logger.log("readyState: ", container.readyState);
  937. });
  938. }
  939. this.statistics.sendDetectedAudioProblem(
  940. new Error(JSON.stringify(errorContent)));
  941. // Prints JSON.stringify(errorContent) to be able to see all properties of
  942. // errorContent from torture
  943. logger.error("Audio problem detected. The audio is received but not played",
  944. errorContent);
  945. };
  946. /**
  947. * Logs an "application log" message
  948. */
  949. JitsiConference.prototype.sendApplicationLog = function(message) {
  950. Statistics.sendLog(message);
  951. };
  952. /**
  953. * Setups the listeners needed for the conference.
  954. * @param conference the conference
  955. */
  956. function setupListeners(conference) {
  957. conference.xmpp.addListener(
  958. XMPPEvents.CALL_INCOMING, function (jingleSession, jingleOffer, now) {
  959. if (conference.room.isFocus(jingleSession.peerjid)) {
  960. // Accept incoming call
  961. conference.room.setJingleSession(jingleSession);
  962. conference.room.connectionTimes["session.initiate"] = now;
  963. try{
  964. jingleSession.initialize(false /* initiator */,
  965. conference.room);
  966. } catch (error) {
  967. GlobalOnErrorHandler.callErrorHandler(error);
  968. };
  969. conference.rtc.onIncommingCall(jingleSession);
  970. jingleSession.acceptOffer(jingleOffer, null,
  971. function (error) {
  972. GlobalOnErrorHandler.callErrorHandler(error);
  973. logger.error(
  974. "Failed to accept incoming Jingle session", error);
  975. }
  976. );
  977. // Start callstats as soon as peerconnection is initialized,
  978. // do not wait for XMPPEvents.PEERCONNECTION_READY, as it may never
  979. // happen in case if user doesn't have or denied permission to
  980. // both camera and microphone.
  981. conference.statistics.startCallStats(jingleSession, conference.settings);
  982. conference.statistics.startRemoteStats(jingleSession.peerconnection);
  983. } else {
  984. // Error cause this should never happen unless something is wrong!
  985. var errmsg
  986. = "Rejecting session-initiate from non-focus user: "
  987. + jingleSession.peerjid;
  988. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  989. logger.error(errmsg);
  990. }
  991. });
  992. conference.room.addListener(XMPPEvents.ICE_RESTARTING, function () {
  993. // All data channels have to be closed, before ICE restart
  994. // otherwise Chrome will not trigger "opened" event for the channel
  995. // established with the new bridge
  996. conference.rtc.closeAllDataChannels();
  997. });
  998. conference.room.addListener(XMPPEvents.LOCAL_UFRAG_CHANGED, function (ufrag) {
  999. Statistics.sendLog("Local ufrag: " + ufrag);
  1000. });
  1001. conference.room.addListener(XMPPEvents.REMOTE_UFRAG_CHANGED, function (ufrag) {
  1002. Statistics.sendLog("Remote ufrag: " + ufrag);
  1003. });
  1004. conference.room.addListener(XMPPEvents.REMOTE_TRACK_ADDED,
  1005. function (data) {
  1006. var track = conference.rtc.createRemoteTrack(data);
  1007. if (track) {
  1008. conference.onTrackAdded(track);
  1009. }
  1010. }
  1011. );
  1012. conference.room.addListener(XMPPEvents.REMOTE_TRACK_REMOVED,
  1013. function (streamId, trackId) {
  1014. conference.getParticipants().forEach(function(participant) {
  1015. var tracks = participant.getTracks();
  1016. for(var i = 0; i < tracks.length; i++) {
  1017. if(tracks[i]
  1018. && tracks[i].getStreamId() == streamId
  1019. && tracks[i].getTrackId() == trackId) {
  1020. var track = participant._tracks.splice(i, 1)[0];
  1021. track = conference.rtc.removeRemoteTrack(
  1022. participant.getId(), track);
  1023. if (track) {
  1024. conference.eventEmitter.emit(
  1025. JitsiConferenceEvents.TRACK_REMOVED, track);
  1026. }
  1027. return;
  1028. }
  1029. }
  1030. });
  1031. }
  1032. );
  1033. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  1034. function (value) {
  1035. // set isMutedByFocus when setAudioMute Promise ends
  1036. conference.rtc.setAudioMute(value).then(
  1037. function() {
  1038. conference.isMutedByFocus = true;
  1039. },
  1040. function() {
  1041. logger.warn(
  1042. "Error while audio muting due to focus request");
  1043. });
  1044. }
  1045. );
  1046. conference.room.addListener(XMPPEvents.SUBJECT_CHANGED, function (subject) {
  1047. conference.eventEmitter.emit(JitsiConferenceEvents.SUBJECT_CHANGED,
  1048. subject);
  1049. });
  1050. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  1051. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  1052. });
  1053. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  1054. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1055. JitsiConferenceErrors.CONNECTION_ERROR, pres);
  1056. });
  1057. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  1058. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1059. JitsiConferenceErrors.CONNECTION_ERROR, pres);
  1060. });
  1061. conference.room.addListener(XMPPEvents.ROOM_MAX_USERS_ERROR,
  1062. function (pres) {
  1063. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1064. JitsiConferenceErrors.CONFERENCE_MAX_USERS, pres);
  1065. });
  1066. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  1067. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  1068. });
  1069. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  1070. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  1071. });
  1072. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  1073. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  1074. });
  1075. conference.room.addListener(XMPPEvents.RESERVATION_ERROR, function (code, msg) {
  1076. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.RESERVATION_ERROR, code, msg);
  1077. });
  1078. conference.room.addListener(XMPPEvents.GRACEFUL_SHUTDOWN, function () {
  1079. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  1080. });
  1081. conference.room.addListener(XMPPEvents.JINGLE_FATAL_ERROR, function (session, error) {
  1082. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.JINGLE_FATAL_ERROR, error);
  1083. });
  1084. conference.room.addListener(XMPPEvents.MUC_DESTROYED, function (reason) {
  1085. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_DESTROYED, reason);
  1086. });
  1087. conference.room.addListener(XMPPEvents.CHAT_ERROR_RECEIVED, function (err, msg) {
  1088. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_ERROR, JitsiConferenceErrors.CHAT_ERROR, err, msg);
  1089. });
  1090. conference.room.addListener(XMPPEvents.FOCUS_DISCONNECTED, function (focus, retrySec) {
  1091. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_DISCONNECTED, focus, retrySec);
  1092. });
  1093. conference.room.addListener(XMPPEvents.FOCUS_LEFT, function () {
  1094. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_LEFT);
  1095. });
  1096. conference.room.setParticipantPropertyListener(function (node, from) {
  1097. var participant = conference.getParticipantById(from);
  1098. if (!participant) {
  1099. return;
  1100. }
  1101. participant.setProperty(
  1102. node.tagName.substring("jitsi_participant_".length),
  1103. node.value);
  1104. });
  1105. conference.room.addListener(XMPPEvents.KICKED, function () {
  1106. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  1107. });
  1108. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  1109. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  1110. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  1111. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  1112. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  1113. // log all events for the recorder operated by the moderator
  1114. if (conference.statistics && conference.isModerator()) {
  1115. conference.on(JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  1116. function (status, error) {
  1117. Statistics.sendLog(
  1118. "[Recorder] status: " + status
  1119. + (error? " error: " + error : ""));
  1120. });
  1121. }
  1122. });
  1123. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  1124. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  1125. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  1126. });
  1127. conference.room.addListener(XMPPEvents.RECORDER_STATE_CHANGED,
  1128. function (state) {
  1129. conference.eventEmitter.emit(
  1130. JitsiConferenceEvents.RECORDER_STATE_CHANGED, state);
  1131. });
  1132. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  1133. conference.eventEmitter.emit(
  1134. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  1135. });
  1136. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  1137. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  1138. });
  1139. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED,
  1140. function (error) {
  1141. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1142. JitsiConferenceErrors.SETUP_FAILED, error);
  1143. });
  1144. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  1145. conference.authEnabled = authEnabled;
  1146. conference.authIdentity = authIdentity;
  1147. conference.eventEmitter.emit(JitsiConferenceEvents.AUTH_STATUS_CHANGED, authEnabled, authIdentity);
  1148. });
  1149. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  1150. var id = Strophe.getResourceFromJid(jid);
  1151. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  1152. });
  1153. conference.room.addListener(XMPPEvents.PRESENCE_STATUS, function (jid, status) {
  1154. var id = Strophe.getResourceFromJid(jid);
  1155. var participant = conference.getParticipantById(id);
  1156. if (!participant || participant._status === status) {
  1157. return;
  1158. }
  1159. participant._status = status;
  1160. conference.eventEmitter.emit(JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  1161. });
  1162. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  1163. if(conference.lastDominantSpeaker !== id && conference.room) {
  1164. conference.lastDominantSpeaker = id;
  1165. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  1166. }
  1167. if (conference.statistics && conference.myUserId() === id) {
  1168. // We are the new dominant speaker.
  1169. conference.statistics.sendDominantSpeakerEvent();
  1170. }
  1171. });
  1172. conference.rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, function () {
  1173. var now = window.performance.now();
  1174. logger.log("(TIME) data channel opened ", now);
  1175. conference.room.connectionTimes["data.channel.opened"] = now;
  1176. });
  1177. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  1178. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  1179. });
  1180. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  1181. function (lastNEndpoints, endpointsEnteringLastN) {
  1182. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  1183. lastNEndpoints, endpointsEnteringLastN);
  1184. });
  1185. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  1186. function (audioMuted, videoMuted) {
  1187. conference.startAudioMuted = audioMuted;
  1188. conference.startVideoMuted = videoMuted;
  1189. // mute existing local tracks because this is initial mute from
  1190. // Jicofo
  1191. conference.getLocalTracks().forEach(function (track) {
  1192. if (conference.startAudioMuted && track.isAudioTrack()) {
  1193. track.mute();
  1194. }
  1195. if (conference.startVideoMuted && track.isVideoTrack()) {
  1196. track.mute();
  1197. }
  1198. });
  1199. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  1200. });
  1201. conference.room.addPresenceListener("startmuted", function (data, from) {
  1202. var isModerator = false;
  1203. if (conference.myUserId() === from && conference.isModerator()) {
  1204. isModerator = true;
  1205. } else {
  1206. var participant = conference.getParticipantById(from);
  1207. if (participant && participant.isModerator()) {
  1208. isModerator = true;
  1209. }
  1210. }
  1211. if (!isModerator) {
  1212. return;
  1213. }
  1214. var startAudioMuted = data.attributes.audio === 'true';
  1215. var startVideoMuted = data.attributes.video === 'true';
  1216. var updated = false;
  1217. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  1218. conference.startMutedPolicy.audio = startAudioMuted;
  1219. updated = true;
  1220. }
  1221. if (startVideoMuted !== conference.startMutedPolicy.video) {
  1222. conference.startMutedPolicy.video = startVideoMuted;
  1223. updated = true;
  1224. }
  1225. if (updated) {
  1226. conference.eventEmitter.emit(
  1227. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  1228. conference.startMutedPolicy
  1229. );
  1230. }
  1231. });
  1232. conference.rtc.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  1233. conference.room.updateDeviceAvailability(devices);
  1234. });
  1235. conference.room.addPresenceListener("devices", function (data, from) {
  1236. var isAudioAvailable = false;
  1237. var isVideoAvailable = false;
  1238. data.children.forEach(function (config) {
  1239. if (config.tagName === 'audio') {
  1240. isAudioAvailable = config.value === 'true';
  1241. }
  1242. if (config.tagName === 'video') {
  1243. isVideoAvailable = config.value === 'true';
  1244. }
  1245. });
  1246. var availableDevices;
  1247. if (conference.myUserId() === from) {
  1248. availableDevices = conference.availableDevices;
  1249. } else {
  1250. var participant = conference.getParticipantById(from);
  1251. if (!participant) {
  1252. return;
  1253. }
  1254. availableDevices = participant._availableDevices;
  1255. }
  1256. var updated = false;
  1257. if (availableDevices.audio !== isAudioAvailable) {
  1258. updated = true;
  1259. availableDevices.audio = isAudioAvailable;
  1260. }
  1261. if (availableDevices.video !== isVideoAvailable) {
  1262. updated = true;
  1263. availableDevices.video = isVideoAvailable;
  1264. }
  1265. if (updated) {
  1266. conference.eventEmitter.emit(
  1267. JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED,
  1268. from, availableDevices);
  1269. }
  1270. });
  1271. if(conference.statistics) {
  1272. //FIXME: Maybe remove event should not be associated with the conference.
  1273. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  1274. var userId = null;
  1275. var resource = conference.rtc.getResourceBySSRC(ssrc);
  1276. if (!resource)
  1277. return;
  1278. conference.rtc.setAudioLevel(resource, level);
  1279. });
  1280. conference.statistics.addAudioProblemListener(function (ssrc) {
  1281. conference._reportAudioProblem(ssrc)
  1282. });
  1283. conference.statistics.addConnectionStatsListener(function (stats) {
  1284. var ssrc2resolution = stats.resolution;
  1285. var id2resolution = {};
  1286. // preprocess resolutions: group by user id, skip incorrect
  1287. // resolutions etc.
  1288. Object.keys(ssrc2resolution).forEach(function (ssrc) {
  1289. var resolution = ssrc2resolution[ssrc];
  1290. if (!resolution.width || !resolution.height ||
  1291. resolution.width == -1 || resolution.height == -1) {
  1292. return;
  1293. }
  1294. var id = conference.rtc.getResourceBySSRC(ssrc);
  1295. if (!id) {
  1296. return;
  1297. }
  1298. // ssrc to resolution map for user id
  1299. var idResolutions = id2resolution[id] || {};
  1300. idResolutions[ssrc] = resolution;
  1301. id2resolution[id] = idResolutions;
  1302. });
  1303. stats.resolution = id2resolution;
  1304. conference.eventEmitter.emit(
  1305. JitsiConferenceEvents.CONNECTION_STATS, stats);
  1306. });
  1307. conference.room.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  1308. function () {
  1309. conference.statistics.dispose();
  1310. });
  1311. conference.room.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  1312. function (pc) {
  1313. conference.statistics.sendIceConnectionFailedEvent(pc);
  1314. conference.room.eventEmitter.emit(
  1315. XMPPEvents.CONFERENCE_SETUP_FAILED,
  1316. new Error("ICE fail"));
  1317. });
  1318. conference.rtc.addListener(RTCEvents.TRACK_ATTACHED,
  1319. function(track, container) {
  1320. var ssrc = track.getSSRC();
  1321. if (!container.id || !ssrc) {
  1322. return;
  1323. }
  1324. conference.statistics.associateStreamWithVideoTag(
  1325. ssrc, track.isLocal(), track.getUsageLabel(), container.id);
  1326. });
  1327. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  1328. function (track) {
  1329. if(!track.isLocal())
  1330. return;
  1331. var type = (track.getType() === "audio")? "audio" : "video";
  1332. conference.statistics.sendMuteEvent(track.isMuted(), type);
  1333. });
  1334. conference.room.addListener(XMPPEvents.CREATE_OFFER_FAILED, function (e, pc) {
  1335. conference.statistics.sendCreateOfferFailed(e, pc);
  1336. });
  1337. conference.room.addListener(XMPPEvents.CREATE_ANSWER_FAILED, function (e, pc) {
  1338. conference.statistics.sendCreateAnswerFailed(e, pc);
  1339. });
  1340. conference.room.addListener(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  1341. function (e, pc) {
  1342. conference.statistics.sendSetLocalDescFailed(e, pc);
  1343. }
  1344. );
  1345. conference.room.addListener(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  1346. function (e, pc) {
  1347. conference.statistics.sendSetRemoteDescFailed(e, pc);
  1348. }
  1349. );
  1350. conference.room.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  1351. function (e, pc) {
  1352. conference.statistics.sendAddIceCandidateFailed(e, pc);
  1353. }
  1354. );
  1355. }
  1356. }
  1357. module.exports = JitsiConference;