Repository navigation
feat: 劣化流检测与连接轮换 — 根治长时间录制后画面卡成一团(#450) - #520
Open
zzuu080603 wants to merge 2 commits into
Open
zzuu080603 wants to merge 2 commits into
zzuu080603 wants to merge 2 commits into
Conversation
added 2 commits
November 10, 2026 11:28
…ding rot (ihmily#450) Local-lab experiments (local HTTP sources with injected faults, ffprobe quantification) showed: - TS copy-recording is already robust against timestamp jumps, lost HLS segments and EXT-X-DISCONTINUITY: the mpegts muxer re-monotonizes the timeline and content survives (3-4s lost at the seam). - -use_wallclock_as_timestamps is actively harmful: when the download runs faster than realtime (HLS catch-up) it compresses the whole recording into a fraction of a second of timeline. - Corrupt packets are the real unrecoverable failure: copy mode either drops them (content gaps, frozen/smeared GOPs - issue ihmily#450) or passes them through; the only effective remedy is reconnecting for a fresh stream URL once the source starts rotting. This adds two restart triggers to the ffmpeg recording loop: 1. degraded-stream detection: _monitor_stderr (an extension of _capture_stream_tail) timestamps every 'Packet corrupt'/'invalid data found' line; when stream_error_threshold (default 30, 0 disables) arrive within a 60s window the recorder stops gracefully and recheck_live_status() restarts the room with a fresh URL. 2. optional connection rotation: max_stream_connection_duration (default 0 = off) proactively reconnects after N seconds, before long-lived CDN sessions degrade. Both go through the graceful stop path, so files are finalized properly; the new 'degraded' flag lets recheck_live_status distinguish these restarts from manual stops. Verified end-to-end with a real ffmpeg subprocess against a local infinite corrupted source: corruption events counted, restart triggered within seconds, output finalized, recheck re-armed (15/15 checks including no-false-positive controls).
Adversarial review of the first version found the damage potential was
almost entirely on the restart side. Fixes:
- circuit breaker: 3 degraded restarts within 10 minutes stop the
automatic restart (restart storms on platforms that keep serving a
rotten stream); the periodic live check takes over at its normal pace.
- same-live continuation: a rotation is not a new live session — is_live
and notified_* flags are kept so 'live started' notifications/desktop
alerts are not re-sent every rotation, the end notification is not
swallowed when the anchor went offline during a rotation, and the
recorded duration is banked before start_update() resets counters.
- every degraded exit path restarts, including killed ffmpeg (returncode
outside {0,255}) and broken-stdin/exception paths — exactly what a
rotten stream tends to cause; previously those skipped the restart and
reported a recording error instead.
- degraded restarts bypass the 25s min-valid-duration gate (short
rotations must not poison the room into RECORDING_ERROR) and the
flv-preferred exclusion (we stopped on purpose).
- stream_error_threshold=0 now really disables detection (no 'or'
fallback); thresholds above 200 can no longer silently disable it
(event list trimmed relative to the threshold on read).
- corruption lines are counted per line with cross-chunk buffering: a
message split at a 4KB boundary is no longer missed and bursts inside
one chunk are no longer counted once.
Verified: 24/24 checks (end-to-end with real ffmpeg + local corrupted
infinite stream, circuit breaker, same-live semantics, gate bypasses,
threshold edges, line counting).
Author
|
对本 PR 做了一轮对抗性审查(red-team),发现初版的重启侧存在多个严重问题,已在 commit 0c087a1 全部修复:
验证扩至 24/24(端到端真实 ffmpeg + 熔断/同场语义/门槛绕过/阈值边界/行计数)。 另注:本 PR 与 #515(stalled 检测)都修改了 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📜 标题(Title)
🔍 描述(Description)
实验背景(本地流媒体实验室)
为定位 #450(录 1 小时后画面卡成一团/闪频)、#484(HLS 录制卡顿)、#474(音画不同步/加速),搭建了本地实验环境:HTTP 服务器模拟直播源 + 三类故障注入(时间戳跳变/HLS 丢段与 DISCONTINUITY/坏数据)+ ffprobe 量化(时长、帧数、时间轴空洞、音画漂移)。结论:
-use_wallclock_as_timestamps有害无益:下载快于实时(HLS 追赶)时,它把整段录制压进极短的时间轴(实验中 748 帧被压进 0.2 秒)——这正是『画面加速』的形态。留此结论防止未来引入。本 PR 的修复(StreamCap 层,不动 ffmpeg 参数)
_monitor_stderr(扩展自_capture_stream_tail)对 stderr 中每条Packet corrupt/invalid data found记录时间戳;60 秒窗口内达到stream_error_threshold(默认 30,0 禁用)条即判定流源劣化 → 走已有的优雅停止路径(正确收尾文件)→recheck_live_status以新标志degraded放行,自动用新的流地址重启录制。max_stream_connection_duration(默认 0=关闭)超时主动轮换连接,在 CDN 会话劣化之前拿到新地址,防患于未然。两个触发都复用优雅停止链路(写 q → 等待 → 兜底 kill),文件完整 finalize,不产生损坏文件。
📝 类型(Type of Change)
🏗️ 测试(Testing)
端到端(真实 ffmpeg 子进程 + 本地无限坏流 HTTP 源),15/15 通过:
should_stop/degraded置位 → 优雅停止(输出文件完整)→recheck_live_status放行并调用check_if_live重启,全程 6.1sNon-monotonic DTS等)不计数、不误触发;阈值 0 禁用;连接轮换的触发/未到期/禁用三态实验数据可复现:实验脚本含本地 HTTP 服务器造流、故障注入与 ffprobe 量化方法,欢迎审查复验。
📋 检查清单(Checklist)
stream_error_threshold/max_stream_connection_duration为可选高级项,默认行为不变)Fixes #450
Helps #484
Helps #474