From f0ed1bb00ba7ddee684db60deb3fbcb488707722 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Tue, 17 May 2022 18:06:39 -0700 Subject: [PATCH] rebroadcast: better stream termination logging --- plugins/prebuffer-mixin/package-lock.json | 4 ++-- plugins/prebuffer-mixin/package.json | 2 +- plugins/prebuffer-mixin/src/main.ts | 12 +++++++----- plugins/prebuffer-mixin/src/rfc4571.ts | 16 ++++++++-------- plugins/prebuffer-mixin/src/rtsp-session.ts | 18 ++++++++++-------- 5 files changed, 28 insertions(+), 24 deletions(-) diff --git a/plugins/prebuffer-mixin/package-lock.json b/plugins/prebuffer-mixin/package-lock.json index e613cc257..35b818012 100644 --- a/plugins/prebuffer-mixin/package-lock.json +++ b/plugins/prebuffer-mixin/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/prebuffer-mixin", - "version": "0.1.270", + "version": "0.1.272", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/prebuffer-mixin", - "version": "0.1.270", + "version": "0.1.272", "license": "Apache-2.0", "dependencies": { "@scrypted/common": "file:../../common", diff --git a/plugins/prebuffer-mixin/package.json b/plugins/prebuffer-mixin/package.json index 5d68116ed..c7b658ce4 100644 --- a/plugins/prebuffer-mixin/package.json +++ b/plugins/prebuffer-mixin/package.json @@ -1,6 +1,6 @@ { "name": "@scrypted/prebuffer-mixin", - "version": "0.1.270", + "version": "0.1.272", "description": "Rebroadcast and Prebuffer for VideoCameras.", "author": "Scrypted", "license": "Apache-2.0", diff --git a/plugins/prebuffer-mixin/src/main.ts b/plugins/prebuffer-mixin/src/main.ts index 8dfadd5b2..89ebfe071 100644 --- a/plugins/prebuffer-mixin/src/main.ts +++ b/plugins/prebuffer-mixin/src/main.ts @@ -742,7 +742,7 @@ class PrebufferSession { return; } this.console.warn('SEI packet detected while operating with Scrypted Parser as default. Restarting rebroadcast.'); - session.kill(); + session.kill(new Error('restarting due to SEI packet detection')); this.startPrebufferSession(); } } @@ -789,7 +789,7 @@ class PrebufferSession { if (probingAudioCodec) { this.console.warn('Audio probe complete, ending rebroadcast session and restarting with detected codecs.'); - session.kill(); + session.kill(new Error('audio probe completed, restarting')); return this.startPrebufferSession(); } @@ -886,7 +886,7 @@ class PrebufferSession { return; } this.console.log(this.streamName, 'terminating rebroadcast due to inactivity'); - session.kill(); + session.kill(new Error('stream inactivity')); }, 30000); } @@ -1278,7 +1278,9 @@ class PrebufferMixin extends SettingsMixinDeviceBase session.kill()); + session?.parserSessionPromise?.then(session => session.kill(new Error('rebroadcast settings changed'))); } this.ensurePrebufferSessions(); } @@ -1399,7 +1401,7 @@ class PrebufferMixin extends SettingsMixinDeviceBase { this.console.log('prebuffer session released'); - parserSession.kill(); + parserSession.kill(new Error('rebroadcast disabled')); session.clearPrebuffers(); }); } diff --git a/plugins/prebuffer-mixin/src/rfc4571.ts b/plugins/prebuffer-mixin/src/rfc4571.ts index c8a8d85f9..b558483b9 100644 --- a/plugins/prebuffer-mixin/src/rfc4571.ts +++ b/plugins/prebuffer-mixin/src/rfc4571.ts @@ -84,10 +84,10 @@ export function startRFC4571Parser(console: Console, socket: Readable, sdp: stri sessionKilled = resolve; }); - const kill = () => { + const kill = (error?: Error) => { if (isActive) { events.emit('killed'); - events.emit('error', new Error('killed')); + events.emit('error', error || new Error('killed')); } isActive = false; sessionKilled(); @@ -95,10 +95,10 @@ export function startRFC4571Parser(console: Console, socket: Readable, sdp: stri }; socket.on('close', () => { - kill(); + kill(new Error('rfc4751 socket closed')); }); - socket.on('error', () => { - kill(); + socket.on('error', e => { + kill(e); }); const { resetActivityTimer } = setupActivityTimer('rtsp', kill, events, options?.timeout); @@ -184,7 +184,7 @@ export function startRFC4571Parser(console: Console, socket: Readable, sdp: stri throw e; }) .finally(() => { - kill(); + kill(new Error('parser exited')); }); @@ -196,8 +196,8 @@ export function startRFC4571Parser(console: Console, socket: Readable, sdp: stri return inputVideoResolution; }, get isActive() { return isActive }, - kill() { - kill(); + kill(error?: Error) { + kill(error); }, killed, resetActivityTimer, diff --git a/plugins/prebuffer-mixin/src/rtsp-session.ts b/plugins/prebuffer-mixin/src/rtsp-session.ts index 48650005d..7a546628e 100644 --- a/plugins/prebuffer-mixin/src/rtsp-session.ts +++ b/plugins/prebuffer-mixin/src/rtsp-session.ts @@ -39,10 +39,10 @@ export async function startRtspSession(console: Console, url: string, mediaStrea sessionKilled = resolve; }); - const kill = () => { + const kill = (error?: Error) => { if (isActive) { events.emit('killed'); - events.emit('error', new Error('killed')); + events.emit('error', error || new Error('killed')); } isActive = false; sessionKilled(); @@ -50,10 +50,10 @@ export async function startRtspSession(console: Console, url: string, mediaStrea }; rtspClient.client.on('close', () => { - kill(); + kill(new Error('rtsp socket closed')); }); - rtspClient.client.on('error', () => { - kill(); + rtspClient.client.on('error', e => { + kill(e); }); const { resetActivityTimer } = setupActivityTimer('rtsp', kill, events, options?.rtspRequestTimeout); @@ -177,7 +177,9 @@ export async function startRtspSession(console: Console, url: string, mediaStrea // don't start parsing until next tick when this function returns to allow // event handlers to be set prior to parsing. - process.nextTick(() => rtspClient.readLoop().finally(kill)); + process.nextTick(() => rtspClient.readLoop() + .catch(e => kill(e)) + .finally(() => kill(new Error('rtsp read loop exited')))); // this return block is intentional, to ensure that the remaining code happens sync. return (() => { @@ -217,8 +219,8 @@ export async function startRtspSession(console: Console, url: string, mediaStrea return inputVideoResolution; }, get isActive() { return isActive }, - kill() { - kill(); + kill(error?: Error) { + kill(error); }, killed, resetActivityTimer,