Sunday, August 16, 2015

HOWTO - Raspberry Pi HD IP Camera (based on UV4L - Video4Linux drivercollection)



For anyone searching for high quality video surveillance on a tinkering budget, will admit that the Raspberry Pi provides a reasonable and affordable option.
When you search the internet for IP Cameras capable of doing higher resolutions than 640x480 pixels, you will notice a remarkable difference in price range.  There is a price gap between the standard home consumer IP Camera and the more professional higher resolution IP Camera, so if you want high quality home surveillance video feed but don't want to bear the price, the Raspberry Pi IP Camera is for you.

All Raspberry Pi Camera modules (currently as of writing there are three different models available) are capable of doing high definition resolution (and i'm talking about 2592x1944px).  For people interested in the full specifications of the Camera, they can be found on the Raspberry Foundation web site at: https://www.raspberrypi.org/documentation/hardware/camera.md


A bill of material for this guide looks as follows (prices are from adafruit's site):

A Raspberry Pi 2 board with power supply: 39,95$













A power supply for the Raspberry: 5,95$















A Raspberry Pi Spy Camera module: 39,95$
















A dummy IP Camera Housing: 5$















This brings the total cost of our bill of material at 90,85$, not too bad for a capable high definition IP Camera.

First job would obviously be, mounting all the parts together, but that's a bit beyond the scope of this tutorial.  If you're a bit of a diy handyman, this shouldn't be an issue.
Once you have all parts mounted together, let's continue on the software side.

First step is downloading the Raspbian image from the Raspberry Foundation web site.
Once downloaded, unzip your image (if you downloaded the zip archive) and write it onto an sdcard with win32diskimager.

Next, let's continue booting up the image and doing some preparation work.
Once you boot the Foundation's Raspbian image for the first time, you'll end up in the initial configuration menu, make sure to enable the Camera module here, or your Camera module won't be able to start.


Now let's make sure that our Raspberry Pi is up to date with all the latest hotfixes, to update firmware and software, punch in below one-liner:
sudo apt-get update && sudo apt-get -y upgrade && echo y | sudo rpi-update && sudo reboot
Reboot your Raspberry Pi after this operation to properly activate the firmware update, if any.

Next thing is to load the UV4L2 suite of drivers onto our Raspberry Pi.
A clear set of instructions for this can be found on the linux projects website, but if you're lazy just like I am, you can just punch in the instructions below:

To install UV4L open a terminal and type the following command:
sudo curl http://www.linux-projects.org/listing/uv4l_repo/lrkey.asc | sudo apt-key add -

Add the following line to the file /etc/apt/sources.list :

echo "deb http://www.linux-projects.org/listing/uv4l_repo/raspbian/ wheezy main" | sudo tee -a /etc/apt/sources.list

Update your apt cache:
sudo apt-get update

Now let's install our UV4L2 driver suite requirements:
sudo apt-get install uv4l uv4l-raspicam uv4l-raspicam-extras uv4l-server uv4l-webrtc

After installing the UV4L2 suite of drivers, these will be started automatically after reboot or u can start or restart them manually with below command:
sudo service uv4l_raspicam restart

Upon start the drivers will load their configuration from below configuration file, you can edit it by pasting the same
sudo nano /etc/uv4l/uv4l-raspicam.conf

To get an overview of available parameters which you can list in the configuration file:
uv4l --help --driver raspicam --driver-help

Or to kill a running driver: 
pkill uv4l

Once you have started the driver, a web gui will be available which u can use to configure parameters on the fly.
http://ip-of-your-raspberry:8080

This concludes the initial set-up of the IP Camera with drivers and control panel.  
The camera will only be useful this way to be viewed via the control panel and a web browser.
If you want to connect it via VLC or a Video Recording Solution, like for example Surveillance Station on a Synology Nas, we need to install an extra server to run the RTSP protocol to serve the video feed.

There's a nice project called h264_v4l2_rtspserver over on Github written by a user named Michel Promonet which provide us with such a server, the only drawback is that you need to compile it yourself, but since you're a DIY handyman, follow below guide:

  • sudo apt-get -y install cmake libv4l-dev liblivemedia-dev liblog4 cpp5-dev
  • git clone https://github.com/mpromonet/h264_v4l2_rtspserver.git
  • cd h264_v4l2_rtspserver/
  • cmake . && make
  • cpack .
  • sudo dpkg -i h264_v4l2_rtspserver*.deb

The RTSP server is now compiled and installed, the only thing what remains is to make it start at boot, for this purpose I wrote a little service script to start it via the normal debian services system.  

Copy and paste below script in a file, run this: sudo nano /etc/init.d/rtspserver then copy below into the file and save it.
#!/bin/sh### BEGIN INIT INFO
# Provides: RTSPSERVER
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 
5# Default-Stop:      0 1 6
# Description:       Provides the Mpromoneth rtsp server
### END INIT INFO
SCRIPT="/home/pi/h264_v4l2_rtspserver/h264_v4l2_rtspserver -P 8554 -Q 10 -r -s -W 2592 -H 1944 -F 15"
RUNAS=root
NAME=h264_v4l2_rtspserver
PIDFILE=/var/run/$NAME.pid
LOGFILE=/var/log/$NAME.log

start() {  if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then    echo 'Service already running' >&2    return 1  fi  echo 'Starting service…' >&2  local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"  su -c "$CMD" $RUNAS > "$PIDFILE"  echo 'Service started' >&2}

stop() {  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then    echo 'Service not running' >&2    return 1  fi  echo 'Stopping service…' >&2  kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"  echo 'Service stopped' >&2}

uninstall() {  echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "  local SURE  read SURE  if [ "$SURE" = "yes" ]; then    stop    rm -f "$PIDFILE"    echo "Notice: log file was not removed: '$LOGFILE'" >&2    update-rc.d -f <NAME> remove    rm -fv "$0"  fi}

status() {        printf "%-50s" "Checking $NAME..."    if [ -f $PIDFILE ]; then        PID=$(cat $PIDFILE)            if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then                printf "%s\n" "The process appears to be dead but pidfile still exists"            else                echo "Running, the PID is $PID"            fi    else        printf "%s\n" "Service not running"    fi}

case "$1" in  start)    start    ;;  stop)    stop    ;;  status)    status    ;;  uninstall)    uninstall    ;;  restart)    stop    start    ;;  *)    echo "Usage: $0 {start|stop|status|restart|uninstall}"esac

