Don't like ads? PRO users don't see any ads ;-)
Guest

output_sel.c

By: a guest on Sep 24th, 2012  |  syntax: None  |  size: 5.80 KB  |  hits: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include<gst/gst.h>
  2. #include<glib.h>
  3. #define NUM_VIDEO_BUFFERS 500
  4.  
  5.         GMainLoop *loop;       
  6.         GstElement *pipeline,*source,*sink2,*decode,*osel,*sink,*sink1;
  7.         GstBus *bus;
  8.         static GstPad *sinkpad_decoder=NULL;
  9.         static GstPad *sinkpad_filesink=NULL;
  10.         static GstPad *osel_src1 = NULL;
  11.         static GstPad *osel_src2 = NULL;
  12.         gint64 pos, len;
  13.         GstFormat fm = GST_FORMAT_TIME;
  14.  
  15. static void on_pad_added (GstElement *element,GstPad *pad,gpointer data)
  16. {
  17.         GstCaps *caps;
  18.         GstStructure *str;
  19.         gchar *name;
  20.         gboolean ch;
  21.         //GstElement *demuxer = (GstElement *) data;
  22.  
  23.        
  24.         caps = gst_pad_get_caps (pad);
  25.         g_assert(caps != NULL);
  26.         str = gst_caps_get_structure (caps, 0);
  27.         g_assert(str != NULL);
  28.  
  29.         const gchar *c = gst_structure_get_name(str);
  30.         name=gst_caps_to_string(caps);
  31.         g_print("%s\n\n",name);
  32.  
  33.          /* We can now link this pad with the vorbis-decoder sink pad */
  34.         g_print ("Dynamic pad created, linking demuxer/decoder\n");
  35.        
  36.        
  37.        
  38.         if (g_strrstr (c, "video") || g_strrstr (c, "image"))
  39.          {
  40.         g_debug ("Linking video pad to autovideosink");
  41.         gchar *name;
  42.         name=gst_pad_get_name(pad);
  43.         g_printf("\n Linking the video pad.");
  44.        
  45.         ch=gst_element_link_pads(element,name,osel,"sink");
  46.         if(ch==FALSE)
  47.          {
  48.          g_printf("Error While Linking VIDEO pad");
  49.          exit(-1);
  50.          }
  51.          }
  52.  
  53.         /*if (g_strrstr (c, "audio")) {
  54.         g_debug ("Linking audio pad to queuea");
  55.         gchar *name;
  56.         name=gst_pad_get_name(pad);
  57.         g_printf("\n Linking the AUDIO pad.");
  58.         ch=gst_element_link_pads(element,name,sink1,"sink");
  59.         if(ch==FALSE) {
  60.          g_printf("Error while linking Audio Pad");
  61.         exit(-1);
  62.          }
  63.          }*/
  64.  gst_caps_unref (caps);
  65. }
  66.  
  67.  
  68.  
  69. gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data)
  70. {
  71.         GMainLoop *loop = (GMainLoop *)data;
  72.        
  73.          switch (GST_MESSAGE_TYPE (msg))
  74.                 {
  75.  
  76.                          case GST_MESSAGE_EOS:
  77.                                                 g_print ("End of stream\n");
  78.                                                 g_main_loop_quit (loop);
  79.                                                 break;
  80.  
  81.                         case GST_MESSAGE_ERROR: {
  82.                                                  gchar  *debug;
  83.                                                  GError *error;
  84.  
  85.                                                  gst_message_parse_error (msg, &error, &debug);
  86.                                                  g_free (debug);
  87.  
  88.                                                  g_printerr ("Error: %s\n", error->message);
  89.                                                  g_error_free (error);
  90.  
  91.                                                  g_main_loop_quit (loop);
  92.                                                  break;
  93.                                                  }
  94.                          default:
  95.                                   break;
  96.                 }
  97.  
  98.         return TRUE;
  99. }
  100.  
  101.  
  102. static gboolean
  103. switch_cb (gpointer user_data)
  104. {
  105.   GstElement *sel = GST_ELEMENT (user_data);
  106.   GstPad *old_pad, *new_pad = NULL;
  107.  
  108.   g_object_get (G_OBJECT (sel), "active-pad", &old_pad, NULL);
  109.  
  110.   if (old_pad == osel_src1)
  111.     new_pad = osel_src2;
  112.   else
  113.     new_pad = osel_src1;
  114.  
  115.   g_object_set (G_OBJECT (sel), "active-pad", new_pad, NULL);
  116.  
  117.   g_print ("switched from %s:%s to %s:%s\n", GST_DEBUG_PAD_NAME (old_pad),
  118.       GST_DEBUG_PAD_NAME (new_pad));
  119.  
  120.   gst_object_unref (old_pad);
  121.  
  122.   return TRUE;
  123.  
  124. }
  125.  
  126.  
  127. static gboolean cb_print_position (GstElement *pipeline)
  128. {
  129.   gint64 pos, len;
  130.   GstFormat fm = GST_FORMAT_TIME;
  131.  
  132.         //gst_element_query_position(pipeline,&fm,&pos);
  133.        
  134.                                
  135.  
  136.   if (gst_element_query_position (pipeline, &fm, &pos)
  137.     && gst_element_query_duration (pipeline, &fm, &len)) {
  138.     g_print ("Time: %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "\r",
  139.              GST_TIME_ARGS (pos), GST_TIME_ARGS (len));
  140.   }
  141.  
  142.   /* call me again */
  143.   return TRUE;
  144. }
  145.  
  146.  
  147.  
  148. int main(int argc, char *argv[])
  149. {
  150.        
  151.  
  152.        
  153.  
  154.         /*initializing*/
  155.         gst_init(&argc,&argv);
  156.  
  157.         loop = g_main_loop_new(NULL,FALSE);
  158.  
  159.         /*creating elements*/
  160.  
  161.        
  162.  
  163.         pipeline = gst_pipeline_new("MY PIPELINE");
  164.  
  165.        
  166.        
  167.         source =  gst_element_factory_make("filesrc","file-source");
  168.        
  169.         osel = gst_element_factory_make("output-selector","selector"); 
  170.        
  171.         decode =  gst_element_factory_make("decodebin","decode");
  172.        
  173.         sink1 =  gst_element_factory_make("xvimagesink","audiooutput1");
  174.  
  175.         sink =  gst_element_factory_make("xvimagesink","audiooutput");
  176.        
  177.         /*set location property*/
  178.         g_object_set(G_OBJECT(source),"location",argv[1],NULL);
  179.         //g_object_set (G_OBJECT (decode), "async-handling", TRUE, NULL);
  180.         g_object_set (G_OBJECT (osel), "resend-latest", TRUE, NULL);
  181.         g_object_set (G_OBJECT (sink), "sync", FALSE, NULL);
  182.         g_object_set (G_OBJECT (sink1), "sync", FALSE, NULL);
  183.  
  184.         g_object_set(G_OBJECT(source),"num-buffers",NUM_VIDEO_BUFFERS,NULL);
  185.  
  186.         /*set bus signals*/
  187.         bus=gst_pipeline_get_bus(GST_PIPELINE(pipeline));
  188.         gst_bus_add_watch(bus,bus_call,loop);
  189.         gst_object_unref(bus);
  190.  
  191.  
  192.         /*add bins , topmost pipeline*/
  193.         gst_bin_add_many(GST_BIN(pipeline),source,osel,decode,sink,sink1,NULL);
  194.  
  195.         gst_element_link(source,decode);
  196.  
  197.         g_signal_connect (decode, "pad-added", G_CALLBACK (on_pad_added), NULL);
  198.  
  199.                
  200.        
  201.         sinkpad_decoder = gst_element_get_static_pad (sink, "sink");
  202.         if(sinkpad_decoder==NULL)
  203.         g_printf("\n NOT CREATED decoder sinkpad.");
  204.         osel_src1 = gst_element_get_request_pad (osel, "src%d");
  205.  
  206.         if (gst_pad_link (osel_src1, sinkpad_decoder) != GST_PAD_LINK_OK) {
  207.         g_print ("linking output-selector to decoder failed\n");
  208.         return -1;
  209.          }
  210.        
  211.         sinkpad_filesink = gst_element_get_static_pad (sink1, "sink");
  212.         if(sinkpad_filesink==NULL)
  213.         g_printf("\n NOT CREATED filesink sinkpad.");
  214.         osel_src2 = gst_element_get_request_pad (osel, "src%d");
  215.        
  216.        
  217.         if (gst_pad_link (osel_src2, sinkpad_filesink) != GST_PAD_LINK_OK) {
  218.         g_print ("linking output-selector to decoder failed\n");
  219.         return -1;
  220.          }
  221.        
  222.  
  223.        
  224.        
  225.         //g_signal_connect (decode1, "pad-added", G_CALLBACK (on_pad_added), NULL);
  226.        
  227.         g_timeout_add (200, (GSourceFunc) switch_cb,osel);
  228.        
  229.         /*play state*/
  230.         gst_element_set_state (pipeline, GST_STATE_PLAYING);
  231.        
  232.         /*timeout.. call function*/
  233.        
  234.        
  235.  
  236.        
  237.         g_main_loop_run(loop);
  238.  
  239.         gst_element_set_state (pipeline, GST_STATE_NULL);
  240.  
  241.         g_print ("Deleting pipeline\n");
  242.         gst_object_unref (GST_OBJECT (pipeline));
  243.  
  244.         return 0;
  245. }