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

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