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

JitsiConference.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* global Strophe, $ */
  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 RTCEvents = require("./service/RTC/RTCEvents");
  7. var EventEmitter = require("events");
  8. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  9. var JitsiParticipant = require("./JitsiParticipant");
  10. var Statistics = require("./modules/statistics/statistics");
  11. var JitsiDTMFManager = require('./modules/DTMF/JitsiDTMFManager');
  12. var JitsiTrackEvents = require("./JitsiTrackEvents");
  13. /**
  14. * Creates a JitsiConference object with the given name and properties.
  15. * Note: this constructor is not a part of the public API (objects should be
  16. * created using JitsiConnection.createConference).
  17. * @param options.config properties / settings related to the conference that will be created.
  18. * @param options.name the name of the conference
  19. * @param options.connection the JitsiConnection object for this JitsiConference.
  20. * @constructor
  21. */
  22. function JitsiConference(options) {
  23. if(!options.name || options.name.toLowerCase() !== options.name) {
  24. logger.error("Invalid conference name (no conference name passed or it"
  25. + "contains invalid characters like capital letters)!");
  26. return;
  27. }
  28. this.options = options;
  29. this.connection = this.options.connection;
  30. this.xmpp = this.connection.xmpp;
  31. this.eventEmitter = new EventEmitter();
  32. this.room = this.xmpp.createRoom(this.options.name, null, null, this.options.config);
  33. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  34. this.rtc = new RTC(this.room, options);
  35. if(!RTC.options.disableAudioLevels)
  36. this.statistics = new Statistics();
  37. setupListeners(this);
  38. this.participants = {};
  39. this.lastActiveSpeaker = null;
  40. this.dtmfManager = null;
  41. this.somebodySupportsDTMF = false;
  42. }
  43. /**
  44. * Joins the conference.
  45. * @param password {string} the password
  46. */
  47. JitsiConference.prototype.join = function (password) {
  48. if(this.room)
  49. this.room.join(password, this.connection.tokenPassword);
  50. };
  51. /**
  52. * Leaves the conference.
  53. */
  54. JitsiConference.prototype.leave = function () {
  55. if(this.xmpp && this.room)
  56. this.xmpp.leaveRoom(this.room.roomjid);
  57. this.room = null;
  58. };
  59. /**
  60. * Returns the local tracks.
  61. */
  62. JitsiConference.prototype.getLocalTracks = function () {
  63. if (this.rtc) {
  64. return this.rtc.localStreams;
  65. } else {
  66. return [];
  67. }
  68. };
  69. /**
  70. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  71. * in JitsiConferenceEvents.
  72. * @param eventId the event ID.
  73. * @param handler handler for the event.
  74. *
  75. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  76. */
  77. JitsiConference.prototype.on = function (eventId, handler) {
  78. if(this.eventEmitter)
  79. this.eventEmitter.on(eventId, handler);
  80. };
  81. /**
  82. * Removes event listener
  83. * @param eventId the event ID.
  84. * @param [handler] optional, the specific handler to unbind
  85. *
  86. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  87. */
  88. JitsiConference.prototype.off = function (eventId, handler) {
  89. if(this.eventEmitter)
  90. this.eventEmitter.removeListener(eventId, handler);
  91. };
  92. // Common aliases for event emitter
  93. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on;
  94. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off;
  95. /**
  96. * Receives notifications from another participants for commands / custom events
  97. * (send by sendPresenceCommand method).
  98. * @param command {String} the name of the command
  99. * @param handler {Function} handler for the command
  100. */
  101. JitsiConference.prototype.addCommandListener = function (command, handler) {
  102. if(this.room)
  103. this.room.addPresenceListener(command, handler);
  104. };
  105. /**
  106. * Removes command listener
  107. * @param command {String} the name of the command
  108. */
  109. JitsiConference.prototype.removeCommandListener = function (command) {
  110. if(this.room)
  111. this.room.removePresenceListener(command);
  112. };
  113. /**
  114. * Sends text message to the other participants in the conference
  115. * @param message the text message.
  116. */
  117. JitsiConference.prototype.sendTextMessage = function (message) {
  118. if(this.room)
  119. this.room.sendMessage(message);
  120. };
  121. /**
  122. * Send presence command.
  123. * @param name the name of the command.
  124. * @param values Object with keys and values that will be send.
  125. **/
  126. JitsiConference.prototype.sendCommand = function (name, values) {
  127. if(this.room) {
  128. this.room.addToPresence(name, values);
  129. this.room.sendPresence();
  130. }
  131. };
  132. /**
  133. * Send presence command one time.
  134. * @param name the name of the command.
  135. * @param values Object with keys and values that will be send.
  136. **/
  137. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  138. this.sendCommand(name, values);
  139. this.removeCommand(name);
  140. };
  141. /**
  142. * Send presence command.
  143. * @param name the name of the command.
  144. * @param values Object with keys and values that will be send.
  145. * @param persistent if false the command will be sent only one time
  146. **/
  147. JitsiConference.prototype.removeCommand = function (name) {
  148. if(this.room)
  149. this.room.removeFromPresence(name);
  150. };
  151. /**
  152. * Sets the display name for this conference.
  153. * @param name the display name to set
  154. */
  155. JitsiConference.prototype.setDisplayName = function(name) {
  156. if(this.room){
  157. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  158. this.room.sendPresence();
  159. }
  160. };
  161. /**
  162. * Adds JitsiLocalTrack object to the conference.
  163. * @param track the JitsiLocalTrack object.
  164. */
  165. JitsiConference.prototype.addTrack = function (track) {
  166. this.room.addStream(track.getOriginalStream(), function () {
  167. this.rtc.addLocalStream(track);
  168. var muteHandler = this._fireMuteChangeEvent.bind(this, track);
  169. var stopHandler = this.removeTrack.bind(this, track);
  170. var audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  171. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, muteHandler);
  172. track.addEventListener(JitsiTrackEvents.TRACK_STOPPED, stopHandler);
  173. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, audioLevelHandler);
  174. this.addEventListener(JitsiConferenceEvents.TRACK_REMOVED, function (someTrack) {
  175. if (someTrack !== track) {
  176. return;
  177. }
  178. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, muteHandler);
  179. track.removeEventListener(JitsiTrackEvents.TRACK_STOPPED, stopHandler);
  180. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, audioLevelHandler);
  181. });
  182. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  183. }.bind(this));
  184. };
  185. /**
  186. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  187. * @param audioLevel the audio level
  188. */
  189. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  190. this.eventEmitter.emit(
  191. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  192. this.myUserId(), audioLevel);
  193. };
  194. /**
  195. * Fires TRACK_MUTE_CHANGED change conference event.
  196. * @param track the JitsiTrack object related to the event.
  197. */
  198. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  199. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  200. };
  201. /**
  202. * Removes JitsiLocalTrack object to the conference.
  203. * @param track the JitsiLocalTrack object.
  204. */
  205. JitsiConference.prototype.removeTrack = function (track) {
  206. this.room.removeStream(track.getOriginalStream(), function(){
  207. this.rtc.removeLocalStream(track);
  208. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  209. }.bind(this));
  210. };
  211. /**
  212. * Get role of the local user.
  213. * @returns {string} user role: 'moderator' or 'none'
  214. */
  215. JitsiConference.prototype.getRole = function () {
  216. return this.room.role;
  217. };
  218. /**
  219. * Check if local user is moderator.
  220. * @returns {boolean} true if local user is moderator, false otherwise.
  221. */
  222. JitsiConference.prototype.isModerator = function () {
  223. return this.room.isModerator();
  224. };
  225. /**
  226. * Elects the participant with the given id to be the selected participant or the speaker.
  227. * @param id the identifier of the participant
  228. */
  229. JitsiConference.prototype.selectParticipant = function(participantId) {
  230. if (this.rtc) {
  231. this.rtc.selectedEndpoint(participantId);
  232. }
  233. };
  234. /**
  235. *
  236. * @param id the identifier of the participant
  237. */
  238. JitsiConference.prototype.pinParticipant = function(participantId) {
  239. if (this.rtc) {
  240. this.rtc.pinEndpoint(participantId);
  241. }
  242. };
  243. /**
  244. * Returns the list of participants for this conference.
  245. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  246. */
  247. JitsiConference.prototype.getParticipants = function() {
  248. return Object.keys(this.participants).map(function (key) {
  249. return this.participants[key];
  250. }, this);
  251. };
  252. /**
  253. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  254. * undefined if there isn't one).
  255. * @param id the id of the participant.
  256. */
  257. JitsiConference.prototype.getParticipantById = function(id) {
  258. return this.participants[id];
  259. };
  260. JitsiConference.prototype.onMemberJoined = function (jid, email, nick) {
  261. var id = Strophe.getResourceFromJid(jid);
  262. var participant = new JitsiParticipant(id, this, nick);
  263. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id);
  264. this.participants[id] = participant;
  265. this.connection.xmpp.connection.disco.info(
  266. jid, "node", function(iq) {
  267. participant._supportsDTMF = $(iq).find(
  268. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  269. this.updateDTMFSupport();
  270. }.bind(this)
  271. );
  272. };
  273. JitsiConference.prototype.onMemberLeft = function (jid) {
  274. var id = Strophe.getResourceFromJid(jid);
  275. delete this.participants[id];
  276. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id);
  277. };
  278. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  279. var id = Strophe.getResourceFromJid(jid);
  280. var participant = this.getParticipantById(id);
  281. if (!participant) {
  282. return;
  283. }
  284. participant._role = role;
  285. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  286. };
  287. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  288. var id = Strophe.getResourceFromJid(jid);
  289. var participant = this.getParticipantById(id);
  290. if (!participant) {
  291. return;
  292. }
  293. participant._displayName = displayName;
  294. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  295. };
  296. JitsiConference.prototype.onTrackAdded = function (track) {
  297. var id = track.getParticipantId();
  298. var participant = this.getParticipantById(id);
  299. if (!participant) {
  300. return;
  301. }
  302. // add track to JitsiParticipant
  303. participant._tracks.push(track);
  304. var emitter = this.eventEmitter;
  305. track.addEventListener(
  306. JitsiTrackEvents.TRACK_STOPPED,
  307. function () {
  308. // remove track from JitsiParticipant
  309. var pos = participant._tracks.indexOf(track);
  310. if (pos > -1) {
  311. participant._tracks.splice(pos, 1);
  312. }
  313. emitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  314. }
  315. );
  316. track.addEventListener(
  317. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  318. function () {
  319. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  320. }
  321. );
  322. track.addEventListener(
  323. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  324. function (audioLevel) {
  325. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  326. }
  327. );
  328. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  329. };
  330. JitsiConference.prototype.updateDTMFSupport = function () {
  331. var somebodySupportsDTMF = false;
  332. var participants = this.getParticipants();
  333. // check if at least 1 participant supports DTMF
  334. for (var i = 0; i < participants.length; i += 1) {
  335. if (participants[i].supportsDTMF()) {
  336. somebodySupportsDTMF = true;
  337. break;
  338. }
  339. }
  340. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  341. this.somebodySupportsDTMF = somebodySupportsDTMF;
  342. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  343. }
  344. };
  345. /**
  346. * Allows to check if there is at least one user in the conference
  347. * that supports DTMF.
  348. * @returns {boolean} true if somebody supports DTMF, false otherwise
  349. */
  350. JitsiConference.prototype.isDTMFSupported = function () {
  351. return this.somebodySupportsDTMF;
  352. };
  353. /**
  354. * Returns the local user's ID
  355. * @return {string} local user's ID
  356. */
  357. JitsiConference.prototype.myUserId = function () {
  358. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  359. };
  360. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  361. if (!this.dtmfManager) {
  362. var connection = this.connection.xmpp.connection.jingle.activecall.peerconnection;
  363. if (!connection) {
  364. logger.warn("cannot sendTones: no conneciton");
  365. return;
  366. }
  367. var tracks = this.getLocalTracks().filter(function (track) {
  368. return track.isAudioTrack();
  369. });
  370. if (!tracks.length) {
  371. logger.warn("cannot sendTones: no local audio stream");
  372. return;
  373. }
  374. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  375. }
  376. this.dtmfManager.sendTones(tones, duration, pause);
  377. };
  378. /**
  379. * Returns true if the recording is supproted and false if not.
  380. */
  381. JitsiConference.prototype.isRecordingSupported = function () {
  382. if(this.room)
  383. return this.room.isRecordingSupported();
  384. return false;
  385. };
  386. /**
  387. * Returns null if the recording is not supported, "on" if the recording started
  388. * and "off" if the recording is not started.
  389. */
  390. JitsiConference.prototype.getRecordingState = function () {
  391. if(this.room)
  392. return this.room.getRecordingState();
  393. return "off";
  394. }
  395. /**
  396. * Returns the url of the recorded video.
  397. */
  398. JitsiConference.prototype.getRecordingURL = function () {
  399. if(this.room)
  400. return this.room.getRecordingURL();
  401. return null;
  402. }
  403. /**
  404. * Starts/stops the recording
  405. * @param token a token for authentication.
  406. */
  407. JitsiConference.prototype.toggleRecording = function (token, followEntity) {
  408. if(this.room)
  409. return this.room.toggleRecording(token, followEntity);
  410. return new Promise(function(resolve, reject){
  411. reject(new Error("The conference is not created yet!"))});
  412. }
  413. /**
  414. * Dials a number.
  415. * @param number the number
  416. */
  417. JitsiConference.prototype.dial = function (number) {
  418. if(this.room)
  419. return this.room.dial(number);
  420. return new Promise(function(resolve, reject){
  421. reject(new Error("The conference is not created yet!"))});
  422. }
  423. /**
  424. * Hangup an existing call
  425. */
  426. JitsiConference.prototype.hangup = function () {
  427. if(this.room)
  428. return this.room.hangup();
  429. return new Promise(function(resolve, reject){
  430. reject(new Error("The conference is not created yet!"))});
  431. }
  432. /**
  433. * Returns the phone number for joining the conference.
  434. */
  435. JitsiConference.prototype.getPhoneNumber = function () {
  436. if(this.room)
  437. return this.room.getPhoneNumber();
  438. return null;
  439. }
  440. /**
  441. * Returns the pin for joining the conference with phone.
  442. */
  443. JitsiConference.prototype.getPhonePin = function () {
  444. if(this.room)
  445. return this.room.getPhonePin();
  446. return null;
  447. }
  448. /**
  449. * Returns the connection state for the current room. Its ice connection state
  450. * for its session.
  451. */
  452. JitsiConference.prototype.getConnectionState = function () {
  453. if(this.room)
  454. return this.room.getConnectionState();
  455. return null;
  456. }
  457. /**
  458. * Setups the listeners needed for the conference.
  459. * @param conference the conference
  460. */
  461. function setupListeners(conference) {
  462. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  463. conference.rtc.onIncommingCall(event);
  464. if(conference.statistics)
  465. conference.statistics.startRemoteStats(event.peerconnection);
  466. });
  467. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  468. function (data, sid, thessrc) {
  469. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  470. if (track) {
  471. conference.onTrackAdded(track);
  472. }
  473. }
  474. );
  475. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  476. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  477. });
  478. // FIXME
  479. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  480. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  481. // });
  482. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  483. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  484. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  485. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  486. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  487. });
  488. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  489. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  490. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  491. });
  492. conference.room.addListener(XMPPEvents.RECORDING_STATE_CHANGED,
  493. function () {
  494. conference.eventEmitter.emit(
  495. JitsiConferenceEvents.RECORDING_STATE_CHANGED);
  496. });
  497. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  498. conference.eventEmitter.emit(
  499. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  500. });
  501. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  502. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  503. });
  504. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  505. conference.eventEmitter.emit(JitsiConferenceEvents.SETUP_FAILED);
  506. });
  507. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  508. if(conference.lastActiveSpeaker !== id && conference.room) {
  509. conference.lastActiveSpeaker = id;
  510. conference.eventEmitter.emit(JitsiConferenceEvents.ACTIVE_SPEAKER_CHANGED, id);
  511. }
  512. });
  513. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  514. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  515. });
  516. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  517. function (lastNEndpoints, endpointsEnteringLastN) {
  518. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  519. lastNEndpoints, endpointsEnteringLastN);
  520. });
  521. if(conference.statistics) {
  522. //FIXME: Maybe remove event should not be associated with the conference.
  523. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  524. var userId = null;
  525. var jid = conference.room.getJidBySSRC(ssrc);
  526. if (!jid)
  527. return;
  528. conference.rtc.setAudioLevel(jid, level);
  529. });
  530. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  531. function () {
  532. conference.statistics.dispose();
  533. });
  534. // FIXME: Maybe we should move this.
  535. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  536. // conference.room.updateDeviceAvailability(devices);
  537. // });
  538. }
  539. }
  540. module.exports = JitsiConference;