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

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