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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.onMemberLeft = this.onMemberLeft.bind(this);
  26. this._chatRoom.eventEmitter.addListener(
  27. XMPPEvents.PRESENCE_RECEIVED, this.onPresence);
  28. this._chatRoom.eventEmitter.addListener(
  29. XMPPEvents.MUC_MEMBER_LEFT, this.onMemberLeft);
  30. }
  31. /**
  32. * Finds an existing recording session by session ID.
  33. *
  34. * @param {string} sessionID - The session ID associated with the recording.
  35. * @returns {JibriSession|undefined}
  36. */
  37. getSession(sessionID) {
  38. return this._sessions[sessionID];
  39. }
  40. /**
  41. * Find a session with a specific jibri JID.
  42. *
  43. * @param {string} jibriJid the JID to search for.
  44. * @returns
  45. */
  46. getSessionByJibriJid(jibriJid) {
  47. let s;
  48. Object.values(this._sessions).forEach(session => {
  49. if (session.getJibriJid() === jibriJid) {
  50. s = session;
  51. }
  52. });
  53. return s;
  54. }
  55. /**
  56. * Callback to invoke to parse through a presence update to find recording
  57. * related updates (from Jibri participant doing the recording and the
  58. * focus which controls recording).
  59. *
  60. * @param {Object} event - The presence data from the pubsub event.
  61. * @param {Node} event.presence - An XMPP presence update.
  62. * @param {boolean} event.fromHiddenDomain - Whether or not the update comes
  63. * from a participant that is trusted but not visible, as would be the case
  64. * with the Jibri recorder participant.
  65. * @returns {void}
  66. */
  67. onPresence({ fromHiddenDomain, presence }) {
  68. if (recordingXMLUtils.isFromFocus(presence)) {
  69. this._handleFocusPresence(presence);
  70. } else if (fromHiddenDomain) {
  71. this._handleJibriPresence(presence);
  72. }
  73. }
  74. /**
  75. * Handle a participant leaving the room.
  76. * @param {string} jid the JID of the participant that left.
  77. */
  78. onMemberLeft(jid) {
  79. const session = this.getSessionByJibriJid(jid);
  80. if (session) {
  81. const prevStatus = session.getStatus();
  82. // Setting to ''
  83. session.setStatus('');
  84. session.setJibriJid(null);
  85. if (session.getStatus() !== prevStatus) {
  86. this._emitSessionUpdate(session);
  87. }
  88. }
  89. }
  90. /**
  91. * Start a recording session.
  92. *
  93. * @param {Object} options - Configuration for the recording.
  94. * @param {string} [options.appData] - Data specific to the app/service that
  95. * the result file will be uploaded.
  96. * @param {string} [optional] options.broadcastId - The channel on which a
  97. * live stream will occur.
  98. * @param {string} options.mode - The mode in which recording should be
  99. * started. Recognized values are "file" and "stream".
  100. * @param {string} [optional] options.streamId - The stream key to be used
  101. * for live stream broadcasting. Required for live streaming.
  102. * @returns {Promise} A promise for starting a recording, which will pass
  103. * back the session on success. The promise resolves after receiving an
  104. * acknowledgment of the start request success or fail.
  105. */
  106. startRecording(options) {
  107. const session = new JibriSession({
  108. ...options,
  109. connection: this._chatRoom.connection
  110. });
  111. return session.start({
  112. appData: options.appData,
  113. broadcastId: options.broadcastId,
  114. focusMucJid: this._chatRoom.focusMucJid,
  115. streamId: options.streamId
  116. })
  117. .then(() => {
  118. // Only store the session and emit if the session has not been
  119. // added already. This is a workaround for the session getting
  120. // created due to a presence update to announce a "pending"
  121. // recording being received before JibriSession#start finishes.
  122. if (!this.getSession(session.getID())) {
  123. this._addSession(session);
  124. this._emitSessionUpdate(session);
  125. }
  126. return session;
  127. })
  128. .catch(error => {
  129. this._emitSessionUpdate(session);
  130. return Promise.reject(error);
  131. });
  132. }
  133. /**
  134. * Stop a recording session.
  135. *
  136. * @param {string} sessionID - The ID associated with the recording session
  137. * to be stopped.
  138. * @returns {Promise} The promise resolves after receiving an
  139. * acknowledgment of the stop request success or fail.
  140. */
  141. stopRecording(sessionID) {
  142. const session = this.getSession(sessionID);
  143. if (session) {
  144. return session.stop({ focusMucJid: this._chatRoom.focusMucJid });
  145. }
  146. return Promise.reject(new Error('Could not find session'));
  147. }
  148. /**
  149. * Stores a reference to the passed in JibriSession.
  150. *
  151. * @param {string} session - The JibriSession instance to store.
  152. * @returns {void}
  153. */
  154. _addSession(session) {
  155. this._sessions[session.getID()] = session;
  156. }
  157. /**
  158. * Create a new instance of a recording session and stores a reference to
  159. * it.
  160. *
  161. * @param {string} sessionID - The session ID of the recording in progress.
  162. * @param {string} status - The current status of the recording session.
  163. * @param {string} mode - The recording mode of the session.
  164. * @returns {JibriSession}
  165. */
  166. _createSession(sessionID, status, mode) {
  167. const session = new JibriSession({
  168. connection: this._chatRoom.connection,
  169. focusMucJid: this._chatRoom.focusMucJid,
  170. mode,
  171. sessionID,
  172. status
  173. });
  174. this._addSession(session);
  175. return session;
  176. }
  177. /**
  178. * Notifies listeners of an update to a recording session.
  179. *
  180. * @param {JibriSession} session - The session that has been updated.
  181. * @param {string|undefined} initiator - The jid of the initiator of the update.
  182. */
  183. _emitSessionUpdate(session, initiator) {
  184. this._chatRoom.eventEmitter.emit(
  185. XMPPEvents.RECORDER_STATE_CHANGED, session, initiator);
  186. }
  187. /**
  188. * Parses presence to update an existing JibriSession or to create a new
  189. * JibriSession.
  190. *
  191. * @param {Node} presence - An XMPP presence update.
  192. * @returns {void}
  193. */
  194. _handleFocusPresence(presence) {
  195. const jibriStatus = recordingXMLUtils.getFocusRecordingUpdate(presence);
  196. if (!jibriStatus) {
  197. return;
  198. }
  199. const { error, initiator, recordingMode, sessionID, status } = jibriStatus;
  200. // We'll look for an existing session or create one (in case we're a
  201. // participant joining a call with an existing recording going on).
  202. let session = this.getSession(sessionID);
  203. // Handle the case where a status update is received in presence but
  204. // the local participant has joined while the JibriSession has already
  205. // ended.
  206. if (!session && status === 'off') {
  207. logger.warn(
  208. 'Ignoring recording presence update',
  209. 'Received a new session with status off.');
  210. return;
  211. }
  212. // Jicofo sends updates via presence, and any extension in presence
  213. // is sent until it is explicitly removed. It's difficult for
  214. // Jicofo to know when a presence has been sent once, so it won't
  215. // remove jibri status extension. This means we may receive the same
  216. // status update more than once, so check for that here
  217. if (session
  218. && session.getStatus() === status
  219. && session.getError() === error) {
  220. logger.warn('Ignoring duplicate presence update: ',
  221. JSON.stringify(jibriStatus));
  222. return;
  223. }
  224. if (!session) {
  225. session = this._createSession(sessionID, status, recordingMode);
  226. }
  227. session.setStatusFromJicofo(status);
  228. if (error) {
  229. session.setError(error);
  230. }
  231. this._emitSessionUpdate(session, initiator);
  232. }
  233. /**
  234. * Handles updates from the Jibri which can broadcast a YouTube URL that
  235. * needs to be updated in a JibriSession.
  236. *
  237. * @param {Node} presence - An XMPP presence update.
  238. * @returns {void}
  239. */
  240. _handleJibriPresence(presence) {
  241. const { liveStreamViewURL, mode, sessionID }
  242. = recordingXMLUtils.getHiddenDomainUpdate(presence);
  243. if (!sessionID) {
  244. logger.warn(
  245. 'Ignoring potential jibri presence due to no session id.');
  246. return;
  247. }
  248. let session = this.getSession(sessionID);
  249. if (!session) {
  250. session = this._createSession(sessionID, 'on', mode);
  251. }
  252. // When a jibri is present the status is always 'on';
  253. session.setStatus('on');
  254. session.setJibriJid(presence.getAttribute('from'));
  255. session.setLiveStreamViewURL(liveStreamViewURL);
  256. this._emitSessionUpdate(session);
  257. }
  258. }
  259. export default RecordingManager;