Problem
When ONVIF motion recording triggers, recordings have a long and uncontrollable pre-roll ( more than 2 minutes) because start_mp4_recording_with_trigger() opens a fresh RTSP connection to go2rtc, which delivers from the start of its own internal segment buffer regardless of the configured pre_detection_buffer value.
Current State (from source audit)
The architecture for a proper fix already exists but is incomplete:
packet_buffer_t and feed_packet_to_event_buffer() are implemented and correct
motion_recording_context_t already holds a packet_buffer_t *buffer and the full state machine (BUFFERING → RECORDING → FINALIZING)
- However,
flush_packet_callback() in onvif_motion_recording.c is a stub that only logs — no packets are actually written
feed_packet_to_event_buffer() has no callers outside its own file — the buffer is allocated but never filled
- The flush happens before the
mp4_writer exists, so even a real callback would have nothing to write to
The working reference is unified_detection_thread.c, which runs a persistent RTSP reader per stream, fills packet_buffer continuously, and flushes directly into an mp4_writer on detection — no new go2rtc connection needed.
Proposed Fix
Port the unified_detection_thread approach to the ONVIF recording path:
- Add a persistent feeder thread per ONVIF stream (permanent RTSP connection to go2rtc)
- Feed every packet into the existing
packet_buffer via feed_packet_to_event_buffer()
- Implement the real
flush_packet_callback (using mp4_writer_initialize + mp4_writer_write_packet, same pattern as UDT)
- On motion event: create
mp4_writer first, flush buffer, then continue writing live packets directly — no new RTSP connection
This completes Phase 2 of ONVIF_MOTION_RECORDING.md (Buffer Management).
Question
Are you OK with me submitting a PR for this? Happy to align on anything before I start.
Problem
When ONVIF motion recording triggers, recordings have a long and uncontrollable pre-roll ( more than 2 minutes) because
start_mp4_recording_with_trigger()opens a fresh RTSP connection to go2rtc, which delivers from the start of its own internal segment buffer regardless of the configuredpre_detection_buffervalue.Current State (from source audit)
The architecture for a proper fix already exists but is incomplete:
packet_buffer_tandfeed_packet_to_event_buffer()are implemented and correctmotion_recording_context_talready holds apacket_buffer_t *bufferand the full state machine (BUFFERING → RECORDING → FINALIZING)flush_packet_callback()inonvif_motion_recording.cis a stub that only logs — no packets are actually writtenfeed_packet_to_event_buffer()has no callers outside its own file — the buffer is allocated but never filledmp4_writerexists, so even a real callback would have nothing to write toThe working reference is
unified_detection_thread.c, which runs a persistent RTSP reader per stream, fillspacket_buffercontinuously, and flushes directly into anmp4_writeron detection — no new go2rtc connection needed.Proposed Fix
Port the
unified_detection_threadapproach to the ONVIF recording path:packet_bufferviafeed_packet_to_event_buffer()flush_packet_callback(usingmp4_writer_initialize+mp4_writer_write_packet, same pattern as UDT)mp4_writerfirst, flush buffer, then continue writing live packets directly — no new RTSP connectionThis completes Phase 2 of
ONVIF_MOTION_RECORDING.md(Buffer Management).Question
Are you OK with me submitting a PR for this? Happy to align on anything before I start.