From 66bcc19d8514bf94b658b65d6b51c68f64b0c6d9 Mon Sep 17 00:00:00 2001 From: Tom Sightler Date: Fri, 28 Mar 2025 22:49:25 -0400 Subject: [PATCH] Remove useless tweaks --- devices/camera.js | 10 ++++++--- devices/stream/lib/peer-connection.js | 2 +- devices/stream/lib/streaming-session.js | 4 ---- devices/stream/live-worker.js | 28 +++++++++++------------ devices/stream/live.js | 7 +++--- devices/stream/snapshot.js | 30 ++++++++++++++----------- 6 files changed, 42 insertions(+), 39 deletions(-) diff --git a/devices/camera.js b/devices/camera.js index f9aa83a..838a811 100644 --- a/devices/camera.js +++ b/devices/camera.js @@ -34,7 +34,9 @@ export default class Camera extends RingPolledDevice { this.data = { motion: { active_ding: false, - duration: savedState?.motion?.duration ? savedState.motion.duration : 30, + duration: savedState?.motion?.duration + ? savedState.motion.duration + : this.device.data.settings.video_settings.clip_length_max, publishedDuration: false, last_ding: 0, last_ding_expires: 0, @@ -48,8 +50,10 @@ export default class Camera extends RingPolledDevice { ...this.device.isDoorbot ? { ding: { active_ding: false, - duration: savedState?.ding?.duration ? savedState.ding.duration : 30, - publishedDurations: false, + duration: savedState?.ding?.duration + ? savedState.ding.duration + : this.device.data.settings.video_settings.clip_length_max, + publishedDuration: false, last_ding: 0, last_ding_expires: 0, last_ding_time: 'none', diff --git a/devices/stream/lib/peer-connection.js b/devices/stream/lib/peer-connection.js index 687fcb8..e00990f 100644 --- a/devices/stream/lib/peer-connection.js +++ b/devices/stream/lib/peer-connection.js @@ -51,7 +51,7 @@ export class WeriftPeerConnection extends Subscribed { { type: 'nack', parameter: 'pli' }, { type: 'goog-remb' }, ], - parameters: 'packetization-mode=1;profile-level-id=42001f;level-asymmetry-allowed=1', + parameters: 'packetization-mode=1;profile-level-id=64002a;level-asymmetry-allowed=1', }), new RTCRtpCodecParameters({ mimeType: "video/rtx", diff --git a/devices/stream/lib/streaming-session.js b/devices/stream/lib/streaming-session.js index 306b3b5..573674e 100644 --- a/devices/stream/lib/streaming-session.js +++ b/devices/stream/lib/streaming-session.js @@ -114,12 +114,8 @@ export class StreamingSession extends Subscribed { this.onCallEnded.pipe(take(1)).subscribe(() => ff.stop()) - console.log(inputSdp) ff.writeStdin(inputSdp) - // Request a key frame now that ffmpeg is ready to receive - this.requestKeyFrame() - return { port: altVideoPort, sdp: altVideoSdp diff --git a/devices/stream/live-worker.js b/devices/stream/live-worker.js index 002c554..9a8fa76 100644 --- a/devices/stream/live-worker.js +++ b/devices/stream/live-worker.js @@ -3,11 +3,11 @@ import { WebrtcConnection } from './lib/webrtc-connection.js' import { StreamingSession } from './lib/streaming-session.js' class LiveStreamWorker { - constructor(deviceName, doorbotId) { - this.deviceName = deviceName - this.doorbotId = doorbotId + constructor(workerData) { + this.deviceName = workerData.deviceName + this.deviceId = workerData.deviceId + this.doorbotId = workerData.doorbotId this.liveStream = null - this.altVideoData = null this.stopping = false this.processMessages() @@ -69,8 +69,8 @@ class LiveStreamWorker { const streamConnection = new WebrtcConnection(streamData.ticket, cameraData) this.liveStream = new StreamingSession(cameraData, streamConnection) - this.handleCallState() - this.handleCallEnded() + this.subscribeCallState() + this.subscribeCallEnded() await this.startTranscoding(streamData.rtspPublishUrl) } catch (error) { this.logError(error) @@ -79,7 +79,7 @@ class LiveStreamWorker { } } - handleCallState() { + subscribeCallState() { this.liveStream.connection.pc.onConnectionState.subscribe(async (state) => { switch(state) { case 'connected': @@ -97,7 +97,7 @@ class LiveStreamWorker { }) } - handleCallEnded() { + subscribeCallEnded() { this.liveStream.onCallEnded.subscribe(() => { this.logInfo('Live stream WebRTC session has disconnected') this.updateState('inactive') @@ -112,26 +112,26 @@ class LiveStreamWorker { input: [ '-probesize', '32K', '-analyzeduration', '0', + '-buffer_size', '1048576' ], audio: [ '-map', '0:a', - '-c:a:0', 'aac', + '-c:a:0', 'aac', '-map', '0:a', - '-c:a:1', 'copy' + '-c:a:0', 'copy' ], video: [ '-map', '0:v', '-c:v', 'copy' ], output: [ - '-ss', '0.2', '-flags', '+global_header', '-f', 'rtsp', '-rtsp_transport', 'tcp', rtspPublishUrl ]} - this.altVideoData = await this.liveStream.startTranscoding(transcodingConfig) + await this.liveStream.startTranscoding(transcodingConfig) this.logInfo('Live stream transcoding process has started') } @@ -170,10 +170,10 @@ class LiveStreamWorker { } updateState(state) { - parentPort.postMessage({ type: 'state', data: state, altVideoData: this.altVideoData }) + parentPort.postMessage({ type: 'state', data: state }) } } // Initialize the worker -const worker = new LiveStreamWorker(workerData.deviceName, workerData.doorbotId) +const worker = new LiveStreamWorker(workerData) export default worker \ No newline at end of file diff --git a/devices/stream/live.js b/devices/stream/live.js index d206b5f..9bb4404 100644 --- a/devices/stream/live.js +++ b/devices/stream/live.js @@ -48,13 +48,13 @@ export class LiveStream { } clearSession(status) { - this.status = status - this.session = false this.altVideoData = false + this.session = false + this.status = status + this.unbindAltVideoPorts() } bindAltVideoPorts() { - console.log(this.altVideoData.sdp) if (this.altVideoData) { this.rtpSocket = dgram.createSocket('udp4') this.rtcpSocket = dgram.createSocket('udp4') @@ -108,7 +108,6 @@ export class LiveStream { } async stop() { - this.unbindAltVideoPorts() if (this.session) { this.worker.postMessage({ command: 'stop' }) } diff --git a/devices/stream/snapshot.js b/devices/stream/snapshot.js index a436019..ac1b60f 100644 --- a/devices/stream/snapshot.js +++ b/devices/stream/snapshot.js @@ -48,10 +48,13 @@ export class SnapshotStream { '-f', 'mpegts', '-probesize', '32k', '-analyzeduration', '0', + '-fflags', '+discardcorrupt+genpts', + '-r', '5', + '-ss', '0.2', '-i', 'pipe:', - '-ss', '.2', '-c:v', 'copy', '-avioflags', 'direct', + '-flags', '+global_header', '-f', 'rtsp', '-rtsp_transport', 'tcp', rtspPublishUrl @@ -71,11 +74,12 @@ export class SnapshotStream { '-f', 'image2pipe', '-probesize', '32k', '-analyzeduration', '0', + '-r', '20', '-i', 'pipe:', '-vf', 'scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2', '-sws_flags', 'lanczos', '-c:v', 'libx264', - '-b:v', '6M', + '-b:v', '3M', '-r', '5', '-g', '1', '-preset', 'ultrafast', @@ -130,18 +134,17 @@ export class SnapshotStream { this.livesnaps.session = spawn(pathToFfmpeg, [ '-hide_banner', '-protocol_whitelist', 'pipe,udp,rtp,fd,file,crypto', - '-fflags', 'nobuffer', '-flags', 'low_delay', - '-use_wallclock_as_timestamps', '1', - '-itsoffset', '-0.2', '-probesize', '32K', '-analyzeduration', '0', + '-fflags', '+genpts', + '-r', '20', '-f', 'sdp', '-i', 'pipe:', '-vf', 'scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2', '-sws_flags', 'lanczos', '-c:v', 'libx264', - '-b:v', '6M', + '-b:v', '3M', '-preset', 'ultrafast', '-tune', 'zerolatency', '-r', '5', @@ -163,6 +166,7 @@ export class SnapshotStream { }) this.livesnaps.session.on('close', async () => { + liveStream.bindAltVideoPorts() this.mqttCamera.debug('The live snapshot stream has stopped') this.mqttCamera.updateSnapshot('interval') this.livesnaps.session.stdout.unpipe(this.rtsp.session.stdin) @@ -171,20 +175,20 @@ export class SnapshotStream { }) // The livesnap stream will stop after the specified duration - this.livesnaps.timeout = Date.now() + 5000 + (duration * 1000) - while (this.livesnaps.active && (Date.now() < this.livesnaps.timeout)) { + const livesnapsTimeout = Date.now() + 5000 + (duration * 1000) + while (this.livesnaps.active && (Date.now() < livesnapsTimeout)) { await utils.sleep(1) } } else { this.mqttCamera.debug('The live snapshot stream failed starting the live stream') } - if (this.livesnaps.session) { - this.livesnaps.session.kill() + if (this.livesnaps.active && this.livesnaps.session) { + this.livesnaps.session?.kill() } - if (this.keepalive.session) { - this.keepalive.session.kill() + if (this.keepalive.active && this.keepalive.session) { + this.keepalive.session?.kill() } }) @@ -198,7 +202,7 @@ export class SnapshotStream { this.interval = setInterval(async () => { if (this.status === 'active') { try { - this.snapshot.session.stdin.write(this.mqttCamera.data.snapshot.image) + this.snapshot.session?.stdin?.write(this.mqttCamera.data.snapshot.image) } catch { this.mqttCamera.debug('Writing image to snapshot stream failed') this.stop()