Skip to content

Raspberry Pi h264 RTSP Low Latency Camera Instructions

edited July 2020 in SecuritySpy
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.
https://thepihut.com/products/raspberry-pi-camera-board-night-vision-and-fisheye-160-lens-5mp

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
#optional
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.

https://pastebin.com/raw/u9erCFei

> 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
[Unit]
Description=UV4L Raspicam Text
After=uv4l_raspicam.service

[Service]
Type=simple
ExecStart=/usr/share/uv4l/raspicam/uv4l-time-overlay /dev/video1 /usr/share/uv4l/raspicam/textnew.json
Restart=on-abnormal
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target


> 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
fi

#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:

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

DONE!

Comments

  • 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!
  • edited April 2021
    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.

    [Unit]
    Description=V4L RTSP Server
    After=uv4l_raspicam.service network.target
    Requires=uv4l_raspicam.service

    [Service]
    Type=exec
    Environment=LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so
    ExecStart=/usr/local/bin/v4l2rtspserver -U larry:larry -F10 -W 1920 -H 1080 -P 8555 /dev/video0
    Restart=on-failure
    LimitNOFILE=65536

    [Install]
    WantedBy=multi-user.target
  • 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?
  • edited September 2021
    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?)
    Thanks.
  • Now that Raspbian Bullseye is out, raspicam has been deprecated in favor of libcamera. In testing libcamera on a Pi Zero 2W (ARM7 core), I get low latency streaming from the raspberry pi cameras. I can configure (after enabling the camera in raspy-config) a RPI based streaming server with the command (or service version of same):

    libcamera-vid -t 0 --inline --listen --width 640 --height 480 --codec h264 -n -o tcp://0.0.0.0:8554

    I can then connect to the stream using VLC on a different machine by opening the network URL:

    tcp/h264://raspberrypi:8554

    My problem is opening the same stream using SecuritySpy. Can anyone tell me if I need to pipe the steam though a different application to add RTMP/RTSP controls, or can I receive the stream on SecuitySpy. BTW - This works at larger frame sizes as the camera supports up to 2592x1944.

  • Hi @mmatyas I can't provide a full answer as I'm not familiar with this, but I can tell you that SecuritySpy requires an RTSP stream, so as long as you can get the pi software to produce this then SecuritySpy can consume it.

  • does this help?

    https://www.tomshardware.com/how-to/stream-live-video-raspberry-pi

    the author pipes the raspivid output through

    cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8080/}' :demux=h264
    


  • Hi All,

    I now have one of the new Raspberry Pi Camera Module v3 wide-angle cameras, and have been able to get it to stream nicely from Bullseye to Security Spy using rtsp-simple-server (https://github.com/aler9/rtsp-simple-server#from-a-raspberry-pi-camera)

    I just ran the following on a fresh install of Bullseye:

    pi@piv3cam:~ $ mkdir rtsp-simple-server
    pi@piv3cam:~ $ cd rtsp-simple-server/
    pi@piv3cam:~/rtsp-simple-server $
    pi@piv3cam:~/rtsp-simple-server $ wget https://github.com/aler9/rtsp-simple-server/releases/download/v0.21.2/rtsp-simple-server_v0.21.2_linux_armv7.tar.gz
    pi@piv3cam:~/rtsp-simple-server $ tar -zxvf rtsp-simple-server_v0.21.2_linux_armv7.tar.gz
    

    Within rtsp-simple-server.yaml I've so far used the following values:

    logLevel: info
    logDestinations: [stdout]
    logFile: rtsp-simple-server.log
    readTimeout: 10s
    writeTimeout: 10s
    readBufferCount: 512
    runOnConnectRestart: no
    
    rtspDisable: no
    protocols: [udp, multicast, tcp]
    encryption: "no"
    rtspAddress: :8554
    rtspsAddress: :8322
    rtpAddress: :8000
    rtcpAddress: :8001
    multicastIPRange: 224.1.0.0/16
    multicastRTPPort: 8002
    multicastRTCPPort: 8003
    authMethods: [basic, digest]
    
    paths:
      cam:
        source: rpiCamera
        rpiCameraWidth: 1920
        rpiCameraHeight: 1080
        rpiCameraVFlip: true
        rpiCameraFPS: 10
        readUser: user
        readPass: userpass
    

    Started the server by running:

    pi@piv3cam:~/rtsp-simple-server $ ./rtsp-simple-server
    

    Used these settings within SecuritySpy:

    It looks like the ability to add a clock to the video stream is being worked on so I'll update this post when it is available.

Sign In or Register to comment.