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.

RecordingManager.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { getLogger } from 'jitsi-meet-logger';
  2. import XMPPEvents from '../../service/xmpp/XMPPEvents';
  3. import JibriSession from './JibriSession';
  4. import recordingXMLUtils from './recordingXMLUtils';
  5. const logger = getLogger(__filename);
  6. /**
  7. * A class responsible for starting and stopping recording sessions and emitting
  8. * state updates for them.
  9. */
  10. class RecordingManager {
  11. /**
  12. * Initialize {@code RecordingManager} with other objects that are necessary
  13. * for starting a recording.
  14. *
  15. * @param {ChatRoom} chatRoom - The chat room to handle.
  16. * @returns {void}
  17. */
  18. constructor(chatRoom) {
  19. /**
  20. * All known recording sessions from the current conference.
  21. */
  22. this._sessions = {};
  23. this._chatRoom = chatRoom;
  24. this.onPresence = this.onPresence.bind(this);
  25. this._chatRoom.eventEmitter.addListener(
  26. XMPPEvents.PRESENCE_RECEIVED, this.onPresence);
  27. }
  28. /**
  29. * Finds an existing recording session by session ID.
  30. *
  31. * @param {string} sessionID - The session ID associated with the recording.
  32. * @returns {JibriSession|undefined}
  33. */
  34. getSession(sessionID) {
  35. return this._sessions[sessionID];
  36. }
  37. /**
  38. * Callback to invoke to parse through a presence update to find recording
  39. * related updates (from Jibri participant doing the recording and the
  40. * focus which controls recording).
  41. *
  42. * @param {Object} event - The presence data from the pubsub event.
  43. * @param {Node} event.presence - An XMPP presence update.
  44. * @param {boolean} event.fromHiddenDomain - Whether or not the update comes
  45. * from a participant that is trusted but not visible, as would be the case
  46. * with the Jibri recorder participant.
  47. * @returns {void}
  48. */
  49. onPresence({ fromHiddenDomain, presence }) {
  50. if (recordingXMLUtils.isFromFocus(presence)) {
  51. this._handleFocusPresence(presence);
  52. } else if (fromHiddenDomain) {
  53. this._handleJibriPresence(presence);
  54. }
  55. }
  56. /**
  57. * Start a recording session.
  58. *
  59. * @param {Object} options - Configuration for the recording.
  60. * @param {string} [optional] options.broadcastId - The channel on which a
  61. * live stream will occur.
  62. * @param {string} options.mode - The mode in which recording should be
  63. * started. Recognized values are "file" and "stream".
  64. * @param {string} [optional] options.streamId - The stream key to be used
  65. * for live stream broadcasting. Required for live streaming.
  66. * @returns {Promise} A promise for starting a recording, which will pass
  67. * back the session on success. The promise resolves after receiving an
  68. * acknowledgment of the start request success or fail.
  69. */
  70. startRecording(options) {
  71. const session = new JibriSession({
  72. ...options,
  73. connection: this._chatRoom.connection
  74. });
  75. return session.start({
  76. broadcastId: options.broadcastId,
  77. focusMucJid: this._chatRoom.focusMucJid,
  78. streamId: options.streamId
  79. })
  80. .then(() => {
  81. // Only store the session and emit if the session has not been
  82. // added already. This is a workaround for the session getting
  83. // created due to a presence update to announce a "pending"
  84. // recording being received before JibriSession#start finishes.
  85. if (!this.getSession(session.getID())) {
  86. this._addSession(session);
  87. this._emitSessionUpdate(session);
  88. }
  89. return session;
  90. })
  91. .catch(error => {
  92. this._emitSessionUpdate(session);
  93. return Promise.reject(error);
  94. });
  95. }
  96. /**
  97. * Stop a recording session.
  98. *
  99. * @param {string} sessionID - The ID associated with the recording session
  100. * to be stopped.
  101. * @returns {Promise} The promise resolves after receiving an
  102. * acknowledgment of the stop request success or fail.
  103. */
  104. stopRecording(sessionID) {
  105. const session = this.getSession(sessionID);
  106. if (session) {
  107. return session.stop({ focusMucJid: this._chatRoom.focusMucJid });
  108. }
  109. return Promise.reject(new Error('Could not find session'));
  110. }
  111. /**
  112. * Stores a reference to the passed in JibriSession.
  113. *
  114. * @param {string} session - The JibriSession instance to store.
  115. * @returns {void}
  116. */
  117. _addSession(session) {
  118. this._sessions[session.getID()] = session;
  119. }
  120. /**
  121. * Create a new instance of a recording session and stores a reference to
  122. * it.
  123. *
  124. * @param {string} sessionID - The session ID of the recording in progress.
  125. * @param {string} status - The current status of the recording session.
  126. * @param {string} mode - The recording mode of the session.
  127. * @returns {JibriSession}
  128. */
  129. _createSession(sessionID, status, mode) {
  130. const session = new JibriSession({
  131. connection: this._chatRoom.connection,
  132. focusMucJid: this._chatRoom.focusMucJid,
  133. mode,
  134. sessionID,
  135. status
  136. });
  137. this._addSession(session);
  138. return session;
  139. }
  140. /**
  141. * Notifies listeners of an update to a recording session.
  142. *
  143. * @param {JibriSession} session - The session that has been updated.
  144. */
  145. _emitSessionUpdate(session) {
  146. this._chatRoom.eventEmitter.emit(
  147. XMPPEvents.RECORDER_STATE_CHANGED, session);
  148. }
  149. /**
  150. * Parses presence to update an existing JibriSession or to create a new
  151. * JibriSession.
  152. *
  153. * @param {Node} presence - An XMPP presence update.
  154. * @returns {void}
  155. */
  156. _handleFocusPresence(presence) {
  157. const jibriStatus = recordingXMLUtils.getFocusRecordingUpdate(presence);
  158. if (!jibriStatus) {
  159. return;
  160. }
  161. const { sessionID, status, error, recordingMode } = jibriStatus;
  162. // We'll look for an existing session or create one (in case we're a
  163. // participant joining a call with an existing recording going on).
  164. let session = this.getSession(sessionID);
  165. // Handle the case where a status update is received in presence but
  166. // the local participant has joined while the JibriSession has already
  167. // ended.
  168. if (!session && status === 'off') {
  169. logger.warn(
  170. 'Ignoring recording presence update',
  171. 'Received a new session with status off.');
  172. return;
  173. }
  174. if (!session) {
  175. session = this._createSession(sessionID, status, recordingMode);
  176. }
  177. session.setStatus(status);
  178. if (error) {
  179. session.setError(error);
  180. }
  181. this._emitSessionUpdate(session);
  182. }
  183. /**
  184. * Handles updates from the Jibri which can broadcast a YouTube URL that
  185. * needs to be updated in a JibriSession.
  186. *
  187. * @param {Node} presence - An XMPP presence update.
  188. * @returns {void}
  189. */
  190. _handleJibriPresence(presence) {
  191. const { liveStreamViewURL, mode, sessionID }
  192. = recordingXMLUtils.getHiddenDomainUpdate(presence);
  193. if (!sessionID) {
  194. logger.warn(
  195. 'Ignoring potential jibri presence due to no session id.');
  196. return;
  197. }
  198. let session = this.getSession(sessionID);
  199. if (!session) {
  200. session = this._createSession(sessionID, '', mode);
  201. }
  202. session.setLiveStreamViewURL(liveStreamViewURL);
  203. this._emitSessionUpdate(session);
  204. }
  205. }
  206. export default RecordingManager;