/* gcc -g -Wall `pkg-config --cflags --libs gstreamer-0.8` -o bug bug.c */

#include <gst/gst.h>

static GstElement *pipeline;

void
cb_new_pad (GstElement *decodebin,
	    GstPad     *pad,
	    gboolean    last,
	    gpointer    data)
{
  GstCaps                 *caps;
  GstStructure            *str;
  const gchar             *mimetype;

  caps = gst_pad_get_caps (pad);
  str = gst_caps_get_structure (caps, 0);
  mimetype = gst_structure_get_name (str);

  if (g_str_has_prefix (mimetype, "audio/"))
    {
      printf ("Audio pad\n");
    }
  else if (g_str_has_prefix (mimetype, "video/")) 
    {
      printf ("Video pad\n");
    }
  else
    {
      printf ("Other pad\n");
    }
}


void
cb_no_more_pad (GstElement *decodebin,
		GstPad     *pad,
		gboolean    last,
		gpointer    data)
{
  printf ("No more pad\n");
  
  gst_element_set_state (pipeline, GST_STATE_PAUSED);
}


int main (int argc, char *argv[])
{
  GstElement *filesrc;
  GstElement *decoder;
  int         i;


  gst_init (&argc, &argv);
  
  pipeline = gst_pipeline_new (NULL);

  filesrc = gst_element_factory_make ("filesrc", "source");
  g_object_set (G_OBJECT (filesrc),
		"location", "/mnt/win_d/provi/blood_small.ogm", NULL);
  decoder = gst_element_factory_make ("decodebin", "decoder");
  gst_bin_add_many (GST_BIN (pipeline), filesrc, decoder, NULL);
  gst_element_link (filesrc, decoder);

  g_signal_connect (G_OBJECT (decoder),
		    "new-decoded-pad",
		    G_CALLBACK (cb_new_pad), NULL);
  g_signal_connect (G_OBJECT (decoder),
		    "no-more-pads",
		    G_CALLBACK (cb_no_more_pad), NULL);
  gst_element_set_state (pipeline, GST_STATE_PLAYING);
  
  for (i = 0 ; gst_bin_iterate (GST_BIN (pipeline)) && (++i <= 40) ;)
    {
      printf ("I : %d\n", i);
    }

  return 1;
}
