modified lib-jitsi-meet dev repo
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JibriSession.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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|undefined} 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|undefined} 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 creator's jid of the session.
  105. * @param {JitsiParticipant} participant - The creator of the session.
  106. */
  107. setInitiator(participant) {
  108. this._initiator = participant;
  109. }
  110. /**
  111. * Sets the jid of the participant that stopped the session.
  112. * @param {JitsiParticipant} participant - The participant's jid,
  113. * that stopped the session.
  114. */
  115. setTerminator(participant) {
  116. this._terminator = participant;
  117. }
  118. /**
  119. * Sends a message to start the actual recording.
  120. *
  121. * @param {Object} options - Additional arguments for starting the
  122. * recording.
  123. * @param {string} [options.appData] - Data specific to the app/service that
  124. * the result file will be uploaded.
  125. * @param {string} [options.broadcastId] - The broadcast ID of an
  126. * associated YouTube stream, used for knowing the URL from which the stream
  127. * can be viewed.
  128. * @param {string} options.focusMucJid - The JID of the focus participant
  129. * that controls recording.
  130. * @param {streamId} options.streamId - Necessary for live streaming, this
  131. * is the the stream key needed to start a live streaming session with the
  132. * streaming service provider.
  133. * @returns Promise
  134. */
  135. start({ appData, broadcastId, focusMucJid, streamId }) {
  136. return new Promise((resolve, reject) => {
  137. this._connection.sendIQ(
  138. this._createIQ({
  139. action: 'start',
  140. appData,
  141. focusMucJid,
  142. broadcastId,
  143. streamId
  144. }),
  145. result => {
  146. // All users will eventually receive the 'pending' status
  147. // from the backend, but for the user initiating the session
  148. // it's better to give some instant feedback that recording
  149. // is starting so fire 'pending' here manually.
  150. this.setStatus('pending');
  151. this._setSessionID(
  152. recordingXMLUtils.getSessionIdFromIq(result));
  153. resolve();
  154. },
  155. error => {
  156. this._setErrorFromIq(error);
  157. reject(error);
  158. });
  159. });
  160. }
  161. /**
  162. * Sends a message to actually stop the recording session.
  163. *
  164. * @param {Object} options - Additional arguments for stopping the
  165. * recording.
  166. * @param {Object} options.focusMucJid - The JID of the focus participant
  167. * that controls recording.
  168. * @returns Promise
  169. */
  170. stop({ focusMucJid }) {
  171. return new Promise((resolve, reject) => {
  172. this._connection.sendIQ(
  173. this._createIQ({
  174. action: 'stop',
  175. focusMucJid
  176. }),
  177. resolve,
  178. reject);
  179. });
  180. }
  181. /**
  182. * Generates the message to change the status of the recording session.
  183. *
  184. * @param {string} status - The new status to which the recording session
  185. * should transition.
  186. * @param {string} [options.appData] - Data specific to the app/service that
  187. * the result file will be uploaded.
  188. * @param {string} [options.broadcastId] - The broadcast ID of an
  189. * associated YouTube stream, used for knowing the URL from which the stream
  190. * can be viewed.
  191. * @param {string} options.focusMucJid - The JID of the focus participant
  192. * that controls recording.
  193. * @param {streamId} options.streamId - Necessary for live streaming, this
  194. * is the the stream key needed to start a live streaming session with the
  195. * streaming service provider.
  196. * @returns Object - The XMPP IQ message.
  197. */
  198. _createIQ({ action, appData, broadcastId, focusMucJid, streamId }) {
  199. return $iq({
  200. to: focusMucJid,
  201. type: 'set'
  202. })
  203. .c('jibri', {
  204. 'xmlns': 'http://jitsi.org/protocol/jibri',
  205. 'action': action,
  206. 'app_data': appData,
  207. 'recording_mode': this._mode,
  208. 'streamid': streamId,
  209. 'you_tube_broadcast_id': broadcastId
  210. })
  211. .up();
  212. }
  213. /**
  214. * Handles the error from an iq and stores the error.
  215. *
  216. * @param {Node} errorIq - The error response from an Iq.
  217. * @private
  218. * @returns {void}
  219. */
  220. _setErrorFromIq(errorIq) {
  221. const error = errorIq.getElementsByTagName('error')[0];
  222. this.setError(error.children[0].tagName);
  223. }
  224. /**
  225. * Sets the known session ID for this recording session.
  226. *
  227. * @param {string} sessionID
  228. * @private
  229. * @returns {void}
  230. */
  231. _setSessionID(sessionID) {
  232. this._sessionID = sessionID;
  233. }
  234. }