Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post here to indicate any hardware you have used and which is known to work with ZoneMinder. Not for questions.
postfux
Posts: 20
Joined: Thu Mar 03, 2005 11:33 am
Location: Austria

Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by postfux »

This is a little tutorial on how to use a Raspbery Pi with camera module
as an input for Zoneminder on a Linuxbox.


This is only a tutorial on what i researched and collected from
Internet at so many different pages that brought me to this goal,
it took my very long to find a acceptable solution to archive this.

This solution is working for me and hopefully usefull for others too,

Before i tried different things like running Zoneminder on the RaspberryPi without real success.
In my first attemps i tryed to stream over vlc with 25fps but then my Zoneminder used ~30-40% CPU per stream!!!
and i couldn't get vlc running stable with lower framerate, and also the delay was 3 to 5 seconds!!!
Other solution like mjpegstreamer, gstreamer, uv4l, etc didn't work to my expects.


I use raspivid -> ffmpeg -> ffserver ----->>>>> ffmepg -> Zoneminder.
It streams the video at 5fps with a resolution at 640x360, both can be
increased if you have a powerfull Linuxbox where Zoneminder is running.

Advantages:

Code: Select all

+++ The Pi is running at ~5% CPU usage wich is realy low.
+++ Zoneminder on my Linuxbox uses arround ~5-10% more CPU per stream
+++ The picture quality is really good, much better then all my Analog cameras
+++ The delay from the Picture to Zoneminder is at acceptable ~1 second
+++ Raspberry Pi + Camera module costs ~60€ wich is less then most Cameras 
Disadvantages:

Code: Select all

--- The setup is very complex and takes much time
--- If the connection between the Pi and the Zoneminder gets lost, 
      it takes serveral time until it functions again.
--- The PiCam is not very good at lowlight condition
You need
o) Raspberry Pi preferable Model B
o) Raspberry Picam module
o) A Pi-case or a dummy-camera-case
o) SD-Card
o) Raspbian installed on the SD card
o) Zoneminder compiled with self compiled ffmpeg

Enable Camera Module with

Code: Select all

sudo raspi-config  -> 5 Enable Camera -> Enable -> Finish -> reboot
make a complete upgrade of raspbian and install additional packages

Code: Select all

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libx264-dev git
I reccomend putting the ffmpeg tempfile on ram instead of sdcard by adding following

Code: Select all

sudo mkdir /tmp2
sudo nano /etc/fstab
add:

Code: Select all

tmpfs           /tmp2              tmpfs   defaults,noatime,mode=1777 0       0
mount tempfs in ram

Code: Select all

mount /tmp2
create downloaddir or change to your own

Code: Select all

mkdir ~/download
cd ~/download
download ffmpeg from git repository, don't use ffmpeg from the Raspbian packages it has no support for x264

Code: Select all

git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
configure ffmpeg and enable x264 (MPEG-4 AVC)

Code: Select all

cd ffmpeg
./configure --enable-libx264 --enable-gpl
the following step will take very long on the pi up to 3 hours, alternatively you can crosscompile
on another linux but this is out of the scope of this post

Code: Select all

make
sudo make install
create a file with

Code: Select all

nano /etc/ffserver.conf
and paste this:

Code: Select all

#/etc/ffserver.conf
Port 8554
BindAddress 0.0.0.0
MaxHTTPConnections 2000
MaxClients 1000
MaxBandwidth 100000
CustomLog -
RTSPPort 5454
RTSPBindAddress 0.0.0.0

<Feed feed1.ffm>
   File /tmp2/feed1.ffm
   FileMaxSize 2000K
   ACL allow 127.0.0.1
   ACL allow 192.168.0.0 192.168.255.255
</Feed>

<Stream pi.sdp>
   Format rtp
   Feed feed1.ffm
   noAudio
</Stream>

<Stream stat.html>
   Format status
   ACL allow localhost
   ACL allow 192.168.0.0 192.168.255.255
</Stream>

<Redirect index.html>
   URL http://www.ffmpeg.org/
</Redirect>
you can adjust it to your needs especialy the

Code: Select all

