Raspberry PI RTSP Guide

This is a quick guide to run an RTSP service on the raspberry pi so that you can view the pi camera using suitable clients such are vlc or gstreamer from a remote machine. Or even from another Raspberry PI. For this I am starting of with a completly fresh minimum raspbian image. I have used 2017-03-02-raspbian-jessie-lite

Some obvious first steps.

Change the password for user "pi"

sudo raspi-config Enable ssh Enable camera Increase memory split to 256MB

sudo apt-get update

sudo apt-get dist-upgrade

reboot

Note: To enable ssh / camera they are under the interface options in the raspi-config program

Install Packages

We are going to need to compile things later on so we need gstreamer and a build environment that will work for the camera plugin. These can take some time to run as the gstreamer packages will also pull down quite a large list of dependencies. This can take some time to run so you may want to combine the commands or all of the packages onto a single apt-get command.

sudo apt-get install vim git

sudo apt-get install gstreamer1.0-plugins-bad gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-tools libgstreamer1.0-dev libgstreamer1.0-0-dbg libgstreamer1.0-0 gstreamer1.0-omx gstreamer1.0-omx-dbg libgstreamer-plugins-base1.0-dev gtk-doc-tools

Gstreamer camera source

We need to build the gstreamer rpi camera source so we can access the camera from a gstreamer pipeline. I ran these command from /home/pi/src in order to make this happen. This does not get included yet with gstreamer and isn't available to install from apt-get.

git clone https://github.com/thaytan/gst-rpicamsrc.git

cd gst-rpicamsrc

./autogen.sh

make

sudo make install

Lets run some tests for the new gstreamer plugin.

The rpicamsrc should be visible in the gst-inspect list. Example: "gst-inspect |grep rpicamsrc"

You should also be able to see the list of extensive properties that the element can support for controlling the camera by running "gst-inspect rpicamsrc"

For a really quick video test at this point to make sure the camera is working we can run a gstreamer pipeline that will display the camera image on the streamer and just "dump" the h274 data that is generated. I used this command. "GST_DEBUG=3 gst-launch-1.0 -v rpicamsrc keyframe-interval=30 ! fakesink silent=false". If it works you should see some lines of text flying up the screen and a preview window with the camera image on the raspberry pi screen.

Note: I really suggest trying the above. I had a loose camera cable to the camera module and had the following error as an example.

0:00:02.662390832 2301 0x71201200 ERROR rpicamsrc RaspiCapture.c:757:camera_control_callback: Received unexpected camera control callback event, 0x4f525245

Gstreamer RTSP server library

Since the version of gstreamer on raspbian is so old. It doesn't ship with the new gstreamer rtsp server. So for this we need to clone it switch to the correct branch and compile and install it. This can be found at https://cgit.freedesktop.org/gstreamer/gst-rtsp-server/ and take note of the output from gst-launch --version which on this system is 1.4.4 so we will need to switch to the 1.4 branch in order to compile and install the rtsp server.

git clone git://anongit.freedesktop.org/gstreamer/gst-rtsp-server

cd gst-rtsp-server

git checkout 1.4

./autogen.sh

make

sudo make install

RTSP Sever

So in order to get this completely working we can use an example program that comes with the gst-rtsp-server source code we downloaded. We will be specifically interested in the example code from gst-rtsp-server/examples/test-launch.c This will have been compiled but will not have been install with the library. All we need is a valid pipeline and the example already gives us an h264 pipeline "( videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96 )". The PI is of course not powerful enough to run with the x264enc element. But the rpicamsrc produces h264 data directly as shown from "gst-inspect-1.0 rpicamsrc"

Pad Templates: SRC template: 'src' Availability: Always Capabilities: video/x-h264 width: [ 1, 2147483647 ] height: [ 1, 2147483647 ] framerate: [ 0/1, 2147483647/1 ] stream-format: byte-stream alignment: nal profile: { baseline, main, high } image/jpeg width: [ 1, 2147483647 ] height: [ 1, 2147483647 ] framerate: [ 0/1, 2147483647/1 ] video/x-raw format: { I420, RGB, BGR, RGBA } width: [ 1, 2147483647 ] height: [ 1, 2147483647 ] framerate: [ 0/1, 2147483647/1 ]

So for a suitable pipeline I trying the following first from gst-launch to see if it runs ok. This should produce a 2mbit stream with key frames produced every 15 frames (1 key frame per second) and and frame rate of 15 per second.

GST_DEBUG=3 gst-launch-1.0 -v rpicamsrc preview=false bitrate=2000000 keyframe-interval=15 \ ! video/x-h264, framerate=15/1 ! h264parse ! fakesink silent=false

Now for a final test. From the gst-rtsp-server/examples directory I run the following. Though it prints the URL as 127.0.0.1. This can be replaced with the ip address of the pi.

./test-launch "( rpicamsrc preview=false bitrate=2000000 keyframe-interval=15 ! video/x-h264, framerate=15/1 ! h264parse ! rtph264pay name=pay0 pt=96 )"

From a remote machine I can then access this using vlc. From ubuntu I ran "vlc rtsp://piaddress:8544/" and get a slightly lagged video stream. The reason for the lag is mostly due to the buffering inside vlc that is hard to control. If you want something really low latency try the following gstreamer pipeline from the remote machine or change the setting in vlc so it doesn't buffer.

gst-launch-1.0 rtspsrc location="rtsp://piaddress:8554/test" latency=0 \ ! rtph264depay ! decodebin ! videoconvert ! ximagesink

Some proof





Some further ideas

If you look at the output of the "gst-inspect rpicamsrc" you will find options to be able to flip / rotate the image as well as control specific capture / encoding settings available on the camera.

This will currently only support single client being connected. Making it support multiple tcp clients requires a much more complex application as it need to split the pipeline into multiple components

Authentication is probably also an important feature. There is more example code with gst-rtsp-server which will demonstrate this.