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

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