Skip to content

Instantly share code, notes, and snippets.

@mohan43u
Created January 3, 2015 10:17
Show Gist options
  • Save mohan43u/b05ff3b18cf8d89e2b0b to your computer and use it in GitHub Desktop.
Save mohan43u/b05ff3b18cf8d89e2b0b to your computer and use it in GitHub Desktop.
test gstreamer bin
#include <stdio.h>
#include <stdlib.h>
#include <glib-unix.h>
#include <gst/gst.h>
typedef struct {
GstElement *bin;
GstElement *in;
GstElement *decodebin;
GstElement *aout;
GstElement *vout;
} TestBinElements;
typedef struct {
gchar *file;
TestBinElements *elements;
GstElement *pipeline;
GMainLoop *mainloop;
GstBus *msgbus;
} TestBin;
gboolean testbin_msgbus_cb(GstBus *bus, GstMessage *message, gpointer *data) {
TestBin *this = (TestBin *) data;
switch(GST_MESSAGE_TYPE(message)) {
case(GST_MESSAGE_EOS): g_main_loop_quit(this->mainloop);
break;
case(GST_MESSAGE_STATE_CHANGED): {
GstState old;
GstState new;
gst_message_parse_state_changed(message, &old, &new, NULL);
g_printerr("%d: state changed from %s to %s on %s\n",
GST_MESSAGE_SEQNUM(message),
gst_element_state_get_name(old),
gst_element_state_get_name(new),
GST_MESSAGE_SRC_NAME(message));
}
break;
default: g_printerr("%d: received %s from %s..\n",
GST_MESSAGE_SEQNUM(message),
GST_MESSAGE_TYPE_NAME(message),
GST_MESSAGE_SRC_NAME(message));
}
return TRUE;
}
void testbin_pad_added_cb(GstElement *element, GstPad *pad, gpointer data) {
TestBin *this = (TestBin *) data;
GstPad *gpad = gst_ghost_pad_new(NULL, pad);
gst_pad_set_active(gpad, TRUE);
gst_element_add_pad(this->elements->bin, gpad);
}
void testbin_no_more_pads_cb(GstElement *element, gpointer data) {
TestBin *this = (TestBin *) data;
gst_element_link(this->elements->bin,
this->elements->vout);
gst_element_link(this->elements->bin,
this->elements->aout);
}
void testbin_elements_init(TestBin *this) {
this->elements = g_new(TestBinElements, 1);
this->elements->bin = gst_bin_new("testbin0");
this->elements->in = gst_element_factory_make("filesrc", NULL);
this->elements->decodebin = gst_element_factory_make("decodebin", NULL);
this->elements->vout = gst_element_factory_make("autovideosink", NULL);
this->elements->aout = gst_element_factory_make("autoaudiosink", NULL);
gst_bin_add_many(GST_BIN(this->pipeline),
this->elements->bin,
this->elements->vout,
this->elements->aout,
NULL);
gst_bin_add_many(GST_BIN(this->elements->bin),
this->elements->in,
this->elements->decodebin,
NULL);
gst_element_link(this->elements->in,
this->elements->decodebin);
g_object_set(this->elements->in,
"location",
this->file,
NULL);
g_signal_connect(this->elements->decodebin,
"pad-added",
G_CALLBACK(testbin_pad_added_cb),
this);
g_signal_connect(this->elements->decodebin,
"no-more-pads",
G_CALLBACK(testbin_no_more_pads_cb),
this);
}
int main(int argc, char *argv[]) {
if(argc < 2) {
g_printerr("[usage] %s file\n", argv[0]);
exit(EXIT_FAILURE);
}
if(! gst_is_initialized()) gst_init(NULL, NULL);
TestBin testbin;
testbin.file = argv[1];
testbin.mainloop = g_main_loop_new(NULL, FALSE);
testbin.pipeline = gst_pipeline_new("pipeline0");
testbin_elements_init(&testbin);
testbin.msgbus = gst_element_get_bus(GST_ELEMENT(testbin.pipeline));
gst_bus_add_watch_full(testbin.msgbus,
G_PRIORITY_DEFAULT,
(GstBusFunc) testbin_msgbus_cb,
&testbin,
NULL);
gst_element_set_state(testbin.pipeline, GST_STATE_PLAYING);
g_main_loop_run(testbin.mainloop);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment