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 gstremer.sh 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 ./autogen.sh 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: http://pastebin.com/nRFp3gjR

Test
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://127.0.0.1/rtmp/live 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.

16 comments:

  1. For some reason this is working for me:
    raspivid -t 0 -w 1280 -h 720 -fps 30 -b 1000000 -o - | gst-launch -e fdsrc fd=0 ! video/x-h264,width=1280,height=720 ! h264parse config-interval=2 ! filesink location="video.h264"

    ReplyDelete
    Replies
    1. That's recording to a filesink, does it work with rtmpsink?

      Delete
  2. Hello, what is the address I need to access the feed with from either vlc or mx player? Also can you explain the buffer hack a little bit more? Specifically what you mean with "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"....
    Thank you

    ReplyDelete
    Replies
    1. The URL you need to open is "rtmp:///rtmp/live".
      First I start the stream from pi and open the url in MX player. It'll start playing the stream in couple of seconds. Then I kill the stream on the pi with ctrl+C and wait for MX player to play all the video it has buffered(you know its done when you see the last frame freeze). Then just start the streaming again from the pi. MX player will immediately continue playing the stream without a buffer.

      Delete
    2. URL is "rtmp://*raspberry pi ip-adress here*/rtmp/live".

      Delete
  3. sorry please help me !
    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'
    0:00:00.488023319 19194 0x201c1a0 ERROR GST_PIPELINE ./grammar.y:690:priv_gst_parse_yyparse: no element "rtmpsink"
    0:00:00.489368292 19194 0x201c1a0 ERROR GST_PIPELINE ./grammar.y:775:priv_gst_parse_yyparse: link has no sink [source=Project-Id-Version: gstreamer 0.8.1
    Report-Msgid-Bugs-To: http://bugzilla.gnome.org/
    POT-Creation-Date: 2014-07-19 16:11+0200
    PO-Revision-Date: 2004-04-26 10:36-0400
    Last-Translator: Gareth Owen
    Language-Team: English (British)
    Language: en_GB
    MIME-Version: 1.0
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: 8bit
    @0x201d800]
    WARNING: erroneous pipeline: no element "rtmpsink"

    ReplyDelete
    Replies
    1. It seems that you have no rtmpsink installed. Did you compile and install nginx AND rtmp-module? Maybe you had some errors. Try to install it again? but definitely the problem is that you have no rtmp-module installed

      Delete
  4. Please help me !
    I can not register a script to autostart Raspberry Pi. If written in rc.local error event ERROR: pipeline could not be constructed: no element "rpicamsrc"

    ReplyDelete
    Replies
    1. Make sure you installed gst-rpicamsrc succesfully. I had problems with that. Run the commands I pasted in pastebin one by one and see if you spot any errors. (this was the paste: http://pastebin.com/S1zGK9kP)

      Delete
  5. Hi,
    Can you explain me how to see the stream in Windows instead of in Android?
    Thanks a lot!

    ReplyDelete
    Replies
    1. Just Open the stream URL in VLC player in Windows. Its gonna have some lag, because of buffering.

      Delete
    2. Hi,
      Actually i am working in a different project, not with the quadrocopter. In the raspberry Pi i use rpicamsrc as a source and udpsink as a sink. I want to receive the stream in Windows or Linux by using VLC just to check if the client system received the stream or not. How can i do that with VLC? Can you help me to explain a bit more about that?
      Thanks a lot!

      Delete
  6. I was going to add more methods here, but I found that this first worked really well. Sorry for leaving the post a bit raw. Also I have been working with the quadcopter controlling code alot.

    ReplyDelete
  7. Hello,

    Is there any difference between gstreamer and mjpg-streamer apart from H.264 capability of the first? Have you compared the 2? Is gstreamer more efficient in terms of delivering better framerate and resolution?

    ReplyDelete
    Replies
    1. mjpg-streamer in combination with raspivid has a latency of only one picture per second...

      Delete
  8. How do you watch the video with VLC, which is la command for that?

    ReplyDelete