modified lib-jitsi-meet dev repo
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.1KB

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