Using a Raspberry Pi as a Monitor for Ubiquiti UniFi Video Cameras
20th Mar 2016, 19:04:02
Based upon Raspberry Pi 4-Cam Matrix Viewer Appliance.
I really like Ubiquiti's UniFi CCTV system, but what it lacks is an easy way to have a live view of what the cameras are seeing. It's possible to log in to the NVR and use the live view feature, but this requires someone to enter the credentials and navigate a web browser to the correct page.
Thankfully, each camera has its own RTSP stream, so it is possible to display a stream of the footage direct from the cameras. From the 'Cameras' tab in the UniFi NVR web interface, open a 'Camera details' section, and then in 'Video', open the 'RTSP Service' dropdown. The URL for the stream takes the form of rtsp://NVR_IP:7447/CAMERA_ID_CHANNEL where channel is 0, 1 or 2 depending on the desired quality of the stream.
I am using a Raspberry Pi 3 to display the images. The process of installing Raspbian - the Pi-specific Debian distribution - is very well documented so I will not reproduce the steps here.
omxplayer is a command line video player developed specifically for the Raspberry Pi. This is the only package necessary:
$ sudo apt-get install omxplayer
omxplayer will display your stream anywhere on the screen and at any size, you just need to tell it what you want. For example:
$ omxplayer --win "0 0 1280 720" \ rtsp://n.n.n.n:7447/xxxxxxxx-yyyy-zzzz-pppp-rrrrrrrrrrrr_0
will stream your camera at 1280x720 positioned at 0,0 - the top left corner of the screen. The geometry you want to use depends on the size and aspect ratio of your screen as well as the number of cameras you want to display. I have a screen that is 1280x1024 and I want to display three images. More typically you might want a matrix of four cameras so it would be best to use a 1920x1080 display for this. Experiment to find what works for you.
When you're happy with your
omxplayer commands, you'll probably want to make them start automatically when the system boots. At first I tried using an init script to do this, but occasionally the RTSP streams would be interrupted and omxplayer would exit. I'd heard systemd was good at this kind of thing, and it proves to be the case. It's very easy to tell systemd to start these processes and make sure that they are restarted if they stop.
I decided to make a separate systemd service for each camera like so:
#/etc/systemd/system/ubntcamtop.service [Unit] Description=ubnt top camera Wants=network-online.target After=network.target network-online.target [Service] Restart=always ExecStart=/usr/bin/omxplayer --win "0 0 1280 720" rtsp://n.n.n.n:7447/xxxxxxxx-yyyy-zzzz-pppp-rrrrrrrrrrrr_0 [Install] WantedBy=multi-user.target
#/etc/systemd/system/ubntcambl.service [Unit] Description=ubnt bottom left camera Wants=network-online.target After=network.target network-online.target [Service] Restart=always ExecStart=/usr/bin/omxplayer --win "0 720 540 1024" rtsp://p.p.p.p:7447/xxxxxxxx-yyyy-zzzz-pppp-rrrrrrrrrrrr_0 [Install] WantedBy=multi-user.target
#/etc/systemd/system/ubntcambr.service [Unit] Description=ubnt bottom right camera Wants=network-online.target After=network.target network-online.target [Service] Restart=always ExecStart=/usr/bin/omxplayer --win "740 720 1280 1024" rtsp://q.q.q.q:7447/xxxxxxxx-yyyy-zzzz-pppp-rrrrrrrrrrrr_0 [Install] WantedBy=multi-user.target
Having created these jobs, systemd needs to be told about them:
$ sudo systemctl daemon-reload $ sudo systemctl enable ubntcamtop.service $ sudo systemctl enable ubntcambl.service $ sudo systemctl enable ubntcambr.service
That's pretty much it. Reboot the system and you should find that it will display your streams automatically without the need to log in or manually intervene in any way. If any of the omxplayer processes stop, they'll be started again in seconds. Go ahead and try it,
kill -9 any of the omxplayer processes and they'll start again.