You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JitsiConference.js 8.4KB

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