Real Time Streaming Protocol and the Raspberry Pi – Video for Linux 2

I wanted my Raspberry Pi to generate an RTSP stream. In order to use video streaming on a lot of Home Automation systems, you need a true RTSP stream to integrate your camera with the rest of the system. I googled v4l2 RTSP server and I found just that! (Good name Guys!) v4l2 stands for Video for Linux 2. v4l one wasn’t great and it was named after v4w (video for windows (yuck)). I really like the versatility of v4l2rtspserver. It is supported by a ton of software. It supports a ton of hardware. BUT, You need to download it from git and then compile it and install it. But it’s worth the trouble! Let’s take a look at the steps required to get this service up and running on a Raspberry Pi.

Enable the Raspberry Pi Camera

From the command line, run “sudo raspi-config”. You’ll get a message about the default user, but you can just hit enter. Then, “Interface Options” > “Camera” > “Would you like to enable the camera?” Hit enter on <yes> and then <ok> and then tab down to <Finish>. Enter and reboot. Once you ssh back in, you should have a working camera. You can test it with the raspistill program that comes with Buster. (In bullseye, raspistill and raspivid are gone. You’ll have to look at libcamera.)

raspistill -v -o test.jpg

That will create a jpg file called test.jpg in your current directory. You’ll need to transfer it off to view it if you’re running headless. Otherwise, you can browse to it in the GUI. If your camera’s not working, double-check the connections. The shiny tips side of the ribbon cable should be facing the board side of the connector, not the top sliding lock part. Once you’re confident that your hardware is set up properly, you can move on to getting the rtsp server running.

How to compile and install v4l2rtspserver

Here’s the steps to install v4l2rstpserver on your Raspberry Pi. Essentially, we’re going to install some programming tools, get a copy of the software, compile it, and then finally copy the executable into place. You can cut and paste this whole block to your Pi:

apt -y install cmake liblog4cpp5-dev libv4l-dev git
git clone https://github.com/mpromonet/v4l2rtspserver.git
cd v4l2rtspserver/
cmake .
make
sudo make install

Easy-peasy. Let’s run this new software with a minimum of options to see if it works:

v4l2rtspserver -W 1920 -H 1080 -F 15 -P 7447 /dev/video0

You can play with the options. I’m using 1920 for the width and 1080 for the height. Frames per second is set to 15 and I want the service listening on port 7447. /dev/video0 is the pi camera. If you get fancy and plug in a USB camera, the device name will be different.

Now, go to your favorite network stream viewer and give it the following URI:

rtsp://video.local:7447/unicast

video.local is the FQDN of my raspberry. 7447 is the port we assigned above. You can customize this line for your environment. So, far, so good. Running from the command line lets you tweek the options to your liking. Let’s get this running as a service so the video server starts up at bootup and stays running while the Raspberry is on.

Running v4l2rtspserver as a Service

We’ll need to create a unit file and put the options we finalized into a file that the Raspberry will load at boot. Create a file called: /lib/systemd/system/v4l2rtspserver.service Here’s the contents:

[Unit]
Description=V4L2 RTSP server
After=network.target

[Service]
Type=simple
Restart=always
RestartSec=5
ExecStart=/usr/local/bin/v4l2rtspserver -W 640 -H 480 -F 15 -P 7447 /dev/video0
WorkingDirectory=/usr/local/share/v4l2rtspserver

[Install]
WantedBy=multi-user.target

As with any new service file, you’ll need to enable it, or set it to load at boot, so give the commands:

sudo systemctl enable v4l2rtspserver

And then you can either reboot or start it with:

sudo systemctl start v4l2rtspserver

Modify the Video Stream

Short of trying to rearrange your little hardware setup, you might want to play with these commands to make corrections:

v4l2-ctl --set-ctrl vertical_flip=1
v4l2-ctl --set-ctrl horizontal_flip=1

Finally, if you’re trying to get v4l2rtspserver working in Bullseye, you’re going to have to tinker with /boot/config.txt Running raspi-config doesn’t seem to do the trick! Here’s the end of mine. You should recognize some of the lines from yours.

# Uncomment some or all of these to enable the optional hardware interfaces
#dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on

# Uncomment this to enable infrared communication.
#dtoverlay=gpio-ir,gpio_pin=17
#dtoverlay=gpio-ir-tx,gpio_pin=18

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

# Enable DRM VC4 V3D drive
[pi4]
dtoverlay=vc4-fkms-v3d
max_framebuffers=2

[all]
gpu_mem=128
start_x=1
arm_64bit=1

2 thoughts on “Real Time Streaming Protocol and the Raspberry Pi – Video for Linux 2

  1. Dear John
    On my Raspberry pi 2W zero, I did tried a few days bet if I run “v4l2-ctl –set-ctrl vertical_flip=1” the system comes back with “vertical_flip unknown command”
    Any idea what I’m missing here?
    When I get this to work than I will try to get is to work on Motioneye

  2. Hi John,

    libcamera is the new reccomended way to access PI Camera, and Raspicam is the legacy one. Any idea on how to get a working RTSP server using libcamera (v4l2rtspserver doesn’t support it yet).

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.