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

JitsiConference.js 34KB

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