2

I have a webcam sitting on a network with low bandwith and I need to create an api endpoint in order to broadcast the video stream to many clients. So far I have been able to do so, the only issue is that for each request, my controller opens up a new stream to the webcam and forwards it to the client.

What I would like to do is to have a single stream/webcam service accessing the webcam and have the listeners access to the same stream, but I wasn't able to find information online regarding a Stream with multiple listeners. Is this feasible?

Here's the code I am using

[ApiController]
[Route("[controller]")]
public class Webcam : ControllerBase
{
    private readonly ILogger<Webcam> _logger;

    public Webcam(ILogger<Webcam> logger)
    {
        _logger = logger;
        _stream = new Lazy<Task<Stream>>
            (
                GetStream,
                true
            );
    }

    private Task<Stream> GetStream()
    {
        _logger.LogInformation("Opening stream");
        return _cli.Value.GetStreamAsync(WebCamUrl);
    }

    static readonly Lazy<HttpClient> _cli = new();
    const string WebCamUrl = "http://172.31.11.21/mjpg/video.mjpg?camera=3";

    public readonly Lazy<Task<Stream>> _stream;

    [HttpGet]
    public async Task<IActionResult> Test()
    {

        var result = new FileStreamResult(await _stream.Value, "multipart/x-mixed-replace;boundary=myboundary")
        {
            EnableRangeProcessing = true
        };
        return result;
    }
}
  • Too bad, you can't stream just one stream to multiple clients. You could "split" the stream and stream "copies" to multiple clients. – Jeroen van Langen Dec 03 '21 at 13:34
  • 1
    UDP is usually preferred for multicast video streaming. [networking - TCP vs UDP on video stream - Stack Overflow](https://stackoverflow.com/questions/6187456/tcp-vs-udp-on-video-stream) – Richard Deeming Dec 03 '21 at 13:36
  • @RichardDeeming the clients are also browsers, is that even allowed? – Niccolò Pecora Dec 03 '21 at 13:39
  • @JeroenvanLangen you can (in theory) continuously read webcam strream on server, and write what you read to all connected clients response streams (for as long as at least one client is connected). – Evk Dec 03 '21 at 13:43
  • @NiccolòPecora Seemingly not. [MDN](https://developer.mozilla.org/en-US/docs/Web/Guide/Audio_and_video_delivery/Live_streaming_web_audio_and_video) lists HTTP, RTMP *(Adobe Flash only)*, RTSP *(not supported natively in most browsers)*, and RTSP 2.0 *(currently in development)* as options, and warns that *"no browser currently supports anything other than HTTP without requiring plugins, although this looks set to change"*. – Richard Deeming Dec 03 '21 at 13:43
  • @JeroenvanLangen How would I accomplish that? I've tried copying the contents to a memory stream but I run into the same issue – Niccolò Pecora Dec 03 '21 at 13:46
  • @Evk "write what you read to all connected clients" So you multiplex it.. multiple streams – Jeroen van Langen Dec 03 '21 at 13:52
  • @Evk how do I write what I read to multiple HttpRequest without reading concurrently from the same stream? – Niccolò Pecora Dec 03 '21 at 16:24

0 Answers0