Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

JitsiConference.js 10KB

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