rtsp-stream/lib/index.js

110 lines
4.2 KiB
JavaScript

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var child_process_1 = require("child_process");
var events_1 = require("events");
var fs_1 = __importDefault(require("fs"));
var Rtsp = /** @class */ (function (_super) {
__extends(Rtsp, _super);
function Rtsp(connectionString) {
var _this = _super.call(this) || this;
_this.onError = function (err) {
console.error('Failed to start stream: ' + err.message);
};
_this.onStdErrorData = function (data) {
if (!_this._started) {
_this._started = true;
}
data.toString().split(/\n/).forEach(function (line) {
console.debug(line);
});
};
_this.onExit = function (code, signal) {
var message = 'FFmpeg exited with code: ' + code + ' and signal: ' + signal;
if (code == null || code === 255) {
if (_this._childProcess && _this._childProcess.killed) {
console.debug(message + ' (Expected)');
}
else {
console.error(message + ' (Unexpected)');
}
}
else {
console.error(message + ' (Error)');
}
};
_this.onData = function (data) {
if (data.length > 1) {
_this._buffer = _this._buffer ? Buffer.concat([_this._buffer, data]) : _this._buffer = Buffer.from(data);
//End of image
if (data[data.length - 2].toString(16) == "ff" && data[data.length - 1].toString(16) == "d9") {
_this.emit('data', _this._buffer);
_this._buffer = Buffer.from('');
console.log();
}
}
};
_this._started = false;
_this._connecteionString = connectionString;
_this._childProcess = undefined;
_this._buffer = Buffer.from('');
return _this;
}
Object.defineProperty(Rtsp.prototype, "isStarted", {
get: function () {
return this._started;
},
enumerable: true,
configurable: true
});
Rtsp.prototype.start = function () {
var _a;
var args = "-i " + this._connecteionString + " -r 30 -f image2 -update 1 -";
this._childProcess = child_process_1.spawn("ffmpeg", args.split(/\s+/));
if (!this._childProcess) {
return;
}
(_a = this._childProcess.stdout) === null || _a === void 0 ? void 0 : _a.on('data', this.onData);
this._childProcess.on('exit', this.onExit);
this._childProcess.on('error', this.onError);
if (this._childProcess.stderr) {
this._childProcess.stderr.on('data', this.onStdErrorData);
}
};
Rtsp.prototype.close = function () {
this._childProcess && this._childProcess.kill('SIGKILL');
this.emit('closed');
};
Rtsp.prototype.getStdin = function () {
return this._childProcess ? this._childProcess.stdin : null;
};
return Rtsp;
}(events_1.EventEmitter));
exports.Rtsp = Rtsp;
var rtsp = new Rtsp("rtsp://brandon:asdf1234@192.168.1.229/live");
rtsp.on('data', function (data) {
rtsp.close();
fs_1.default.writeFile("/Users/brandonwatson/Documents/Git/Gitea/asdf.jpeg", data, 'base64', function (err) {
if (err) {
console.log(err);
process.exit(1);
}
});
});
rtsp.start();
//# sourceMappingURL=index.js.map