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

8
workspace.code-workspace Normal file
View File

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