Search code examples
c#asp.net-mvcvideo-streamingrtspip-camera

Background playback for RTSP IP Camera in .net framework and output to http stream


I'm currently investigating the best method to allow clients live playback IP camera where my server is the source that uses RTSP to grab the stream, so at the same time I need to be able to output that live playback to HTML5 standard player "the video tag" and whenever I want, I need to be able to get a snapshot pretty quickly.

I tried using WebRequest to fetch a snapshot but the issue is that it takes around 1 second for the camera to prepare the snapshot.

I tried another solution like in here Extract thumbnail from RTSP but it takes 2 seconds for that image to be ready for my application.

Update 1

I managed to get a single frame by using ffmpeg.exe via the command line interface and passing the args:

"-i rtsp://UN:PW@IP:554/live -vframes 1 -f singlejpeg -"

And then after I start the process, I read the stream into output where it's my image binaries using:

process.StandardOutput.BaseStream.CopyTo(output);

Now my only left problem is that I wanted to keep reading the rtsp and get the binaries in a rate of for example "2 frames per second". Any working code ideas?


Solution

  • Alright, I think I figured out how to do what I needed to do. First, note down the command args that will work with no issues with ffmpeg:

    -rtsp_transport tcp -i rtsp://UN:PW@IP:554/live -err_detect ignore_err -r 10 -f image2pipe -

    Next, you have so setup your process this way:

    using (var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    Arguments = arguments,
                    FileName = fFMpegExePath
                },
                EnableRaisingEvents = true
            })
            {
                // code body
            }
    

    Now you need to process.Start() and then start listening to the process.StandardOutput.BaseStream as FileStream and handle the reading byte after byte. Note that I set a low frame rate -r 10 because if not set at all, the stream will hang for no visible reason but in this case I tested the task for more than 2 hours and it never fail.

    I hope it helps you all who faced this issue.