feat: 添加Hdmi视频串流后端
This commit is contained in:
234
server/src/Services/HttpHdmiVideoStreamService.cs
Normal file
234
server/src/Services/HttpHdmiVideoStreamService.cs
Normal file
@@ -0,0 +1,234 @@
|
||||
using System.Net;
|
||||
using System.Collections.Concurrent;
|
||||
using Peripherals.HdmiInClient;
|
||||
|
||||
namespace server.Services;
|
||||
|
||||
public class HdmiVideoStreamEndpoint
|
||||
{
|
||||
public string BoardId { get; set; } = "";
|
||||
public string MjpegUrl { get; set; } = "";
|
||||
public string VideoUrl { get; set; } = "";
|
||||
public string SnapshotUrl { get; set; } = "";
|
||||
}
|
||||
|
||||
public class HttpHdmiVideoStreamService : BackgroundService
|
||||
{
|
||||
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private HttpListener? _httpListener;
|
||||
private readonly int _serverPort = 4322;
|
||||
private readonly ConcurrentDictionary<string, HdmiIn> _hdmiInDict = new();
|
||||
private bool _isEnabled = true;
|
||||
|
||||
public override async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_httpListener = new HttpListener();
|
||||
_httpListener.Prefixes.Add($"http://*:{_serverPort}/");
|
||||
_httpListener.Start();
|
||||
logger.Info($"HDMI Video Stream Service started on port {_serverPort}");
|
||||
|
||||
await base.StartAsync(cancellationToken);
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
HttpListenerContext? context = null;
|
||||
try
|
||||
{
|
||||
context = await _httpListener.GetContextAsync();
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// Listener closed, exit loop
|
||||
break;
|
||||
}
|
||||
catch (HttpListenerException)
|
||||
{
|
||||
// Listener closed, exit loop
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "Error in GetContextAsync");
|
||||
break;
|
||||
}
|
||||
if (context != null)
|
||||
_ = HandleRequestAsync(context, stoppingToken);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_httpListener?.Close();
|
||||
logger.Info("HDMI Video Stream Service stopped.");
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
logger.Info("Stopping HDMI Video Stream Service...");
|
||||
_isEnabled = false;
|
||||
_httpListener?.Close(); // 立即关闭监听器,唤醒阻塞
|
||||
await base.StopAsync(cancellationToken);
|
||||
}
|
||||
|
||||
// 获取/创建 HdmiIn 实例
|
||||
private HdmiIn? GetOrCreateHdmiIn(string boardId)
|
||||
{
|
||||
if (_hdmiInDict.TryGetValue(boardId, out var hdmiIn))
|
||||
return hdmiIn;
|
||||
|
||||
var db = new Database.AppDataConnection();
|
||||
if (db == null)
|
||||
{
|
||||
logger.Error("Failed to create HdmiIn instance");
|
||||
return null;
|
||||
}
|
||||
|
||||
var boardRet = db.GetBoardByID(Guid.Parse(boardId));
|
||||
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
|
||||
{
|
||||
logger.Error($"Failed to get board with ID {boardId}");
|
||||
return null;
|
||||
}
|
||||
|
||||
var board = boardRet.Value.Value;
|
||||
|
||||
hdmiIn = new HdmiIn(board.IpAddr, board.Port, 0); // taskID 可根据实际需求调整
|
||||
_hdmiInDict[boardId] = hdmiIn;
|
||||
return hdmiIn;
|
||||
}
|
||||
|
||||
private async Task HandleRequestAsync(HttpListenerContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
var path = context.Request.Url?.AbsolutePath ?? "/";
|
||||
var boardId = context.Request.QueryString["boardId"];
|
||||
if (string.IsNullOrEmpty(boardId))
|
||||
{
|
||||
await SendErrorAsync(context.Response, "Missing boardId");
|
||||
return;
|
||||
}
|
||||
|
||||
var hdmiIn = GetOrCreateHdmiIn(boardId);
|
||||
if (hdmiIn == null)
|
||||
{
|
||||
await SendErrorAsync(context.Response, "Invalid boardId or board not available");
|
||||
return;
|
||||
}
|
||||
|
||||
if (path == "/snapshot")
|
||||
{
|
||||
await HandleSnapshotRequestAsync(context.Response, hdmiIn, cancellationToken);
|
||||
}
|
||||
else if (path == "/mjpeg")
|
||||
{
|
||||
await HandleMjpegStreamAsync(context.Response, hdmiIn, cancellationToken);
|
||||
}
|
||||
else if (path == "/video")
|
||||
{
|
||||
await SendVideoHtmlPageAsync(context.Response, boardId);
|
||||
}
|
||||
else
|
||||
{
|
||||
await SendIndexHtmlPageAsync(context.Response, boardId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleSnapshotRequestAsync(HttpListenerResponse response, HdmiIn hdmiIn, CancellationToken cancellationToken)
|
||||
{
|
||||
var frameResult = await hdmiIn.ReadFrame();
|
||||
if (frameResult.IsSuccessful)
|
||||
{
|
||||
response.ContentType = "image/jpeg";
|
||||
await response.OutputStream.WriteAsync(frameResult.Value, 0, frameResult.Value.Length, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
response.StatusCode = 500;
|
||||
await response.OutputStream.WriteAsync(System.Text.Encoding.UTF8.GetBytes("Failed to get snapshot"));
|
||||
}
|
||||
response.Close();
|
||||
}
|
||||
|
||||
private async Task HandleMjpegStreamAsync(HttpListenerResponse response, HdmiIn hdmiIn, CancellationToken cancellationToken)
|
||||
{
|
||||
response.ContentType = "multipart/x-mixed-replace; boundary=--frame";
|
||||
while (!cancellationToken.IsCancellationRequested && _isEnabled)
|
||||
{
|
||||
var frameResult = await hdmiIn.ReadFrame();
|
||||
if (frameResult.IsSuccessful)
|
||||
{
|
||||
var header = $"--frame\r\nContent-Type: image/jpeg\r\nContent-Length: {frameResult.Value.Length}\r\n\r\n";
|
||||
await response.OutputStream.WriteAsync(System.Text.Encoding.ASCII.GetBytes(header));
|
||||
await response.OutputStream.WriteAsync(frameResult.Value, 0, frameResult.Value.Length, cancellationToken);
|
||||
await response.OutputStream.WriteAsync(System.Text.Encoding.ASCII.GetBytes("\r\n"));
|
||||
}
|
||||
await Task.Delay(33, cancellationToken); // ~30fps
|
||||
}
|
||||
response.Close();
|
||||
}
|
||||
|
||||
private async Task SendVideoHtmlPageAsync(HttpListenerResponse response, string boardId)
|
||||
{
|
||||
string html = $@"<html><body>
|
||||
<h1>HDMI Video Stream for Board {boardId}</h1>
|
||||
<img src='/mjpeg?boardId={boardId}' />
|
||||
</body></html>";
|
||||
response.ContentType = "text/html";
|
||||
await response.OutputStream.WriteAsync(System.Text.Encoding.UTF8.GetBytes(html));
|
||||
response.Close();
|
||||
}
|
||||
|
||||
private async Task SendIndexHtmlPageAsync(HttpListenerResponse response, string boardId)
|
||||
{
|
||||
string html = $@"<html><body>
|
||||
<h1>Welcome to HDMI Video Stream Service</h1>
|
||||
<a href='/video?boardId={boardId}'>View Video Stream for Board {boardId}</a>
|
||||
</body></html>";
|
||||
response.ContentType = "text/html";
|
||||
await response.OutputStream.WriteAsync(System.Text.Encoding.UTF8.GetBytes(html));
|
||||
response.Close();
|
||||
}
|
||||
|
||||
private async Task SendErrorAsync(HttpListenerResponse response, string message)
|
||||
{
|
||||
response.StatusCode = 400;
|
||||
await response.OutputStream.WriteAsync(System.Text.Encoding.UTF8.GetBytes(message));
|
||||
response.Close();
|
||||
}
|
||||
|
||||
public List<HdmiVideoStreamEndpoint>? GetAllVideoEndpoints()
|
||||
{
|
||||
var db = new Database.AppDataConnection();
|
||||
var boards = db?.GetAllBoard();
|
||||
if (boards == null)
|
||||
return null;
|
||||
|
||||
var endpoints = new List<HdmiVideoStreamEndpoint>();
|
||||
foreach (var board in boards)
|
||||
{
|
||||
endpoints.Add(new HdmiVideoStreamEndpoint
|
||||
{
|
||||
BoardId = board.ID.ToString(),
|
||||
MjpegUrl = $"http://{Global.localhost}:{_serverPort}/mjpeg?boardId={board.ID}",
|
||||
VideoUrl = $"http://{Global.localhost}:{_serverPort}/video?boardId={board.ID}",
|
||||
SnapshotUrl = $"http://{Global.localhost}:{_serverPort}/snapshot?boardId={board.ID}"
|
||||
});
|
||||
}
|
||||
return endpoints;
|
||||
}
|
||||
|
||||
public HdmiVideoStreamEndpoint GetVideoEndpoint(string boardId)
|
||||
{
|
||||
return new HdmiVideoStreamEndpoint
|
||||
{
|
||||
BoardId = boardId,
|
||||
MjpegUrl = $"http://{Global.localhost}:{_serverPort}/mjpeg?boardId={boardId}",
|
||||
VideoUrl = $"http://{Global.localhost}:{_serverPort}/video?boardId={boardId}",
|
||||
SnapshotUrl = $"http://{Global.localhost}:{_serverPort}/snapshot?boardId={boardId}"
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user