#include <gst/gst.h>

#define GST_CREATE_EL(el, kind, name) do {				\
	el = gst_element_factory_make(kind, name);			\
	g_assert(el);										\
	g_print("Created %s of kind %s with name %s, %p\n",	\
		#el, kind, name, el);							\
	if (el != pipe)										\
		gst_bin_add(GST_BIN(pipe), el);					\
} while (0)
#define SINK "alsa"

GMainLoop *loop;
GstElement *pipe, *src, *conv, *res, *sink;
int timer = 0;

void
CreatePipeline()
{
	GST_CREATE_EL(pipe, "pipeline", "pipeline");
	GST_CREATE_EL(src, "audiotestsrc", "src");
	GST_CREATE_EL(conv, "audioconvert", "converter");
	GST_CREATE_EL(res, "audioresample", "resampler");
	GST_CREATE_EL(sink, SINK"sink", sink);
	gst_element_link_many(src, conv, res, sink, NULL);
}

gboolean
IdleTimer(gpointer data)
{
	timer ++;
	g_print("Timer: %d\n", timer);
	if ((timer % 10) == 0) {
		gst_element_set_state(pipe, GST_STATE_NULL);
		g_main_loop_quit(loop);
	}
	return TRUE;
}

int
main(int ac, char **av)
{
	gst_init(&ac, &av);
	loop = g_main_loop_new(NULL, FALSE);
	g_timeout_add(100, IdleTimer, NULL);

	CreatePipeline();
	gst_element_set_state(pipe, GST_STATE_PLAYING);
	g_main_loop_run(loop);
	g_object_unref(pipe);

	g_print("done\n");

	CreatePipeline();
	gst_element_set_state(pipe, GST_STATE_PLAYING);
	g_main_loop_run(loop);
	g_object_unref(pipe);

	return (0);
}
