ASP.NET CORE 中的流代理直播流

Tim*_*Tim 3 c# video-streaming asp.net-core

我有一个网络摄像头的流 URL,它返回“multipart/x-mixed-replace;boundary=myboundary”的内容类型,假设可以通过http://mywebcam/livrestream.cgi访问它

我想在 ASP.NET CORE 中创建一个可以返回相同流的代理。

我创建了一条获取流的路线:

[Route("api/test")]
[HttpGet]
public async Task<HttpResponseMessage> Test()
{
    var client = new HttpClient();
    var inputStream = await client.GetStreamAsync("http://mywebcam/livrestream.cgi");
    var response = new HttpResponseMessage();
    response.Content = new PushStreamContent((stream, httpContent, transportContext) =>
    {
        // what to do ?
    }, "video/mp4");
    return response;
}
Run Code Online (Sandbox Code Playgroud)

看来我得用PushStreamContent了。但我该怎么办呢?定期查询流的无限 while 循环?还有别的事吗?

Nko*_*osi 7

HttpResponseMessage不再作为asp.net-core框架中的一等公民使用,并将被序列化为普通对象模型。

Asp.net Core 内置了对范围请求的支持。

从您的可访问链接检索流并传递包含适当内容类型的流。

static Lazy<HttpClient> client = new Lazy<HttpClient>();
const string WebCamUrl = "http://mywebcam/livrestream.cgi";

[Route("api/test")]
[HttpGet]
public async Task<IActionResult> Test() {        
    var contentType = "multipart/x-mixed-replace;boundary=myboundary";
    Stream stream = await client.Value.GetStreamAsync(WebCamUrl);
    var result = new FileStreamResult(stream, contentType) {
         EnableRangeProcessing = true
    };
    return result;
}
Run Code Online (Sandbox Code Playgroud)