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

JitsiConference.js 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. /**
  16. * Creates a JitsiConference object with the given name and properties.
  17. * Note: this constructor is not a part of the public API (objects should be
  18. * created using JitsiConnection.createConference).
  19. * @param options.config properties / settings related to the conference that will be created.
  20. * @param options.name the name of the conference
  21. * @param options.connection the JitsiConnection object for this JitsiConference.
  22. * @constructor
  23. */
  24. function JitsiConference(options) {
  25. if(!options.name || options.name.toLowerCase() !== options.name) {
  26. logger.error("Invalid conference name (no conference name passed or it"
  27. + "contains invalid characters like capital letters)!");
  28. return;
  29. }
  30. this.options = options;
  31. this.connection = this.options.connection;
  32. this.xmpp = this.connection.xmpp;
  33. this.eventEmitter = new EventEmitter();
  34. this.room = this.xmpp.createRoom(this.options.name, this.options.config);
  35. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  36. this.rtc = new RTC(this.room, options);
  37. if(!RTC.options.disableAudioLevels)
  38. this.statistics = new Statistics();
  39. setupListeners(this);
  40. this.participants = {};
  41. this.lastDominantSpeaker = null;
  42. this.dtmfManager = null;
  43. this.somebodySupportsDTMF = false;
  44. this.authEnabled = false;
  45. this.authIdentity;
  46. this.startAudioMuted = false;
  47. this.startVideoMuted = false;
  48. this.tracks = [];
  49. }
  50. /**
  51. * Joins the conference.
  52. * @param password {string} the password
  53. */
  54. JitsiConference.prototype.join = function (password) {
  55. if(this.room)
  56. this.room.join(password);
  57. };
  58. /**
  59. * Check if joined to the conference.
  60. */
  61. JitsiConference.prototype.isJoined = function () {
  62. return this.room && this.room.joined;
  63. };
  64. /**
  65. * Leaves the conference.
  66. */
  67. JitsiConference.prototype.leave = function () {
  68. if(this.xmpp && this.room)
  69. this.xmpp.leaveRoom(this.room.roomjid);
  70. this.room = null;
  71. };
  72. /**
  73. * Returns name of this conference.
  74. */
  75. JitsiConference.prototype.getName = function () {
  76. return this.options.name;
  77. };
  78. /**
  79. * Check if authentication is enabled for this conference.
  80. */
  81. JitsiConference.prototype.isAuthEnabled = function () {
  82. return this.authEnabled;
  83. };
  84. /**
  85. * Check if user is logged in.
  86. */
  87. JitsiConference.prototype.isLoggedIn = function () {
  88. return !!this.authIdentity;
  89. };
  90. /**
  91. * Get authorized login.
  92. */
  93. JitsiConference.prototype.getAuthLogin = function () {
  94. return this.authIdentity;
  95. };
  96. /**
  97. * Check if external authentication is enabled for this conference.
  98. */
  99. JitsiConference.prototype.isExternalAuthEnabled = function () {
  100. return this.room && this.room.moderator.isExternalAuthEnabled();
  101. };
  102. /**
  103. * Get url for external authentication.
  104. * @param {boolean} [urlForPopup] if true then return url for login popup,
  105. * else url of login page.
  106. * @returns {Promise}
  107. */
  108. JitsiConference.prototype.getExternalAuthUrl = function (urlForPopup) {
  109. return new Promise(function (resolve, reject) {
  110. if (!this.isExternalAuthEnabled()) {
  111. reject();
  112. return;
  113. }
  114. if (urlForPopup) {
  115. this.room.moderator.getPopupLoginUrl(resolve, reject);
  116. } else {
  117. this.room.moderator.getLoginUrl(resolve, reject);
  118. }
  119. }.bind(this));
  120. };
  121. /**
  122. * Returns the local tracks.
  123. */
  124. JitsiConference.prototype.getLocalTracks = function () {
  125. if (this.rtc) {
  126. return this.rtc.localStreams;
  127. } else {
  128. return [];
  129. }
  130. };
  131. /**
  132. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  133. * in JitsiConferenceEvents.
  134. * @param eventId the event ID.
  135. * @param handler handler for the event.
  136. *
  137. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  138. */
  139. JitsiConference.prototype.on = function (eventId, handler) {
  140. if(this.eventEmitter)
  141. this.eventEmitter.on(eventId, handler);
  142. };
  143. /**
  144. * Removes event listener
  145. * @param eventId the event ID.
  146. * @param [handler] optional, the specific handler to unbind
  147. *
  148. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  149. */
  150. JitsiConference.prototype.off = function (eventId, handler) {
  151. if(this.eventEmitter)
  152. this.eventEmitter.removeListener(eventId, handler);
  153. };
  154. // Common aliases for event emitter
  155. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on;
  156. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off;
  157. /**
  158. * Receives notifications from another participants for commands / custom events
  159. * (send by sendPresenceCommand method).
  160. * @param command {String} the name of the command
  161. * @param handler {Function} handler for the command
  162. */
  163. JitsiConference.prototype.addCommandListener = function (command, handler) {
  164. if(this.room)
  165. this.room.addPresenceListener(command, handler);
  166. };
  167. /**
  168. * Removes command listener
  169. * @param command {String} the name of the command
  170. */
  171. JitsiConference.prototype.removeCommandListener = function (command) {
  172. if(this.room)
  173. this.room.removePresenceListener(command);
  174. };
  175. /**
  176. * Sends text message to the other participants in the conference
  177. * @param message the text message.
  178. */
  179. JitsiConference.prototype.sendTextMessage = function (message) {
  180. if(this.room)
  181. this.room.sendMessage(message);
  182. };
  183. /**
  184. * Send presence command.
  185. * @param name the name of the command.
  186. * @param values Object with keys and values that will be send.
  187. **/
  188. JitsiConference.prototype.sendCommand = function (name, values) {
  189. if(this.room) {
  190. this.room.addToPresence(name, values);
  191. this.room.sendPresence();
  192. }
  193. };
  194. /**
  195. * Send presence command one time.
  196. * @param name the name of the command.
  197. * @param values Object with keys and values that will be send.
  198. **/
  199. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  200. this.sendCommand(name, values);
  201. this.removeCommand(name);
  202. };
  203. /**
  204. * Send presence command.
  205. * @param name the name of the command.
  206. * @param values Object with keys and values that will be send.
  207. * @param persistent if false the command will be sent only one time
  208. **/
  209. JitsiConference.prototype.removeCommand = function (name) {
  210. if(this.room)
  211. this.room.removeFromPresence(name);
  212. };
  213. /**
  214. * Sets the display name for this conference.
  215. * @param name the display name to set
  216. */
  217. JitsiConference.prototype.setDisplayName = function(name) {
  218. if(this.room){
  219. // remove previously set nickname
  220. this.room.removeFromPresence("nick");
  221. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  222. this.room.sendPresence();
  223. }
  224. };
  225. /**
  226. * Adds JitsiLocalTrack object to the conference.
  227. * @param track the JitsiLocalTrack object.
  228. */
  229. JitsiConference.prototype.addTrack = function (track) {
  230. this.room.addStream(track.getOriginalStream(), function () {
  231. this.rtc.addLocalStream(track);
  232. if (this.startAudioMuted && track.isAudioTrack()) {
  233. track.mute();
  234. }
  235. if (this.startVideoMuted && track.isVideoTrack()) {
  236. track.mute();
  237. }
  238. if (track.startMuted) {
  239. track.mute();
  240. }
  241. this.tracks.push(track);
  242. var muteHandler = this._fireMuteChangeEvent.bind(this, track);
  243. var stopHandler = this.removeTrack.bind(this, track);
  244. var audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  245. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, muteHandler);
  246. track.addEventListener(JitsiTrackEvents.TRACK_STOPPED, stopHandler);
  247. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, audioLevelHandler);
  248. this.addEventListener(JitsiConferenceEvents.TRACK_REMOVED, function (someTrack) {
  249. if (someTrack !== track) {
  250. return;
  251. }
  252. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, muteHandler);
  253. track.removeEventListener(JitsiTrackEvents.TRACK_STOPPED, stopHandler);
  254. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, audioLevelHandler);
  255. });
  256. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  257. }.bind(this));
  258. };
  259. /**
  260. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  261. * @param audioLevel the audio level
  262. */
  263. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  264. this.eventEmitter.emit(
  265. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  266. this.myUserId(), audioLevel);
  267. };
  268. /**
  269. * Fires TRACK_MUTE_CHANGED change conference event.
  270. * @param track the JitsiTrack object related to the event.
  271. */
  272. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  273. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  274. };
  275. /**
  276. * Removes JitsiLocalTrack object to the conference.
  277. * @param track the JitsiLocalTrack object.
  278. */
  279. JitsiConference.prototype.removeTrack = function (track) {
  280. var pos = this.tracks.indexOf(track);
  281. if (pos > -1) {
  282. this.tracks.splice(pos, 1);
  283. }
  284. if(!this.room){
  285. if(this.rtc)
  286. this.rtc.removeLocalStream(track);
  287. return;
  288. }
  289. this.room.removeStream(track.getOriginalStream(), function(){
  290. this.rtc.removeLocalStream(track);
  291. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  292. }.bind(this));
  293. };
  294. /**
  295. * Get role of the local user.
  296. * @returns {string} user role: 'moderator' or 'none'
  297. */
  298. JitsiConference.prototype.getRole = function () {
  299. return this.room.role;
  300. };
  301. /**
  302. * Check if local user is moderator.
  303. * @returns {boolean} true if local user is moderator, false otherwise.
  304. */
  305. JitsiConference.prototype.isModerator = function () {
  306. return this.room.isModerator();
  307. };
  308. /**
  309. * Set password for the room.
  310. * @param {string} password new password for the room.
  311. * @returns {Promise}
  312. */
  313. JitsiConference.prototype.lock = function (password) {
  314. if (!this.isModerator()) {
  315. return Promise.reject();
  316. }
  317. var conference = this;
  318. return new Promise(function (resolve, reject) {
  319. conference.room.lockRoom(password || "", function () {
  320. resolve();
  321. }, function (err) {
  322. reject(err);
  323. }, function () {
  324. reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED);
  325. });
  326. });
  327. };
  328. /**
  329. * Remove password from the room.
  330. * @returns {Promise}
  331. */
  332. JitsiConference.prototype.unlock = function () {
  333. return this.lock();
  334. };
  335. /**
  336. * Elects the participant with the given id to be the selected participant or the speaker.
  337. * @param id the identifier of the participant
  338. */
  339. JitsiConference.prototype.selectParticipant = function(participantId) {
  340. if (this.rtc) {
  341. this.rtc.selectedEndpoint(participantId);
  342. }
  343. };
  344. /**
  345. *
  346. * @param id the identifier of the participant
  347. */
  348. JitsiConference.prototype.pinParticipant = function(participantId) {
  349. if (this.rtc) {
  350. this.rtc.pinEndpoint(participantId);
  351. }
  352. };
  353. /**
  354. * Returns the list of participants for this conference.
  355. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  356. */
  357. JitsiConference.prototype.getParticipants = function() {
  358. return Object.keys(this.participants).map(function (key) {
  359. return this.participants[key];
  360. }, this);
  361. };
  362. /**
  363. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  364. * undefined if there isn't one).
  365. * @param id the id of the participant.
  366. */
  367. JitsiConference.prototype.getParticipantById = function(id) {
  368. return this.participants[id];
  369. };
  370. /**
  371. * Kick participant from this conference.
  372. * @param {string} id id of the participant to kick
  373. */
  374. JitsiConference.prototype.kickParticipant = function (id) {
  375. var participant = this.getParticipantById(id);
  376. if (!participant) {
  377. return;
  378. }
  379. this.room.kick(participant.getJid());
  380. };
  381. /**
  382. * Kick participant from this conference.
  383. * @param {string} id id of the participant to kick
  384. */
  385. JitsiConference.prototype.muteParticipant = function (id) {
  386. var participant = this.getParticipantById(id);
  387. if (!participant) {
  388. return;
  389. }
  390. this.room.muteParticipant(participant.getJid(), true);
  391. };
  392. JitsiConference.prototype.onMemberJoined = function (jid, nick, role) {
  393. var id = Strophe.getResourceFromJid(jid);
  394. if (id === 'focus' || this.myUserId() === id) {
  395. return;
  396. }
  397. var participant = new JitsiParticipant(jid, this, nick);
  398. participant._role = role;
  399. this.participants[id] = participant;
  400. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  401. this.xmpp.connection.disco.info(
  402. jid, "node", function(iq) {
  403. participant._supportsDTMF = $(iq).find(
  404. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  405. this.updateDTMFSupport();
  406. }.bind(this)
  407. );
  408. };
  409. JitsiConference.prototype.onMemberLeft = function (jid) {
  410. var id = Strophe.getResourceFromJid(jid);
  411. if (id === 'focus' || this.myUserId() === id) {
  412. return;
  413. }
  414. var participant = this.participants[id];
  415. delete this.participants[id];
  416. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  417. };
  418. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  419. var id = Strophe.getResourceFromJid(jid);
  420. var participant = this.getParticipantById(id);
  421. if (!participant) {
  422. return;
  423. }
  424. participant._role = role;
  425. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  426. };
  427. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  428. var id = Strophe.getResourceFromJid(jid);
  429. var participant = this.getParticipantById(id);
  430. if (!participant) {
  431. return;
  432. }
  433. participant._displayName = displayName;
  434. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  435. };
  436. JitsiConference.prototype.onTrackAdded = function (track) {
  437. var id = track.getParticipantId();
  438. var participant = this.getParticipantById(id);
  439. if (!participant) {
  440. return;
  441. }
  442. // add track to JitsiParticipant
  443. participant._tracks.push(track);
  444. var emitter = this.eventEmitter;
  445. track.addEventListener(
  446. JitsiTrackEvents.TRACK_STOPPED,
  447. function () {
  448. // remove track from JitsiParticipant
  449. var pos = participant._tracks.indexOf(track);
  450. if (pos > -1) {
  451. participant._tracks.splice(pos, 1);
  452. }
  453. emitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  454. }
  455. );
  456. track.addEventListener(
  457. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  458. function () {
  459. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  460. }
  461. );
  462. track.addEventListener(
  463. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  464. function (audioLevel) {
  465. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  466. }
  467. );
  468. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  469. };
  470. JitsiConference.prototype.updateDTMFSupport = function () {
  471. var somebodySupportsDTMF = false;
  472. var participants = this.getParticipants();
  473. // check if at least 1 participant supports DTMF
  474. for (var i = 0; i < participants.length; i += 1) {
  475. if (participants[i].supportsDTMF()) {
  476. somebodySupportsDTMF = true;
  477. break;
  478. }
  479. }
  480. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  481. this.somebodySupportsDTMF = somebodySupportsDTMF;
  482. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  483. }
  484. };
  485. /**
  486. * Allows to check if there is at least one user in the conference
  487. * that supports DTMF.
  488. * @returns {boolean} true if somebody supports DTMF, false otherwise
  489. */
  490. JitsiConference.prototype.isDTMFSupported = function () {
  491. return this.somebodySupportsDTMF;
  492. };
  493. /**
  494. * Returns the local user's ID
  495. * @return {string} local user's ID
  496. */
  497. JitsiConference.prototype.myUserId = function () {
  498. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  499. };
  500. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  501. if (!this.dtmfManager) {
  502. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  503. if (!connection) {
  504. logger.warn("cannot sendTones: no conneciton");
  505. return;
  506. }
  507. var tracks = this.getLocalTracks().filter(function (track) {
  508. return track.isAudioTrack();
  509. });
  510. if (!tracks.length) {
  511. logger.warn("cannot sendTones: no local audio stream");
  512. return;
  513. }
  514. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  515. }
  516. this.dtmfManager.sendTones(tones, duration, pause);
  517. };
  518. /**
  519. * Returns true if the recording is supproted and false if not.
  520. */
  521. JitsiConference.prototype.isRecordingSupported = function () {
  522. if(this.room)
  523. return this.room.isRecordingSupported();
  524. return false;
  525. };
  526. /**
  527. * Returns null if the recording is not supported, "on" if the recording started
  528. * and "off" if the recording is not started.
  529. */
  530. JitsiConference.prototype.getRecordingState = function () {
  531. if(this.room)
  532. return this.room.getRecordingState();
  533. return "off";
  534. }
  535. /**
  536. * Returns the url of the recorded video.
  537. */
  538. JitsiConference.prototype.getRecordingURL = function () {
  539. if(this.room)
  540. return this.room.getRecordingURL();
  541. return null;
  542. }
  543. /**
  544. * Starts/stops the recording
  545. */
  546. JitsiConference.prototype.toggleRecording = function (options) {
  547. if(this.room)
  548. return this.room.toggleRecording(options, function (status, error) {
  549. this.eventEmitter.emit(
  550. JitsiConferenceEvents.RECORDING_STATE_CHANGED, status, error);
  551. }.bind(this));
  552. this.eventEmitter.emit(
  553. JitsiConferenceEvents.RECORDING_STATE_CHANGED, "error",
  554. new Error("The conference is not created yet!"));
  555. }
  556. /**
  557. * Returns true if the SIP calls are supported and false otherwise
  558. */
  559. JitsiConference.prototype.isSIPCallingSupported = function () {
  560. if(this.room)
  561. return this.room.isSIPCallingSupported();
  562. return false;
  563. }
  564. /**
  565. * Dials a number.
  566. * @param number the number
  567. */
  568. JitsiConference.prototype.dial = function (number) {
  569. if(this.room)
  570. return this.room.dial(number);
  571. return new Promise(function(resolve, reject){
  572. reject(new Error("The conference is not created yet!"))});
  573. }
  574. /**
  575. * Hangup an existing call
  576. */
  577. JitsiConference.prototype.hangup = function () {
  578. if(this.room)
  579. return this.room.hangup();
  580. return new Promise(function(resolve, reject){
  581. reject(new Error("The conference is not created yet!"))});
  582. }
  583. /**
  584. * Returns the phone number for joining the conference.
  585. */
  586. JitsiConference.prototype.getPhoneNumber = function () {
  587. if(this.room)
  588. return this.room.getPhoneNumber();
  589. return null;
  590. }
  591. /**
  592. * Returns the pin for joining the conference with phone.
  593. */
  594. JitsiConference.prototype.getPhonePin = function () {
  595. if(this.room)
  596. return this.room.getPhonePin();
  597. return null;
  598. }
  599. /**
  600. * Returns the connection state for the current room. Its ice connection state
  601. * for its session.
  602. */
  603. JitsiConference.prototype.getConnectionState = function () {
  604. if(this.room)
  605. return this.room.getConnectionState();
  606. return null;
  607. }
  608. /**
  609. * Make all new participants mute their audio/video on join.
  610. * @param {boolean} audioMuted if audio should be muted.
  611. * @param {boolean} videoMuted if video should be muted.
  612. */
  613. JitsiConference.prototype.setStartMuted = function (audioMuted, videoMuted) {
  614. if (!this.isModerator()) {
  615. return;
  616. }
  617. this.room.removeFromPresence("startmuted");
  618. this.room.addToPresence("startmuted", {
  619. attributes: {
  620. audio: audioMuted,
  621. video: videoMuted,
  622. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  623. }
  624. });
  625. this.room.sendPresence();
  626. };
  627. /**
  628. * Check if audio is muted on join.
  629. */
  630. JitsiConference.prototype.isStartAudioMuted = function () {
  631. return this.startAudioMuted;
  632. };
  633. /**
  634. * Check if video is muted on join.
  635. */
  636. JitsiConference.prototype.isStartVideoMuted = function () {
  637. return this.startVideoMuted;
  638. };
  639. /**
  640. * Setups the listeners needed for the conference.
  641. * @param conference the conference
  642. */
  643. function setupListeners(conference) {
  644. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  645. conference.rtc.onIncommingCall(event);
  646. if(conference.statistics)
  647. conference.statistics.startRemoteStats(event.peerconnection);
  648. });
  649. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  650. function (data, sid, thessrc) {
  651. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  652. if (track) {
  653. conference.onTrackAdded(track);
  654. }
  655. }
  656. );
  657. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  658. function (value) {
  659. conference.rtc.setAudioMute(value);
  660. }
  661. );
  662. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  663. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  664. });
  665. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  666. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  667. });
  668. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  669. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  670. });
  671. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  672. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  673. });
  674. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  675. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  676. });
  677. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  678. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  679. });
  680. // FIXME
  681. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  682. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  683. // });
  684. conference.room.addListener(XMPPEvents.KICKED, function () {
  685. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  686. });
  687. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  688. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  689. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  690. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  691. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  692. });
  693. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  694. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  695. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  696. });
  697. conference.room.addListener(XMPPEvents.RECORDING_STATE_CHANGED,
  698. function () {
  699. conference.eventEmitter.emit(
  700. JitsiConferenceEvents.RECORDING_STATE_CHANGED);
  701. });
  702. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  703. conference.eventEmitter.emit(
  704. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  705. });
  706. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  707. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  708. });
  709. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  710. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.SETUP_FAILED);
  711. });
  712. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  713. conference.authEnabled = authEnabled;
  714. conference.authIdentity = authIdentity;
  715. });
  716. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  717. var id = Strophe.getResourceFromJid(jid);
  718. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  719. });
  720. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  721. if(conference.lastDominantSpeaker !== id && conference.room) {
  722. conference.lastDominantSpeaker = id;
  723. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  724. }
  725. });
  726. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  727. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  728. });
  729. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  730. function (lastNEndpoints, endpointsEnteringLastN) {
  731. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  732. lastNEndpoints, endpointsEnteringLastN);
  733. });
  734. conference.xmpp.addListener(XMPPEvents.PASSWORD_REQUIRED, function () {
  735. conference.eventEmitter.emit(JitsiConferenceErrors.PASSWORD_REQUIRED);
  736. });
  737. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS, function (audioMuted, videoMuted) {
  738. conference.startAudioMuted = audioMuted;
  739. conference.startVideoMuted = videoMuted;
  740. // mute existing local tracks because this is initial mute from Jicofo
  741. conference.tracks.forEach(function (track) {
  742. if (conference.startAudioMuted && track.isAudioTrack()) {
  743. track.mute();
  744. }
  745. if (conference.startVideoMuted && track.isVideoTrack()) {
  746. track.mute();
  747. }
  748. });
  749. var initiallyMuted = audioMuted || videoMuted;
  750. conference.eventEmitter.emit(
  751. JitsiConferenceEvents.START_MUTED,
  752. conference.startAudioMuted,
  753. conference.startVideoMuted,
  754. initiallyMuted
  755. );
  756. });
  757. conference.room.addPresenceListener("startmuted", function (data, from) {
  758. var isModerator = false;
  759. if (conference.myUserId() === from && conference.isModerator()) {
  760. isModerator = true;
  761. } else {
  762. var participant = conference.getParticipantById(from);
  763. if (participant && participant.isModerator()) {
  764. isModerator = true;
  765. }
  766. }
  767. if (!isModerator) {
  768. return;
  769. }
  770. var startAudioMuted = data.attributes.audio === 'true';
  771. var startVideoMuted = data.attributes.video === 'true';
  772. var updated = false;
  773. if (startAudioMuted !== conference.startAudioMuted) {
  774. conference.startAudioMuted = startAudioMuted;
  775. updated = true;
  776. }
  777. if (startVideoMuted !== conference.startVideoMuted) {
  778. conference.startVideoMuted = startVideoMuted;
  779. updated = true;
  780. }
  781. if (updated) {
  782. conference.eventEmitter.emit(
  783. JitsiConferenceEvents.START_MUTED,
  784. conference.startAudioMuted,
  785. conference.startVideoMuted,
  786. false
  787. );
  788. }
  789. });
  790. if(conference.statistics) {
  791. //FIXME: Maybe remove event should not be associated with the conference.
  792. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  793. var userId = null;
  794. var jid = conference.room.getJidBySSRC(ssrc);
  795. if (!jid)
  796. return;
  797. conference.rtc.setAudioLevel(jid, level);
  798. });
  799. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  800. function () {
  801. conference.statistics.dispose();
  802. });
  803. // FIXME: Maybe we should move this.
  804. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  805. // conference.room.updateDeviceAvailability(devices);
  806. // });
  807. }
  808. }
  809. module.exports = JitsiConference;