Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

recording.js 9.5KB

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