#include <gst/gst.h>
#include <glib.h>


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 *uridecodebin,
              GstPad     *pad,
              gpointer    data)
{
  GstCaps* caps = gst_pad_get_caps (pad);
  GstStructure* str = gst_caps_get_structure (caps, 0);

  g_print ("on_pad_added, caps: %s\n", gst_caps_to_string(caps));

    if (g_strrstr (gst_structure_get_name (str), "video/x-raw")) {

      GstElement *sink = gst_element_factory_make ("xvimagesink", NULL);

      GstElement *pipeline = (GstElement *) data;

      GstCaps *videocaps = gst_caps_new_simple("video/x-raw-yuv", "format", GST_TYPE_FOURCC, GST_STR_FOURCC("I420"), NULL);

      gst_bin_add (GST_BIN (pipeline), sink);

      gst_element_link_filtered (uridecodebin, sink, videocaps); // KO
      //gst_element_link (uridecodebin, sink); // OK

      gst_caps_unref (videocaps);

      gst_element_link (uridecodebin, sink);

      gst_element_set_state(sink, GST_STATE_PLAYING);
  }

  gst_caps_unref (caps);
}



int
main (int   argc,
      char *argv[])
{
  GMainLoop *loop;

  GstElement *pipeline, *uridecodebin;
  GstBus *bus;

  /* Initialisation */
  gst_init (&argc, &argv);

  loop = g_main_loop_new (NULL, FALSE);


  /* Check input arguments */
  if (argc != 2) {
    g_printerr ("Usage: %s file:///home/julien/Videos/sample.avi\n", argv[0]);
    return -1;
  }

  /* Create gstreamer elements */
  pipeline = gst_pipeline_new ("video-player");
  uridecodebin  = gst_element_factory_make ("uridecodebin", NULL);

  if (!pipeline || !uridecodebin) {
    g_printerr ("One element could not be created. Exiting.\n");
    return -1;
  }

  /* Set up the pipeline */

  g_object_set (G_OBJECT (uridecodebin), "uri", argv[1], NULL);

  /* we add a message handler */
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  gst_bus_add_watch (bus, bus_call, loop);
  gst_object_unref (bus);

  gst_bin_add (GST_BIN (pipeline), uridecodebin);

  g_signal_connect (uridecodebin, "pad-added", G_CALLBACK (on_pad_added), pipeline);

  /* Set the pipeline to "playing" state*/
  g_print ("Now playing: %s\n", argv[1]);
  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  /* Iterate */
  g_print ("Running...\n");
  g_main_loop_run (loop);

  /* Out of the main loop, clean up nicely */
  g_print ("Returned, stopping playback\n");
  gst_element_set_state (pipeline, GST_STATE_NULL);

  g_print ("Deleting pipeline\n");
  gst_object_unref (GST_OBJECT (pipeline));

  return 0;
}