Adding more options

This commit is contained in:
watsonb8 2020-11-27 12:30:56 -05:00
parent d59a58f63d
commit 9b92a43c2f
3 changed files with 113 additions and 78 deletions

View File

@ -1,4 +1,4 @@
import { ChildProcess, spawn } from "child_process"
import { ChildProcess, spawn } from "child_process";
import { EventEmitter } from "events";
import { Writable } from "stream";
import { IOptions } from "./options";
@ -12,27 +12,37 @@ export class Rtsp extends EventEmitter {
private _started: boolean;
private _buffer: Buffer;
private _options: IOptions;
private _paused: boolean;
constructor(connectionString: string, options: IOptions) {
super();
this._started = false;
this._connecteionString = connectionString;
this._childProcess = undefined;
this._buffer = Buffer.from('');
this._buffer = Buffer.from("");
this._options = options;
this._paused = false;
this.onData = this.onData.bind(this);
}
public get isStarted(): boolean {
return this._started;
}
public get isPaused(): boolean {
return this._paused;
}
public start(): void {
const argStrings = [
// `-rtsp_transport tcp`,
`-i ${this._connecteionString}`,
`-r ${this._options.rate ?? 10}`,
`-f image2`,
`-codec:v ${this._options.codec ?? 'libx264'}`,
`-update 1 -`
this._options.image
? `-f image2`
: `-codec:v ${this._options.codec ?? "libx264"}`,
`-update 1 -`,
];
const args = argStrings.join(" ");
this._childProcess = spawn("ffmpeg", args.split(/\s+/));
@ -41,18 +51,26 @@ export class Rtsp extends EventEmitter {
return;
}
this._childProcess.stdout?.on('data', this.onData)
this._childProcess.on('exit', this.onExit);
this._childProcess.on('error', this.onError);
this._childProcess.stdout?.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);
this._childProcess.stderr.on("data", this.onStdErrorData);
}
}
public close(): void {
this._childProcess && this._childProcess.kill('SIGKILL');
this.emit('closed');
this._childProcess && this._childProcess.kill("SIGKILL");
this.emit("closed");
}
public pause(): void {
this._paused = true;
}
public resume(): void {
this._paused = false;
}
public getStdin(): Writable | null {
@ -60,34 +78,42 @@ export class Rtsp extends EventEmitter {
}
private onError = (err: Error): void => {
this.emit('error', new Error('Failed to start stream: ' + err.message));
}
this.emit("error", new Error("Failed to start stream: " + err.message));
};
private onStdErrorData = (data: any): void => {
if (!this._started) {
this._started = true;
}
let msg = "";
data.toString().split(/\n/).forEach((line: string) => {
msg += `line\n`;
data
.toString()
.split(/\n/)
.forEach((line: string) => {
msg += `${line}\n`;
});
this.emit("error", msg);
}
};
private onExit = (code: number, signal: NodeJS.Signals): void => {
const message = 'FFmpeg exited with code: ' + code + ' and signal: ' + signal;
this.emit('closed', message);
}
const message =
"FFmpeg exited with code: " + code + " and signal: " + signal;
this.emit("closed", message);
};
private onData = (data: Buffer): void => {
if (data.length > 1) {
this._buffer = this._buffer ? Buffer.concat([this._buffer, data]) : this._buffer = Buffer.from(data);
private onData(data: Buffer): void {
if (!this._paused && 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) == ef1 && data[data.length -1].toString(16) == ef2){
this.emit('data', this._buffer)
this._buffer = Buffer.from('');
if (
data[data.length - 2].toString(16) == ef1 &&
data[data.length - 1].toString(16) == ef2
) {
this.emit("data", this._buffer);
this._buffer = Buffer.from("");
}
}
}

View File

@ -1,6 +1,7 @@
export interface IOptions {
rate?: number,
quality?: number,
resolution?: string,
codec?: string,
rate?: number;
quality?: number;
resolution?: string;
codec?: string;
image?: boolean;
}

8
workspace.code-workspace Normal file
View File

@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}