Skip to content

Feature/rtsp stability improvements#21

Merged
themactep merged 4 commits intostablefrom
feature/rtsp-stability-improvements
Mar 9, 2026
Merged

Feature/rtsp stability improvements#21
themactep merged 4 commits intostablefrom
feature/rtsp-stability-improvements

Conversation

@themactep
Copy link
Copy Markdown
Owner

No description provided.

Changes based on analysis of go2rtc's RTSP server implementation:

1. Use encoder timestamps instead of gettimeofday() for video PTS
   - Eliminates timestamp jitter caused by scheduling/queue delays
   - Anchors IMP encoder's monotonic microsecond clock to wall-clock
   - Greatly improves NVR compatibility (Frigate, HA, etc.)

2. Use non-blocking write() instead of write_wait() for RTSP frames
   - Prevents slow RTSP clients from stalling the encoder poll loop
   - Drop-oldest policy (like go2rtc) instead of blocking producer
   - Fixes cascading stalls affecting MP4 recording and video taps

3. Increase default OutPacketBuffer::maxSize from 500KB to 1MB
   - Prevents silent truncation of large IDR frames on high-res streams
   - Aligns with go2rtc's 1MB max buffer strategy

4. Add SO_SNDTIMEO on RTP sockets (configurable rtsp.send_timeout)
   - Detects and disconnects stalled/unresponsive RTSP clients
   - Default: 5 seconds (matches go2rtc's TCP write deadline)
   - Prevents indefinite hangs when client stops consuming data

5. Dynamic SDP regeneration on encoder config changes
   - Checks SPS/PPS/VPS against last known values on each DESCRIBE
   - Invalidates cached fSDPLines when codec config changes
   - Enables resolution/profile changes without RTSP server restart
…ntinuity

SPS/PPS packs from IMP_Encoder_GetStream() may carry timestamp=0 while
the IDR pack carries the real encoder timestamp.  Using per-pack timestamps
caused IMPDeviceSource's re-anchor logic to fire on SPS (delta < 0), then
the IDR's full encoder-uptime timestamp produced a massive forward DTS jump.

Fix: use nal_ts (last pack's timestamp) for all packs in VideoWorker.
Defense-in-depth: add a >2s forward-jump guard in IMPDeviceSource that
re-anchors on impossible per-frame deltas.
Per ONVIF Streaming Spec Section 5.3, backchannel tracks must be part
of the main media session, not a separate endpoint. Add backchannel
subsessions (AAC, PCMU, PCMA) to ch0's ServerMediaSession with a
require tag so they only appear in the SDP when the client sends
'Require: www.onvif.org/ver20/backchannel' in the DESCRIBE request.

Works with the corresponding live555 patch that adds:
- generateSDPDescription() filtering by Require header
- SETUP/PLAY fallback for clients that omit the stream prefix
Replace per-frame ERROR logs with rate-limited WARN that reports the
count of dropped frames in the last 5-second window. Prevents log
flooding when a slow consumer falls behind on audio or video channels.
@themactep themactep merged commit f663f07 into stable Mar 9, 2026
0 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant