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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* global $, $iq, config, connection, focusMucJid, messageHandler,
  2. Toolbar, Util, Promise */
  3. var XMPPEvents = require("../../service/XMPP/XMPPEvents");
  4. function Recording(ee, connection, focusMucJid) {
  5. this.eventEmitter = ee;
  6. this.connection = connection;
  7. this.state = "off";
  8. this.focusMucJid = focusMucJid;
  9. this.url = null;
  10. this._isSupported = false;
  11. }
  12. Recording.prototype.handleJibriPresence = function (jibri) {
  13. var attributes = jibri.attributes;
  14. if(!attributes)
  15. return;
  16. this._isSupported =
  17. (attributes.status && attributes.status !== "undefined");
  18. if(this._isSupported) {
  19. this.url = attributes.url || null;
  20. this.state = attributes.status || "off";
  21. }
  22. this.eventEmitter.emit(XMPPEvents.RECORDING_STATE_CHANGED);
  23. };
  24. Recording.prototype.setRecording = function (state, streamId, callback,
  25. errCallback){
  26. if (state == this.state){
  27. return;
  28. }
  29. // FIXME jibri does not accept IQ without 'url' attribute set ?
  30. var iq = $iq({to: this.focusMucJid, type: 'set'})
  31. .c('jibri', {
  32. xmlns: 'http://jitsi.org/protocol/jibri',
  33. action: (state === 'on') ? 'start' : 'stop',
  34. streamid: streamId
  35. }).up();
  36. console.log('Set jibri recording: '+state, iq);
  37. this.connection.sendIQ(
  38. iq,
  39. function (result) {
  40. callback($(result).find('jibri').attr('state'),
  41. $(result).find('jibri').attr('url'));
  42. },
  43. function (error) {
  44. console.log('Failed to start recording, error: ', error);
  45. errCallback(error);
  46. });
  47. };
  48. Recording.prototype.toggleRecording = function (token) {
  49. var self = this;
  50. return new Promise(function(resolve, reject) {
  51. if (!token) {
  52. console.error("No token passed!");
  53. reject(new Error("No token passed!"));
  54. }
  55. var oldState = self.state;
  56. var newState = (oldState === 'off' || !oldState) ? 'on' : 'off';
  57. self.setRecording(newState,
  58. token,
  59. function (state, url) {
  60. console.log("New recording state: ", state);
  61. if (state && state !== oldState) {
  62. self.state = state;
  63. self.url = url;
  64. resolve();
  65. } else {
  66. reject(new Error("State not changed!"));
  67. }
  68. },
  69. function (error) {
  70. reject(error);
  71. }
  72. );
  73. });
  74. };
  75. /**
  76. * Returns true if the recording is supproted and false if not.
  77. */
  78. Recording.prototype.isSupported = function () {
  79. return this._isSupported;
  80. };
  81. /**
  82. * Returns null if the recording is not supported, "on" if the recording started
  83. * and "off" if the recording is not started.
  84. */
  85. Recording.prototype.getState = function () {
  86. return this.state;
  87. };
  88. /**
  89. * Returns the url of the recorded video.
  90. */
  91. Recording.prototype.getURL = function () {
  92. return this.url;
  93. };
  94. module.exports = Recording;