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.