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.

JibriSession.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import { $iq } from 'strophe.js';
  2. import recordingXMLUtils from './recordingXMLUtils';
  3. /**
  4. * Represents a recording session.
  5. */
  6. export default class JibriSession {
  7. /**
  8. * Initializes a new JibriSession instance.
  9. *
  10. * @constructor
  11. */
  12. constructor(options = {}) {
  13. this._connection = options.connection;
  14. this._mode = options.mode;
  15. this._jibriJid = null;
  16. this._statusFromJicofo = '';
  17. this._setSessionID(options.sessionID);
  18. this.setStatus(options.status);
  19. }
  20. /**
  21. * Returns the error related to the session instance, if any.
  22. *
  23. * @returns {string|undefined}
  24. */
  25. getError() {
  26. return this._error;
  27. }
  28. /**
  29. * Returns the session ID of the session instance.
  30. *
  31. * @returns {string|undefined}
  32. */
  33. getID() {
  34. return this._sessionID;
  35. }
  36. /**
  37. * Returns the initiator of the session instance.
  38. *
  39. * @returns {JitsiParticipant|string} The participant that started the session.
  40. */
  41. getInitiator() {
  42. return this._initiator;
  43. }
  44. /**
  45. * Returns the streaming URL of the session.
  46. *
  47. * @returns {string|undefined}
  48. */
  49. getLiveStreamViewURL() {
  50. return this._liveStreamViewURL;
  51. }
  52. /**
  53. * Returns the current status of the session.
  54. *
  55. * @returns {string|undefined}
  56. */
  57. getStatus() {
  58. // If _status is not set fallback to the status reported by jicofo.
  59. if (this._status) {
  60. return this._status;
  61. }
  62. return this._statusFromJicofo;
  63. }
  64. /**
  65. * @returns {string|undefined} the JID of jibri associated with this session.
  66. */
  67. getJibriJid() {
  68. return this._jibriJid;
  69. }
  70. /**
  71. * Returns the jid of the participant that stopped the session.
  72. *
  73. * @returns {JitsiParticipant|string} The participant that stopped the session.
  74. */
  75. getTerminator() {
  76. return this._terminator;
  77. }
  78. /**
  79. * Returns the current recording mode of the session, such as "file".
  80. *
  81. * @returns {string}
  82. */
  83. getMode() {
  84. return this._mode;
  85. }
  86. /**
  87. * Sets the last known error message related to the session.
  88. *
  89. * @param {string} error - The error string explaining why the session
  90. * entered an error state.
  91. * @returns {void}
  92. */
  93. setError(error) {
  94. this._error = error;
  95. }
  96. /**
  97. * Sets the last live stream URL for the session instance. Usually this is
  98. * a YouTube URL and usually this is only set for "stream" sessions.
  99. *
  100. * @param {string} url - The live stream URL associated with the session.
  101. * @returns {void}
  102. */
  103. setLiveStreamViewURL(url) {
  104. this._liveStreamViewURL = url;
  105. }
  106. /**
  107. * Sets the last known status for this recording session.
  108. *
  109. * @param {string} status - The new status to set.
  110. * @returns {void}
  111. */
  112. setStatus(status) {
  113. this._status = status;
  114. }
  115. /**
  116. * Set the session status reported by jicofo. If a jibri is present in the room,
  117. * the status is always 'on'. Otherwise, we fallback to the status reported by jicofo.
  118. *
  119. * @param {string} status
  120. */
  121. setStatusFromJicofo(status) {
  122. this._statusFromJicofo = status;
  123. }
  124. /**
  125. * Set the JID of the jibri associated with this session.
  126. *
  127. * @param {*} jibriJid
  128. */
  129. setJibriJid(jibriJid) {
  130. this._jibriJid = jibriJid;
  131. }
  132. /**
  133. * Sets the participant that started the session.
  134. * @param {JitsiParticipant | string} participant - The participant or resource id
  135. * if local participant.
  136. */
  137. setInitiator(participant) {
  138. this._initiator = participant;
  139. }
  140. /**
  141. * Sets the participant that stopped the session.
  142. * @param {JitsiParticipant | string} participant - The participant or the resource id
  143. * if local participant.
  144. */
  145. setTerminator(participant) {
  146. this._terminator = participant;
  147. }
  148. /**
  149. * Sends a message to start the actual recording.
  150. *
  151. * @param {Object} options - Additional arguments for starting the
  152. * recording.
  153. * @param {string} [options.appData] - Data specific to the app/service that
  154. * the result file will be uploaded.
  155. * @param {string} [options.broadcastId] - The broadcast ID of an
  156. * associated YouTube stream, used for knowing the URL from which the stream
  157. * can be viewed.
  158. * @param {string} options.focusMucJid - The JID of the focus participant
  159. * that controls recording.
  160. * @param {streamId} options.streamId - Necessary for live streaming, this
  161. * is the stream key needed to start a live streaming session with the
  162. * streaming service provider.
  163. * @returns Promise
  164. */
  165. start({ appData, broadcastId, focusMucJid, streamId }) {
  166. return new Promise((resolve, reject) => {
  167. this._connection.sendIQ(
  168. this._createIQ({
  169. action: 'start',
  170. appData,
  171. focusMucJid,
  172. broadcastId,
  173. streamId
  174. }),
  175. result => {
  176. // All users will eventually receive the 'pending' status
  177. // from the backend, but for the user initiating the session
  178. // it's better to give some instant feedback that recording
  179. // is starting so fire 'pending' here manually.
  180. this.setStatus('pending');
  181. this._setSessionID(
  182. recordingXMLUtils.getSessionIdFromIq(result));
  183. resolve();
  184. },
  185. error => {
  186. this._setErrorFromIq(error);
  187. reject(error);
  188. });
  189. });
  190. }
  191. /**
  192. * Sends a message to actually stop the recording session.
  193. *
  194. * @param {Object} options - Additional arguments for stopping the
  195. * recording.
  196. * @param {Object} options.focusMucJid - The JID of the focus participant
  197. * that controls recording.
  198. * @returns Promise
  199. */
  200. stop({ focusMucJid }) {
  201. return new Promise((resolve, reject) => {
  202. this._connection.sendIQ(
  203. this._createIQ({
  204. action: 'stop',
  205. focusMucJid
  206. }),
  207. resolve,
  208. reject);
  209. });
  210. }
  211. /**
  212. * Generates the message to change the status of the recording session.
  213. *
  214. * @param {string} status - The new status to which the recording session
  215. * should transition.
  216. * @param {string} [options.appData] - Data specific to the app/service that
  217. * the result file will be uploaded.
  218. * @param {string} [options.broadcastId] - The broadcast ID of an
  219. * associated YouTube stream, used for knowing the URL from which the stream
  220. * can be viewed.
  221. * @param {string} options.focusMucJid - The JID of the focus participant
  222. * that controls recording.
  223. * @param {streamId} options.streamId - Necessary for live streaming, this
  224. * is the stream key needed to start a live streaming session with the
  225. * streaming service provider.
  226. * @returns Object - The XMPP IQ message.
  227. */
  228. _createIQ({ action, appData, broadcastId, focusMucJid, streamId }) {
  229. return $iq({
  230. to: focusMucJid,
  231. type: 'set'
  232. })
  233. .c('jibri', {
  234. 'xmlns': 'http://jitsi.org/protocol/jibri',
  235. 'action': action,
  236. 'app_data': appData,
  237. 'recording_mode': this._mode,
  238. 'streamid': streamId,
  239. 'you_tube_broadcast_id': broadcastId
  240. })
  241. .up();
  242. }
  243. /**
  244. * Handles the error from an iq and stores the error.
  245. *
  246. * @param {Node} errorIq - The error response from an Iq.
  247. * @private
  248. * @returns {void}
  249. */
  250. _setErrorFromIq(errorIq) {
  251. const error = errorIq.getElementsByTagName('error')[0];
  252. this.setError(error.children[0].tagName);
  253. }
  254. /**
  255. * Sets the known session ID for this recording session.
  256. *
  257. * @param {string} sessionID
  258. * @private
  259. * @returns {void}
  260. */
  261. _setSessionID(sessionID) {
  262. this._sessionID = sessionID;
  263. }
  264. }