Port             # the port for the strem from ffmpeg to ffserver
BindAddr         # IP for the strem from ffmpeg to ffserver
RTSPPort         # Port for the RTSP stream
RTSPBindAddress  # IP for the RTSP stream
Feed             # the input feed for ffmpeg to stream to
Stream           # the RTSP Stream
ACL allow        # change to your IP address range
No we should be ready for the first test
start the ffstream program and put it in background

Code: Select all

ffstream &
now start the raspivid to stream with ffmpeg to ffserver

Code: Select all

raspivid -b 5000000 -ex night -mm average -op 150 -t 999999999 -w 640 -h 360 -fps 5 -o - |ffmpeg -v verbose -i - -flags +global_header -vcodec copy -r 5 -an http://0.0.0.0:8554/feed1.ffm
you should now be able to use vlc to try to get the stream

Code: Select all

vlc rtsp://<raspberrypiaddr>:5454/pi.sdp
Explanation of the parameters:

Code: Select all

raspivid                         # used to captures the video from the Raspbery Picam
-b 50000000                      # is the bitrate
-ex night                        # better for lowligt condition
-mm average                      # how to measure the ligt
-awb off                         # the picture looks mor natural
-op 150                          # transparent preview on the console (over HDMI)
-t 999999999                     # time for the video -t -1 doesn't function in all versions of raspivid so use a very high value in ms
-w 640                           # width
-h 260                           # high
-fps 5                           # frames/S
-o -                             # output to standart out
|ffmpeg                          # pipe to ffmpeg used to stream the raspivid to ffserver feed1.ffm
-v verbose                       # for debug
-i -                             # input from standart in (pipe)
-flags +global_header            # !!!very important without that you will not get a picture to vlc or zoneminder!!! (
-vcodec                          # copy	copy the videostream without translation
-r 5                             # framrate of the receiving client
-an                              # no audio
http://0.0.0.0:8554/feed1.ffm    # the ffstream port and feedname specified in ffserver.conf
For Zoneminder on the Linuxbox i used the "mastertheknif-ZoneMinder-kfir-0ef2974" sources
and a special version (N-35287-g7076967) of ffmpeg
(I don't remember why i had do choose this version i found it on one Internet-Page
mybe you can also ue the lastest git version)

first install the libx264-dev library

Code: Select all

apt-get install libx264-dev
then create a dir and download the ffmpeg

Code: Select all

mkdir ~/download
cd ~/download
git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
cd ffmpeg
git checkout N-35287-g7076967
./configure --enable-libx264 --enable-gpl
make
make install
download mastertheknif-ZoneMinder-kfir-0ef2974 and unzip it

Code: Select all

wget http://github.com/mastertheknife/ZoneMinder-kfir/tarball/perfpatch
Follow instructions on the original thread uder section 5. Installation
http://www.zoneminder.com/forums/viewto ... it=0ef2974

For me this config line worked

Code: Select all

./configure --with-webdir=/var/www/zoneminder --with-cgidir=/usr/lib/cgi-bin/ --with-webuser=www-data --with-webgroup=www-data \
--with-extralibs=-ldts ZM_DB_PASS=<zmpw> ZM_DB_USER=<zmuser> CXXFLAGS=-D__S --with-ffmpeg=~/download/ffmpeg
If you also have issues with zmfilter exiting with error code 9 you could try ./config with following additional option,
this worket for me as workaround but makes Zoneminder slower.

Code: Select all

--enable-mmap=no
Once Zoneminder ist up and running you can add a new Monitor

Code: Select all

Source Type        ->  Ffmpeg
Source Path        ->  rtsp://<raspberrypiaddr>:5454/pi.sdp
Target Colorspace  ->  24 bit color
Capture Width      ->  640   #according to your stream
Capture Height     ->  360   #according to your stream
All other settings are default one's.
After switching to at least "Monitor" or "Modect" you should be able to see the stream in Zoneminder.

I hope i could help someone with this tutorial, if someone has questions i will try to answer them.
postfux
Posts: 20
Joined: Thu Mar 03, 2005 11:33 am
Location: Austria

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by postfux »

If you get an errormessage while compiling ffmpeg like this:

Code: Select all

libavcodec/libavcodec.a(libx264.o): In function `X264_init':
/ffmpeg/libavcodec/libx264.c:536: undefined reference to `x264_encoder_open_125'
collect2: error: ld returned 1 exit status
make: *** [ffmpeg_g] Error 1
then you should remove again the libx264-dev package and x264

