主码流实时预览改走 UDP + 关掉流分析与重排缓冲
主码流走 RTSP over TCP 时,一旦设备发送缓冲/交换队列有排队,延迟只会越堆越高 且永远追不回来(TCP 不丢帧),现场表现就是「越看越慢」。 - Open() 默认低延迟:-rtsp_transport udp,外加 -analyzeduration 0 -max_delay 0 -reorder_queue_size 0 -avioflags direct -fflags nobuffer -flags low_delay -fps_mode passthrough - 4 秒还收不到帧就杀掉进程、改用 TCP 重开一次(部分网络禁 UDP, 不能让用户因此看不到画面);重开只做一次,并用 RestartGate 串行化 - 复用键带 |ll 后缀,实时预览与回放(OpenUrl 默认 lowLatency=false)不会串流; 回放仍走 TCP,因为回放靠时间戳对齐,丢包会算错延迟 - 补两行日志:ffmpeg 实际用的传输方式、首帧耗时(ms) - README 补齐子码流/主码流两条链路的延迟差异与排查日志 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
8c83275719
commit
41063761f7
@ -403,6 +403,28 @@ cd desktop\installer
|
||||
>
|
||||
> 端口可用 `YUNCANG_LOCAL_WS_PORT` 覆盖;前端用 `VITE_LOCAL_WS` 覆盖默认 `ws://127.0.0.1:17301/`。
|
||||
|
||||
#### 预览延迟(子码流 / 主码流是两条完全不同的链路)
|
||||
|
||||
| 码流 | 链路 | 典型延迟 |
|
||||
| --- | --- | --- |
|
||||
| 子码流(`mode=stream`) | 8000 私有协议 → PlayM4 解码 → 取 JPEG | 低,稳定 |
|
||||
| 主码流(`mode=ffmpeg`) | RTSP → ffmpeg 解码/缩放 → 编码 JPEG | 取决于传输方式 |
|
||||
|
||||
主码流**不能走 TCP**:RTP over TCP 一旦有排队(设备发送缓冲、交换队列),延迟只会越堆越高、
|
||||
而且永远不会自己追回来(TCP 不丢帧)。所以实时预览的 ffmpeg 参数固定为:
|
||||
|
||||
- 传输 `-rtsp_transport udp`(丢包宁可丢帧,不排队;4 秒还收不到帧会**自动回退 TCP**,
|
||||
因为部分网络禁 UDP,不能让用户看不了画面);
|
||||
- `-analyzeduration 0 -max_delay 0 -reorder_queue_size 0 -avioflags direct -fflags nobuffer -flags low_delay -fps_mode passthrough`
|
||||
(不分析流、不攒包、输出侧不补帧);
|
||||
- 回放(`OpenUrl` 的 `lowLatency=false`)**保持 TCP**:回放靠时间戳对齐,丢包会算错延迟。
|
||||
|
||||
排查用日志(`logs\log_日期.txt`):
|
||||
|
||||
- `ffmpeg 已启动:... UDP` / `TCP` —— 实际用的传输方式;
|
||||
- `[ffmpeg] 首帧到达:... 用时 320ms(UDP)` —— 首帧耗时;
|
||||
- `[ffmpeg] UDP 收不到帧,回退 TCP` —— 该网络不通 UDP。
|
||||
|
||||
### 为什么没有单独的「探活 / health」接口
|
||||
|
||||
探活通过 ≠ 真正查询时软件还在(用户可能中途退出了),两次请求之间状态会变。
|
||||
|
||||
@ -84,7 +84,7 @@ public sealed class HikFfmpegService : IDisposable
|
||||
public int Open(string host, int rtspPort, string userName, string password, string streamId, int width = 1280, int fps = 25, int quality = 6)
|
||||
{
|
||||
var url = BuildRtspUrl(host, rtspPort, userName, password, streamId);
|
||||
return OpenUrl(url, $"{host}:{rtspPort}|{streamId}", width, fps, quality);
|
||||
return OpenUrl(url, $"{host}:{rtspPort}|{streamId}", width, fps, quality, lowLatency: true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -92,7 +92,12 @@ public sealed class HikFfmpegService : IDisposable
|
||||
///
|
||||
/// <paramref name="keyPrefix"/> 是复用键前缀,**不能含密码**(地址里有账号密码)。
|
||||
/// </summary>
|
||||
public int OpenUrl(string url, string keyPrefix, int width = 1280, int fps = 25, int quality = 6)
|
||||
/// <param name="lowLatency">
|
||||
/// 实时预览用:RTP 走 **UDP**(TCP 一旦排队延迟只会越堆越高)+ 关掉流分析/重排缓冲;
|
||||
/// UDP 4 秒收不到帧会自动回退 TCP(部分网络禁 UDP,不能因此看不了画面)。
|
||||
/// 回放**不要**开:回放靠时间戳对齐,丢包/丢帧会算错延迟。
|
||||
/// </param>
|
||||
public int OpenUrl(string url, string keyPrefix, int width = 1280, int fps = 25, int quality = 6, bool lowLatency = false)
|
||||
{
|
||||
if (!IsAvailable)
|
||||
{
|
||||
@ -101,7 +106,9 @@ public sealed class HikFfmpegService : IDisposable
|
||||
"也可以设置环境变量 YUNCANG_FFMPEG 指定 ffmpeg.exe,或改用「满帧率窗口」看主码流。");
|
||||
}
|
||||
|
||||
var key = $"{keyPrefix}|{width}|{fps}|{quality}";
|
||||
var key = lowLatency
|
||||
? $"{keyPrefix}|{width}|{fps}|{quality}|ll"
|
||||
: $"{keyPrefix}|{width}|{fps}|{quality}";
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
@ -119,13 +126,14 @@ public sealed class HikFfmpegService : IDisposable
|
||||
Key = key,
|
||||
Description = key,
|
||||
RefCount = 1,
|
||||
LowLatency = lowLatency,
|
||||
};
|
||||
|
||||
StartProcess(stream, url, width, fps, quality);
|
||||
StartProcess(stream, url, width, fps, quality, lowLatency);
|
||||
|
||||
_streams[handle] = stream;
|
||||
_byKey[key] = handle;
|
||||
AppLog.Write($"ffmpeg 已启动:{key}({width} 宽,{fps}fps,q={quality})handle={handle}");
|
||||
AppLog.Write($"ffmpeg 已启动:{key}({width} 宽,{fps}fps,q={quality},{stream.Transport.ToUpperInvariant()})handle={handle}");
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
@ -202,25 +210,45 @@ public sealed class HikFfmpegService : IDisposable
|
||||
return $"rtsp://{Uri.EscapeDataString(userName)}:{Uri.EscapeDataString(password)}@{host}:{rtspPort}/Streaming/Channels/{streamId}";
|
||||
}
|
||||
|
||||
private void StartProcess(FfmpegStream stream, string url, int width, int fps, int quality)
|
||||
/// <summary>UDP 收不到帧时,等多久回退 TCP</summary>
|
||||
private static readonly TimeSpan UdpFallbackDelay = TimeSpan.FromSeconds(4);
|
||||
|
||||
private void StartProcess(FfmpegStream stream, string url, int width, int fps, int quality, bool lowLatency)
|
||||
{
|
||||
stream.Transport = lowLatency ? "udp" : "tcp";
|
||||
|
||||
var scale = fps is > 0 and < 25
|
||||
? $"scale={width}:-2,fps={fps}"
|
||||
: $"scale={width}:-2";
|
||||
|
||||
// 低延迟参数(实测:不关流分析/重排,首帧和稳态都会多出几百毫秒):
|
||||
// -analyzeduration 0 不花时间分析流
|
||||
// -max_delay / -reorder_queue_size 0 不为了“凑齐顺序”而攒包(实时画面宁可丢帧)
|
||||
// -fflags nobuffer 解码器不预读
|
||||
// -fps_mode passthrough 不在输出侧重复帧凑固定帧率
|
||||
var latencyArgs = lowLatency
|
||||
? new[] { "-analyzeduration", "0", "-max_delay", "0", "-reorder_queue_size", "0", "-avioflags", "direct" }
|
||||
: Array.Empty<string>();
|
||||
|
||||
var args = string.Join(' ',
|
||||
"-nostdin",
|
||||
"-loglevel", "error",
|
||||
"-rtsp_transport", "tcp",
|
||||
"-fflags", "nobuffer",
|
||||
"-flags", "low_delay",
|
||||
"-i", Quote(url),
|
||||
"-vf", Quote(scale),
|
||||
"-f", "mjpeg",
|
||||
"-q:v", quality.ToString(),
|
||||
"-an",
|
||||
"-flush_packets", "1",
|
||||
"pipe:1");
|
||||
new[] { "-nostdin", "-loglevel", "error", "-rtsp_transport", stream.Transport }
|
||||
.Concat(latencyArgs)
|
||||
.Concat(new[]
|
||||
{
|
||||
"-fflags", "nobuffer",
|
||||
"-flags", "low_delay",
|
||||
"-i", Quote(url),
|
||||
"-vf", Quote(scale),
|
||||
})
|
||||
.Concat(lowLatency ? new[] { "-fps_mode", "passthrough" } : Array.Empty<string>())
|
||||
.Concat(new[]
|
||||
{
|
||||
"-f", "mjpeg",
|
||||
"-q:v", quality.ToString(),
|
||||
"-an",
|
||||
"-flush_packets", "1",
|
||||
"pipe:1",
|
||||
}));
|
||||
|
||||
var psi = new ProcessStartInfo
|
||||
{
|
||||
@ -235,6 +263,7 @@ public sealed class HikFfmpegService : IDisposable
|
||||
|
||||
var process = new Process { StartInfo = psi, EnableRaisingEvents = true };
|
||||
stream.Process = process;
|
||||
stream.StartedAtTicks = Environment.TickCount64;
|
||||
|
||||
if (!process.Start())
|
||||
{
|
||||
@ -259,12 +288,45 @@ public sealed class HikFfmpegService : IDisposable
|
||||
});
|
||||
|
||||
// stdout:按 JPEG 的 FFD8…FFD9 切帧
|
||||
Task.Run(() => PumpFrames(stream));
|
||||
Task.Run(() => PumpFrames(stream, process));
|
||||
|
||||
// 低延迟默认走 UDP:部分网络/设备禁 UDP,4 秒还没出帧就回退 TCP(不能让用户看不了画面)
|
||||
if (lowLatency && stream.Transport == "udp") ScheduleUdpFallback(stream, url, width, fps, quality);
|
||||
}
|
||||
|
||||
private static void PumpFrames(FfmpegStream stream)
|
||||
/// <summary>UDP 收不到帧就杀掉进程、改用 TCP 重开一次(只重开一次)</summary>
|
||||
private void ScheduleUdpFallback(FfmpegStream stream, string url, int width, int fps, int quality)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(UdpFallbackDelay).ConfigureAwait(false);
|
||||
if (stream.Disposed || stream.FramesReceived > 0) return;
|
||||
|
||||
lock (stream.RestartGate)
|
||||
{
|
||||
// 再查一次:可能在等锁的这会儿出帧了
|
||||
if (stream.Disposed || stream.FramesReceived > 0) return;
|
||||
|
||||
AppLog.Write($"[ffmpeg] UDP 收不到帧,回退 TCP:{stream.Description}");
|
||||
|
||||
try { if (stream.Process is { HasExited: false }) stream.Process.Kill(true); } catch { /* 忽略 */ }
|
||||
try { stream.Process?.Dispose(); } catch { /* 忽略 */ }
|
||||
stream.Process = null;
|
||||
|
||||
StartProcess(stream, url, width, fps, quality, lowLatency: false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AppLog.Write($"[ffmpeg] 回退 TCP 失败:{ex.Message}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void PumpFrames(FfmpegStream stream, Process process)
|
||||
{
|
||||
var process = stream.Process!;
|
||||
var buffer = new List<byte>(256 * 1024);
|
||||
var chunk = new byte[64 * 1024];
|
||||
|
||||
@ -354,20 +416,47 @@ public sealed class HikFfmpegService : IDisposable
|
||||
public Process? Process { get; set; }
|
||||
public string? LastError { get; set; }
|
||||
|
||||
/// <summary>实时预览(UDP 优先 + 低延迟参数)</summary>
|
||||
public bool LowLatency { get; init; }
|
||||
|
||||
/// <summary>当前进程用的 RTP 传输方式:udp / tcp</summary>
|
||||
public string Transport { get; set; } = "tcp";
|
||||
|
||||
/// <summary>当前进程启动时刻(TickCount64,用来看首帧要多久)</summary>
|
||||
public long StartedAtTicks { get; set; }
|
||||
|
||||
/// <summary>已收到的帧数(UDP 回退 TCP 靠它判断“到底出没出画面”)</summary>
|
||||
public long FramesReceived { get; private set; }
|
||||
|
||||
/// <summary>已关闭(回退任务不能再把进程拉起来)</summary>
|
||||
public bool Disposed { get; private set; }
|
||||
|
||||
/// <summary>串行化“回退重开”</summary>
|
||||
public object RestartGate { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 新帧入队:覆盖「最新一帧」(背压),同时**逐帧通知订阅者**(回放对齐要每一帧)
|
||||
/// </summary>
|
||||
public void Enqueue(byte[] frame)
|
||||
{
|
||||
Action<byte[]>[] snapshot;
|
||||
bool first;
|
||||
|
||||
lock (_sync)
|
||||
{
|
||||
_latest = frame;
|
||||
_version++;
|
||||
FramesReceived++;
|
||||
first = FramesReceived == 1;
|
||||
snapshot = _subscribers.Count == 0 ? Array.Empty<Action<byte[]>>() : _subscribers.ToArray();
|
||||
}
|
||||
|
||||
if (first)
|
||||
{
|
||||
var cost = StartedAtTicks > 0 ? Environment.TickCount64 - StartedAtTicks : -1;
|
||||
AppLog.Write($"[ffmpeg] 首帧到达:{Description} 用时 {cost}ms({Transport.ToUpperInvariant()})");
|
||||
}
|
||||
|
||||
// 回调在 ffmpeg 读线程上:只允许做入队这类快操作
|
||||
foreach (var handler in snapshot)
|
||||
{
|
||||
@ -429,6 +518,8 @@ public sealed class HikFfmpegService : IDisposable
|
||||
|
||||
public void Terminate()
|
||||
{
|
||||
Disposed = true;
|
||||
|
||||
try
|
||||
{
|
||||
if (Process is { HasExited: false })
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user