Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JitsiConference.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 StreamEventTypes = require("./service/RTC/StreamEventTypes");
  7. var RTCEvents = require("./service/RTC/RTCEvents");
  8. var EventEmitter = require("events");
  9. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  10. var JitsiParticipant = require("./JitsiParticipant");
  11. var Statistics = require("./modules/statistics/statistics");
  12. var JitsiDTMFManager = require('./modules/DTMF/JitsiDTMFManager');
  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(!options.config.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(send by sendPresenceCommand method).
  97. * @param command {String} the name of the command
  98. * @param handler {Function} handler for the command
  99. */
  100. JitsiConference.prototype.addCommandListener = function (command, handler) {
  101. if(this.room)
  102. this.room.addPresenceListener(command, handler);
  103. };
  104. /**
  105. * Removes command listener
  106. * @param command {String} the name of the command
  107. */
  108. JitsiConference.prototype.removeCommandListener = function (command) {
  109. if(this.room)
  110. this.room.removePresenceListener(command);
  111. };
  112. /**
  113. * Sends text message to the other participants in the conference
  114. * @param message the text message.
  115. */
  116. JitsiConference.prototype.sendTextMessage = function (message) {
  117. if(this.room)
  118. this.room.sendMessage(message);
  119. };
  120. /**
  121. * Send presence command.
  122. * @param name the name of the command.
  123. * @param values Object with keys and values that will be send.
  124. **/
  125. JitsiConference.prototype.sendCommand = function (name, values) {
  126. if(this.room) {
  127. this.room.addToPresence(name, values);
  128. this.room.sendPresence();
  129. }
  130. };
  131. /**
  132. * Send presence command one time.
  133. * @param name the name of the command.
  134. * @param values Object with keys and values that will be send.
  135. **/
  136. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  137. this.sendCommand(name, values);
  138. this.removeCommand(name);
  139. };
  140. /**
  141. * Send presence command.
  142. * @param name the name of the command.
  143. * @param values Object with keys and values that will be send.
  144. * @param persistent if false the command will be sent only one time
  145. **/
  146. JitsiConference.prototype.removeCommand = function (name) {
  147. if(this.room)
  148. this.room.removeFromPresence(name);
  149. };
  150. /**
  151. * Sets the display name for this conference.
  152. * @param name the display name to set
  153. */
  154. JitsiConference.prototype.setDisplayName = function(name) {
  155. if(this.room){
  156. this.room.addToPresence("nick", {
  157. attributes: {
  158. xmlns: 'http://jabber.org/protocol/nick'
  159. }, value: name
  160. });
  161. this.room.sendPresence();
  162. }
  163. };
  164. /**
  165. * Adds JitsiLocalTrack object to the conference.
  166. * @param track the JitsiLocalTrack object.
  167. */
  168. JitsiConference.prototype.addTrack = function (track) {
  169. this.rtc.addLocalStream(track);
  170. this.room.addStream(track.getOriginalStream(), function () {});
  171. };
  172. /**
  173. * Removes JitsiLocalTrack object to the conference.
  174. * @param track the JitsiLocalTrack object.
  175. */
  176. JitsiConference.prototype.removeTrack = function (track) {
  177. this.room.removeStream(track.getOriginalStream());
  178. this.rtc.removeLocalStream(track);
  179. };
  180. /**
  181. * Get role of the local user.
  182. * @returns {string} user role: 'moderator' or 'none'
  183. */
  184. JitsiConference.prototype.getRole = function () {
  185. return this.room.role;
  186. };
  187. /**
  188. * Check if local user is moderator.
  189. * @returns {boolean} true if local user is moderator, false otherwise.
  190. */
  191. JitsiConference.prototype.isModerator = function () {
  192. return this.room.isModerator();
  193. };
  194. /**
  195. * Elects the participant with the given id to be the selected participant or the speaker.
  196. * @param id the identifier of the participant
  197. */
  198. JitsiConference.prototype.selectParticipant = function(participantId) {
  199. if (this.rtc) {
  200. this.rtc.selectedEndpoint(participantId);
  201. }
  202. };
  203. /**
  204. *
  205. * @param id the identifier of the participant
  206. */
  207. JitsiConference.prototype.pinParticipant = function(participantId) {
  208. if (this.rtc) {
  209. this.rtc.pinEndpoint(participantId);
  210. }
  211. };
  212. /**
  213. * Returns the list of participants for this conference.
  214. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  215. */
  216. JitsiConference.prototype.getParticipants = function() {
  217. return Object.keys(this.participants).map(function (key) {
  218. return this.participants[key];
  219. }, this);
  220. };
  221. /**
  222. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  223. * undefined if there isn't one).
  224. * @param id the id of the participant.
  225. */
  226. JitsiConference.prototype.getParticipantById = function(id) {
  227. return this.participants[id];
  228. };
  229. JitsiConference.prototype.onMemberJoined = function (jid, email, nick) {
  230. var id = Strophe.getResourceFromJid(jid);
  231. var participant = new JitsiParticipant(id, this, nick);
  232. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id);
  233. this.participants[id] = participant;
  234. this.connection.xmpp.connection.disco.info(
  235. jid, "" /* node */, function(iq) {
  236. participant._supportsDTMF = $(iq).find('>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  237. this.updateDTMFSupport();
  238. }.bind(this)
  239. );
  240. };
  241. JitsiConference.prototype.onMemberLeft = function (jid) {
  242. var id = Strophe.getResourceFromJid(jid);
  243. delete this.participants[id];
  244. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id);
  245. };
  246. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  247. var id = Strophe.getResourceFromJid(jid);
  248. var participant = this.getParticipantById(id);
  249. if (!participant) {
  250. return;
  251. }
  252. participant._role = role;
  253. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  254. };
  255. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  256. var id = Strophe.getResourceFromJid(jid);
  257. var participant = this.getParticipantById(id);
  258. if (!participant) {
  259. return;
  260. }
  261. participant._displayName = displayName;
  262. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  263. };
  264. JitsiConference.prototype.onTrackAdded = function (track) {
  265. var id = track.getParticipantId();
  266. var participant = this.getParticipantById(id);
  267. participant._tracks.push(track);
  268. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  269. };
  270. JitsiConference.prototype.onTrackRemoved = function (track) {
  271. var id = track.getParticipantId();
  272. var participant = this.getParticipantById(id);
  273. var pos = participant._tracks.indexOf(track);
  274. if (pos > -1) {
  275. participant._tracks.splice(pos, 1);
  276. }
  277. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  278. };
  279. JitsiConference.prototype.updateDTMFSupport = function () {
  280. var somebodySupportsDTMF = false;
  281. var participants = this.getParticipants();
  282. // check if at least 1 participant supports DTMF
  283. for (var i = 0; i < participants.length; i += 1) {
  284. if (participants[i].supportsDTMF()) {
  285. somebodySupportsDTMF = true;
  286. break;
  287. }
  288. }
  289. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  290. this.somebodySupportsDTMF = somebodySupportsDTMF;
  291. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  292. }
  293. };
  294. /**
  295. * Allows to check if there is at least one user in the conference
  296. * that supports DTMF.
  297. * @returns {boolean} true if somebody supports DTMF, false otherwise
  298. */
  299. JitsiConference.prototype.isDTMFSupported = function () {
  300. return this.somebodySupportsDTMF;
  301. };
  302. /**
  303. * Returns the local user's ID
  304. * @return {string} local user's ID
  305. */
  306. JitsiConference.prototype.myUserId = function () {
  307. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  308. };
  309. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  310. if (!this.dtmfManager) {
  311. var connection = this.connection.xmpp.connection.jingle.activecall.peerconnection;
  312. if (!connection) {
  313. logger.warn("cannot sendTones: no conneciton");
  314. return;
  315. }
  316. var tracks = this.getLocalTracks().filter(function (track) {
  317. return track.isAudioTrack();
  318. });
  319. if (!tracks.length) {
  320. logger.warn("cannot sendTones: no local audio stream");
  321. return;
  322. }
  323. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  324. }
  325. this.dtmfManager.sendTones(tones, duration, pause);
  326. };
  327. /**
  328. * Setups the listeners needed for the conference.
  329. * @param conference the conference
  330. */
  331. function setupListeners(conference) {
  332. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  333. conference.rtc.onIncommingCall(event);
  334. if(conference.statistics)
  335. conference.statistics.startRemoteStats(event.peerconnection);
  336. });
  337. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  338. conference.rtc.createRemoteStream.bind(conference.rtc));
  339. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  340. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  341. });
  342. // FIXME
  343. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  344. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  345. // });
  346. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  347. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  348. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  349. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  350. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  351. });
  352. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  353. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  354. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  355. });
  356. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  357. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  358. });
  359. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  360. conference.eventEmitter.emit(JitsiConferenceEvents.SETUP_FAILED);
  361. });
  362. conference.rtc.addListener(
  363. StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, conference.onTrackAdded.bind(conference)
  364. );
  365. //FIXME: Maybe remove event should not be associated with the conference.
  366. conference.rtc.addListener(
  367. StreamEventTypes.EVENT_TYPE_REMOTE_ENDED, conference.onTrackRemoved.bind(conference)
  368. );
  369. //FIXME: Maybe remove event should not be associated with the conference.
  370. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, function (stream) {
  371. conference.removeTrack(stream);
  372. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, stream);
  373. });
  374. conference.rtc.addListener(StreamEventTypes.TRACK_MUTE_CHANGED, function (track) {
  375. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  376. });
  377. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  378. if(conference.lastActiveSpeaker !== id && conference.room) {
  379. conference.lastActiveSpeaker = id;
  380. conference.eventEmitter.emit(JitsiConferenceEvents.ACTIVE_SPEAKER_CHANGED, id);
  381. }
  382. });
  383. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  384. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  385. });
  386. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  387. function (lastNEndpoints, endpointsEnteringLastN) {
  388. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  389. lastNEndpoints, endpointsEnteringLastN);
  390. });
  391. if(conference.statistics) {
  392. //FIXME: Maybe remove event should not be associated with the conference.
  393. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  394. var userId = null;
  395. if (ssrc === Statistics.LOCAL_JID) {
  396. userId = conference.myUserId();
  397. } else {
  398. var jid = conference.room.getJidBySSRC(ssrc);
  399. if (!jid)
  400. return;
  401. userId = Strophe.getResourceFromJid(jid);
  402. }
  403. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  404. userId, level);
  405. });
  406. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_LOCAL_CREATED, function (stream) {
  407. conference.statistics.startLocalStats(stream);
  408. });
  409. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  410. function () {
  411. conference.statistics.dispose();
  412. });
  413. // FIXME: Maybe we should move this.
  414. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  415. // conference.room.updateDeviceAvailability(devices);
  416. // });
  417. }
  418. }
  419. module.exports = JitsiConference;