Reply
Emerging Member
Posts: 90
Registered: ‎07-16-2014
Kudos: 76
Accepted Solution

[Tutorial] Raspberry Pi 3 RTSP Stream Viewer

[ Edited ]

 

 ***( Updated 5/27/18 )***

*** Version 0.81 (Alpha) ***

 

New Code

As some of you may or may not know, I own and operate a small business. I installed a Unifi Video Camera System for a client with two RPI RTSP Screens. The person overseeing the project took interest in the RPI Screens and ended up rewriting practically all the code. Huge Thank You to AnonymousDog. New Code Below, Enjoy!

 

*** AnonymousDog Notes***

This code is alpha quality, and I have only tested this version on one live system.  While the bulk of this code has been tested in production for weeks, I encourage you to use at your own risk.

 

*** Features***

  • Feed rotation: You can display more feeds than you have room to display on screen at any one time by rotating feeds on and off screen.  It is possible, for instance, to display 6 mid-res feeds on a display grid whose on  screen windows are 2x2.  Two are always off screen, but all are rotated on screen (and off) within a few seconds.  This is possible due to omxplayer's registration and integration with dbus; calls to dbus are what actually moves the feeds around the screen.
  • Error correcting feed startup and playback (watchdog): This service registers each omxplayer instance with dbus, and the repair script checks in with dbus to ask each omxplayer whether it's still playing and, during startup, whether playback has progressed beyond zero seconds.  During startup, the service will make one attempt to restart feeds that failed to play after the maximum retries allowed.  The repair cron job will quit and restart feeds that do not answer (through dbus) that they are currently playing.  We no longer rely on screen to exit when omxplayer crashes (in order to detect and restart a failed feed).
  • displaycameras.service is a legit systemd service which means it will actually wait for network (and dbus) before starting.  No more worries about DHCP, waiting for network, or extended monitor startup times due to slow network.  systemd will also clean up behind the service if it fails to stop cleanly.  This also means it is fully configurable with the default tools like 'systemctl' and provides proper service status information.
  • For yet more diagnostic information, the primary script may be executed directly.  It provides copious error output if and when errors occur.  This makes it easy to configure and test before putting the monitor in production.  See the Testing section in the README.
  • An installer that installs pre-requisites, ensures the basic environment is reasonable for displaycameras' purposes (e.g., adequate gpu memory, display overscan disabled, etc.), and provides instruction for configuraton.
  • Separation of data and code: Scripts, service files, and cron jobs are deposited as discrete files in predictable Linux directories where experienced administrators will expect them (e.g., /usr/bin, /etc/systemd/system/, and /etc/cron.d/ respectively).  Configuration data is kept in files in /etc/displaycameras.  This means you can edit your feeds or windows or camera names without having to do a 'systemctl --daemon-reload' every time.
  • No dependency on or operational overhead from screen: Every screen session consumes resources and keeps a running user session intact the whole time the service displays cameras (using prior efforts).  This derivation does not use screen.  It instead directly launches an instance of omxplayer for each camera feed, registers that at a unique address with dbus, and uses dbus to perform watchdog and repair functions on the omxplayer instances.  Optional screen blanking introduces a non-essential dependency on fbi, but, if missing, the screen blanking fails silently, and feeds still display.  The only critical dependency is now omxplayer.
  • Installation, configuration, and removal instructions in the README file.
  • (Optional) automatic display resolution detection and window/camera/feed configuration for different display resolutions.  See README file.
  • (Optional) screen blanking: To prevent console display from showing during rotation or startup, enabling screen blanking will display a black image on the entire screen over the console but "under" the frame buffer layer where omxplayer displays the feeds.

Why?

This Tutorial will hopefully help anyone looking to create a cheap RTSP viewer for themselves or clients.  I had to create this for a resturant client that wanted to see certain points of his resturant while he was rolling out pizza dough. 

 

Main Items

 

IMG_20160914_041533.jpgIMG_20160916_015748.jpgBruno-RaspberryPiLR.jpg

----------------------------------------------------------------------------------------------------------------------------

 

1. Download the latest Raspbian Noob .img

https://www.raspberrypi.org/downloads/raspbian/

 

2. Download win32diskimager

https://sourceforge.net/projects/win32diskimager/

 

3. After writing the img file to the sd card and booting up the pi, go to terminal window.

 

4. Once in a terminal window type

 

sudo raspi-config

 

 5. Select these options:

(3) Boot Options > Wait For Network at Boot

 

(4) Localisation Options > Change Timezone > (Select Appropriate Timezone)

 

(5) Interfacing Options >  SSH >  Yes

 

