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

recording.js 8.6KB

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