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

recording.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* global $, $iq */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. const logger = getLogger(__filename);
  4. const XMPPEvents = require('../../service/xmpp/XMPPEvents');
  5. const JitsiRecorderErrors = require('../../JitsiRecorderErrors');
  6. const GlobalOnErrorHandler = require('../util/GlobalOnErrorHandler');
  7. function Recording(type, eventEmitter, connection, focusMucJid, jirecon,
  8. roomjid) {
  9. this.eventEmitter = eventEmitter;
  10. this.connection = connection;
  11. this.state = null;
  12. this.focusMucJid = focusMucJid;
  13. this.jirecon = jirecon;
  14. this.url = null;
  15. this.type = type;
  16. this._isSupported
  17. = !(
  18. (type === Recording.types.JIRECON && !this.jirecon)
  19. || (type !== Recording.types.JIBRI
  20. && type !== Recording.types.COLIBRI));
  21. /**
  22. * The ID of the jirecon recording session. Jirecon generates it when we
  23. * initially start recording, and it needs to be used in subsequent requests
  24. * to jirecon.
  25. */
  26. this.jireconRid = null;
  27. this.roomjid = roomjid;
  28. }
  29. Recording.types = {
  30. COLIBRI: 'colibri',
  31. JIRECON: 'jirecon',
  32. JIBRI: 'jibri'
  33. };
  34. Recording.status = {
  35. ON: 'on',
  36. OFF: 'off',
  37. AVAILABLE: 'available',
  38. UNAVAILABLE: 'unavailable',
  39. PENDING: 'pending',
  40. RETRYING: 'retrying',
  41. BUSY: 'busy',
  42. FAILED: 'failed'
  43. };
  44. Recording.action = {
  45. START: 'start',
  46. STOP: 'stop'
  47. };
  48. Recording.prototype.handleJibriPresence = function(jibri) {
  49. const attributes = jibri.attributes;
  50. if (!attributes) {
  51. return;
  52. }
  53. const newState = attributes.status;
  54. logger.log('Handle jibri presence : ', newState);
  55. if (newState === this.state) {
  56. return;
  57. }
  58. if (newState === 'undefined') {
  59. this.state = Recording.status.UNAVAILABLE;
  60. } else if (newState === Recording.status.OFF) {
  61. if (!this.state
  62. || this.state === 'undefined'
  63. || this.state === Recording.status.UNAVAILABLE) {
  64. this.state = Recording.status.AVAILABLE;
  65. } else {
  66. this.state = Recording.status.OFF;
  67. }
  68. } else {
  69. this.state = newState;
  70. }
  71. this.eventEmitter.emit(XMPPEvents.RECORDER_STATE_CHANGED, this.state);
  72. };
  73. Recording.prototype.setRecordingJibri
  74. = function(state, callback, errCallback, options = {}) {
  75. if (state === this.state) {
  76. errCallback(JitsiRecorderErrors.INVALID_STATE);
  77. }
  78. // FIXME jibri does not accept IQ without 'url' attribute set ?
  79. const iq
  80. = $iq({ to: this.focusMucJid,
  81. type: 'set' })
  82. .c('jibri', {
  83. 'xmlns': 'http://jitsi.org/protocol/jibri',
  84. 'action': state === Recording.status.ON
  85. ? Recording.action.START
  86. : Recording.action.STOP,
  87. 'streamid': options.streamId
  88. })
  89. .up();
  90. logger.log(`Set jibri recording: ${state}`, iq.nodeTree);
  91. logger.log(iq.nodeTree);
  92. this.connection.sendIQ(
  93. iq,
  94. result => {
  95. logger.log('Result', result);
  96. const jibri = $(result).find('jibri');
  97. callback(jibri.attr('state'), jibri.attr('url'));
  98. },
  99. error => {
  100. logger.log('Failed to start recording, error: ', error);
  101. errCallback(error);
  102. });
  103. };
  104. Recording.prototype.setRecordingJirecon
  105. = function(state, callback, errCallback) {
  106. if (state === this.state) {
  107. errCallback(new Error('Invalid state!'));
  108. }
  109. const iq = $iq({ to: this.jirecon,
  110. type: 'set' })
  111. .c('recording', { xmlns: 'http://jitsi.org/protocol/jirecon',
  112. action: state === Recording.status.ON
  113. ? Recording.action.START
  114. : Recording.action.STOP,
  115. mucjid: this.roomjid });
  116. if (state === Recording.status.OFF) {
  117. iq.attrs({ rid: this.jireconRid });
  118. }
  119. logger.log('Start recording');
  120. const self = this;
  121. this.connection.sendIQ(
  122. iq,
  123. result => {
  124. // TODO wait for an IQ with the real status, since this is
  125. // provisional?
  126. // eslint-disable-next-line newline-per-chained-call
  127. self.jireconRid = $(result).find('recording').attr('rid');
  128. logger.log(
  129. `Recording ${
  130. state === Recording.status.ON ? 'started' : 'stopped'
  131. }(jirecon)${result}`);
  132. self.state = state;
  133. if (state === Recording.status.OFF) {
  134. self.jireconRid = null;
  135. }
  136. callback(state);
  137. },
  138. error => {
  139. logger.log('Failed to start recording, error: ', error);
  140. errCallback(error);
  141. });
  142. };
  143. // Sends a COLIBRI message which enables or disables (according to 'state')
  144. // the recording on the bridge. Waits for the result IQ and calls 'callback'
  145. // with the new recording state, according to the IQ.
  146. Recording.prototype.setRecordingColibri
  147. = function(state, callback, errCallback, options) {
  148. const elem = $iq({ to: this.focusMucJid,
  149. type: 'set' });
  150. elem.c('conference', {
  151. xmlns: 'http://jitsi.org/protocol/colibri'
  152. });
  153. elem.c('recording', { state,
  154. token: options.token });
  155. const self = this;
  156. this.connection.sendIQ(elem,
  157. result => {
  158. logger.log('Set recording "', state, '". Result:', result);
  159. const recordingElem = $(result).find('>conference>recording');
  160. const newState = recordingElem.attr('state');
  161. self.state = newState;
  162. callback(newState);
  163. if (newState === 'pending') {
  164. self.connection.addHandler(iq => {
  165. // eslint-disable-next-line newline-per-chained-call
  166. const state = $(iq).find('recording').attr('state');
  167. if (state) {
  168. self.state = newState;
  169. callback(state);
  170. }
  171. }, 'http://jitsi.org/protocol/colibri', 'iq', null, null, null);
  172. }
  173. },
  174. error => {
  175. logger.warn(error);
  176. errCallback(error);
  177. }
  178. );
  179. };
  180. Recording.prototype.setRecording
  181. = function(state, callback, errCallback, options) {
  182. switch (this.type) {
  183. case Recording.types.JIRECON:
  184. this.setRecordingJirecon(state, callback, errCallback, options);
  185. break;
  186. case Recording.types.COLIBRI:
  187. this.setRecordingColibri(state, callback, errCallback, options);
  188. break;
  189. case Recording.types.JIBRI:
  190. this.setRecordingJibri(state, callback, errCallback, options);
  191. break;
  192. default: {
  193. const errmsg = 'Unknown recording type!';
  194. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  195. logger.error(errmsg);
  196. break;
  197. }
  198. }
  199. };
  200. /**
  201. * Starts/stops the recording.
  202. * @param token token for authentication
  203. * @param statusChangeHandler {function} receives the new status as argument.
  204. */
  205. Recording.prototype.toggleRecording = function(options, statusChangeHandler) {
  206. const oldState = this.state;
  207. // If the recorder is currently unavailable we throw an error.
  208. if (oldState === Recording.status.UNAVAILABLE
  209. || oldState === Recording.status.FAILED) {
  210. statusChangeHandler(Recording.status.FAILED,
  211. JitsiRecorderErrors.RECORDER_UNAVAILABLE);
  212. } else if (oldState === Recording.status.BUSY) {
  213. statusChangeHandler(Recording.status.BUSY,
  214. JitsiRecorderErrors.RECORDER_BUSY);
  215. }
  216. // If we're about to turn ON the recording we need either a streamId or
  217. // an authentication token depending on the recording type. If we don't
  218. // have any of those we throw an error.
  219. if ((oldState === Recording.status.OFF
  220. || oldState === Recording.status.AVAILABLE)
  221. && ((!options.token && this.type === Recording.types.COLIBRI)
  222. || (!options.streamId && this.type === Recording.types.JIBRI))) {
  223. statusChangeHandler(Recording.status.FAILED,
  224. JitsiRecorderErrors.NO_TOKEN);
  225. logger.error('No token passed!');
  226. return;
  227. }
  228. const newState = oldState === Recording.status.AVAILABLE
  229. || oldState === Recording.status.OFF
  230. ? Recording.status.ON
  231. : Recording.status.OFF;
  232. const self = this;
  233. logger.log('Toggle recording (old state, new state): ', oldState, newState);
  234. this.setRecording(
  235. newState,
  236. (state, url) => {
  237. // If the state is undefined we're going to wait for presence
  238. // update.
  239. if (state && state !== oldState) {
  240. self.state = state;
  241. self.url = url;
  242. statusChangeHandler(state);
  243. }
  244. },
  245. error => statusChangeHandler(Recording.status.FAILED, error),
  246. options);
  247. };
  248. /**
  249. * Returns true if the recording is supproted and false if not.
  250. */
  251. Recording.prototype.isSupported = function() {
  252. return this._isSupported;
  253. };
  254. /**
  255. * Returns null if the recording is not supported, "on" if the recording started
  256. * and "off" if the recording is not started.
  257. */
  258. Recording.prototype.getState = function() {
  259. return this.state;
  260. };
  261. /**
  262. * Returns the url of the recorded video.
  263. */
  264. Recording.prototype.getURL = function() {
  265. return this.url;
  266. };
  267. module.exports = Recording;