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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import { getLogger } from '@jitsi/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} [options.appData] - Data specific to the app/service that
  61. * the result file will be uploaded.
  62. * @param {string} [optional] options.broadcastId - The channel on which a
  63. * live stream will occur.
  64. * @param {string} options.mode - The mode in which recording should be
  65. * started. Recognized values are "file" and "stream".
  66. * @param {string} [optional] options.streamId - The stream key to be used
  67. * for live stream broadcasting. Required for live streaming.
  68. * @returns {Promise} A promise for starting a recording, which will pass
  69. * back the session on success. The promise resolves after receiving an
  70. * acknowledgment of the start request success or fail.
  71. */
  72. startRecording(options) {
  73. const session = new JibriSession({
  74. ...options,
  75. connection: this._chatRoom.connection
  76. });
  77. return session.start({
  78. appData: options.appData,
  79. broadcastId: options.broadcastId,
  80. focusMucJid: this._chatRoom.focusMucJid,
  81. streamId: options.streamId
  82. })
  83. .then(() => {
  84. // Only store the session and emit if the session has not been
  85. // added already. This is a workaround for the session getting
  86. // created due to a presence update to announce a "pending"
  87. // recording being received before JibriSession#start finishes.
  88. if (!this.getSession(session.getID())) {
  89. this._addSession(session);
  90. this._emitSessionUpdate(session);
  91. }
  92. return session;
  93. })
  94. .catch(error => {
  95. this._emitSessionUpdate(session);
  96. return Promise.reject(error);
  97. });
  98. }
  99. /**
  100. * Stop a recording session.
  101. *
  102. * @param {string} sessionID - The ID associated with the recording session
  103. * to be stopped.
  104. * @returns {Promise} The promise resolves after receiving an
  105. * acknowledgment of the stop request success or fail.
  106. */
  107. stopRecording(sessionID) {
  108. const session = this.getSession(sessionID);
  109. if (session) {
  110. return session.stop({ focusMucJid: this._chatRoom.focusMucJid });
  111. }
  112. return Promise.reject(new Error('Could not find session'));
  113. }
  114. /**
  115. * Stores a reference to the passed in JibriSession.
  116. *
  117. * @param {string} session - The JibriSession instance to store.
  118. * @returns {void}
  119. */
  120. _addSession(session) {
  121. this._sessions[session.getID()] = session;
  122. }
  123. /**
  124. * Create a new instance of a recording session and stores a reference to
  125. * it.
  126. *
  127. * @param {string} sessionID - The session ID of the recording in progress.
  128. * @param {string} status - The current status of the recording session.
  129. * @param {string} mode - The recording mode of the session.
  130. * @returns {JibriSession}
  131. */
  132. _createSession(sessionID, status, mode) {
  133. const session = new JibriSession({
  134. connection: this._chatRoom.connection,
  135. focusMucJid: this._chatRoom.focusMucJid,
  136. mode,
  137. sessionID,
  138. status
  139. });
  140. this._addSession(session);
  141. return session;
  142. }
  143. /**
  144. * Notifies listeners of an update to a recording session.
  145. *
  146. * @param {JibriSession} session - The session that has been updated.
  147. * @param {string|undefined} initiator - The jid of the initiator of the update.
  148. */
  149. _emitSessionUpdate(session, initiator) {
  150. this._chatRoom.eventEmitter.emit(
  151. XMPPEvents.RECORDER_STATE_CHANGED, session, initiator);
  152. }
  153. /**
  154. * Parses presence to update an existing JibriSession or to create a new
  155. * JibriSession.
  156. *
  157. * @param {Node} presence - An XMPP presence update.
  158. * @returns {void}
  159. */
  160. _handleFocusPresence(presence) {
  161. const jibriStatus = recordingXMLUtils.getFocusRecordingUpdate(presence);
  162. if (!jibriStatus) {
  163. return;
  164. }
  165. const { error, initiator, recordingMode, sessionID, status } = jibriStatus;
  166. // We'll look for an existing session or create one (in case we're a
  167. // participant joining a call with an existing recording going on).
  168. let session = this.getSession(sessionID);
  169. // Handle the case where a status update is received in presence but
  170. // the local participant has joined while the JibriSession has already
  171. // ended.
  172. if (!session && status === 'off') {
  173. logger.warn(
  174. 'Ignoring recording presence update',
  175. 'Received a new session with status off.');
  176. return;
  177. }
  178. // Jicofo sends updates via presence, and any extension in presence
  179. // is sent until it is explicitly removed. It's difficult for
  180. // Jicofo to know when a presence has been sent once, so it won't
  181. // remove jibri status extension. This means we may receive the same
  182. // status update more than once, so check for that here
  183. if (session
  184. && session.getStatus() === status
  185. && session.getError() === error) {
  186. logger.warn('Ignoring duplicate presence update: ',
  187. JSON.stringify(jibriStatus));
  188. return;
  189. }
  190. if (!session) {
  191. session = this._createSession(sessionID, status, recordingMode);
  192. }
  193. session.setStatus(status);
  194. if (error) {
  195. session.setError(error);
  196. }
  197. this._emitSessionUpdate(session, initiator);
  198. }
  199. /**
  200. * Handles updates from the Jibri which can broadcast a YouTube URL that
  201. * needs to be updated in a JibriSession.
  202. *
  203. * @param {Node} presence - An XMPP presence update.
  204. * @returns {void}
  205. */
  206. _handleJibriPresence(presence) {
  207. const { liveStreamViewURL, mode, sessionID }
  208. = recordingXMLUtils.getHiddenDomainUpdate(presence);
  209. if (!sessionID) {
  210. logger.warn(
  211. 'Ignoring potential jibri presence due to no session id.');
  212. return;
  213. }
  214. let session = this.getSession(sessionID);
  215. if (!session) {
  216. session = this._createSession(sessionID, '', mode);
  217. }
  218. session.setLiveStreamViewURL(liveStreamViewURL);
  219. this._emitSessionUpdate(session);
  220. }
  221. }
  222. export default RecordingManager;