Making a RTSP Server out of a Raspberry PI in 15 minutes or less

Recently, I was performing some video analytics against a new Reolink PoE camera.  Nice new camera, BUT it struggled with OpenCV, namely, I was getting the following type of errors:

[h264 @ 0xafb9ec0] left block unavailable for requested intra4x4 mode -1
[h264 @ 0xafb9ec0] error while decoding MB 0 28, bytestream 11378
[h264 @ 0xafb9500] error while decoding MB 38 19, bytestream -26
[h264 @ 0xafb9500] left block unavailable for requested intra4x4 mode -1
[h264 @ 0xafb9500] error while decoding MB 0 28, bytestream 694
[h264 @ 0xafb9500] error while decoding MB 15 22, bytestream -18
[h264 @ 0xafb9ec0] error while decoding MB 30 19, bytestream -10
[h264 @ 0xafb9500] error while decoding MB 48 19, bytestream -26
[h264 @ 0xafb9080] error while decoding MB 10 20, bytestream -10
[h264 @ 0xafb9a00] left block unavailable for requested intra4x4 mode -1
[h264 @ 0xafb9a00] error while decoding MB 0 24, bytestream 842
[h264 @ 0xafb9ec0] error while decoding MB 59 29, bytestream -20
[h264 @ 0xafb9ec0] left block unavailable for requested intra4x4 mode -1
[h264 @ 0xafb9ec0] error while decoding MB 0 26, bytestream 620
[h264 @ 0xafb9a00] error while decoding MB 42 23, bytestream -22
[h264 @ 0xafb9ec0] error while decoding MB 46 21, bytestream -32
[h264 @ 0xafb9080] left block unavailable for requested intra4x4 mode -1
[h264 @ 0xafb9080] error while decoding MB 0 20, bytestream 902
[h264 @ 0xafb9500] left block unavailable for requested intra4x4 mode -1
[h264 @ 0xafb9500] error while decoding MB 0 22, bytestream 608
[h264 @ 0xafb9500] error while decoding MB 38 19, bytestream -28

Because the processing I needed to do was sub second, I could not rely on the sporadic errors and as such had to find a different solution.  [Note, the RTSP server on Pi still produces some error, but no where near as many as the Reolink did.  Also, I did open a ticket with Reolink and they could not solve the issue.  :(]

I set out to see if I could easily convert a raspberry pi + camera to do the job.

While I could have used most any USB camera, I chose to use the Pi camera, just because I already had it (and no use for it).

Install Process:

  1. install Raspbian Stretch on the device (~6 minutes using Raspbian Lite)

2. log into the system and switch user to root

su root

3. update the system and install git and cmake

apt update && apt install git cmake

4. download the source for v412rtspserver

git clone https://github.com/mpromonet/v4l2rtspserver.git

5. make and install the code (~5 minutes)

cd v4l2rtspserver && cmake . && make && make install

6. add the following command to your /etc/rc.local

v4l2rtspserver /dev/video0 &

7. in VLC, open network stream to:  rtsp://{IPAddressOfYourPI}:8554/unicast

You should see something similar to below:

image

From a performance perspective, there is very little CPU or memory used, as shown below, meaning this could easily run on a PI Zero:

proc

Note, not all cameras support the H264, as discussed here: https://github.com/mpromonet/v4l2rtspserver/issues/88

 

 

17 thoughts on “Making a RTSP Server out of a Raspberry PI in 15 minutes or less

  1. If any of You have problem to install v4l2rtspserver only with command:
    pi:raspberry: sudo cmake . && make && make install
    Then become root:
    pi:raspberry: su
    enter password and then install that how I made it work for me gl 🙂

    Like

  2. I can’t seem to make this work. It really looks like it could be a solution I am looking for. After pasting in your commands when i do TOP the v4l2 server isn’t running. I also rebooted. What do I need to do ?

    Like

      1. Yes I have the V2 camera. I went with a fresh install and copied your procedures, but what I get is…NOTICE] /home/pi/v4l2rtspserver/src/main.cpp:599
        VIDIOC_STREAMOFF: Bad file descriptor
        VIDIOC_REQBUFS: Bad file descriptor
        Create V4L2 Source…/dev/video0

        When I run the top command I do not see any v4l2 server running. Am i missing something?
        I appreciate your help.

        Like

  3. Are you still able to assist ksaye? I’d really like to get this running. IS there an opportunity to download an image of an SD that works so I can look at it??

    Liked by 1 person

  4. Just an update, as I finally got back to this. It works! Thanks so much. I know with raspivid and such you can flip the video and etc..can you do that with this? Also cange bitrate and resolution? Thank you.

    Like

  5. Do you have any idea how to decrease the cache with the V4l2 server. The stream that I am receiving is quite delayed (upwards of 2 to 3 seconds) and I was hoping that decreasing a cache value would decrease the delay.

    Like

  6. Does your software use a Docker container?
    Can it be set up as a service to run automatically at startup – even when nobody is logged in?

    After a reboot it is not active for me. I have to log in and manually start at command line.

    Regards,
    Mike

    Like

  7. Just to add performing the command v4l2rtspserver /dev/video0 and it does not produce the first 3 errors above but I still get the error below when connecting to the server.

    2021-03-02 16:19:51,136 [NOTICE] – /home/pi/rpos/v4l2rtspserver/v4l2wrapper/src/V4l2MmapDevice.cpp:73
    Device /dev/video0 nb buffer:10
    2021-03-02 16:19:51,532 [NOTICE] – /home/pi/rpos/v4l2rtspserver/main.cpp:449
    Create Source …/dev/video0
    2021-03-02 16:19:51,536 [NOTICE] – /home/pi/rpos/v4l2rtspserver/inc/V4l2RTSPServer.h:84
    Play this stream using the URL “rtsp://192.168.X.X:8554/unicast”
    2021-03-02 16:19:51,540 [NOTICE] – /home/pi/rpos/v4l2rtspserver/src/DeviceSource.cpp:93
    begin thread
    RTCPInstance::RTCPInstance error: totSessionBW parameter should not be zero!

    Also I tried RPI-update and still the same issues.

    Like

Leave a comment