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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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} [optional] options.broadcastId - The broadcast ID of an
  93. * associated YouTube stream, used for knowing the URL from which the stream
  94. * can be viewed.
  95. * @param {string} options.focusMucJid - The JID of the focus participant
  96. * that controls recording.
  97. * @param {streamId} options.streamId - Necessary for live streaming, this
  98. * is the the stream key needed to start a live streaming session with the
  99. * streaming service provider.
  100. * @returns Promise
  101. */
  102. start({ broadcastId, focusMucJid, streamId }) {
  103. return new Promise((resolve, reject) => {
  104. this._connection.sendIQ(
  105. this._createIQ({
  106. action: 'start',
  107. focusMucJid,
  108. broadcastId,
  109. streamId
  110. }),
  111. result => {
  112. // All users will eventually receive the 'pending' status
  113. // from the backend, but for the user initiating the session
  114. // it's better to give some instant feedback that recording
  115. // is starting so fire 'pending' here manually.
  116. this.setStatus('pending');
  117. this._setSessionID(
  118. recordingXMLUtils.getSessionIdFromIq(result));
  119. resolve();
  120. },
  121. error => {
  122. this._setErrorFromIq(error);
  123. reject(error);
  124. });
  125. });
  126. }
  127. /**
  128. * Sends a message to actually stop the recording session.
  129. *
  130. * @param {Object} options - Additional arguments for stopping the
  131. * recording.
  132. * @param {Object} options.focusMucJid - The JID of the focus participant
  133. * that controls recording.
  134. * @returns Promise
  135. */
  136. stop({ focusMucJid }) {
  137. return new Promise((resolve, reject) => {
  138. this._connection.sendIQ(
  139. this._createIQ({
  140. action: 'stop',
  141. focusMucJid
  142. }),
  143. resolve,
  144. reject);
  145. });
  146. }
  147. /**
  148. * Generates the message to change the status of the recording session.
  149. *
  150. * @param {string} status - The new status to which the recording session
  151. * should transition.
  152. * @param {string} [optional] options.broadcastId - The broadcast ID of an
  153. * associated YouTube stream, used for knowing the URL from which the stream
  154. * can be viewed.
  155. * @param {string} options.focusMucJid - The JID of the focus participant
  156. * that controls recording.
  157. * @param {streamId} options.streamId - Necessary for live streaming, this
  158. * is the the stream key needed to start a live streaming session with the
  159. * streaming service provider.
  160. * @returns Object - The XMPP IQ message.
  161. */
  162. _createIQ({ action, broadcastId, focusMucJid, streamId }) {
  163. return $iq({
  164. to: focusMucJid,
  165. type: 'set'
  166. })
  167. .c('jibri', {
  168. 'xmlns': 'http://jitsi.org/protocol/jibri',
  169. 'action': action,
  170. 'recording_mode': this._mode,
  171. 'streamid': streamId,
  172. 'you_tube_broadcast_id': broadcastId
  173. })
  174. .up();
  175. }
  176. /**
  177. * Handles the error from an iq and stores the error.
  178. *
  179. * @param {Node} errorIq - The error response from an Iq.
  180. * @private
  181. * @returns {void}
  182. */
  183. _setErrorFromIq(errorIq) {
  184. const error = errorIq.getElementsByTagName('error')[0];
  185. this.setError(error.children[0].tagName);
  186. }
  187. /**
  188. * Sets the known session ID for this recording session.
  189. *
  190. * @param {string} sessionID
  191. * @private
  192. * @returns {void}
  193. */
  194. _setSessionID(sessionID) {
  195. this._sessionID = sessionID;
  196. }
  197. }