- #include<gst/gst.h>
- #include<glib.h>
- #define NUM_VIDEO_BUFFERS 500
- GMainLoop *loop;
- GstElement *pipeline,*source,*sink2,*decode,*osel,*sink,*sink1;
- GstBus *bus;
- static GstPad *sinkpad_decoder=NULL;
- static GstPad *sinkpad_filesink=NULL;
- static GstPad *osel_src1 = NULL;
- static GstPad *osel_src2 = NULL;
- gint64 pos, len;
- GstFormat fm = GST_FORMAT_TIME;
- static void on_pad_added (GstElement *element,GstPad *pad,gpointer data)
- {
- GstCaps *caps;
- GstStructure *str;
- gchar *name;
- gboolean ch;
- //GstElement *demuxer = (GstElement *) data;
- caps = gst_pad_get_caps (pad);
- g_assert(caps != NULL);
- str = gst_caps_get_structure (caps, 0);
- g_assert(str != NULL);
- const gchar *c = gst_structure_get_name(str);
- name=gst_caps_to_string(caps);
- g_print("%s\n\n",name);
- /* We can now link this pad with the vorbis-decoder sink pad */
- g_print ("Dynamic pad created, linking demuxer/decoder\n");
- if (g_strrstr (c, "video") || g_strrstr (c, "image"))
- {
- g_debug ("Linking video pad to autovideosink");
- gchar *name;
- name=gst_pad_get_name(pad);
- g_printf("\n Linking the video pad.");
- ch=gst_element_link_pads(element,name,osel,"sink");
- if(ch==FALSE)
- {
- g_printf("Error While Linking VIDEO pad");
- exit(-1);
- }
- }
- /*if (g_strrstr (c, "audio")) {
- g_debug ("Linking audio pad to queuea");
- gchar *name;
- name=gst_pad_get_name(pad);
- g_printf("\n Linking the AUDIO pad.");
- ch=gst_element_link_pads(element,name,sink1,"sink");
- if(ch==FALSE) {
- g_printf("Error while linking Audio Pad");
- exit(-1);
- }
- }*/
- gst_caps_unref (caps);
- }
- 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 gboolean
- switch_cb (gpointer user_data)
- {
- GstElement *sel = GST_ELEMENT (user_data);
- GstPad *old_pad, *new_pad = NULL;
- g_object_get (G_OBJECT (sel), "active-pad", &old_pad, NULL);
- if (old_pad == osel_src1)
- new_pad = osel_src2;
- else
- new_pad = osel_src1;
- g_object_set (G_OBJECT (sel), "active-pad", new_pad, NULL);
- g_print ("switched from %s:%s to %s:%s\n", GST_DEBUG_PAD_NAME (old_pad),
- GST_DEBUG_PAD_NAME (new_pad));
- gst_object_unref (old_pad);
- return TRUE;
- }
- static gboolean cb_print_position (GstElement *pipeline)
- {
- gint64 pos, len;
- GstFormat fm = GST_FORMAT_TIME;
- //gst_element_query_position(pipeline,&fm,&pos);
- if (gst_element_query_position (pipeline, &fm, &pos)
- && gst_element_query_duration (pipeline, &fm, &len)) {
- g_print ("Time: %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "\r",
- GST_TIME_ARGS (pos), GST_TIME_ARGS (len));
- }
- /* call me again */
- return TRUE;
- }
- int main(int argc, char *argv[])
- {
- /*initializing*/
- gst_init(&argc,&argv);
- loop = g_main_loop_new(NULL,FALSE);
- /*creating elements*/
- pipeline = gst_pipeline_new("MY PIPELINE");
- source = gst_element_factory_make("filesrc","file-source");
- osel = gst_element_factory_make("output-selector","selector");
- decode = gst_element_factory_make("decodebin","decode");
- sink1 = gst_element_factory_make("xvimagesink","audiooutput1");
- sink = gst_element_factory_make("xvimagesink","audiooutput");
- /*set location property*/
- g_object_set(G_OBJECT(source),"location",argv[1],NULL);
- //g_object_set (G_OBJECT (decode), "async-handling", TRUE, NULL);
- g_object_set (G_OBJECT (osel), "resend-latest", TRUE, NULL);
- g_object_set (G_OBJECT (sink), "sync", FALSE, NULL);
- g_object_set (G_OBJECT (sink1), "sync", FALSE, NULL);
- g_object_set(G_OBJECT(source),"num-buffers",NUM_VIDEO_BUFFERS,NULL);
- /*set bus signals*/
- bus=gst_pipeline_get_bus(GST_PIPELINE(pipeline));
- gst_bus_add_watch(bus,bus_call,loop);
- gst_object_unref(bus);
- /*add bins , topmost pipeline*/
- gst_bin_add_many(GST_BIN(pipeline),source,osel,decode,sink,sink1,NULL);
- gst_element_link(source,decode);
- g_signal_connect (decode, "pad-added", G_CALLBACK (on_pad_added), NULL);
- sinkpad_decoder = gst_element_get_static_pad (sink, "sink");
- if(sinkpad_decoder==NULL)
- g_printf("\n NOT CREATED decoder sinkpad.");
- osel_src1 = gst_element_get_request_pad (osel, "src%d");
- if (gst_pad_link (osel_src1, sinkpad_decoder) != GST_PAD_LINK_OK) {
- g_print ("linking output-selector to decoder failed\n");
- return -1;
- }
- sinkpad_filesink = gst_element_get_static_pad (sink1, "sink");
- if(sinkpad_filesink==NULL)
- g_printf("\n NOT CREATED filesink sinkpad.");
- osel_src2 = gst_element_get_request_pad (osel, "src%d");
- if (gst_pad_link (osel_src2, sinkpad_filesink) != GST_PAD_LINK_OK) {
- g_print ("linking output-selector to decoder failed\n");
- return -1;
- }
- //g_signal_connect (decode1, "pad-added", G_CALLBACK (on_pad_added), NULL);
- g_timeout_add (200, (GSourceFunc) switch_cb,osel);
- /*play state*/
- gst_element_set_state (pipeline, GST_STATE_PLAYING);
- /*timeout.. call function*/
- g_main_loop_run(loop);
- gst_element_set_state (pipeline, GST_STATE_NULL);
- g_print ("Deleting pipeline\n");
- gst_object_unref (GST_OBJECT (pipeline));
- return 0;
- }