1

I have a stream that I'm consuming from a camera. The stream data is returned in a callback function and what I would like to do is create an API endpoint that will return this stream to the requester as long as the stream is streaming.

Not sure if it's possible. The idea is to have the client download the current live stream.

In old versions of ASP.NET I know I could do:

Response.OutputStream.Write(buffer, 0, buffer.Length);

But in minimal api I see that I need to return a stream:

return Results.Stream(<MY_STREAM>);

My code looks like this for now:

app.MapGet("/stream", async (string deviceId, string token, int authType, int channel) =>
{
    // Code for connecting to the camera

    device.OnStreamData = new EventHandler(Device_OnStreamData);
    await device.StartStreamAsync(IntPtr.Zero, channel);

    while (true) //temporary here
    {
        System.Diagnostics.Debug.WriteLine("Running...");
        await Task.Delay(1000);
    }

    return "Hello";
});

void Device_OnStreamData(object sender, EventArgs e)
{
    StreamDataEventArgs streamData = (StreamDataEventArgs)e;
    System.Diagnostics.Debug.WriteLine("User = " + streamData.User + " | Type = " + streamData.Type + " | Data length = " + streamData.Data.Length);
}

Please ignore infinite loop - it's here just for testing that what I wish to do is possible. If successful this will be replaced with condition as long as camera is connected.

developer82
  • 13,237
  • 21
  • 88
  • 153
  • It is theoretically possible for clients to download live streams. But I don't understand that in your situation, the stream is always transmitting, or is it a specific streaming similar to downloading files. What are your current test results? – Chen Apr 21 '23 at 02:42
  • Here are two posts that transmit live streams: one [uses a proxy](https://stackoverflow.com/questions/58010263/stream-proxy-a-live-stream-in-asp-net-core), and the other [uses an external library for processing](https://stackoverflow.com/questions/73184619/receive-video-from-a-source-preprocess-and-stream-live-preview-to-the-client). maybe it will help you. – Chen Apr 21 '23 at 02:44
  • Thanks. In my case it's always transmitting. I successfully wrote an ASP.NET MVC controller that does what I described above. But I wish to do it with minimal APIs as well - I'm guessing there must be a way. – developer82 Apr 21 '23 at 04:13

0 Answers0