123 lines
4.4 KiB
C#
123 lines
4.4 KiB
C#
namespace SopHelper.Engine.Camera;
|
||
|
||
/// <summary>USB 相机连接与采集生命周期管理,推理管线始终订阅同一个服务。</summary>
|
||
public sealed class CameraService : ICameraService
|
||
{
|
||
private readonly ICameraService _uvc;
|
||
private ICameraService? _active;
|
||
private readonly SemaphoreSlim _operations = new(1, 1);
|
||
private bool _disposed;
|
||
private CameraInfo? _connectedCamera;
|
||
private UvcCaptureOptions? _configuredUsbOptions;
|
||
private string _modeDescription = "";
|
||
|
||
public CameraService(ICameraService? uvc = null)
|
||
{
|
||
_uvc = uvc ?? new UvcCameraService();
|
||
_uvc.FrameArrived += frame => FrameArrived?.Invoke(frame);
|
||
_uvc.ConnectionChanged += connected => ConnectionChanged?.Invoke(connected);
|
||
_uvc.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);
|
||
return await _uvc.EnumerateAsync().ConfigureAwait(false);
|
||
}
|
||
finally { _operations.Release(); }
|
||
}
|
||
|
||
public async Task<bool> ConnectAsync(CameraInfo camera)
|
||
{
|
||
await _operations.WaitAsync().ConfigureAwait(false);
|
||
try
|
||
{
|
||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||
if (camera.Kind != CameraKind.Uvc) throw new ArgumentException("仅支持 USB(UVC)相机", nameof(camera));
|
||
if (_active?.IsConnected == true && camera.IsSameDevice(_connectedCamera)
|
||
&& camera.UseCompatibilityBackend == _connectedCamera!.UseCompatibilityBackend)
|
||
return true;
|
||
_connectedCamera = null; _configuredUsbOptions = null;
|
||
if (_active is not null) await Task.Run(_active.Disconnect).ConfigureAwait(false);
|
||
_active = _uvc;
|
||
var connected = await _active.ConnectAsync(camera).ConfigureAwait(false);
|
||
if (connected)
|
||
_connectedCamera = new CameraInfo {Kind=camera.Kind,Index=camera.Index,
|
||
UseCompatibilityBackend=camera.UseCompatibilityBackend};
|
||
return connected;
|
||
}
|
||
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("请先连接相机");
|
||
options.Validate();
|
||
if (_configuredUsbOptions == options) return _modeDescription;
|
||
_configuredUsbOptions = null;
|
||
_modeDescription = await _active.ConfigureUvcAsync(options).ConfigureAwait(false);
|
||
_configuredUsbOptions = options with { };
|
||
return _modeDescription;
|
||
}
|
||
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();
|
||
_active = null;
|
||
_connectedCamera = null; _configuredUsbOptions = null;
|
||
}
|
||
finally { _operations.Release(); }
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
// 与尚未完成的连接/枚举串行;退出后不允许后台任务重新打开相机。
|
||
_operations.Wait();
|
||
try
|
||
{
|
||
if (_disposed) return;
|
||
_disposed = true;
|
||
try { _uvc.Dispose(); }
|
||
finally { _active = null; }
|
||
}
|
||
finally { _operations.Release(); }
|
||
}
|
||
}
|