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

JitsiConference.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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)
  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('>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  268. this.updateDTMFSupport();
  269. }.bind(this)
  270. );
  271. };
  272. JitsiConference.prototype.onMemberLeft = function (jid) {
  273. var id = Strophe.getResourceFromJid(jid);
  274. delete this.participants[id];
  275. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id);
  276. };
  277. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  278. var id = Strophe.getResourceFromJid(jid);
  279. var participant = this.getParticipantById(id);
  280. if (!participant) {
  281. return;
  282. }
  283. participant._role = role;
  284. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  285. };
  286. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  287. var id = Strophe.getResourceFromJid(jid);
  288. var participant = this.getParticipantById(id);
  289. if (!participant) {
  290. return;
  291. }
  292. participant._displayName = displayName;
  293. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  294. };
  295. JitsiConference.prototype.onTrackAdded = function (track) {
  296. var id = track.getParticipantId();
  297. var participant = this.getParticipantById(id);
  298. if (!participant) {
  299. return;
  300. }
  301. // add track to JitsiParticipant
  302. participant._tracks.push(track);
  303. var emitter = this.eventEmitter;
  304. track.addEventListener(
  305. JitsiTrackEvents.TRACK_STOPPED,
  306. function () {
  307. // remove track from JitsiParticipant
  308. var pos = participant._tracks.indexOf(track);
  309. if (pos > -1) {
  310. participant._tracks.splice(pos, 1);
  311. }
  312. emitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  313. }
  314. );
  315. track.addEventListener(
  316. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  317. function () {
  318. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  319. }
  320. );
  321. track.addEventListener(
  322. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  323. function (audioLevel) {
  324. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  325. }
  326. );
  327. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  328. };
  329. JitsiConference.prototype.updateDTMFSupport = function () {
  330. var somebodySupportsDTMF = false;
  331. var participants = this.getParticipants();
  332. // check if at least 1 participant supports DTMF
  333. for (var i = 0; i < participants.length; i += 1) {
  334. if (participants[i].supportsDTMF()) {
  335. somebodySupportsDTMF = true;
  336. break;
  337. }
  338. }
  339. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  340. this.somebodySupportsDTMF = somebodySupportsDTMF;
  341. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  342. }
  343. };
  344. /**
  345. * Allows to check if there is at least one user in the conference
  346. * that supports DTMF.
  347. * @returns {boolean} true if somebody supports DTMF, false otherwise
  348. */
  349. JitsiConference.prototype.isDTMFSupported = function () {
  350. return this.somebodySupportsDTMF;
  351. };
  352. /**
  353. * Returns the local user's ID
  354. * @return {string} local user's ID
  355. */
  356. JitsiConference.prototype.myUserId = function () {
  357. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  358. };
  359. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  360. if (!this.dtmfManager) {
  361. var connection = this.connection.xmpp.connection.jingle.activecall.peerconnection;
  362. if (!connection) {
  363. logger.warn("cannot sendTones: no conneciton");
  364. return;
  365. }
  366. var tracks = this.getLocalTracks().filter(function (track) {
  367. return track.isAudioTrack();
  368. });
  369. if (!tracks.length) {
  370. logger.warn("cannot sendTones: no local audio stream");
  371. return;
  372. }
  373. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  374. }
  375. this.dtmfManager.sendTones(tones, duration, pause);
  376. };
  377. /**
  378. * Setups the listeners needed for the conference.
  379. * @param conference the conference
  380. */
  381. function setupListeners(conference) {
  382. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  383. conference.rtc.onIncommingCall(event);
  384. if(conference.statistics)
  385. conference.statistics.startRemoteStats(event.peerconnection);
  386. });
  387. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  388. function (data, sid, thessrc) {
  389. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  390. if (track) {
  391. conference.onTrackAdded(track);
  392. }
  393. }
  394. );
  395. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  396. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  397. });
  398. // FIXME
  399. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  400. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  401. // });
  402. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  403. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  404. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  405. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  406. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  407. });
  408. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  409. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  410. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  411. });
  412. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  413. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  414. });
  415. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  416. conference.eventEmitter.emit(JitsiConferenceEvents.SETUP_FAILED);
  417. });
  418. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  419. if(conference.lastActiveSpeaker !== id && conference.room) {
  420. conference.lastActiveSpeaker = id;
  421. conference.eventEmitter.emit(JitsiConferenceEvents.ACTIVE_SPEAKER_CHANGED, id);
  422. }
  423. });
  424. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  425. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  426. });
  427. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  428. function (lastNEndpoints, endpointsEnteringLastN) {
  429. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  430. lastNEndpoints, endpointsEnteringLastN);
  431. });
  432. if(conference.statistics) {
  433. //FIXME: Maybe remove event should not be associated with the conference.
  434. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  435. var userId = null;
  436. var jid = conference.room.getJidBySSRC(ssrc);
  437. if (!jid)
  438. return;
  439. conference.rtc.setAudioLevel(jid, level);
  440. });
  441. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  442. function () {
  443. conference.statistics.dispose();
  444. });
  445. // FIXME: Maybe we should move this.
  446. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  447. // conference.room.updateDeviceAvailability(devices);
  448. // });
  449. }
  450. }
  451. module.exports = JitsiConference;