3

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 ?

Tim
  • 1,749
  • 3
  • 24
  • 48
  • Why using `HttpResponseMessage`? That is no longer used in asp.net-core. Secondly, also take headers into consideration to allow for the parts/chunks (ie: range headers) – Nkosi Sep 23 '19 at 15:02
  • why not, but i don't know what I should do in PushStreamContent ? or there is another way to accomplish that ? – Tim Sep 23 '19 at 15:26
  • see my solution for similar question about `PushStreamContent` here: https://stackoverflow.com/a/75781314/4983201 – afruzan Mar 19 '23 at 16:12

1 Answers1

7

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;
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • I don't know if it's working, the video doesn't launch in a dashjs player : – Tim Sep 24 '19 at 06:23
  • in the network inspector, I can see the request is "pending" – Tim Sep 24 '19 at 06:24
  • and finaly, i have net::ERR_ABORTED 500 (Internal Server Error) – Tim Sep 24 '19 at 06:25
  • @Tim have you tried debugging the test endpoint to see where the error is thrown? – Nkosi Sep 24 '19 at 12:23
  • @Tim might also need to pass the range headers from the client on to the cgi end point. – Nkosi Sep 24 '19 at 12:24
  • if I copy/paste the http://mywebcam/livrestream.cgi url in chrome, the video of my webcam is correctly streaming ; but maybe chrome is doing something more – Tim Sep 24 '19 at 14:17
  • @Tim check updated code. The code needs to act like the browser. Curious though. Is the cgi return a raw feed or a document? – Nkosi Sep 24 '19 at 14:19
  • @Tim I would also suggest stepping through the code in debug mode to narrow down where the error is happening. – Nkosi Sep 24 '19 at 14:20
  • When I put the webcam stream in chrome, I have theses headers : HTTP/1.1 200 OK Server: nginx Date: Wed, 25 Sep 2019 06:35:35 GMT Content-Type: multipart/x-mixed-replace;boundary=myboundary Transfer-Encoding: chunked Connection: keep-alive Keep-Alive: timeout=20 ; the type is "document" ; and now, with headers range, it's stuck on the SendAsync request. (note I had to convert from System.Net.Http.RangeHeaderValue and microsoft.net.http.RangeHeaderValue – Tim Sep 25 '19 at 06:59
  • thanks to have taken the time to answer me, I will give up, I think there's to many things I don't understand. I'll give ou the bounty reward – Tim Sep 25 '19 at 09:12