Tim
Tim

Reputation: 1769

Stream proxy a live stream in ASP.NET CORE

I have a stream url of my webcam, that returns a content-type of "multipart/x-mixed-replace;boundary=myboundary", let say that it is accessible via http://mywebcam/livrestream.cgi

I would like to create a proxy in ASP.NET CORE that can return the same stream.

I've created a route that get the stream :

[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;
}

It seems that I have to use PushStreamContent. But what should I do ? An endless while loop that query regulary the stream ? something else ?

Upvotes: 4

Views: 4919

Answers (1)

Nkosi
Nkosi

Reputation: 247641

HttpResponseMessage is no longer used as a first class citizen in asp.net-core framework and will be serialized as a normal object model.

Asp.net Core has built-in support for range requests.

Retrieve the stream from your accessible link and pass the stream on including the appropriate content type.

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;
}

Upvotes: 7

Related Questions