stream receiver in C
xname
root at xname.cc
Fri Aug 24 11:26:57 PDT 2012
Hello there,
My stream receiver is now working;
under request, I will share it here:
(forgive my bad practice to paste code in the mailing list)
=== hellostream.c ====
/* video stream player - xname 2012
compile it with:
gcc -Wall hellostream.c -o hellostream $(pkg-config --cflags --libs
gstreamer-0.10) */
#include <gst/gst.h>
#include <glib.h>
static gboolean
bus_call (GstBus *bus,
GstMessage *msg,
gpointer data)
{
GMainLoop *loop = (GMainLoop *) data;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_EOS:
g_print ("End of stream\n");
g_main_loop_quit (loop);
break;
case GST_MESSAGE_ERROR: {
gchar *debug;
GError *error;
gst_message_parse_error (msg, &error, &debug);
g_free (debug);
g_printerr ("Error: %s\n", error->message);
g_error_free (error);
g_main_loop_quit (loop);
break;
}
default:
break;
}
return TRUE;
}
int
main (int argc,
char *argv[])
{
GMainLoop *loop;
GstElement *pipeline, *source, *rtph264, *decoder, *sink;
GstCaps* caps;
GstBus *bus;
/* Initialisation */
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
/* Check input arguments */
if (argc != 1) {
g_printerr ("Usage: no need to declare a file, just make sure you are
emitting on the right IP");
return -1;
}
/* Create gstreamer elements */
pipeline = gst_pipeline_new ("videostream-receiver");
source = gst_element_factory_make ("udpsrc", "stream-source");
rtph264 = gst_element_factory_make ("rtph264depay", "rtpdepay");
decoder = gst_element_factory_make ("ffdec_h264", "ffdec");
sink = gst_element_factory_make ("autovideosink", "video-output");
/* for mac use:
sink = gst_element_factory_make ("glimagesink", "video-output"); */
if (!pipeline || !source || !rtph264 || !decoder || !sink) {
g_printerr ("One element could not be created. Exiting.\n");
return -1;
}
/* Specify the RTP stream that we expect */
caps = gst_caps_new_simple( "application/x-rtp",
"clock-rate", G_TYPE_INT, 90000,
"encoding-name", G_TYPE_STRING, "H264",
NULL);
g_object_set( source, "caps", caps, NULL );
gst_caps_unref( caps );
/* On what port are you listening? */
g_object_set (G_OBJECT(source), "port", 4000, NULL);
/* Add a message handler */
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
/* Add all elements into the pipeline */
gst_bin_add_many (GST_BIN (pipeline),
source, rtph264, decoder,sink, NULL);
/* We link the elements together */
gst_element_link_many (source, rtph264, decoder, sink, NULL);
/* Set the pipeline to "playing" state */
g_print ("Receiving stream...\n");
gst_element_set_state (pipeline, GST_STATE_PLAYING);
/* Iterate */
g_print ("Running...\n");
g_main_loop_run (loop);
/* Out of the main loop, clean up nicely */
g_print ("Returned, stopping playback\n");
gst_element_set_state (pipeline, GST_STATE_NULL);
g_print ("Deleting pipeline\n");
gst_object_unref (GST_OBJECT (pipeline));
return 0;
}
More information about the gstreamer-devel
mailing list