Code: Select all

apt-get remove libx264-dev x264
and compile instead libx264 by yourself using this instructions
http://ffmpeg.org/trac/ffmpeg/wiki/How% ... %20libx264

Info found on this page: http://stackoverflow.com/questions/1183 ... r-open-125
jameswilson
Posts: 5111
Joined: Wed Jun 08, 2005 8:07 pm
Location: Midlands UK

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by jameswilson »

Very interesting. Do you have some images to share this picam grabs? Also whats it like in low light?
James Wilson

Disclaimer: The above is pure theory and may work on a good day with the wind behind it. etc etc.
http://www.securitywarehouse.co.uk
postfux
Posts: 20
Joined: Thu Mar 03, 2005 11:33 am
Location: Austria

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by postfux »

Hi

Attached are some sample pics allready saved in ZoneMinder.
They are captured @ 640x360 cause my ZoneMinder Server ist not so powerfull.
But for tests i tried even with 1920x1080 and it functioned.
The cam is not so good at low light, if it's possible you could
use a halogen spotlite with a PIR sensor to have better ligth condition.

The raspivid has a setting exposure for night witch makes it a little bit better,
and also a setting for verylong, altought this seems to be broken in the latest Software
version as the Picam board crashes and you have to restart the Pi. Hopefully it gets
fixed soon.

Greetings Markus
Sample 2
Sample 2
080-capture.jpg (37.04 KiB) Viewed 37236 times
Sample 1
Sample 1
141-capture.jpg (71.7 KiB) Viewed 37236 times
Sample 3
Sample 3
026-capture.jpg (82.63 KiB) Viewed 37236 times
incade
Posts: 3
Joined: Fri Dec 20, 2013 7:51 pm

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by incade »

Thanks very much for the post! I've been looking into starting a few Pi Cam modules with Zoneminder and wondered if it'd be possible.

I have already ordered a case, wifi dongle, and camera module for the first Pi to be put to use soon.
incade
Posts: 3
Joined: Fri Dec 20, 2013 7:51 pm

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by incade »

So, I was able to get this successfully working following your instructions, thanks again.

However, I seem to have a 2-3s delay in the stream with the settings all exactly matching yours. I'm overclocked to 'High' on Raspbian (950Mhz), and currently still connected via Ethernet (100Mbps). Any tips anyone could provide to reduce the delay? I know the delay seems to be the big killer with the RPi, but it seems like plenty of people have found ways around it.. I'm just having a hard time finding a method that works; this one is closest so far.
postfux
Posts: 20
Joined: Thu Mar 03, 2005 11:33 am
Location: Austria

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by postfux »

Hi Incade

I'm glad that i could help someone!!
Wich method are you using?
pi --> ffserver ->> ffmepg --> network --> ffplay
or
pi --> ffserver ->> ffmepg --> network --> vlc

With the first method i got ~1 seconds delay, tested with filming an analog clock with a second pointer,
the recieved stream was allmost exactly 1 second delayed. Even watching the cam on the Zoneminder
true ffplay was not much difference.

With vlc i still had ~2 or more seconds delay.

BR.: Markus
incade
Posts: 3
Joined: Fri Dec 20, 2013 7:51 pm

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by incade »

I'm currently using raspivid -> ffserver -> ffmpeg -> network (Wifi) -> ffplay, and honestly hadn't quite figured out the delay issue. Ultimately, I ended up deciding that the delay wasn't a huge issue for me, as I'm using this as a security system in hopes to catch the face of someone stealing packages.. so delay won't matter much.

I'd say at this point I'm still experiencing a 2-3s delay in video feed, though.

By the way, I noticed one of the cons you listed was "Poor low-light conditions"; just thought I'd mention how I was able to work around this pretty nicely. I'm not sure if this came out after your post, but the new Camera Module Pi NOIR, does not include the infrared filter that comes on the standard camera module. I am using these, coupled with a 198-LED 850nm IR illuminator from Amazon and getting quite impressive night vision images. At first, they were showing up as common "pale ghostly blurs" due to the exposure time in "night" mode; however, I found a hack of types somewhere online where you can run '-ex night -ISO 800' and this seems to really cut down on the blur. In fact, I can easily make out faces walking up to the door at night. I believe this is because the night mode by default runs at ISO 1600, which is just way too high unless you're doing astrophotography - you won't need it that high with an illuminator.

