/*
 * reproducer for deadlock
 *
 * gcc -o deadlock deadlock.c -g `pkg-config --libs --cflags gstreamer-1.0` -Wall
 */
#include <stdio.h>
#include <gst/gst.h>

int main(int argc, char **argv)
{
    GstElement *pipeline;
    GstElement *audiosrc;
    GstElement *audiofilter;
    GstElement *audiosink;
    GstCaps *caps;

    gst_init(&argc, &argv);

    pipeline = gst_pipeline_new("pipeline");
    g_assert(pipeline);

    audiosrc = gst_element_factory_make("audiotestsrc", "audiotestsrc");
    g_assert(audiosrc);

    audiofilter = gst_element_factory_make("capsfilter", "capsfilter");
    g_assert(audiofilter);
    caps = gst_caps_new_simple("audio/x-raw", "channels", G_TYPE_INT, 2, NULL);
    g_object_set(G_OBJECT(audiofilter), "caps",caps, NULL);
    gst_caps_unref(caps);

    audiosink = gst_element_factory_make("jackaudiosink", "jackaudiosink");
    g_assert(audiosink);
    g_object_set(G_OBJECT(audiosink), "client-name", "test", NULL);

    g_print("Adding elements\n");
    gst_bin_add_many(GST_BIN(pipeline), audiosrc, audiofilter, audiosink, NULL);

    gst_element_link_many(audiosrc, audiofilter, audiosink, NULL);

    g_print("Set pipeline state to playing\n");
    gst_element_set_state(pipeline, GST_STATE_PLAYING);

    g_print("Restart jack daemon and press enter\n");
    getchar();

    g_print("Set pipeline state to null\n");
    gst_element_set_state(pipeline, GST_STATE_NULL);
    g_print("Unref pipeline\n");
    gst_object_unref(pipeline);

    return 0;
}