(7) Advanced Options > Expand File System

 

(7) Advanced Options > Memory Split > 256

 

5. Now that you have booted into console, update your pi (This will take awhile)

 

sudo apt-get update && sudo apt-get upgrade -y && sudo reboot

  

6. I recommend you assign the PI a Static IP Address (Not Required) 

 

7. Once logged back into your PI edit the dhcpcd.conf file

 

sudo nano /etc/dhcpcd.conf

8. Next, uncomment the static ip example configuration 

Example of Proper Configuration 

Note: I left the ip6 address commented

interface eth0
static ip_address=192.168.0.10/24
#static ip6_address=fd51:42f8:caae:d92e::ff/64
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 8.8.8.8 

9. Exit dhcpcd.conf file by pressing Ctrl + X and Y to confirm overwrite

 

10. Reboot the PI

If you are having difficulties, follow this link: 

Raspberry PI Static IP

 

11. Next go to your desktop and download putty.  This program will allow us to remotely

manage our pi and allow us to copy and paste, saving lots of time.

http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

  

12. Now open putty.  Type the ip address you gave your pi in the ip address bar.  Now

click “open”.  If everything went correctly a shell window should open asking for your

Username.

 

Raspberry Pi Default Login:

Username = pi

Password = raspberry 

 

13. Once Logged into PI download the software to the pi (displaycameras.tgz available to download below)

wget -nH https://community.ubnt.com/ubnt/attachments/ubnt/airVision_board/54835/7/displaycameras-0.8.3.1.tar.gz

14. Once download is complete extact file

sudo tar -xvzf ./displaycameras_systemd.tgz

15. Navigate to the README File

cd displaycameras
nano README

 

Resources:

https://community.spiceworks.com/how_to/123787-raspberry-pi-powered-surveillance-camera-monitoring-d...

 

https://www.bitpi.co/2015/03/18/raspberry-pi-surveillance-monitor/

Attachment
Attachment

Accepted Solutions
Emerging Member
Posts: 53
Registered: ‎11-17-2014
Kudos: 23
Solutions: 2

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

I've spent quite a bit of time with this today and seem to have it working well (only time will tell!)

 

I'm running a 6 camera grid.

 

I found a typo in the code for the first large camera.  The line for the first camera has a variable of 'top_left'  this should say 'large_left'.  When the repair cron job would run it would start a new large left camera every time as it didn't show in the list.  Here is the incorrect code:

 

 

#First Colmn
large_left="screen -dmS top_left sh -c 'omxplayer...........

 

 

After rebooting I still had issues with cron job. I disabled the cron job and rebooted and let the streams start.  I then ran.

 

 

pi@raspberrypi:~ $ screen -list
No Sockets found in /var/run/screen/S-pi.

So no cameras showed and it started a second overlay

 

 

running as sudo showed the cams

 

 

pi@raspberrypi:~ $ sudo screen -list
There are screens on:
        2215.bottom_right       (04/06/16 09:13:02)     (Detached)
        2186.mid_rcenter        (04/06/16 09:13:01)     (Detached)
        2157.top_right  (04/06/16 09:13:01)     (Detached)
        2128.bottom_left        (04/06/16 09:13:01)     (Detached)
        2106.mid_lcenter        (04/06/16 09:13:01)     (Detached)
        2084.large_left (04/06/16 09:13:01)     (Detached)
6 Sockets in /var/run/screen/S-root.

 

So i've changed the repair part of the script to include sudo and its running well.

 

 

# Restart any camera feeds that have died
repair)
for i in "${camera_feeds[@]}"
do
if !(sudo screen -list | grep -q $i)
then
eval eval '$'$i
echo "$i is now running"
fi
done

Another change I made was to select a non existant audio stream so there was no audio. This is done with '-n -1'

 

 

I was testing this from home over a 30Mbps/10Mbps VPN connection to work where the NVR (Virtual Ubuntu on Hyper-V) is.  I was getting a lot vertical lines on the video like others have said.  When initally testing at work I never had these lines.  I thought is was a bandwidth issue but could see the Pi was only pulling 5Mbps.  I have no issue with chrome on a pc pulling about 11Mbps when using live view.

 

I could see there was UDP requests from the Pi but most of the traffic was TCP. After looking at a post on here which referred back to the omxplayer github page I've used another flag on omx player to only use TCP.  As soon as I did this I had clean video.  Yes!!

 

Here is my code, my changes are in red.

 

 

#!/bin/bash
### BEGIN INIT INFO
# Provides: omxplayer
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Displays camera feeds for monitoring
# Description:
### END INIT INFO

