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

recording.js 9.1KB

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