aisop/src/SopHelper.CrossPlatform.Engine/Camera/CameraService.cs

128 lines
4.3 KiB
C#

namespace SopHelper.Engine.Camera;
/// <summary>统一枚举、按设备类型切换采集后端,推理管线始终订阅同一个服务。</summary>
public sealed class CameraService : ICameraService
{
private readonly ICameraService _uvc;
private readonly ICameraService _hik;
private ICameraService? _active;
private readonly SemaphoreSlim _operations = new(1, 1);
private bool _disposed;
public CameraService(ICameraService? uvc = null, ICameraService? hik = null)
{
_uvc = uvc ?? new UvcCameraService();
_hik = hik ?? new HikvisionCameraService();
foreach (var service in new[] { _uvc, _hik })
{
service.FrameArrived += frame => FrameArrived?.Invoke(frame);
service.ConnectionChanged += connected => ConnectionChanged?.Invoke(connected);
service.Error += message => Error?.Invoke(message);
}
}
public bool IsConnected => _active?.IsConnected == true;
public bool IsGrabbing => _active?.IsGrabbing == true;
public double? MeasuredCaptureFps => _active?.MeasuredCaptureFps;
public event Action<CameraFrame>? FrameArrived;
public event Action<bool>? ConnectionChanged;
public event Action<string>? Error;
public async Task<List<CameraInfo>> EnumerateAsync()
{
await _operations.WaitAsync().ConfigureAwait(false);
try
{
ObjectDisposedException.ThrowIf(_disposed, this);
// 优先展示海康;没有安装 MVS 时仍可使用 UVC。
var cameras = await _hik.EnumerateAsync().ConfigureAwait(false);
cameras.AddRange(await _uvc.EnumerateAsync().ConfigureAwait(false));
return cameras;
}
finally { _operations.Release(); }
}
public async Task<bool> ConnectAsync(CameraInfo camera)
{
await _operations.WaitAsync().ConfigureAwait(false);
try
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (_active is not null) await Task.Run(_active.Disconnect).ConfigureAwait(false);
_active = camera.Kind == CameraKind.Hikvision ? _hik : _uvc;
return await _active.ConnectAsync(camera).ConfigureAwait(false);
}
finally { _operations.Release(); }
}
public async Task SetExposureTimeAsync(double microseconds)
{
await _operations.WaitAsync().ConfigureAwait(false);
try
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (_active is null || !_active.IsConnected) throw new InvalidOperationException("请先连接相机");
await _active.SetExposureTimeAsync(microseconds).ConfigureAwait(false);
}
finally { _operations.Release(); }
}
public async Task<string> ConfigureUvcAsync(UvcCaptureOptions options)
{
await _operations.WaitAsync().ConfigureAwait(false);
try
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (_active is null || !_active.IsConnected) throw new InvalidOperationException("请先连接相机");
return await _active.ConfigureUvcAsync(options).ConfigureAwait(false);
}
finally { _operations.Release(); }
}
public async Task<bool> StartAsync()
{
await _operations.WaitAsync().ConfigureAwait(false);
try
{
ObjectDisposedException.ThrowIf(_disposed, this);
return _active is not null && await _active.StartAsync().ConfigureAwait(false);
}
finally { _operations.Release(); }
}
public void Stop()
{
_operations.Wait();
try { if (!_disposed) _active?.Stop(); }
finally { _operations.Release(); }
}
public void Disconnect()
{
_operations.Wait();
try
{
if (!_disposed) _active?.Disconnect();
}
finally { _operations.Release(); }
}
public void Dispose()
{
// 与尚未完成的连接/枚举串行;退出后不允许后台任务重新打开相机。
_operations.Wait();
try
{
if (_disposed) return;
_disposed = true;
try { _uvc.Dispose(); }
finally
{
try { _hik.Dispose(); }
finally { _active = null; }
}
}
finally { _operations.Release(); }
}
}