Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

JitsiConference.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. var logger = require("jitsi-meet-logger").getLogger(__filename);
  2. var RTC = require("./modules/RTC/RTC");
  3. var XMPPEvents = require("./service/xmpp/XMPPEvents");
  4. var StreamEventTypes = require("./service/RTC/StreamEventTypes");
  5. var RTCEvents = require("./service/RTC/RTCEvents");
  6. var EventEmitter = require("events");
  7. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  8. var JitsiParticipant = require("./JitsiParticipant");
  9. var Statistics = require("./modules/statistics/statistics");
  10. /**
  11. * Creates a JitsiConference object with the given name and properties.
  12. * Note: this constructor is not a part of the public API (objects should be
  13. * created using JitsiConnection.createConference).
  14. * @param options.config properties / settings related to the conference that will be created.
  15. * @param options.name the name of the conference
  16. * @param options.connection the JitsiConnection object for this JitsiConference.
  17. * @constructor
  18. */
  19. function JitsiConference(options) {
  20. if(!options.name || options.name.toLowerCase() !== options.name) {
  21. logger.error("Invalid conference name (no conference name passed or it"
  22. + "contains invalid characters like capital letters)!");
  23. return;
  24. }
  25. this.options = options;
  26. this.connection = this.options.connection;
  27. this.xmpp = this.connection.xmpp;
  28. this.eventEmitter = new EventEmitter();
  29. this.room = this.xmpp.createRoom(this.options.name, null, null, this.options.config);
  30. this.rtc = new RTC(this.room, options);
  31. if(!options.config.disableAudioLevels)
  32. this.statistics = new Statistics();
  33. setupListeners(this);
  34. this.participants = {};
  35. this.lastActiveSpeaker = null;
  36. }
  37. /**
  38. * Joins the conference.
  39. * @param password {string} the password
  40. */
  41. JitsiConference.prototype.join = function (password) {
  42. if(this.room)
  43. this.room.join(password, this.connection.tokenPassword);
  44. }
  45. /**
  46. * Leaves the conference.
  47. */
  48. JitsiConference.prototype.leave = function () {
  49. if(this.xmpp)
  50. this.xmpp.leaveRoom(this.room.roomjid);
  51. this.room = null;
  52. }
  53. /**
  54. * Creates the media tracks and returns them trough the callback.
  55. * @param options Object with properties / settings specifying the tracks which should be created.
  56. * should be created or some additional configurations about resolution for example.
  57. * @returns {Promise.<{Array.<JitsiTrack>}, JitsiConferenceError>} A promise that returns an array of created JitsiTracks if resolved,
  58. * or a JitsiConferenceError if rejected.
  59. */
  60. JitsiConference.prototype.createLocalTracks = function (options) {
  61. if(this.rtc)
  62. return this.rtc.obtainAudioAndVideoPermissions(options || {});
  63. }
  64. /**
  65. * Returns the local tracks.
  66. */
  67. JitsiConference.prototype.getLocalTracks = function () {
  68. if(this.rtc)
  69. return this.rtc.localStreams;
  70. };
  71. /**
  72. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  73. * in JitsiConferenceEvents.
  74. * @param eventId the event ID.
  75. * @param handler handler for the event.
  76. *
  77. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  78. */
  79. JitsiConference.prototype.on = function (eventId, handler) {
  80. if(this.eventEmitter)
  81. this.eventEmitter.on(eventId, handler);
  82. }
  83. /**
  84. * Removes event listener
  85. * @param eventId the event ID.
  86. * @param [handler] optional, the specific handler to unbind
  87. *
  88. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  89. */
  90. JitsiConference.prototype.off = function (eventId, handler) {
  91. if(this.eventEmitter)
  92. this.eventEmitter.removeListener(eventId, listener);
  93. }
  94. // Common aliases for event emitter
  95. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on
  96. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off
  97. /**
  98. * Receives notifications from another participants for commands / custom events(send by sendPresenceCommand method).
  99. * @param command {String} the name of the command
  100. * @param handler {Function} handler for the command
  101. */
  102. JitsiConference.prototype.addCommandListener = function (command, handler) {
  103. if(this.room)
  104. this.room.addPresenceListener(command, handler);
  105. }
  106. /**
  107. * Removes command listener
  108. * @param command {String} the name of the command
  109. */
  110. JitsiConference.prototype.removeCommandListener = function (command) {
  111. if(this.room)
  112. this.room.removePresenceListener(command);
  113. }
  114. /**
  115. * Sends text message to the other participants in the conference
  116. * @param message the text message.
  117. */
  118. JitsiConference.prototype.sendTextMessage = function (message) {
  119. if(this.room)
  120. this.room.sendMessage(message);
  121. }
  122. /**
  123. * Send presence command.
  124. * @param name the name of the command.
  125. * @param values Object with keys and values that will be send.
  126. **/
  127. JitsiConference.prototype.sendCommand = function (name, values) {
  128. if(this.room) {
  129. this.room.addToPresence(name, values);
  130. this.room.sendPresence();
  131. }
  132. }
  133. /**
  134. * Send presence command one time.
  135. * @param name the name of the command.
  136. * @param values Object with keys and values that will be send.
  137. **/
  138. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  139. this.sendCommand(name, values);
  140. this.removeCommand(name);
  141. }
  142. /**
  143. * Send presence command.
  144. * @param name the name of the command.
  145. * @param values Object with keys and values that will be send.
  146. * @param persistent if false the command will be sent only one time
  147. **/
  148. JitsiConference.prototype.removeCommand = function (name) {
  149. if(this.room)
  150. this.room.removeFromPresence(name);
  151. }
  152. /**
  153. * Sets the display name for this conference.
  154. * @param name the display name to set
  155. */
  156. JitsiConference.prototype.setDisplayName = function(name) {
  157. if(this.room){
  158. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  159. this.room.sendPresence();
  160. }
  161. }
  162. /**
  163. * Elects the participant with the given id to be the selected participant or the speaker.
  164. * @param id the identifier of the participant
  165. */
  166. JitsiConference.prototype.selectParticipant = function(participantId) {
  167. if (this.rtc) {
  168. this.rtc.selectedEndpoint(participantId);
  169. }
  170. }
  171. /**
  172. *
  173. * @param id the identifier of the participant
  174. */
  175. JitsiConference.prototype.pinParticipant = function(participantId) {
  176. if(this.rtc)
  177. this.rtc.pinEndpoint(participantId);
  178. }
  179. /**
  180. * Returns the list of participants for this conference.
  181. * @return Object a list of participant identifiers containing all conference participants.
  182. */
  183. JitsiConference.prototype.getParticipants = function() {
  184. return this.participants;
  185. }
  186. /**
  187. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  188. * null if there isn't one).
  189. * @param id the id of the participant.
  190. */
  191. JitsiConference.prototype.getParticipantById = function(id) {
  192. if(this.participants)
  193. return this.participants[id];
  194. return null;
  195. }
  196. JitsiConference.prototype.onMemberJoined = function (jid, email, nick) {
  197. if(this.eventEmitter)
  198. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, Strophe.getResourceFromJid(jid));
  199. // this.participants[jid] = new JitsiParticipant();
  200. }
  201. /**
  202. * Returns the local user's ID
  203. * @return {string} local user's ID
  204. */
  205. JitsiConference.prototype.myUserId = function () {
  206. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  207. }
  208. /**
  209. * Setups the listeners needed for the conference.
  210. * @param conference the conference
  211. */
  212. function setupListeners(conference) {
  213. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  214. conference.rtc.onIncommingCall(event);
  215. if(conference.statistics)
  216. conference.statistics.startRemoteStats(event.peerconnection);
  217. });
  218. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  219. conference.rtc.createRemoteStream.bind(conference.rtc));
  220. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, function (stream) {
  221. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, stream);
  222. });
  223. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_REMOTE_ENDED, function (stream) {
  224. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, stream);
  225. });
  226. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, function (stream) {
  227. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, stream);
  228. });
  229. conference.rtc.addListener(StreamEventTypes.TRACK_MUTE_CHANGED, function (track) {
  230. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  231. });
  232. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  233. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  234. });
  235. // FIXME
  236. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  237. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  238. // });
  239. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  240. if(conference.lastActiveSpeaker !== id && conference.room
  241. && conference.myUserId() !== id) {
  242. conference.lastActiveSpeaker = id;
  243. conference.eventEmitter.emit(JitsiConferenceEvents.ACTIVE_SPEAKER_CHANGED, id);
  244. }
  245. });
  246. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  247. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  248. });
  249. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  250. function (lastNEndpoints, endpointsEnteringLastN) {
  251. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  252. lastNEndpoints, endpointsEnteringLastN);
  253. });
  254. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  255. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT,function (jid) {
  256. conference.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, Strophe.getResourceFromJid(jid));
  257. });
  258. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, function (from, displayName) {
  259. conference.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED,
  260. Strophe.getResourceFromJid(from), displayName);
  261. });
  262. if(conference.statistics) {
  263. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  264. var userId = null;
  265. if (ssrc === Statistics.LOCAL_JID) {
  266. userId = conference.myUserId();
  267. } else {
  268. var jid = conference.room.getJidBySSRC(ssrc);
  269. if (!jid)
  270. return;
  271. userId = Strophe.getResourceFromJid(jid);
  272. }
  273. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  274. userId, level);
  275. });
  276. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_LOCAL_CREATED, function (stream) {
  277. conference.statistics.startLocalStats(stream);
  278. });
  279. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  280. function () {
  281. conference.statistics.dispose();
  282. });
  283. }
  284. }
  285. module.exports = JitsiConference;