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 6.6KB

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