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 11KB

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