EDIT: More importantly, I'm still getting 25fps at night versus the night mode alone would cut it down to 5fps because of exposure time (hence the blurring).

I intend to come back here and post my end-setup at some point.
postfux
Posts: 20
Joined: Thu Mar 03, 2005 11:33 am
Location: Austria

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by postfux »

Hi incade

This with the delay is strange for me, i have no idea at the moment.

No you are right i didn't mention the pi-cam NOIR cause when writing this post it was not available and the one i ordered in October is comming in end of Jannuary.
At RS-Components from Austria they are sold out. :-(
I also read about the ability to remove the IR filter by yourself but this was to risky for me.
Until now i have no experience with the pi-cam NOIR.

Tank you for sharing your hint with the pi-cam NOIR and the ISO value, maybe i will need it when i have the cam.
Nighthowl
Posts: 6
Joined: Sun Aug 11, 2013 1:46 am

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by Nighthowl »

Thanks for the tutorial! Will give it a spin when I get my rasp+cam.

Have you tried using the hardware h264 encoding capabilities in the GPU ? Maybe it could help reduce latency and increase resolution and fps... it's purportedly specced for 1080p @ 30 fps.

http://www.raspberrypi.org/phpBB3/viewt ... 70&t=25022
http://theiopage.blogspot.co.uk/2013/04 ... -with.html
https://groups.google.com/forum/#!topic ... TEg8tLBQ_o
postfux
Posts: 20
Joined: Thu Mar 03, 2005 11:33 am
Location: Austria

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by postfux »

Hi Nightowl

Until now i didn't have time to investigate in this until now, but for me the 1 or 2 Seconds delay is not a problem.

Bye
jemmyn
Posts: 2
Joined: Tue Mar 19, 2013 3:50 pm

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by jemmyn »

I have followed your tutorial and am able to see the video stream in VLC on another machine on the network. The issue is that no matter what size the video is at it plays perfectly for about 30 seconds and then pauses for a good 10 seconds or more and seems to try and catch up to where it left off by playing the past 10 seconds really fast. Anyway from this point on there is a huge delay. It seems to repeat this issue every 30 seconds or so.

Has anyone else run into this, it really is perfect other than this. Any ideas? Can someone help me please? I am happy to buy you a beer! Thanks in advanced!
postfux
Posts: 20
Joined: Thu Mar 03, 2005 11:33 am
Location: Austria

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by postfux »

Hi Jemmyn

Don't use VLC i had also strange behaviours, and much delay.
I recommend using ffmpeg instead (also on Windows)
Here is an example how to start ffplay to view the stream.

Code: Select all

ffplay.exe -i rtsp://192.168.0.166:5454/pi.sdp
It takes some seconds to connect and i even get at the beginning some error messages but you can ignore them.
see attachement.

I think i downloaded ffmpeg from here
http://ffmpeg.zeranoe.com/builds/
you get this link also from http://www.ffmpeg.org/download.html
Good luck and tell me if i could help.

BR Markus
Attachments
ffplay-rpi.jpg
ffplay-rpi.jpg (115.48 KiB) Viewed 32749 times
jteeuw
Posts: 5
Joined: Mon Jun 02, 2014 7:22 am

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by jteeuw »

Hello,

I followed the instructions and compiled ffmpeg , but when I run ffstream , I get a error message : command not found.

greetz
postfux
Posts: 20
Joined: Thu Mar 03, 2005 11:33 am
Location: Austria

Re: Raspberry Pi Cam module Streaming RTSP h264 video to ZM

Post by postfux »

Hi jteeuw

Sorry there is a little error in my tutorial.
Just put this lines in a file called ffstream (or name the file you like)

Code: Select all

#!/bin/sh
raspivid -b 5000000 -ex night -mm average -op 150 -t 999999999 -w 640 -h 360 -fps 5 -o - |ffmpeg -v verbose -i - -flags +global_header -vcodec copy -r 5 -an http://0.0.0.0:8554/feed1.ffm
Now you should be able to run it.
If ffstream is located in the current directory just run

Code: Select all

./ffstream & 
if ffstream is in a different path, just run

Code: Select all

/path/to/file/ffstream &
Greetings Markus
Post Reply