#include <gst/gst.h>

static gboolean
bus_call (GstBus     *bus,
          GstMessage *msg,
          gpointer    data)
{
  GMainLoop *loop = (GMainLoop *) data;
  const GstStructure *s;

  switch (GST_MESSAGE_TYPE (msg)) {

  case GST_MESSAGE_EOS:
    g_print ("End of stream\n");
    g_main_loop_quit (loop);
    break;

  case GST_MESSAGE_ELEMENT:
    s = gst_message_get_structure (msg);
    if (!g_str_equal (gst_structure_get_name (s), "imperfect-timestamp"))
      break;

    g_assert_not_reached ();

    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;
}

int
main (int argc, char* argv[])
{
  GstElement *e;
  GstBus *bus;
  GMainLoop *loop;

  gst_init (&argc, &argv);
  e = gst_parse_launch ("videotestsrc num-buffers=400 ! theoraenc ! identity check-imperfect-timestamp=1 check-perfect=1 ! fakesink", NULL);
  gst_element_set_state (GST_ELEMENT (e), GST_STATE_PLAYING);

  bus = gst_pipeline_get_bus (GST_PIPELINE (e));
  gst_bus_add_watch (bus, bus_call, loop);
  gst_object_unref (bus);

  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);
  
  g_main_loop_unref(loop);
}