# Camera Feeds & Positions

#First Colmn
large_left="screen -dmS large_left sh -c 'omxplayer --avdict rtsp_transport:tcp --win \"0 0 1280 720\" rtsp://192.168.70.8:7447/a08ae983-457f-30fa-912a-9af1ce83326b_1 --live  -n -1'";
mid_lcenter="screen -dmS mid_lcenter sh -c 'omxplayer --avdict rtsp_transport:tcp --win \"640 720 1280 1080\" rtsp://192.168.70.8:7447/178c4047-8681-3218-995c-f0bbcbb5dab6_2 --live -n -1'";
bottom_left="screen -dmS bottom_left sh -c 'omxplayer --avdict rtsp_transport:tcp --win \"0 720 640 1080\" rtsp://192.168.70.8:7447/78f9c6fc-ec5c-3c66-903e-2ccf7b50cb62_2 --live -n -1'";

#Second Colmn
top_right="screen -dmS top_right sh -c 'omxplayer --avdict rtsp_transport:tcp --win \"1280 0 1920 360\" rtsp://192.168.70.8:7447/94488d32-bfc0-35e1-945a-0cdc4ef107c3_2 --live -n -1'";
mid_rcenter="screen -dmS mid_rcenter sh -c 'omxplayer --avdict rtsp_transport:tcp --win \"1280 360 1920 720\" rtsp://192.168.70.8:7447/2682a07f-7b32-3fab-83d9-ad872b81acd5_2 --live -n -1 '";
bottom_right="screen -dmS bottom_right sh -c 'omxplayer --avdict rtsp_transport:tcp --win \"1280 720 1920 1080\" rtsp://192.168.70.8:7447/6585d289-f62f-3543-b123-97eda3d5c6be_2 --live -n -1'";


# Camera Feed Names
# (variable names from above, separated by a space)
camera_feeds=(large_left mid_lcenter bottom_left top_right mid_rcenter bottom_right)

#---- There should be no need to edit anything below this line ----

# Start displaying camera feeds
case "$1" in
start)
for i in "${camera_feeds[@]}"
do
eval eval '$'$i
done
echo "Camera Display Started"
;;

# Stop displaying camera feeds
stop)
sudo killall omxplayer.bin
echo "Camera Display Ended"
;;

# Restart any camera feeds that have died
repair)
for i in "${camera_feeds[@]}"
do
if !(sudo screen -list | grep -q $i)
then
eval eval '$'$i
echo "$i is now running"
fi
done
;;

*)
echo "Usage: /etc/init.d/displaycameras {start|stop|repair}"
exit 1

;;
esac

 

 I've taken the Pi back to work and connected it to a screen. Will see if its still functioning ok on Tuesday when I'm back at work.

View solution in original post


All Replies
Regular Member
Posts: 368
Registered: ‎08-31-2014
Kudos: 87
Solutions: 5

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

I have one rPI B+ laying around ... I will try this when I find some time. 

 

Thanks boss Heart Thumbsup Driving

CCNA1, CCNA2 (ICND1)
Emerging Member
Posts: 90
Registered: ‎07-16-2014
Kudos: 76

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

Let me know how it goes.

Cheers
Regular Member
Posts: 368
Registered: ‎08-31-2014
Kudos: 87
Solutions: 5

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

[ Edited ]

it works

 

- Jessie Lite

- 2 medium streams at full quality

- ethernet

- DHCP enabled

 

HTOP

- ~40% CPU

- utilized 50MB RAM

 

step 18. doesn't work for me , after sudo reboot  the script does not start automatically ... I must manually start the script. Dunno what's wrong, triple checked everything. :/

 

"IMG_20160422_013832.jpg

 

EDIT: ok I probably found the problem

https://community.spiceworks.com/how_to/123787-raspberry-pi-powered-surveillance-camera-monitoring-d...

"I found that if I did not set a static IP address, Omxplayer did not load on startup. I had enabled "Wait for Network at Boot" in raspi-config, but it did not help."

CCNA1, CCNA2 (ICND1)
Regular Member
Posts: 368
Registered: ‎08-31-2014
Kudos: 87
Solutions: 5

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

1. sudo raspi-config
2. Wait for network to boot

Strangely after like 3-4 minutes the feed appears by itself. I don't know whether it's the cron repair or something is happening in the background. But yeah it works. Man Happy
CCNA1, CCNA2 (ICND1)
Emerging Member
Posts: 90
Registered: ‎07-16-2014
Kudos: 76

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

Yeah if you don't set it to static it doesn't work. I will edit that. Thanks for the info.
Regular Member
Posts: 368
Registered: ‎08-31-2014
Kudos: 87
Solutions: 5

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

