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

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