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

static void gst_element_set_state_sync(GstElement * element,
                                       GstState state) {
    GstStateChangeReturn result =
        gst_element_set_state(element, state);

    switch (result) {
    case GST_STATE_CHANGE_FAILURE:
        g_assert_not_reached();
        /* NOTREACHED */
    case GST_STATE_CHANGE_SUCCESS:
    case GST_STATE_CHANGE_NO_PREROLL:
        return;
    case GST_STATE_CHANGE_ASYNC:
        result = gst_element_get_state(element, NULL, NULL,
                                       10 * 10000000000ULL);
        g_assert(result == GST_STATE_CHANGE_SUCCESS ||
                 result == GST_STATE_CHANGE_NO_PREROLL);
        return;
    }

    g_assert_not_reached();
}


int main(int argc, char * argv[]) {
    gst_init(&argc, &argv);

    GstElement * gstPipeline = gst_pipeline_new(NULL);
    GstElement * gstFakeSrc = gst_element_factory_make("fakesrc", NULL);
    g_object_set(G_OBJECT(gstFakeSrc), "is_live", TRUE, NULL);
    GstElement * gstConvert = gst_element_factory_make("audioconvert", NULL);
    GstElement * gstVorbisEnc = gst_element_factory_make("vorbisenc", NULL);
    GstElement * gstOggMux = gst_element_factory_make("oggmux", NULL);
    GstElement * gstFakeSink = gst_element_factory_make("fakesink", NULL);

    gst_bin_add_many(GST_BIN(gstPipeline), gstFakeSrc, gstConvert,
                     gstVorbisEnc, gstOggMux, gstFakeSink, NULL);
    GstCaps * gstFakeCaps = gst_caps_new_simple("audio/x-raw-int",
                                                "width", G_TYPE_INT, 16,
                                                "endianness", G_TYPE_INT, 1234,
                                                "signed", G_TYPE_BOOLEAN, TRUE,
                                                "depth", G_TYPE_INT, 16,
                                                "rate", G_TYPE_INT, 44100,
                                                "channels", G_TYPE_INT, 1,
                                                NULL);
    gst_element_link_pads_filtered(gstFakeSrc, NULL, gstConvert, NULL,
                                   gstFakeCaps);
    gst_element_link_many(gstConvert, gstVorbisEnc, gstOggMux, gstFakeSink,
                          NULL);

    gst_element_set_state_sync(gstPipeline, GST_STATE_PLAYING);
    fprintf(stderr, "Started playing.\n");
    sleep(5);

    gst_element_set_state_sync(gstPipeline, GST_STATE_PAUSED);
    fprintf(stderr, "Paused playing.\n");
    sleep(5);

    return 0;
}
