Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

JitsiConference.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. * Elects the participant with the given id to be the selected participant or the speaker.
  182. * @param id the identifier of the participant
  183. */
  184. JitsiConference.prototype.selectParticipant = function(participantId) {
  185. if (this.rtc) {
  186. this.rtc.selectedEndpoint(participantId);
  187. }
  188. };
  189. /**
  190. *
  191. * @param id the identifier of the participant
  192. */
  193. JitsiConference.prototype.pinParticipant = function(participantId) {
  194. if (this.rtc) {
  195. this.rtc.pinEndpoint(participantId);
  196. }
  197. };
  198. /**
  199. * Returns the list of participants for this conference.
  200. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  201. */
  202. JitsiConference.prototype.getParticipants = function() {
  203. return Object.keys(this.participants).map(function (key) {
  204. return this.participants[key];
  205. }, this);
  206. };
  207. /**
  208. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  209. * undefined if there isn't one).
  210. * @param id the id of the participant.
  211. */
  212. JitsiConference.prototype.getParticipantById = function(id) {
  213. return this.participants[id];
  214. };
  215. JitsiConference.prototype.onMemberJoined = function (jid, email, nick) {
  216. var id = Strophe.getResourceFromJid(jid);
  217. var participant = new JitsiParticipant(id, this, nick);
  218. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id);
  219. this.participants[id] = participant;
  220. this.connection.xmpp.connection.disco.info(
  221. jid, "" /* node */, function(iq) {
  222. participant._supportsDTMF = $(iq).find('>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  223. this.updateDTMFSupport();
  224. }.bind(this)
  225. );
  226. };
  227. JitsiConference.prototype.onMemberLeft = function (jid) {
  228. var id = Strophe.getResourceFromJid(jid);
  229. delete this.participants[id];
  230. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id);
  231. };
  232. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  233. var id = Strophe.getResourceFromJid(jid);
  234. var participant = this.getParticipantById(id);
  235. if (!participant) {
  236. return;
  237. }
  238. participant._displayName = displayName;
  239. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  240. };
  241. JitsiConference.prototype.onTrackAdded = function (track) {
  242. var id = track.getParticipantId();
  243. var participant = this.getParticipantById(id);
  244. participant._tracks.push(track);
  245. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  246. };
  247. JitsiConference.prototype.onTrackRemoved = function (track) {
  248. var id = track.getParticipantId();
  249. var participant = this.getParticipantById(id);
  250. var pos = participant._tracks.indexOf(track);
  251. if (pos > -1) {
  252. participant._tracks.splice(pos, 1);
  253. }
  254. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  255. };
  256. JitsiConference.prototype.updateDTMFSupport = function () {
  257. var somebodySupportsDTMF = false;
  258. var participants = this.getParticipants();
  259. // check if at least 1 participant supports DTMF
  260. for (var i = 0; i < participants.length; i += 1) {
  261. if (participants[i].supportsDTMF()) {
  262. somebodySupportsDTMF = true;
  263. break;
  264. }
  265. }
  266. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  267. this.somebodySupportsDTMF = somebodySupportsDTMF;
  268. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  269. }
  270. };
  271. /**
  272. * Allows to check if there is at least one user in the conference
  273. * that supports DTMF.
  274. * @returns {boolean} true if somebody supports DTMF, false otherwise
  275. */
  276. JitsiConference.prototype.isDTMFSupported = function () {
  277. return this.somebodySupportsDTMF;
  278. };
  279. /**
  280. * Returns the local user's ID
  281. * @return {string} local user's ID
  282. */
  283. JitsiConference.prototype.myUserId = function () {
  284. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  285. };
  286. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  287. if (!this.dtmfManager) {
  288. var connection = this.connection.xmpp.connection.jingle.activecall.peerconnection;
  289. if (!connection) {
  290. logger.warn("cannot sendTones: no conneciton");
  291. return;
  292. }
  293. var tracks = this.getLocalTracks().filter(function (track) {
  294. return track.isAudioTrack();
  295. });
  296. if (!tracks.length) {
  297. logger.warn("cannot sendTones: no local audio stream");
  298. return;
  299. }
  300. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  301. }
  302. this.dtmfManager.sendTones(tones, duration, pause);
  303. };
  304. /**
  305. * Setups the listeners needed for the conference.
  306. * @param conference the conference
  307. */
  308. function setupListeners(conference) {
  309. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  310. conference.rtc.onIncommingCall(event);
  311. if(conference.statistics)
  312. conference.statistics.startRemoteStats(event.peerconnection);
  313. });
  314. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  315. conference.rtc.createRemoteStream.bind(conference.rtc));
  316. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  317. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  318. });
  319. // FIXME
  320. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  321. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  322. // });
  323. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  324. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  325. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  326. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  327. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  328. });
  329. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  330. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  331. });
  332. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  333. conference.eventEmitter.emit(JitsiConferenceEvents.SETUP_FAILED);
  334. });
  335. conference.rtc.addListener(
  336. StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, conference.onTrackAdded.bind(conference)
  337. );
  338. //FIXME: Maybe remove event should not be associated with the conference.
  339. conference.rtc.addListener(
  340. StreamEventTypes.EVENT_TYPE_REMOTE_ENDED, conference.onTrackRemoved.bind(conference)
  341. );
  342. //FIXME: Maybe remove event should not be associated with the conference.
  343. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, function (stream) {
  344. conference.removeTrack(stream);
  345. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, stream);
  346. });
  347. conference.rtc.addListener(StreamEventTypes.TRACK_MUTE_CHANGED, function (track) {
  348. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  349. });
  350. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  351. if(conference.lastActiveSpeaker !== id && conference.room) {
  352. conference.lastActiveSpeaker = id;
  353. conference.eventEmitter.emit(JitsiConferenceEvents.ACTIVE_SPEAKER_CHANGED, id);
  354. }
  355. });
  356. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  357. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  358. });
  359. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  360. function (lastNEndpoints, endpointsEnteringLastN) {
  361. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  362. lastNEndpoints, endpointsEnteringLastN);
  363. });
  364. if(conference.statistics) {
  365. //FIXME: Maybe remove event should not be associated with the conference.
  366. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  367. var userId = null;
  368. if (ssrc === Statistics.LOCAL_JID) {
  369. userId = conference.myUserId();
  370. } else {
  371. var jid = conference.room.getJidBySSRC(ssrc);
  372. if (!jid)
  373. return;
  374. userId = Strophe.getResourceFromJid(jid);
  375. }
  376. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  377. userId, level);
  378. });
  379. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_LOCAL_CREATED, function (stream) {
  380. conference.statistics.startLocalStats(stream);
  381. });
  382. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  383. function () {
  384. conference.statistics.dispose();
  385. });
  386. // FIXME: Maybe we should move this.
  387. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  388. // conference.room.updateDeviceAvailability(devices);
  389. // });
  390. }
  391. }
  392. module.exports = JitsiConference;