packServer/desktop/Services/Hik/HikFfmpegService.cs
liufumingren e8923dbe56 feat(desktop): 多路回放按 OSD 时间对齐,合并时摆回同一时间轴
- 每路下载后读 NET_DVR_GetPlayBackOsdTime 得到真实起播时间,算出相对请求起点的偏移,
  返回 PlaybackClip(Path, StartOffset);合并时按偏移补 -itsoffset,画面不再错位
- 各路都从自己的关键帧起播 → 单路内容多了就裁掉,最后一路固定以收尾时间对齐
- 回放预览改成多路同播:每路独立 FfmpegStream 订阅帧、按第一帧到达时间对齐,
  队列超长时跳到最新帧(追帧),窗口结束即停;去掉「关闭」按钮改由流程自动关闭
- FfmpegStream 支持多订阅者(同一 rtsp 只拉一次),避免多路预览重复解码

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-21 00:47:43 +08:00

448 lines
15 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
/// <summary>
/// 按**完整 RTSP 地址**打开一路(回放预览用:地址里带 starttime/endtime)
///
/// <paramref name="keyPrefix"/> 是复用键前缀,**不能含密码**(地址里有账号密码)。
/// </summary>
public int OpenUrl(string url, string keyPrefix, int width = 1280, int fps = 25, int quality = 6)
{
if (!IsAvailable)
{
throw new HikException(uint.MaxValue,
"未找到 ffmpeg:安装目录下 native\\ffmpeg\\ffmpeg.exe 缺失(安装包可能不完整,建议重新安装本软件);" +
"也可以设置环境变量 YUNCANG_FFMPEG 指定 ffmpeg.exe,或改用「满帧率窗口」看主码流。");
}
var key = $"{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,
};
StartProcess(stream, url, width, fps, quality);
_streams[handle] = stream;
_byKey[key] = handle;
AppLog.Write($"ffmpeg 已启动:{key}({width} 宽,{fps}fps,q={quality})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}";
}
private void StartProcess(FfmpegStream stream, string url, int width, int fps, int quality)
{
var scale = fps is > 0 and < 25
? $"scale={width}:-2,fps={fps}"
: $"scale={width}:-2";
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");
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;
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));
}
private static void PumpFrames(FfmpegStream stream)
{
var process = stream.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>
/// 新帧入队:覆盖「最新一帧」(背压),同时**逐帧通知订阅者**(回放对齐要每一帧)
/// </summary>
public void Enqueue(byte[] frame)
{
Action<byte[]>[] snapshot;
lock (_sync)
{
_latest = frame;
_version++;
snapshot = _subscribers.Count == 0 ? Array.Empty<Action<byte[]>>() : _subscribers.ToArray();
}
// 回调在 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()
{
try
{
if (Process is { HasExited: false })
{
Process.Kill(true);
}
}
catch
{
// 忽略
}
try { Process?.Dispose(); } catch { /* 忽略 */ }
}
}
}