Saturday, June 7, 2014

Raspberry pi camera module video livestreaming

Some experimenting with different methods. I have a problem with my Asus usb-n10 wifi dongle at the moment, so all results are over LAN. All these methods presume you have camera board activated from raspi-config.

1. Gstreamer 1.2 with nginx rtmp module

Result: Good for FPV
Quality: 720p@15fps, bitrate 1000000
CPU usage %: 17 - 22 
Lag: ~300ms
Notes: Compiling Gstreamer with plugins and nginx with rtsp module will take over 8 hours. I overclocked my RPi with "Medium 900MHz ARM, 250MHz core, 450MHz SDRAM, 2 overvolt" -setting to shave some time off. I'm not sure it helped a lot...
I used these blogs, forums, guides, examples:

1.1. Compile and install Gstreamer 1.2 and gst-rpicamsrc

The file provided gave some errors, so I used all the commands manually. also added gst-rpicamsrc compile and install. Removed libschroedinger-dev from install, since it fails. Ran all ./ and make commands with sudo. Without sudo, some give permission errors.

remember to add this to your .profile:
export GST_PLUGIN_PATH=/usr/local/lib/gstreamer-1.0/
export GST_OMX_CONFIG_DIR=/usr/local/etc/xdg/
export LD_LIBRARY_PATH=/usr/local/lib/

1.2 Compile and install nginx and rtmp-module

I used /home/pi/www instead of /var/www and cleaned conf file

and my /etc/nginx/nginx.conf -file:

Reboot raspberry pi and then start nginx:
sudo service nginx start

First I din't have gst-rpicamsrc installed and tried to pipe raspivid to gstreamers fdsrc like this:
raspivid -t 0 -w 640 -h 480 -b 1000000 -fps 25 -o - | gst-launch-1.0 fdsrc ! h264parse config-interval=2 ! video/x-h264, framerate=25 ! flvmux ! rtmpsink location='rtmp:// live=1' THIS CODE DOES NOT WORK

That did not work and it caused a weird harware lock in the gpu or cameramodule. I had to wait some time to get raspivid working again.

Luckily gst-rpicamsrc fixed this issue and here is the working command:
gst-launch-1.0 rpicamsrc bitrate=1000000 ! video/x-h264,width=1280,height=720,framerate=15/1,profile=high ! h264parse ! flvmux ! rtmpsink location='rtmp://localhost/rtmp/live live=1'

Now you can access the stream with VLC from your computer. Note that VLC will add couple of seconds of lag because of buffering. I'm planning to use android to watch the live stream from my quad, so I tried to open the stream on MXPlayer for Android. I found that you can bypass the buffering by opening the rtmp stream on MXPlayer, then shutting it on the raspberry pi and after 5 seconds start the streaming again from the rpi. The bitrate parameter (currently 1000000 = 1mb) can be set to 4mb or 5mb without affecting cpu usage. Atleast with LAN connection this is ok.

My intention was to research different streaming methods, but the first one turned out to be so good, I didn't bother to continue.

Parts list

The Quad copter


Other parts


Raspberry Pi Quadcopter is a ambitious project to make a wifi controlled flying computer with minimum cost. The final goal is to be able to stream live data to internet from flight, with on board camera and sensors. We started building the quadcopter using this guide.

Some of the parts are old stuff around my house and some already purchased. There will be another post about the parts and where we ordered them.

The project has two parts; the quadcopter and a "ground unit". The Quadcopter is a normal RC quadcopter, but instead of having control unit and radio system, it will have Raspberry Pi with Wlan dongle. The ground unit will have a 12v motorcycle battery, a DD-WRT powered d-link router, wifi tethered 4G smartphone. If the wifi range is not long enough with the stock antenna, then we'll have to get a more powerfull outdoor antenna.

The quadcopter will be controlled with a laptop or other device, which is connected to the same wifi. Software wise, we are going to write the code for the Pi and the controlling software ourselves. There are some open source code for quadcopters that we might recycle for our project. All the code will be available at github later.

There is already some discussion about the project at this reddit post.

We will update all images to this album.