Implement gstreamer Hello World tutorial
http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/chapter-helloworld.html
This commit is contained in:
commit
f34eeafd56
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/oggplay
|
9
Makefile
Normal file
9
Makefile
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
TARGET := oggplay
|
||||||
|
OBJS := oggplay.o
|
||||||
|
CFLAGS := -Wall -O2 $$(pkg-config --cflags gstreamer-1.0)
|
||||||
|
LIBS := $$(pkg-config --libs gstreamer-1.0)
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(OBJS)
|
||||||
|
$(CC) -o $@ $(LDFLAGS) $^ $(LIBS)
|
107
oggplay.c
Normal file
107
oggplay.c
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
#include <gst/gst.h>
|
||||||
|
#include <glib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data);
|
||||||
|
static void on_pad_added(GstElement *element, GstPad *pad, gpointer data);
|
||||||
|
|
||||||
|
int main(int argc, char * argv[])
|
||||||
|
{
|
||||||
|
GstElement *source, *demux, *decode, *conv, *sink, *pipeline;
|
||||||
|
GMainLoop *loop;
|
||||||
|
GstBus *bus;
|
||||||
|
guint bus_watch_id;
|
||||||
|
|
||||||
|
gst_init(NULL, NULL);
|
||||||
|
loop = g_main_loop_new(NULL, FALSE);
|
||||||
|
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
g_printerr("Usage: %s <filename>\n", argv[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
source = gst_element_factory_make("filesrc", "op_source");
|
||||||
|
demux = gst_element_factory_make("oggdemux", "op_demux");
|
||||||
|
decode = gst_element_factory_make("vorbisdec", "op_decode");
|
||||||
|
conv = gst_element_factory_make("audioconvert", "op_conv");
|
||||||
|
sink = gst_element_factory_make("autoaudiosink", "op_sink");
|
||||||
|
pipeline = gst_pipeline_new("op_pipeline");
|
||||||
|
|
||||||
|
if (!source || !demux || !decode || !sink || !pipeline)
|
||||||
|
{
|
||||||
|
g_printerr("Error creating something!\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_object_set(G_OBJECT(source), "location", argv[1], NULL);
|
||||||
|
|
||||||
|
bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
|
||||||
|
bus_watch_id = gst_bus_add_watch(bus, bus_call, loop);
|
||||||
|
gst_object_unref(bus);
|
||||||
|
|
||||||
|
gst_bin_add_many(GST_BIN(pipeline),
|
||||||
|
source, demux, decode, conv, sink, NULL);
|
||||||
|
|
||||||
|
gst_element_link(source, demux);
|
||||||
|
gst_element_link_many(decode, conv, sink, NULL);
|
||||||
|
g_signal_connect(demux, "pad-added", G_CALLBACK(on_pad_added), decode);
|
||||||
|
|
||||||
|
g_print("Beginning play\n");
|
||||||
|
gst_element_set_state(pipeline, GST_STATE_PLAYING);
|
||||||
|
|
||||||
|
g_print("Starting main loop\n");
|
||||||
|
g_main_loop_run(loop);
|
||||||
|
|
||||||
|
g_print("Main loop ended.\n");
|
||||||
|
gst_element_set_state(pipeline, GST_STATE_NULL);
|
||||||
|
|
||||||
|
gst_object_unref(GST_OBJECT(pipeline));
|
||||||
|
g_source_remove(bus_watch_id);
|
||||||
|
g_main_loop_unref(loop);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_pad_added(GstElement *element, GstPad *pad, gpointer data)
|
||||||
|
{
|
||||||
|
GstPad *sinkpad;
|
||||||
|
GstElement *decode = (GstElement *)data;
|
||||||
|
|
||||||
|
g_print("Dynamic pad created, linking demux -> decode\n");
|
||||||
|
|
||||||
|
sinkpad = gst_element_get_static_pad(decode, "sink");
|
||||||
|
gst_pad_link(pad, sinkpad);
|
||||||
|
gst_object_unref(sinkpad);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user