Now change the permissions on this script so it can be executed:
sudo chmod 755 /etc/init.d/rtspserver
And schedule the script so it will start as a service on boot time:
sudo update-rc.d rtspserver defaults
After this reboot your Raspberry Pi to activate the RTSP server.
After the reboot the RTSP server will be available, and you will be able to connect to it with VLC via the following mrl:  rtsp://ip-address-of-your-Raspberry-Pi:8554/unicast


For the people who want to connect this to their Synology Nas, follow below instructions:

First make sure you have installed Surveillance Station on your nas via the webgui.
ssh to your nas and log in

We need to create a camera definition for the Raspberry Pi so we can select this in our Surveillance Station.
To do this create a configuration file for it (just copy and paste the whole damn thing):

cd /volume1/@appstore/SurveillanceStation/device_pack/camera_support/
echo [RaspberryPi*PiCam] | tee RaspberryPi.conf
echo api = rasbpicam-h264 | tee -a RaspberryPi.conf
echo channel_list = 1  | tee -a RaspberryPi.conf
echo default_channel = 1  | tee -a RaspberryPi.conf
echo resolutions_h264 = 2592x1944  | tee -a RaspberryPi.conf
echo default_resolution_h264 = 2592x1944  | tee -a RaspberryPi.conf
echo fps_h264_2592x1944 = 15  | tee -a RaspberryPi.conf
echo default_fps_h264_2592x1944 = 15  | tee -a RaspberryPi.conf
echo default_image_quality = 5  | tee -a RaspberryPi.conf
echo h264 = rtsp  | tee -a RaspberryPi.conf
After creating this configuration file, you will need to restart your Surveillance Station software on your nas to let it pick this configuration file up, do this on your web gui, in the package manager, and restart your Surveillance Station package (stop start).

Now you can start defining your camera in Surveillance Station.
Open Surveillance Station, go over to IP Camera, add Camera, do quick setup and define your Camera as per below screenshot:




















Don't worry about the username and password, confirm to finish.

you will notice after adding the camera that it's disconnected, this is because the Surveillance Station software is looking at the root mrl (without the unicast string behind it).







It looks at:
rtsp://ip-address-of-your-raspberry:8554
and not at:
rtsp://ip-address-of-your-raspberry:8554/unicast
where it needs to look, hence the disconnection.

Currently I only have a work around for this, if someone has another option, please post a message on my blog below.

In Surveillance Station, select your Camera, then click Configuration, Export, give your export a name, and select a destination (in my case web folder) add your Raspberry Pi Camera and finish the export, now delete your camera from Surveillance Station.

Now edit a configuration file in the Camera Export folder which you have just exported:
vi /volume1/web/SSCamExport_RaspberryPi/.ExpCam
Change the follow 2 parameters from:

path = '/'
live_path = '/'
to 
path = 'unicast'
live_path = 'unicast'

Save and re-import your Camera, it should be working now.
Note that, if you make configuration changes to your Camera in Surveillance Station, that u need to redo this procedure.

I hope this write up will be useful for people wanting to experiment with the Raspberry Pi Camera and Surveillance Station, if u like the write up, have any comments, suggestions, or improvements, leave me a message at the bottom of this blog.




