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.
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!
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
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/
VIDIOC_STREAMOFF: Bad file descriptor
VIDIOC_REQBUFS: Bad file descriptor
Re-examining my TLDR approach, trying again.
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
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?
The only problem now is getting it into Home Assistant (video fine, but it's that audio again).
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
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:
Within rtsp-simple-server.yaml I've so far used the following values:
Started the server by running:
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.