#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 *err;

            gst_message_parse_error (msg, &err, &debug);
            g_free (debug);

            g_print ("Error: %s\n", err->message);
            g_error_free (err);

            g_main_loop_quit (loop);
          
            break;
        }

        default:
            break;
    }

    return TRUE;
}

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

    gst_init (&argc, &argv);

    if (argc < 2)
        return 1;

    loop = g_main_loop_new (NULL, FALSE);

    pipeline = gst_pipeline_new ("hello-world");
    playbin = gst_element_factory_make ("playbin", "");

    g_return_val_if_fail (GST_IS_ELEMENT (pipeline), 2);
    g_return_val_if_fail (GST_IS_ELEMENT (playbin), 2);

    bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
    gst_bus_add_watch (bus, bus_call, loop);
    g_object_unref (bus);

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

    gst_bin_add (GST_BIN (pipeline), playbin);
    gst_element_set_state (pipeline, GST_STATE_PLAYING);

    g_main_loop_run (loop);

    gst_element_set_state (pipeline, GST_STATE_NULL);

    g_object_unref (pipeline);

    return 0;
}
