#include <gst/gst.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 *element,
              GstPad     *pad,
              gpointer    data)
{
  GstPad *sinkpad;
  GstElement *converter = (GstElement *) data;

  sinkpad = gst_element_get_static_pad (converter, "sink");

  /* Linking decodebin to videoconvert */
  gst_pad_link (pad, sinkpad);

  gst_object_unref (sinkpad);
}


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

  GstElement *pipeline;
  GstElement *source, *decoder, *converter, *sink;
  GstBus *bus;
  guint bus_watch_id;

  GList *filenames;
  /* GList *get_filenames; */

  /* init */
  gst_init (&argc, &argv);
  loop = g_main_loop_new (NULL, FALSE);

  /* create pipeline */
  pipeline = gst_pipeline_new ("my-pipeline");

  /* create elements */
  source = gst_element_factory_make ("imagesequencesrc", "source");
  decoder = gst_element_factory_make ("decodebin", "decoder");
  converter = gst_element_factory_make ("videoconvert", "converter");
  sink = gst_element_factory_make ("xvimagesink", "sink");

  /* Set location of image sequences*/
  /*
  g_object_set (source,
      "location",
          "/home/cfoch/Pictures/Trash/numbers/1.png,"
          "/home/cfoch/Pictures/Trash/numbers/2.png,"
          "/home/cfoch/Pictures/Trash/numbers/3.png,"
          "/home/cfoch/Pictures/Trash/numbers/4.png,"
          "/home/cfoch/Pictures/Trash/numbers/5.png,"
          "/home/cfoch/Pictures/Trash/numbers/6.png,"
          "/home/cfoch/Pictures/Trash/numbers/7.png,"
          "/home/cfoch/Pictures/Trash/numbers/8.png,"
          "/home/cfoch/Pictures/Trash/numbers/9.png,"
          "/home/cfoch/Pictures/Trash/numbers/10.png",
      NULL);
  */

  filenames = NULL;

  filenames = g_list_append (filenames,
      g_strdup ("/home/cfoch/Pictures/Trash/numbers/1.png"));
  filenames = g_list_append (filenames,
      g_strdup ("/home/cfoch/Pictures/Trash/numbers/2.png"));
  filenames = g_list_append (filenames,
      g_strdup ("/home/cfoch/Pictures/Trash/numbers/3.png"));
  filenames = g_list_append (filenames,
      g_strdup ("/home/cfoch/Pictures/Trash/numbers/4.png"));

  g_object_set (source, "filenames_list", filenames, NULL);
  g_object_set (source, "framerate", "2/1", NULL);

  /* Print the first filename of the current list */
  /* g_object_get (source, "filenames-list", &get_filenames, NULL); */
  /* g_print ("This is the first file: %s\n", (gchar *) get_filenames->data); */

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

  /* must add elements to pipeline before linking them */
  gst_bin_add_many (GST_BIN (pipeline), source, decoder, converter, sink, NULL);

  /* link */
  if (!gst_element_link_many (source, decoder, NULL)) {
    g_warning ("Failed to link elements!");
  } else {
    gst_element_link_pads_full (source, "src", decoder, "sink",
        GST_PAD_LINK_CHECK_NOTHING);
  }

  if (!gst_element_link_many (converter, sink, NULL)) {
    g_warning ("Failed to link elements!");
  }

  g_signal_connect (decoder, "pad-added", G_CALLBACK (on_pad_added), converter);

  /* Set the pipeline to "playing" state*/
  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));
  g_source_remove (bus_watch_id);
  g_main_loop_unref (loop);


  return 0;
}
