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

recording.js 9.1KB

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