您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

recording.js 12KB

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