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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 === '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. options = options || {};
  79. // FIXME jibri does not accept IQ without 'url' attribute set ?
  80. const iq
  81. = $iq({ to: this.focusMucJid,
  82. type: 'set' })
  83. .c('jibri', {
  84. 'xmlns': 'http://jitsi.org/protocol/jibri',
  85. 'action': state === Recording.status.ON
  86. ? Recording.action.START
  87. : Recording.action.STOP,
  88. 'streamid': options.streamId
  89. })
  90. .up();
  91. logger.log(`Set jibri recording: ${state}`, iq.nodeTree);
  92. logger.log(iq.nodeTree);
  93. this.connection.sendIQ(
  94. iq,
  95. result => {
  96. logger.log('Result', result);
  97. const jibri = $(result).find('jibri');
  98. callback(jibri.attr('state'), jibri.attr('url'));
  99. },
  100. error => {
  101. logger.log('Failed to start recording, error: ', error);
  102. errCallback(error);
  103. });
  104. };
  105. Recording.prototype.setRecordingJirecon
  106. = function(state, callback, errCallback) {
  107. if (state == this.state) {
  108. errCallback(new Error('Invalid state!'));
  109. }
  110. const iq = $iq({ to: this.jirecon,
  111. type: 'set' })
  112. .c('recording', { xmlns: 'http://jitsi.org/protocol/jirecon',
  113. action: state === Recording.status.ON
  114. ? Recording.action.START
  115. : Recording.action.STOP,
  116. mucjid: this.roomjid });
  117. if (state === 'off') {
  118. iq.attrs({ rid: this.jireconRid });
  119. }
  120. logger.log('Start recording');
  121. const self = this;
  122. this.connection.sendIQ(
  123. iq,
  124. result => {
  125. // TODO wait for an IQ with the real status, since this is
  126. // provisional?
  127. // eslint-disable-next-line newline-per-chained-call
  128. self.jireconRid = $(result).find('recording').attr('rid');
  129. logger.log(
  130. `Recording ${
  131. state === Recording.status.ON ? 'started' : 'stopped'
  132. }(jirecon)${result}`);
  133. self.state = state;
  134. if (state === Recording.status.OFF) {
  135. self.jireconRid = null;
  136. }
  137. callback(state);
  138. },
  139. error => {
  140. logger.log('Failed to start recording, error: ', error);
  141. errCallback(error);
  142. });
  143. };
  144. // Sends a COLIBRI message which enables or disables (according to 'state')
  145. // the recording on the bridge. Waits for the result IQ and calls 'callback'
  146. // with the new recording state, according to the IQ.
  147. Recording.prototype.setRecordingColibri
  148. = function(state, callback, errCallback, options) {
  149. const elem = $iq({ to: this.focusMucJid,
  150. type: 'set' });
  151. elem.c('conference', {
  152. xmlns: 'http://jitsi.org/protocol/colibri'
  153. });
  154. elem.c('recording', { state,
  155. token: options.token });
  156. const self = this;
  157. this.connection.sendIQ(elem,
  158. result => {
  159. logger.log('Set recording "', state, '". Result:', result);
  160. const recordingElem = $(result).find('>conference>recording');
  161. const newState = recordingElem.attr('state');
  162. self.state = newState;
  163. callback(newState);
  164. if (newState === 'pending') {
  165. self.connection.addHandler(iq => {
  166. // eslint-disable-next-line newline-per-chained-call
  167. const state = $(iq).find('recording').attr('state');
  168. if (state) {
  169. self.state = newState;
  170. callback(state);
  171. }
  172. }, 'http://jitsi.org/protocol/colibri', 'iq', null, null, null);
  173. }
  174. },
  175. error => {
  176. logger.warn(error);
  177. errCallback(error);
  178. }
  179. );
  180. };
  181. Recording.prototype.setRecording
  182. = function(state, callback, errCallback, options) {
  183. switch (this.type) {
  184. case Recording.types.JIRECON:
  185. this.setRecordingJirecon(state, callback, errCallback, options);
  186. break;
  187. case Recording.types.COLIBRI:
  188. this.setRecordingColibri(state, callback, errCallback, options);
  189. break;
  190. case Recording.types.JIBRI:
  191. this.setRecordingJibri(state, callback, errCallback, options);
  192. break;
  193. default: {
  194. const errmsg = 'Unknown recording type!';
  195. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  196. logger.error(errmsg);
  197. break;
  198. }
  199. }
  200. };
  201. /**
  202. * Starts/stops the recording.
  203. * @param token token for authentication
  204. * @param statusChangeHandler {function} receives the new status as argument.
  205. */
  206. Recording.prototype.toggleRecording = function(options, statusChangeHandler) {
  207. const oldState = this.state;
  208. // If the recorder is currently unavailable we throw an error.
  209. if (oldState === Recording.status.UNAVAILABLE
  210. || oldState === Recording.status.FAILED) {
  211. statusChangeHandler(Recording.status.FAILED,
  212. JitsiRecorderErrors.RECORDER_UNAVAILABLE);
  213. } else if (oldState === Recording.status.BUSY) {
  214. statusChangeHandler(Recording.status.BUSY,
  215. JitsiRecorderErrors.RECORDER_BUSY);
  216. }
  217. // If we're about to turn ON the recording we need either a streamId or
  218. // an authentication token depending on the recording type. If we don't
  219. // have any of those we throw an error.
  220. if ((oldState === Recording.status.OFF
  221. || oldState === Recording.status.AVAILABLE)
  222. && ((!options.token && this.type === Recording.types.COLIBRI)
  223. || (!options.streamId && this.type === Recording.types.JIBRI))) {
  224. statusChangeHandler(Recording.status.FAILED,
  225. JitsiRecorderErrors.NO_TOKEN);
  226. logger.error('No token passed!');
  227. return;
  228. }
  229. const newState = oldState === Recording.status.AVAILABLE
  230. || oldState === Recording.status.OFF
  231. ? Recording.status.ON
  232. : Recording.status.OFF;
  233. const self = this;
  234. logger.log('Toggle recording (old state, new state): ', oldState, newState);
  235. this.setRecording(
  236. newState,
  237. (state, url) => {
  238. // If the state is undefined we're going to wait for presence
  239. // update.
  240. if (state && state !== oldState) {
  241. self.state = state;
  242. self.url = url;
  243. statusChangeHandler(state);
  244. }
  245. },
  246. error => statusChangeHandler(Recording.status.FAILED, error),
  247. options);
  248. };
  249. /**
  250. * Returns true if the recording is supproted and false if not.
  251. */
  252. Recording.prototype.isSupported = function() {
  253. return this._isSupported;
  254. };
  255. /**
  256. * Returns null if the recording is not supported, "on" if the recording started
  257. * and "off" if the recording is not started.
  258. */
  259. Recording.prototype.getState = function() {
  260. return this.state;
  261. };
  262. /**
  263. * Returns the url of the recorded video.
  264. */
  265. Recording.prototype.getURL = function() {
  266. return this.url;
  267. };
  268. module.exports = Recording;