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

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