Skip to content

Instantly share code, notes, and snippets.

@lemonlatte
Created December 22, 2019 04:13
Show Gist options
  • Save lemonlatte/799a43288b0e093f9c95ad83ae4962a3 to your computer and use it in GitHub Desktop.
Save lemonlatte/799a43288b0e093f9c95ad83ae4962a3 to your computer and use it in GitHub Desktop.
How to make a Raspberry Pi an RTSP streamer and how to consume this?

How to make a Raspberry Pi an RTSP streamer and how to consume this?

You should perform the following steps on a new SD card. Don't mess with existing solutions. It is not worth the $5 to loose something working.

I'm used to use Raspbian, so my suggestion is to go to https://www.raspberrypi.org/downloads/raspbian/ and download the latest Raspbian Stretch Lite. Flash it to your SD card. Mine have usually 16 GB, but maybe smaller work too. I'm using Etcher on macOS, a great tool for flashing.

After successful flashing enable ssh and prepare a running Wifi configuration.

A Wifi adapter should be attached to your Pi of course.

Change into the root of the flash image and...

touch ssh
touch wpa_supplicant.conf

Take an editor of your choice and add this to wpa_supplicant.conf:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<your_ISO-3166-1_two-letter_country_code>

network={
    ssid="<your_SSID>"
    psk="<your_PSK>"
    key_mgmt=WPA-PSK
}

Change the your_xxx parts according to your environment. Plug the SD card into your Pi and power it on.

Find your Pi's IP. You will know, how to do that.

ssh pi@<your_Pi's_IP>

Password is raspberry, in case you forgot.

sudo raspi-config

In config:

  • Enable camera
  • Expand file system
  • Change the admin password to your favourite one

Save and exit raspi-config

sudo reboot

After reboot ssh again to your Pi and do these steps at console level:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install gstreamer1.0-tools
sudo apt-get install gstreamer1.0-plugins-good
sudo apt-get install gstreamer1.0-plugins-bad
sudo apt-get install gstreamer1.0-plugins-ugly

Test your installation. In order to do this run this command on your Pi:

raspivid -t 0 -w 640 -h 480 -fps 48 -b 2000000 -awb tungsten  -o - | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! gdppay ! tcpserversink host=0.0.0.0 port=5000

And this command on your Mac:

/Library/Frameworks/GStreamer.framework/Commands/gst-launch-1.0 -v tcpclientsrc host=<your_Pi's_IP> port=5000 ! gdpdepay ! rtph264depay ! avdec_h264 ! videoconvert  ! osxvideosink sync=false

or this on your Linux box:

gst-launch-1.0 -v tcpclientsrc host=<your_Pi's_IP> port=5000 ! gdpdepay ! rtph264depay ! avdec_h264 ! videoconvert  ! autovideosink sync=false

You should see a video. This is already H.264, but not RTSP.

So we are going to enable this now.

sudo apt-get install libglib2.0-dev
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

Download gst-rtsp-server from https://gstreamer.freedesktop.org/src/. Should match the installed gstreamer version. This can be checked by

dpkg -l | grep gstreamer

At the time of writing this is 1.10.4

wget https://gstreamer.freedesktop.org/src/gst-rtsp-server/gst-rtsp-server-1.10.4.tar.xz
tar -xf gst-rtsp-server-1.10.4.tar.xz 
cd gst-rtsp-server-1.10.4
./configure
make
sudo make install

Test your setup:

cd examples
./test-launch --gst-debug=3 '( videotestsrc !  x264enc ! rtph264pay name=pay0 pt=96 )'

On your Mac:

/Library/Frameworks/GStreamer.framework/Commands/gst-launch-1.0 -v rtspsrc location=rtsp://<your_Pi's_IP:8554/test latency=0 buffer-mode=auto ! decodebin ! videoconvert ! osxvideosink sync=false

or on your Linux box:

gst-launch-1.0 -v rtspsrc location=rtsp://<your_Pi's_IP>:8554/test latency=0 buffer-mode=auto ! decodebin ! videoconvert ! autovideosink sync=false

You should see a test video.

Now we want to see the camera. For this we use the nice raspivid wrapper gst-rpicamsrc

cd ~
sudo apt-get install git
git clone https://github.com/thaytan/gst-rpicamsrc.git
cd gst-rpicamsrc/
./autogen.sh 
make
sudo make install

Check, if gst-rpicamsrc is installed

gst-inspect-1.0 | grep rpicamsrc

Now for the final test:

cd ../gst-rtsp-server-1.10.4/examples
./test-launch --gst-debug=3 "( rpicamsrc bitrate=8000000 awb-mode=tungsten preview=false ! video/x-h264, width=640, height=480, framerate=30/1 ! h264parse ! rtph264pay name=pay0 pt=96 )"

If it runs, remove --gst-debug=3 and let it run as a deamon by appending & to the command line above.

On your Mac and Linux box the same commands, as above:

/Library/Frameworks/GStreamer.framework/Commands/gst-launch-1.0 -v rtspsrc location=rtsp://<your_Pi's_IP>:8554/test latency=0 buffer-mode=auto ! decodebin ! videoconvert ! osxvideosink sync=false

or

gst-launch-1.0 -v rtspsrc location=rtsp://<your_Pi's_IP>:8554/test latency=0 buffer-mode=auto ! decodebin ! videoconvert ! autovideosink sync=false

For more insight into gst-rpicamsrc and possible other parmeters as already used above:

https://sparkyflight.wordpress.com/tag/gst-rpicamsrc/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment