Sunday, May 10, 2015

HOWTO - Create a Raspberry Pi IP Camera with RTSP server.

At a certain day I wanted to do some video surveillance, see if my kids are safe playing in the street.  After a little while searching on the net for viable camera systems I quickly realised that professional HD IP camera's don't come cheap these days.   Since I'm on a tight budget, I started searching for alternatives for this expensive equipment, and since I was already playing around with a Raspberry Pi decided to go further in that direction.   So I ordered a CSI camera module on some vendor outlet website, and started tinkering with it.  Luckily for me there are tons of articles on the net with information about the Raspberry Pi and it's camera module.
Since I already owned a Synology nas with a surveillance station software on it, the choice to build a compatible IP camera for this purpose was logical, since surveillance station already had a website and mobile applications where I could view my video feed.
If I wanted to build my IP camera to be compatible with my Synology nas, it had to support either MJPEG or H.264 RTSP streaming.  I knew there is a GPU in the Raspberry PI capable of doing H.264 encoding, and a utility called raspivid to drive all this and get this to the stdout of our Rasbian OS, so it seemed logical to take benefit of that knowledge and go the H.264 way for streaming.
Now that we had a way to get our video stream, the only thing that remained was pipe this raw video feed into some RTSP server for dispersion to my Synology nas.
After searching the net (again :-)) for a while found that there are source-code libraries for standards-based RTP/RTCP/RTSP/SIP multimedia streaming, suitable for embedded and/or low-cost streaming applications provided by Live Networks, you can find their website at http://www.live555.com/
Now that I had the whole plot, the only thing that remained was to get busy and put da thing together:

To obtain this camera, you have two options, you can build it yourself according to the below instructions, or you can download a ready-built copy from: http://ronnyvdbr.github.io/RaspberryIPCamera/ featuring a easily configurable PHP/Web interface.

