主码流走 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>
539 lines
19 KiB
C#
539 lines
19 KiB
C#
using System;
|
||
using System.Collections.Concurrent;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace YuncangTrace.LocalService.Services.Hik;
|
||
|
||
/// <summary>
|
||
/// 用 ffmpeg 把设备**主码流**转成 MJPEG(浏览器内嵌用)。
|
||
///
|
||
/// 为什么需要它:主码流是 2560×1440,PlayM4 的 <c>PlayM4_GetJPEG</c> 对这种大帧直接返回 0,
|
||
/// 设备抓帧(<c>NET_DVR_CaptureJPEGPicture_NEW</c>)又只有约 5fps。ffmpeg 在 C 里解码 + 缩放 + 编码,
|
||
/// 实测主码流(1440p → 1280 宽)可以跑到 ~22fps、约 80KB/帧。
|
||
///
|
||
/// ffmpeg 定位顺序:
|
||
/// 1. 环境变量 <c>YUNCANG_FFMPEG</c>(指向 ffmpeg.exe)
|
||
/// 2. 程序目录下 <c>native\ffmpeg\ffmpeg.exe</c>
|
||
/// 3. 系统 PATH 里的 <c>ffmpeg.exe</c>
|
||
///
|
||
/// 同一个 RTSP 地址 + 参数只起**一个** ffmpeg 进程(引用计数),多个页面看同一路不会重复转码。
|
||
/// </summary>
|
||
public sealed class HikFfmpegService : IDisposable
|
||
{
|
||
/// <summary>帧队列上限(超出丢最旧的,防止客户端慢把内存吃满)</summary>
|
||
private const int MaxQueuedFrames = 8;
|
||
|
||
/// <summary>单个 JPEG 帧上限(防止一直等不到 EOI 把缓冲区撑爆)</summary>
|
||
private const int MaxFrameBytes = 8 * 1024 * 1024;
|
||
|
||
private readonly ConcurrentDictionary<int, FfmpegStream> _streams = new();
|
||
private readonly Dictionary<string, int> _byKey = new(StringComparer.OrdinalIgnoreCase);
|
||
private readonly object _gate = new();
|
||
private int _nextHandle;
|
||
private bool _disposed;
|
||
|
||
/// <summary>ffmpeg.exe 路径(找不到为 null)</summary>
|
||
public string? FfmpegPath { get; } = ResolveFfmpeg();
|
||
|
||
public bool IsAvailable => !string.IsNullOrEmpty(FfmpegPath);
|
||
|
||
public int Count => _streams.Count;
|
||
|
||
/// <summary>ffmpeg 定位</summary>
|
||
public static string? ResolveFfmpeg()
|
||
{
|
||
// ① 环境变量
|
||
var fromEnv = Environment.GetEnvironmentVariable("YUNCANG_FFMPEG");
|
||
if (!string.IsNullOrWhiteSpace(fromEnv) && File.Exists(fromEnv)) return fromEnv;
|
||
|
||
// ② 程序目录 native\ffmpeg\ffmpeg.exe
|
||
var local = Path.Combine(AppContext.BaseDirectory, "native", "ffmpeg", "ffmpeg.exe");
|
||
if (File.Exists(local)) return local;
|
||
|
||
// ③ PATH
|
||
var path = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
|
||
foreach (var dir in path.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries))
|
||
{
|
||
try
|
||
{
|
||
var candidate = Path.Combine(dir.Trim(), "ffmpeg.exe");
|
||
if (File.Exists(candidate)) return candidate;
|
||
}
|
||
catch
|
||
{
|
||
// 路径非法跳过
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 启动(或复用)一路主码流转码
|
||
/// </summary>
|
||
/// <param name="streamId">RTSP 码流号:主码流 = 通道序号*100+1,子码流 = *100+2</param>
|
||
/// <param name="width">输出宽度(高度按比例),1280 是画质/带宽的平衡点</param>
|
||
/// <param name="fps">输出帧率上限(0 = 跟源)</param>
|
||
/// <param name="quality">MJPEG 质量 q:v(2 最好 ~ 31 最差;6 约等于高质量)</param>
|
||
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, lowLatency: true);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 按**完整 RTSP 地址**打开一路(回放预览用:地址里带 starttime/endtime)
|
||
///
|
||
/// <paramref name="keyPrefix"/> 是复用键前缀,**不能含密码**(地址里有账号密码)。
|
||
/// </summary>
|
||
/// <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)
|
||
{
|
||
throw new HikException(uint.MaxValue,
|
||
"未找到 ffmpeg:安装目录下 native\\ffmpeg\\ffmpeg.exe 缺失(安装包可能不完整,建议重新安装本软件);" +
|
||
"也可以设置环境变量 YUNCANG_FFMPEG 指定 ffmpeg.exe,或改用「满帧率窗口」看主码流。");
|
||
}
|
||
|
||
var key = lowLatency
|
||
? $"{keyPrefix}|{width}|{fps}|{quality}|ll"
|
||
: $"{keyPrefix}|{width}|{fps}|{quality}";
|
||
|
||
lock (_gate)
|
||
{
|
||
if (_byKey.TryGetValue(key, out var existing) && _streams.TryGetValue(existing, out var alive))
|
||
{
|
||
alive.RefCount++;
|
||
AppLog.Write($"ffmpeg 复用:{key} 引用数={alive.RefCount}");
|
||
return existing;
|
||
}
|
||
|
||
var handle = ++_nextHandle;
|
||
var stream = new FfmpegStream
|
||
{
|
||
Handle = handle,
|
||
Key = key,
|
||
Description = key,
|
||
RefCount = 1,
|
||
LowLatency = lowLatency,
|
||
};
|
||
|
||
StartProcess(stream, url, width, fps, quality, lowLatency);
|
||
|
||
_streams[handle] = stream;
|
||
_byKey[key] = handle;
|
||
AppLog.Write($"ffmpeg 已启动:{key}({width} 宽,{fps}fps,q={quality},{stream.Transport.ToUpperInvariant()})handle={handle}");
|
||
return handle;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取**比 afterVersion 更新**的帧(没有就等到超时)
|
||
///
|
||
/// 只保留「最新一帧」而不是帧队列:多个页面/多个会话共用同一路 ffmpeg 时,
|
||
/// 各自用自己的版本号取帧,互不抢帧,也不会互相拖慢。
|
||
/// </summary>
|
||
public bool TryGetLatest(int handle, long afterVersion, int timeoutMs, out byte[] frame, out long version)
|
||
{
|
||
frame = Array.Empty<byte>();
|
||
version = afterVersion;
|
||
|
||
if (!_streams.TryGetValue(handle, out var stream)) return false;
|
||
|
||
var deadline = Environment.TickCount64 + Math.Max(timeoutMs, 0);
|
||
while (true)
|
||
{
|
||
if (stream.TryGetLatest(afterVersion, out frame, out version)) return true;
|
||
if (Environment.TickCount64 > deadline) return false;
|
||
Thread.Sleep(10);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 订阅这一路的**每一帧**(回调在 ffmpeg 读线程上,只做入队这类快操作)。
|
||
///
|
||
/// 和 <see cref="TryGetLatest"/> 的区别:那边是「只留最新一帧」,适合网页这类只关心画面的场景;
|
||
/// 但**回放对齐不能丢帧** —— ffmpeg 定位到关键帧后会把「关键帧→请求时间」的整段一次性吐出来,
|
||
/// 只留最新帧会把这串的前面全丢掉,拿到的“第一帧”可能已经是两秒后的画面,
|
||
/// 几路按它算延迟就会整体错开(现场就是这个现象)。
|
||
/// </summary>
|
||
public IDisposable Subscribe(int handle, Action<byte[]> onFrame)
|
||
{
|
||
if (!_streams.TryGetValue(handle, out var stream)) return new NoopDisposable();
|
||
return stream.AddSubscriber(onFrame);
|
||
}
|
||
|
||
private sealed class NoopDisposable : IDisposable
|
||
{
|
||
public void Dispose()
|
||
{
|
||
// 没有订阅成功(流已关闭),什么都不用做
|
||
}
|
||
}
|
||
|
||
public bool Close(int handle)
|
||
{
|
||
lock (_gate)
|
||
{
|
||
if (!_streams.TryGetValue(handle, out var stream)) return false;
|
||
|
||
stream.RefCount--;
|
||
if (stream.RefCount > 0)
|
||
{
|
||
AppLog.Write($"ffmpeg 释放引用:handle={handle} 剩余={stream.RefCount}");
|
||
return true;
|
||
}
|
||
|
||
_streams.TryRemove(handle, out _);
|
||
if (_byKey.TryGetValue(stream.Key, out var mapped) && mapped == handle) _byKey.Remove(stream.Key);
|
||
|
||
stream.Terminate();
|
||
AppLog.Write($"ffmpeg 已停止:{stream.Description} handle={handle}");
|
||
return true;
|
||
}
|
||
}
|
||
|
||
private static string BuildRtspUrl(string host, int rtspPort, string userName, string password, string streamId)
|
||
{
|
||
// 海康 RTSP:/Streaming/Channels/{通道序号}{码流号},如 101=第1通道主码流、102=子码流
|
||
return $"rtsp://{Uri.EscapeDataString(userName)}:{Uri.EscapeDataString(password)}@{host}:{rtspPort}/Streaming/Channels/{streamId}";
|
||
}
|
||
|
||
/// <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(' ',
|
||
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
|
||
{
|
||
FileName = FfmpegPath!,
|
||
Arguments = args,
|
||
UseShellExecute = false,
|
||
CreateNoWindow = true,
|
||
RedirectStandardOutput = true,
|
||
RedirectStandardError = true,
|
||
RedirectStandardInput = false,
|
||
};
|
||
|
||
var process = new Process { StartInfo = psi, EnableRaisingEvents = true };
|
||
stream.Process = process;
|
||
stream.StartedAtTicks = Environment.TickCount64;
|
||
|
||
if (!process.Start())
|
||
{
|
||
throw new HikException(uint.MaxValue, "ffmpeg 启动失败");
|
||
}
|
||
|
||
// stderr 只留最后一段用于报错
|
||
Task.Run(() =>
|
||
{
|
||
try
|
||
{
|
||
string? line;
|
||
while ((line = process.StandardError.ReadLine()) != null)
|
||
{
|
||
stream.LastError = line;
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
// 进程退出
|
||
}
|
||
});
|
||
|
||
// stdout:按 JPEG 的 FFD8…FFD9 切帧
|
||
Task.Run(() => PumpFrames(stream, process));
|
||
|
||
// 低延迟默认走 UDP:部分网络/设备禁 UDP,4 秒还没出帧就回退 TCP(不能让用户看不了画面)
|
||
if (lowLatency && stream.Transport == "udp") ScheduleUdpFallback(stream, url, width, fps, quality);
|
||
}
|
||
|
||
/// <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 buffer = new List<byte>(256 * 1024);
|
||
var chunk = new byte[64 * 1024];
|
||
|
||
try
|
||
{
|
||
var stdout = process.StandardOutput.BaseStream;
|
||
int read;
|
||
while ((read = stdout.Read(chunk, 0, chunk.Length)) > 0)
|
||
{
|
||
buffer.AddRange(chunk.AsSpan(0, read).ToArray());
|
||
ExtractFrames(stream, buffer);
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
// 进程被杀 / 管道断开,属于正常结束
|
||
}
|
||
}
|
||
|
||
/// <summary>从缓冲区里切出完整 JPEG 帧</summary>
|
||
private static void ExtractFrames(FfmpegStream stream, List<byte> buffer)
|
||
{
|
||
while (true)
|
||
{
|
||
var start = IndexOf(buffer, 0xFF, 0xD8, 0);
|
||
if (start < 0)
|
||
{
|
||
buffer.Clear();
|
||
return;
|
||
}
|
||
|
||
if (start > 0) buffer.RemoveRange(0, start);
|
||
|
||
var end = IndexOf(buffer, 0xFF, 0xD9, 2);
|
||
if (end < 0)
|
||
{
|
||
if (buffer.Count > MaxFrameBytes) buffer.Clear();
|
||
return;
|
||
}
|
||
|
||
var frame = new byte[end + 2];
|
||
buffer.CopyTo(0, frame, 0, frame.Length);
|
||
buffer.RemoveRange(0, frame.Length);
|
||
|
||
if (frame.Length > 1000) stream.Enqueue(frame);
|
||
}
|
||
}
|
||
|
||
private static int IndexOf(List<byte> buffer, byte first, byte second, int from)
|
||
{
|
||
for (var i = from; i < buffer.Count - 1; i++)
|
||
{
|
||
if (buffer[i] == first && buffer[i + 1] == second) return i;
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
private static string Quote(string value) => $"\"{value.Replace("\"", "\\\"")}\"";
|
||
|
||
public void CloseAll()
|
||
{
|
||
foreach (var handle in _streams.Keys.ToList())
|
||
{
|
||
Close(handle);
|
||
}
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
if (_disposed) return;
|
||
_disposed = true;
|
||
CloseAll();
|
||
}
|
||
|
||
/// <summary>一路 ffmpeg 转码</summary>
|
||
private sealed class FfmpegStream
|
||
{
|
||
private readonly object _sync = new();
|
||
private readonly List<Action<byte[]>> _subscribers = new();
|
||
private byte[] _latest = Array.Empty<byte>();
|
||
private long _version;
|
||
|
||
public int Handle { get; init; }
|
||
public string Key { get; init; } = string.Empty;
|
||
public string Description { get; init; } = string.Empty;
|
||
public int RefCount { get; set; }
|
||
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)
|
||
{
|
||
try
|
||
{
|
||
handler(frame);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AppLog.Write($"[ffmpeg] 帧回调异常({Description}):{ex.Message}");
|
||
}
|
||
}
|
||
}
|
||
|
||
public bool TryGetLatest(long afterVersion, out byte[] frame, out long version)
|
||
{
|
||
lock (_sync)
|
||
{
|
||
version = _version;
|
||
if (_version > afterVersion && _latest.Length > 0)
|
||
{
|
||
frame = _latest;
|
||
return true;
|
||
}
|
||
|
||
frame = Array.Empty<byte>();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public IDisposable AddSubscriber(Action<byte[]> handler)
|
||
{
|
||
lock (_sync) _subscribers.Add(handler);
|
||
return new Subscription(this, handler);
|
||
}
|
||
|
||
private void RemoveSubscriber(Action<byte[]> handler)
|
||
{
|
||
lock (_sync) _subscribers.Remove(handler);
|
||
}
|
||
|
||
private sealed class Subscription : IDisposable
|
||
{
|
||
private FfmpegStream? _owner;
|
||
private readonly Action<byte[]> _handler;
|
||
|
||
public Subscription(FfmpegStream owner, Action<byte[]> handler)
|
||
{
|
||
_owner = owner;
|
||
_handler = handler;
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
_owner?.RemoveSubscriber(_handler);
|
||
_owner = null;
|
||
}
|
||
}
|
||
|
||
public void Terminate()
|
||
{
|
||
Disposed = true;
|
||
|
||
try
|
||
{
|
||
if (Process is { HasExited: false })
|
||
{
|
||
Process.Kill(true);
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
// 忽略
|
||
}
|
||
|
||
try { Process?.Dispose(); } catch { /* 忽略 */ }
|
||
}
|
||
}
|
||
}
|