Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raspberry Pi MJPG-Streamer low latency

I've built a raspberry pi robot. Now I want to stream video from Raspberry Pi onboard camera. I followed this tutorial: http://blog.miguelgrinberg.com/post/how-to-build-and-run-mjpg-streamer-on-the-raspberry-pi/page/2

So I finally made it working, but now I want to get as low latency as possible. It's important to have low latency, cuz controlling a robot with such a lag is impossible.

Any advise ? Have a nice day!

like image 647
A J Avatar asked Jan 04 '14 13:01

A J


3 Answers

You should probably ask this on https://raspberrypi.stackexchange.com/

All potent solutions that can be found as by now use raspivid. It directly encodes the video as H.264/MPEG which is much more efficient as capturing every single frame.

The one which works out best for me so far is - first on you raspberry pi

raspivid -t 999999 -w 1080 -h 720 -fps 25 -hf -b 2000000 -o - | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! gdppay ! tcpserversink host=<IP-OF-PI> port=5000
  • on your PC/viewing device

gst-launch-1.0 -v tcpclientsrc host=<IP-OF-PI> port=5000 ! gdpdepay ! rtph264depay ! avdec_h264 ! videoconvert ! autovideosink sync=false

Source: http://pi.gbaman.info/?p=150

like image 106
Ixbidie Avatar answered Nov 11 '22 14:11

Ixbidie


I think I have found from experimentation that the camera board does most of the processing relieveing the raspi from much load at all. You can see this by running top on the pi as it captures and streams.

First I run the following on a linux client:

nc -l -p 5001 | mplayer -fps 31 -cache 512 -

Then I run the following on the raspi:

/opt/vc/bin/raspivid -t 999999 -o -w 1920 -h 1080 - | nc 192.168.1.__ 5001

This was done over an ethernet connection from raspi to linux desktop both connected to a common ethernet hub.

I have made the following observations:

  • these settings give me a pretty low lag (<100ms)
  • increasing the cache size (on the client) only leads to a larger lag, since the client will buffer more of the stream before it starts
  • decreasing the cache size below some lower limit (512 for me) leads to a player error: "Cannot seek backward in linear streams!"
  • specifying dimensions less than the default 1920x1080 leads to longer delays for smaller dimensions especially when they are less than 640x480
  • specifying bitrates other than the default leads to longer delays
  • I'm not sure what the default bitrate is
  • for any of the scenarios that cause lag, it seems that the lag decreases gradually over time and most configurations I tried seemed to have practically no lag after a minute or so

It's unfortunate that very little technical information seems to be available on the board apart from what commands to run to make it operate. Any more input in the comments or edits to this answer would be appreciated.

like image 36
Octopus Avatar answered Nov 11 '22 13:11

Octopus


I realise this is an old post but I recently needed to do something similar so I created a node Raspberry Pi MJpeg Server were you can pass the compression quality and timeout (number of frames per second).

Start the server:

node raspberry-pi-mjpeg-server.js -p 8080 -w 1280 -l 1024 -q 65 -t 100

Options:

-p, --port        port number (default 8080)
-w, --width       image width (default 640)
-l, --height      image height (default 480)
-q, --quality     jpeg image quality from 0 to 100 (default 85)
-t, --timeout     timeout in milliseconds between frames (default 500)
-h, --help        display this help
-v, --version     show version

Open sourced as I'm sure it will help others.

like image 34
John Doherty Avatar answered Nov 11 '22 14:11

John Doherty