16 comments:

  1. Nice one! Gonna try this asap for my pi cam, but doesnt the pi cam have a max resolution of 1920 x 1080?

    ReplyDelete
  2. It depends, you have several modes to set the sensor.
    The sensor is capable of doing 2592x1944 resolution at 15 frames per second.

    ReplyDelete
  3. Thanks!, i am gonna try it when i got some time, rather have the 1080p30s also so im gonna try that.

    ReplyDelete
  4. As a side note to this tutorial, it's now possible to insert the cam to surveillance station without exporting and importing, if anyone is interested, let me know and i'll post the changes.

    ReplyDelete
    Replies
    1. Yes I'm interested Ronny, I'm a bit slow with the project but I got my camera working and am at the part of adding it to surveillance station. I bought a couple of POE injectors from china and modified them to make it work and now have power wherever I got a cable, and it works too! :)

      Delete
  5. Hi

    When I am installing the rest server using this command which you gave :git clone https://github.com/mpromonet/h264_v4l2_rtspserver.gitcd I am getting an error The requested URL returned error: 403 while accessing https://github.com/mpromonet/h264_v4l2_rtspserver.gitcd/info/refs
    fatal: HTTP request failed.

    Could you please help me?

    Thanks

    ReplyDelete
    Replies
    1. Hi, it seemed that something went wrong with the page format, the blog article is now updated to reflect the commands with a correct formatting, it will make things clear.

      Delete
  6. Hi,

    only one question. Is this also possible with a usb camera? Or is it only possible with the PICamera board? What have to be changed?

    Thanks

    ReplyDelete
  7. Hi, theoretically it should be possible also with a USB WebCamera, although I didn't test this so I can't guarantee it.
    You would require the additional usb-uvc driver from http://www.linux-projects.org/modules/sections/index.php?op=viewarticle&artid=10
    This would support the creation of a device entry in linux for your usb webcam, hence you would be able to address it with the web interface like the other rpi camera module.

    ReplyDelete
  8. Hi,
    I am fairly new to the raspberry pi thing and I have one (actually soo many) question. I have build a Lego robot with 4dc motors connected to a motor driver and that one to the PI of course, also there is the RPI noir camera connected to the PI and I have installed all the necessary and maybe some more stuff for the uv4l setup. Also keep in mind, that using the robo, I have to rely on wifi for the video stream to my laptop or pc, for mobility. Now to my question: can you give me a hint, which driver (uvc, server, mjpgstreamer, or something else) I should use and which parameters in the config file (mine is called uv4l-uvc-conf) and How I can play around with, to get a good video feed (I don't require high res or high frame rate, but the lag in the default stream with uvc as driver is a few seconds long.
    I'm happy to answer any additional questions and would be so happy about any help!

    ReplyDelete
  9. Hi, it sounds like U already know the uv4l framework pretty well, so am a bit suprised with your question. But here it goes.
    You have the userspace framework uv4l (Userspace Video for Linux)
    As the name tells the story, this framework runs in userspace, not in the kernel.
    You can plug driver modules into it, the driver module installs a /dev/videox node which u can address with other uv4l compliant software.
    I personallly worked already with two drivers for this framework, the uv4l-raspicam (a custom driver for the raspberry pi camera) and uv4l-uvc (a usb driver for usb webcams).
    Best thing U can do to experiment around is install the uv4l, uv4l-raspicam, uv4l-raspicam-extras, and uv4l-server from http://www.linux-projects.org.
    You will have a configuration file at /etc/uv4l and a web interface on port 8080 to configure the modules.
    If you are merely looking for an easy gui which is a no-brainer but doesn't expose all the functionality, you can check my PHP/Bootstrap web gui which wraps around all of this at https://ronnyvdbr.github.io/RaspberryIPCamera/

    ReplyDelete
    Replies
    1. Hi,
      I am a student from Taiwan,I have some question,could you teach or hint me?
      I used to capture video in Raspberry Pi 3 by Webcam past 3 weeks ago.
      Now I capture video by Raspicam with uv4l-raspicam.It is working great.
      But usb Webcam cannot work now.How can i use uv4l-uvc let usb Webcam work?
      It is possible to use Raspicam and Webcam at the same time or independent use?

      Delete
    2. Hi,
      I am a student from Taiwan. I have some question for uv4l(user space video for linux).Could you teach or hint me?
      I used to capture video with Raspberry Pi 3 + Netbeans + OpenCV by USB Webcam(Logitech C310) pass 3 weeks ago. Now I use uv4l-raspicam to capture video by Raspicam , It is work greatly. Then I try to use uv4l-uvc to drive Webcam, but it is not work.
      I don’t know how to drive Webcam by uv4l, or it is impossible?
      Can I use uv4l to drive Raspicam and Webcam at the same time or only independent?
      Any help would be greatly appreciated.

      Delete
  10. Ronny,

    Have you had a chance to update the Synology directions? I have done a few of the pre-packaged projects and have low frame rate. 6 fps with Motioneyeos which is doing mjpeg. Are you actually getting the 15 FPS at full frame rate? ALso are you doing wired or wireless. I have been trying wireless with pretty strong signal. Great project going to try it this week on a RP3 and let you know how it goes.

    ReplyDelete
  11. Hi, yes I'm getting 15 fps in full camera resolution, in RTSP server mode I connect it to my VCR equipment with a bit rate of 4 Mbps, it works like a charm. Check out my article below for more details:
    https://random-notes-of-a-sysadmin.blogspot.be/2016/03/howto-set-up-raspberry-ip-camera-on.html

    ReplyDelete
  12. Thank you so much for this indepth installation guide. Very thorough and excellent review. I have finally been able to stream video with uv4l. Very excited to get my project off the ground.

    ReplyDelete