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

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