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.

recording.js 9.2KB

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