Skip to content

Instantly share code, notes, and snippets.

@asamy
Created August 15, 2013 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asamy/6241699 to your computer and use it in GitHub Desktop.
Save asamy/6241699 to your computer and use it in GitHub Desktop.
GStreamer Client Audio Stream Player
/*
* A GStreamer Client Audio Streamer
*
* Written by Ahmed Samy <f.fallen45@gmail.com>
* with some assistance from the GStreamer documentation.
*
* Compile with:
* gcc -Wall filestream.c `pkg-config --libs --cflags gstreamer-1.0`
*
* Run:
* ./a.out http://stream-07.shoutcast.com:8000/bassdrive_mp3_128kbps
*/
#include <gst/gst.h>
/* playbin play flags
* Stolen from the GStreamer documentation. */
typedef enum {
GST_PLAY_FLAG_AUDIO = (1 << 1), /* Audio output */
GST_PLAY_FLAG_TEXT = (1 << 2) /* Subtitle output */
} GstPlayFlags;
static gboolean handle_message(GstBus *bus, GstMessage *msg, GMainLoop *loop)
{
GError *err;
gchar *debug_info;
switch (GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_ERROR:
gst_message_parse_error(msg, &err, &debug_info);
g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
g_printerr("Debugging information: %s\n", debug_info ? debug_info : "none");
g_clear_error(&err);
g_free(debug_info);
g_main_loop_quit(loop);
break;
case GST_MESSAGE_EOS: /* End of stream. */
g_main_loop_quit(loop);
break;
default:
break;
}
return TRUE;
}
int main(int argc, char *argv[])
{
GMainLoop *loop;
GstElement *playbin;
GstBus *bus;
GstStateChangeReturn ret;
gint flags;
gst_init(&argc, &argv);
playbin = gst_element_factory_make("playbin", "playbin");
if (!playbin) {
g_printerr("Could not load the playbin plugin, make sure you got it installed, then re-run\n");
return 1;
}
g_object_set(playbin, "uri", argv[1], NULL);
g_object_get(playbin, "flags", &flags, NULL);
flags |= GST_PLAY_FLAG_AUDIO; /* We only want audio. */
flags &= ~GST_PLAY_FLAG_TEXT;
g_object_set(playbin, "flags", flags, NULL);
loop = g_main_loop_new(NULL, FALSE);
bus = gst_element_get_bus(playbin);
gst_bus_add_watch(bus, (GstBusFunc)handle_message, loop);
ret = gst_element_set_state(playbin, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr("Unable to set the pipeline to the playing state.\n");
gst_object_unref(playbin);
gst_object_unref(loop);
return 1;
}
g_main_loop_run(loop);
g_main_loop_unref(loop);
gst_object_unref(bus);
gst_element_set_state(playbin, GST_STATE_NULL);
gst_object_unref(playbin);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment