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

JitsiConference.js 11KB

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