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

JitsiConference.js 35KB

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