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

JitsiConference.js 35KB

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