Creating an RTSP Stream with GStreamer
Recently, we have been using an IP camera as a baby monitor to watch our 18 month old in his crib. It ran for several months without issue but then, without warning, the WIFI just stopped working. The camera provided an RTSP stream that we had become accustomed to logging into via our Android phones.
Armed with an old PC, a webcam, and a few open source software packages I decided to piece together a quick fix. We have since replaced the IP camera, due to the less than desirable form factor and power consumption of a full blown PC. However, theres no reason the PC could not have been replaced with a BeagleBoard, BeagleBone, Raspberry Pi, etc..
What is RTSP?
Multiparty Multimedia Session Control Working Group (MMUSIC WG) of the Internet Engineering Task Force (IETF)
developed Real Time Streaming Protocol (RTSP) as a means to control streaming servers.
The RTSP protocol is similar to HTTP, but is tailored to better suit multimedia streams.
Read the deets on RTSP @ http://www.ietf.org/rfc/rfc2326.txt
GStreamer, a multimedia swiss army knife, enables you to easily do various processing on a video or audio stream. In GStreamer this processing is structured as a ‘pipeline’ of elements. Elements can be sources which provide an output, sinks that consume a stream, or both source and sink. You can easily test these pipelines on the command line before wrapping the working pipeline into your own application. This is the library I used for our interim baby monitor. The GStreamer library can be used with many languages. I chose to work in C just because of familiarity.
Before we start coding, we’ll have to install a couple packages. You’ll need to install libgstreamer & gstrtspserver. I’m using Ubuntu so I installed them with the following command at the terminal prompt.
sudo apt-get install libgstreamer0.10-dev libgstrtspserver-0.10-dev
Next I created a makefile to tell GCC which libraries I will be linking to.
Create a Makefile and paste in the contents below.
CPPFLAGS += -I. -I$(top_builddir)/include -I/usr/include/gstreamer-0.10 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/libxml2 -I/usr/lib/glib-2.0/include -I/usr/include/gstreamer-0.10/gst/rtsp-server/
OBJ = rtsptest.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CPPFLAGS)
$(CC) -o $@ $^ $(LDFLAGS) $(CPPFLAGS) -pthread -lstdc++ -lgstreamer-0.10 -lgstrtsp-0.10 -lgstrtspserver-0.10 -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lrt -lxml2 -lglib-2.0 -lm
Now we just need to write the code. In this example it is pretty straight forward. Create a rtsptest.c file and fill it up with the code below.
int main (int argc, char *argv)
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
server = gst_rtsp_server_new ();
mapping = gst_rtsp_server_get_media_mapping (server);
factory = gst_rtsp_media_factory_new ();
"( videotestsrc is-live=1 ! x264enc ! rtph264pay name=pay0 pt=96 )");
gst_rtsp_media_factory_set_shared (factory, TRUE);
gst_rtsp_media_mapping_add_factory (mapping, "/test", factory);
gst_rtsp_server_attach (server, NULL);
This will create an RTSP stream of a video test pattern. Not all that useful, but its helpful when debugging pipelines. This could be replaced with a file, or a live camera source. To use a webcam as a live video source, simply replace ‘videotestsrc’ with ‘v4l2src’ on line 17.
When you’ve got the code like you want; just build, run and test. From the command line run the following commands.
You should see VLC pop up in a new window and display whatever video source you requested in the code above.
Earlier I mentioned streaming RTSP in Android. If you are interested in seeing just how easy it is to write an RTSP client in Android, drop a request in the comments.
Edit: Turns out there was some interest in an Android RTSP Client