fix: 修改Camera的初始化命令,同时修改摄像头启动逻辑

This commit is contained in:
2025-07-11 12:42:24 +08:00
parent 8a1d6e52cb
commit 285d3e8585
3 changed files with 237 additions and 310 deletions

View File

@@ -1,5 +1,6 @@
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Peripherals.CameraClient; // 添加摄像头客户端引用
namespace server.Services;
@@ -86,6 +87,7 @@ public class HttpVideoStreamService : BackgroundService
// 摄像头客户端
private Camera? _camera;
private bool _cameraEnable = false;
private string _cameraAddress = "192.168.1.100"; // 默认FPGA地址
private int _cameraPort = 8888; // 默认端口
private readonly object _cameraLock = new object();
@@ -95,11 +97,6 @@ public class HttpVideoStreamService : BackgroundService
private readonly List<HttpListenerResponse> _activeClients = new List<HttpListenerResponse>();
private readonly object _clientsLock = new object();
/// <summary>
/// 获取 / 设置视频流服务是否启用
/// </summary>
public bool Enabled { get; set; } = false;
/// <summary>
/// 获取当前连接的客户端数量
/// </summary>
@@ -144,6 +141,21 @@ public class HttpVideoStreamService : BackgroundService
logger.Info("HttpVideoStreamService 初始化完成,默认摄像头地址: {Address}:{Port}", _cameraAddress, _cameraPort);
}
/// <summary>
/// [TODO:description]
/// </summary>
/// <param name="isEnabled">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
public async Task SetEnable(bool isEnabled)
{
if (_camera == null)
{
throw new Exception("Please config camera first");
}
_cameraEnable = isEnabled;
await _camera.EnableCamera(_cameraEnable);
}
/// <summary>
/// 配置摄像头连接参数
/// </summary>
@@ -292,7 +304,7 @@ public class HttpVideoStreamService : BackgroundService
// 开始生成视频帧
while (!stoppingToken.IsCancellationRequested)
{
if (Enabled)
if (_cameraEnable)
{
await GenerateVideoFrames(stoppingToken);
}
@@ -548,7 +560,7 @@ public class HttpVideoStreamService : BackgroundService
{
var frameInterval = TimeSpan.FromMilliseconds(1000.0 / _frameRate);
while (!cancellationToken.IsCancellationRequested && Enabled)
while (!cancellationToken.IsCancellationRequested && _cameraEnable)
{
try
{
@@ -751,7 +763,7 @@ public class HttpVideoStreamService : BackgroundService
return new ServiceStatus
{
IsRunning = (_httpListener?.IsListening ?? false) && Enabled,
IsRunning = (_httpListener?.IsListening ?? false) && _cameraEnable,
ServerPort = _serverPort,
FrameRate = _frameRate,
Resolution = $"{_frameWidth}x{_frameHeight}",
@@ -761,6 +773,7 @@ public class HttpVideoStreamService : BackgroundService
};
}
/// <summary>
/// 停止 HTTP 视频流服务
/// </summary>
@@ -768,7 +781,7 @@ public class HttpVideoStreamService : BackgroundService
{
logger.Info("正在停止 HTTP 视频流服务...");
Enabled = false;
_cameraEnable = false;
if (_httpListener != null && _httpListener.IsListening)
{