Well , it does work with DHCP enabled. You just need to "wait for network to boot" and then wait like 3-4 minutes after the console appears. Could you try ? Works for me. Man Happy

CCNA1, CCNA2 (ICND1)
Emerging Member
Posts: 90
Registered: ‎07-16-2014
Kudos: 76

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

I did have that problem when I first set it up. I'll give it a shot. But if static IP fixes/speeds it up then why set DHCP?
Regular Member
Posts: 368
Registered: ‎08-31-2014
Kudos: 87
Solutions: 5

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

Network policy.
CCNA1, CCNA2 (ICND1)
Emerging Member
Posts: 90
Registered: ‎07-16-2014
Kudos: 76

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

Gotcha
Regular Member
Posts: 368
Registered: ‎08-31-2014
Kudos: 87
Solutions: 5

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

[ Edited ]

I have issues. I am running 2x RTSP streams twice. in a 2x2 matrix. The bandwith starts at 1.9Mbit/s

After a while the picture starts to blink and go completely mad. But I notice increase in bandwith to 3.4Mbit/s and as the other stream starts to go mad too the bandwith climbs. It completely corrupts the rtsp streams from the NVR. I had to soft reboot the cameras in order it to work properly again. I suspect the cron repair command duplicates TCP sessions until it kills the streams.

 

I am running vnstat via putty and htop too

 

vnstat -i eth0 --live
Monitoring eth0... (press CTRL-C to stop)

rx: 3.47 Mbit/s 486 p/s              tx: 5 kbit/s 4 p/s

 

 

 

This is how it looks like when all 4 go mad. I am using a small 15" EIZO IPS from 2002 beside my desktop to monitor the PI. The 46" TV is downstairs. Got tired running around the house ,lol.

IMG_20160423_165551.jpg

Video

https://www.dropbox.com/s/69an7rgiv1vjsl8/VID_20160423_182846.mp4?dl=0

 

If I step in time and I issue "/etc/init.d/displaycameras stop" then "/etc/init.d/displaycameras start" manually the picture is ok for another 5minutes or so then it starts again.

 

 

UPDATE: Now I disabled the cron job and it seems to be working. Steady RX at 1.90Mbit/s no blinking ,streams are ok, going to leave it sit for an hour or so. 

 

UPDATE2: after disabling/removing the cron job (repair) the streams does not load after reboot at all. No omxplayer process running at all. You just might have been right with the static ip address. As I enabled the cron job and left the config the streams started a minute later as I set it. So...

 

 

 

 

CCNA1, CCNA2 (ICND1)
Emerging Member
Posts: 90
Registered: ‎07-16-2014
Kudos: 76

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

Did you change the names of the cameras at all?
Emerging Member
Posts: 90
Registered: ‎07-16-2014
Kudos: 76

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

Also, what is your RTSP at? Medium at 500 kbps?
Regular Member
Posts: 368
Registered: ‎08-31-2014
Kudos: 87
Solutions: 5

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

[ Edited ]

10FPS 1500kbps 720x400 MEDIUM

15FPS 744Kbps  640x360  LOW

CCNA1, CCNA2 (ICND1)
Emerging Member
Posts: 90
Registered: ‎07-16-2014
Kudos: 76

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

Well that should be fine. Have you modified the script besides the RTSP addresses.
Regular Member
Posts: 368
Registered: ‎08-31-2014
Kudos: 87
Solutions: 5

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

[ Edited ]

haven't touched the /etc/init.d/displaycameras  only the rtsp addresses and added one more flag (--vol -6000) to omxplayer nothing more

CCNA1, CCNA2 (ICND1)
Regular Member
Posts: 368
Registered: ‎08-31-2014
Kudos: 87
Solutions: 5

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

[ Edited ]

can you make a script to run the displaycameras in /etc/network/if-up.d/ ?

 

my friend suggested also  sudo /etc/init.d/displaycameras defaults 99 but that did not work for me either

CCNA1, CCNA2 (ICND1)
Emerging Member
Posts: 90
Registered: ‎07-16-2014
Kudos: 76

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

I am new to Linux. Please explain.
Regular Member
Posts: 368
Registered: ‎08-31-2014
Kudos: 87
Solutions: 5

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

I will try to figure it out. I don't know either. Man Wink

CCNA1, CCNA2 (ICND1)
Emerging Member
Posts: 90
Registered: ‎07-16-2014
Kudos: 76

Re: [Tutorial] RTSP Raspberry Pi B+ Viewer [6-Cam] [4-Cam]

Best of luck
Reply