Download and install Miniban from http://sourceforge.net/projects/minibian/files/latest/download?source=files and put on SD card with win32diskimager.
This is a minimal Rasbian installation which is compatible with the official Foundation Raspbian distribution.  (it's totally not a requirement, you can just as easily start off with the official foundation distribution)


After booting up, log in with root and raspberry as password, then perform below steps to expand the root parition, default this is only 512 Mb, we want this bigger for the rest of our software:

  • fdisk /dev/mmcblk0
  • Then delete partitions with d and create a new with n. You can view the existing table with p.
  • p to see the current start of the main partition
  • d, 2 to delete the main partition
  • n p 2 to create a new primary partition, next you need to enter the start of the old main partition (just press enter), and enter +2G to expand the partition to 2 Gb, Check the p output!
  • w write the new partition table Now you need to reboot:
  • shutdown -r now After the reboot you need to resize the filesystem on the partition. The resize2fs command will resize your filesystem to the new size from the changed partition table.
  • reboot
  • resize2fs /dev/mmcblk0p2


Let's regenerate our SSH keys, to privatise our SSH server:

  • rm /etc/ssh/ssh_host_* && dpkg-reconfigure openssh-server



Set the correct time zone for our Raspberry Pi:
  • echo "Europe/Brussels" > /etc/timezone
  • dpkg-reconfigure -f noninteractive tzdata



Generate locale data for our Raspberry Pi:


  • sed -i "s/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g" /etc/locale.gen
  • /usr/sbin/locale-gen

Update our Raspberry Pi to the latest version:
  • apt-get update
  • apt-get -y upgrade
  • apt-get -y install rpi-update
  • rpi-update
  • reboot

Install raspi-config and enable our camera:
  • apt-get -y install raspi-config
  • execute raspi-config and select 5 to enable camera
  • now reboot

Since the Miniban installation does not have a pi user set up, let create one and configure it:


  • apt-get -y install sudo
  • useradd pi
  • echo 'pi:raspberry'|chpasswd
  • usermod -a -G sudo pi
  • mkdir /home/pi
  • cp /root/.profile /home/pi/.profile
  • cp /root/.bashrc /home/pi/.bashrc
  • chown -R pi /home/pi
  • chgrp -R pi /home/pi
  • chmod -R 755 /home/pi
  • sed -i 's/pi:x:1000:1000::\/home\/pi:\/bin\/sh/pi:x:1000:1000::\/home\/pi:\/bin\/bash/g' /etc/passwd
  • sed -i 's/# export LS_OPTIONS=/export LS_OPTIONS=/g' /home/pi/.bashrc
  • sed -i 's/# eval/eval/g' /home/pi/.bashrc
  • sed -i "s/# alias ls=/alias ls=/g" /home/pi/.bashrc
  • sed -i "s/# alias ll=/alias ll=/g" /home/pi/.bashrc
  • sed -i "s/# alias l=/alias l=/g" /home/pi/.bashrc

Install our development environment for building software:
  • apt-get -y install build-essential


Install Live555, Source-code libraries for standards-based RTP/RTCP/RTSP/SIP multimedia streaming, suitable for embedded and/or low-cost streaming applications.
  • cd /home/pi/
  • wget http://www.live555.com/liveMedia/public/live555-latest.tar.gz
  • tar -zxvf live555-latest.tar.gz
  • mv live live555
  • cd live555
  • sed -i 's/-D_FILE_OFFSET_BITS=64/-D_FILE_OFFSET_BITS=64 -DALLOW_RTSP_SERVER_PORT_REUSE=1/g' ./config.linux
  • ./genMakefiles linux
  • make
Download a small script from the Raspberry Pi foundation forum:
  • wget https://www.raspberrypi.org/forums/download/file.php?id=4285
  • tar -zxvf testRaspi.tar.gz
  • cd raspi
  • (edit the file testRaspi.cpp and change the OutPacketBuffer::maxSize to 400000)
  • make
You can now enable a RTSP stream server, which can be viewed by a variaty of software players by launching the below command:

raspivid -t 0 -fps 30 -g 1 -b 2000000 -h 1920 -w 1080 -o - | ./testRaspi

With VLC player I can connect to the RTSP stream, i'm using 170 ms buffering in VLC to keep the image movement fluid.
I tried connecting this with my Surveillance application in my Synology diskstation (ds411j), this seems to work quite nicely.







7 comments:

  1. Hey Ronny,

    Thanks for the write up, have you done some more testing with your Synology?
    I am interested in test results for the PI cam and Synology surveillance station 7 beta

    ReplyDelete
    Replies
    1. Hi Greg,
      In fact I did. Currently I have 3 Raspberry Pi camera's connected to my Synology nas. They are all running at 2592x1944 resolution at 15 fps.
      The quality is very good. Although it took a while before I figured out how to do it.
      I made another raspbian image and used v4l2 suite of drivers to drive the whole thing, on a Raspberry pi 2 this is just working fabulous.

      Delete
    2. First of all great work! It is up and running within minutes, also get it easy to work with synology surveillance station.
      I am facing only one problem, i cannot get it to work on wifi.

      I am filling in al the desired settings within the admin portal but my wifi adapter is not getting active. I think it is not detected in the installation. My linux knowledge is not that good so i don't know where to check.
      The wifi adapter has working succesfull with this raspberry with kodi but maybe i must install a driver.

      Can you guide me to how to check and set this up correctly?

      Edit: i did some basic checking in the installation. Wifi adapter is recognized and the wifi settings in the wpa_supplicant.conf file are correct. I tried a wpa_cli scan to check if the network is detected. But i get an error permission denied. I also get this when i want to check the wpa_supplicant file, also with sudo?

      Any ideas. I must this getting to work with wifi because i had no wired connection at the place of were i wanted the camera.

      Delete
    3. Hi, probably the firmware for your wifi adapter is not loaded.
      Can U post me the output of lsusb so I can check what firmware U need?

      Delete
  2. Hi Ronny,
    first of all: great work! This was exactly what I was looking for! But unfortunately, I can't use the prepared image directly, because I couldn't get WiFi working. I also want to install a wiringPi solution parallel on the system. So I installed my own image what now works pretty well. Except: rtsp streaming. I couldn't get it working either on the prepared image nor on my own one. Mjpeg works fine, but no rtsp. Where can be the error, when both (image and my own version) won't work with rtsp? I did the installation.sh manually till the part where I lockdown my system. Any ideas, where I can start searching for a solution?

    ReplyDelete
    Replies
    1. Dear Harry, you're posting on an old article I'm afraid. What image did u download actually and what is a wiringPi solution?
      My latest build of a raspberry pi camera image is located on the github project page at http://ronnyvdbr.github.io/RaspberryIPCamera/
      You might have more luck with that.
      The RTSP server part is actually built with software written by Michel Promoneth on github https://github.com/mpromonet/v4l2rtspserver
      For the rtsp server you'll need his software to work on your raspberry pi, follow instructions to compile and you shoudl have success, pls remember to activate your rpi camera first.

      Delete
  3. hi, wget https://www.raspberrypi.org/forums/download/file.php?id=4285 doesnt work. Can somebody check or fix the link

    ReplyDelete