Raspberry Pi h264 RTSP Low Latency Camera Instructions
  • I have put together some instructions for setting up a Raspberry Pi h264 video streaming device that uses uv4l and v4l2rtspserver, along with an embedded timestamp.

    I've set up a few of these now using my instructions below and had it running for a couple of months, so it appears to be fairly robust.

    If you have some Pi's and ribbon cable connected cameras knocking around (I like using 3rd party 160 degree cameras), then these will produce a reasonable quality low latency h264 stream (that uses H/W decoding on my MacBook Pro 2013) at 1.3MP (1296x976).

    It's much better than MotionEye because of the high frame rate and low latency this solution gives you.

    If paired with a 160 degree camera (see link below), these are ideal for close range internal surveillance such as a porch, garage, shed, etc. The only downside is you won't be able to embed an audio stream.

    You can stream over wifi with sufficient bandwidth and signal quality, but I'd recommend wired ethernet if possible. I even have a Raspberry Pi connected with a PoE HAT streaming to my SecuritySpy server.

    Works with the latest Lite Buster Raspberry Pi OS image. Use a fresh image/install to ensure compatibility.

    #Enable the raspberry pi camera within Interfacing Options
    > sudo raspi-config

    #Run the following commands
    > curl http://www.linux-projects.org/listing/uv4l_repo/lpkey.asc | sudo apt-key add -
    > sudo nano /etc/apt/sources.list
    #Insert the following line in the sources.list file
    deb http://www.linux-projects.org/listing/uv4l_repo/raspbian/stretch stretch main

    > sudo apt update
    > sudo apt install cmake liblog4cpp5-dev libv4l-dev git vim uv4l uv4l-server uv4l-raspicam uv4l-raspicam-extras
    > sudo nano /etc/uv4l/uv4l-raspicam.conf
    #find the following lines in uv4l-raspicam.conf and alter them as follows
    encoding = h264
    width = 1296
    height = 972
    framerate = 10 # Can be higher, but set to 10 as per Ben's recommendations.
    text-overlay = yes
    text-filename = /usr/share/uv4l/raspicam/textnew.json
    rotation = 180

    > sudo nano /etc/systemd/system/uv4l_raspicam.service
    #Replace the existing ExecStart line with the following:
    ExecStart=/usr/bin/uv4l -f -k --sched-fifo --mem-lock --config-file=/etc/uv4l/uv4l-raspicam.conf --driver raspicam --driver-config-file=/etc/uv4l/uv4l-raspicam.conf --enable-server off

    #Set up the timestamp overlay
    > sudo nano /usr/share/uv4l/raspicam/uv4l-time-overlay.cpp
    #Given that this forum can't format code properly, copy text from the following pastebin link and paste it into the uv4l-time-overlay.cpp file created above.


    > cd /usr/share/uv4l/raspicam/
    > sudo g++ -o uv4l-time-overlay uv4l-time-overlay.cpp -std=c++11
    > sudo nano /etc/systemd/system/uv4l_raspicam_text.service
    #Copy the text below into this file
    Description=UV4L Raspicam Text

    ExecStart=/usr/share/uv4l/raspicam/uv4l-time-overlay /dev/video1 /usr/share/uv4l/raspicam/textnew.json


    > cd
    > git clone https://github.com/mpromonet/v4l2rtspserver.git
    > cd v4l2rtspserver/
    > cmake .
    > make
    > sudo make install
    > sudo reboot
    #The following line should run ok and not give you any "mmap: No such device" errors. If you do see those errors, check you've enabled the camera in rasps-config and check the ribbon cable is properly connected. Press ctrl+c to exit the command once tested.
    > v4l2rtspserver -U user:password -F10 -W 1296 -H 972 -P 8555 /dev/video1

    > sudo systemctl daemon-reload
    > sudo systemctl enable uv4l_raspicam
    > sudo systemctl enable uv4l_raspicam_text
    > nano ~/.bashrc
    #At the very end of this file, copy and paste the following. Remember to change the user name and password.
    if ! pidof -x "v4l2rtspserver" >/dev/null; then
    v4l2rtspserver -U user:password -F10 -W 1296 -H 972 -P 8555 /dev/video1

    #Enable console auto login
    > sudo raspi-config
    Choose option 3: Boot Options
    Choose option B1: Desktop / CLI
    Choose option B2: Console Autologin
    Select Finish, and reboot the pi.

    Now, on SecuritySpy, use the following device settings:

    RTSP port: 8555
    Username: user
    Password: password
    Profile: Manual configuration
    Format: RTSP (video and audio)
    Request: unicast

  • This is great, many thanks for sharing! This certainly seems like one of the better solutions for turning a Raspberry Pi into an IP camera, I'm sure this will be useful for others.
  • Running sudo curl http://www.linux-projects.org/listing/uv4l_repo/lpkey.asc | sudo apt-key add - gets the error "gpg: no valid OpenPGP data found." Any guidance greatly appreciated!
  • It looks like we need to state https now instead of http, so the following works ok:

    curl https://www.linux-projects.org/listing/uv4l_repo/lpkey.asc | sudo apt-key add -

    See here for the details: https://www.linux-projects.org/uv4l/installation/
  • Thanks Paul2020! That worked perfectly.
  • Third try with no errors at any step. Now getting error at boot:

    VIDIOC_STREAMOFF: Bad file descriptor
    VIDIOC_REQBUFS: Bad file descriptor
  • I didn't See here for the details: https://www.linux-projects.org/uv4l/installation/
    Re-examining my TLDR approach, trying again.
  • Good luck - I still use this to stream a video feed from a Raspi cam into SecuritySpy and it works really well (I use ethernet for reliability).
  • Very helpful - Thanks!

    Rather than auto login, I wanted to run the RTSP server as a service. Below are the contents of my v4l2rtspserver.service file, in case it's useful to anyone else. You'll of course want to change the username and password, and maybe width & height.

    Description=V4L RTSP Server
    After=uv4l_raspicam.service network.target

    ExecStart=/usr/local/bin/v4l2rtspserver -U larry:larry -F10 -W 1920 -H 1080 -P 8555 /dev/video0

  • Good write up ... thank you.

    I'd like to embed audio from a USB mic into this, but I'm unable to find a successful way after looking at the uv4l-raspicam documentation, and a general internet search. I suspect that things are complicated by use of the v4l-rtspserver. Anyone with any ideas, or pointers to appropriate resources on the Web?
  • With a lot more digging, I found that v4l2-rtspserver needs to be compiled with libasound2-dev available Now I have both video & audio :).

    The only problem now is getting it into Home Assistant (video fine, but it's that audio again).
  • @chsims1 I'd love to know what you did to integrate the sound please? Does it sync with the video ok? Which USB mic did you use (ie is it small enough to tuck in near the camera?)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!