feat: 更新api,并更新了串流页面
This commit is contained in:
@@ -4,6 +4,73 @@ using Peripherals.CameraClient; // 添加摄像头客户端引用
|
||||
|
||||
namespace server.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 表示摄像头连接状态信息
|
||||
/// </summary>
|
||||
public class CameraStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// 摄像头的IP地址
|
||||
/// </summary>
|
||||
public string Address { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 摄像头的端口号
|
||||
/// </summary>
|
||||
public int Port { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已配置摄像头
|
||||
/// </summary>
|
||||
public bool IsConfigured { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 摄像头连接字符串(IP:端口)
|
||||
/// </summary>
|
||||
public string ConnectionString { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 表示视频流服务的运行状态
|
||||
/// </summary>
|
||||
public class ServiceStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// 服务是否正在运行
|
||||
/// </summary>
|
||||
public bool IsRunning { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 服务监听的端口号
|
||||
/// </summary>
|
||||
public int ServerPort { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 视频流的帧率(FPS)
|
||||
/// </summary>
|
||||
public int FrameRate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 视频分辨率(如 640x480)
|
||||
/// </summary>
|
||||
public string Resolution { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 当前连接的客户端数量
|
||||
/// </summary>
|
||||
public int ConnectedClients { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前连接的客户端端点列表
|
||||
/// </summary>
|
||||
public List<string> ClientEndpoints { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 摄像头连接状态信息
|
||||
/// </summary>
|
||||
public CameraStatus CameraStatus { get; set; } = new();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// HTTP 视频流服务,用于从 FPGA 获取图像数据并推送到前端网页
|
||||
/// 支持动态配置摄像头地址和端口
|
||||
@@ -28,6 +95,11 @@ 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>
|
||||
@@ -94,7 +166,7 @@ public class HttpVideoStreamService : BackgroundService
|
||||
|
||||
try
|
||||
{
|
||||
await Task.Run(() =>
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
lock (_cameraLock)
|
||||
{
|
||||
@@ -122,6 +194,22 @@ public class HttpVideoStreamService : BackgroundService
|
||||
|
||||
logger.Info("摄像头配置已更新: {Address}:{Port}", _cameraAddress, _cameraPort);
|
||||
}
|
||||
|
||||
// Init Camera
|
||||
{
|
||||
var ret = await _camera.Init();
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error(ret.Error);
|
||||
throw ret.Error;
|
||||
}
|
||||
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error($"Camera Init Failed!");
|
||||
throw new Exception($"Camera Init Failed!");
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
@@ -176,18 +264,15 @@ public class HttpVideoStreamService : BackgroundService
|
||||
/// 获取摄像头连接状态
|
||||
/// </summary>
|
||||
/// <returns>连接状态信息</returns>
|
||||
public object GetCameraStatus()
|
||||
public CameraStatus GetCameraStatus()
|
||||
{
|
||||
lock (_cameraLock)
|
||||
return new CameraStatus
|
||||
{
|
||||
return new
|
||||
{
|
||||
Address = _cameraAddress,
|
||||
Port = _cameraPort,
|
||||
IsConfigured = _camera != null,
|
||||
ConnectionString = $"{_cameraAddress}:{_cameraPort}"
|
||||
};
|
||||
}
|
||||
Address = _cameraAddress,
|
||||
Port = _cameraPort,
|
||||
IsConfigured = _camera != null,
|
||||
ConnectionString = $"{_cameraAddress}:{_cameraPort}"
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -215,7 +300,17 @@ public class HttpVideoStreamService : BackgroundService
|
||||
_ = Task.Run(() => AcceptClientsAsync(stoppingToken), stoppingToken);
|
||||
|
||||
// 开始生成视频帧
|
||||
await GenerateVideoFrames(stoppingToken);
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
if (Enabled)
|
||||
{
|
||||
await GenerateVideoFrames(stoppingToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Task.Delay(500, stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (HttpListenerException ex)
|
||||
{
|
||||
@@ -660,13 +755,13 @@ public class HttpVideoStreamService : BackgroundService
|
||||
/// <summary>
|
||||
/// 获取服务状态信息
|
||||
/// </summary>
|
||||
public object GetServiceStatus()
|
||||
public ServiceStatus GetServiceStatus()
|
||||
{
|
||||
var cameraStatus = GetCameraStatus();
|
||||
|
||||
return new
|
||||
return new ServiceStatus
|
||||
{
|
||||
IsRunning = _httpListener?.IsListening ?? false,
|
||||
IsRunning = (_httpListener?.IsListening ?? false) && Enabled,
|
||||
ServerPort = _serverPort,
|
||||
FrameRate = _frameRate,
|
||||
Resolution = $"{_frameWidth}x{_frameHeight}",
|
||||
@@ -683,6 +778,8 @@ public class HttpVideoStreamService : BackgroundService
|
||||
{
|
||||
logger.Info("正在停止 HTTP 视频流服务...");
|
||||
|
||||
Enabled = false;
|
||||
|
||||
if (_httpListener != null && _httpListener.IsListening)
|
||||
{
|
||||
_httpListener.Stop();
|
||||
|
||||
Reference in New Issue
Block a user