#include <stdlib.h>
#include <clutter-gst/clutter-gst.h>

static void on_pad_added (GstElement *element, GstPad *src, gpointer data) {
	GstPad *sink;
	GstElement *next = (GstElement *) data;
	sink = gst_element_get_static_pad (next, "sink");
	gst_pad_link (src, sink);
	gst_object_unref (sink);
}

int main (int argc, char *argv[]) {
	ClutterTimeline  *timeline;
	ClutterActor     *stage;
	ClutterActor     *texture;
	GstPipeline      *pipeline;
	GstElement	*src;
	GstElement	*demux;
	GstElement	*queue;
	GstElement	*decode;
	GstElement	*sink;

	clutter_init (&argc, &argv);
	gst_init (&argc, &argv);

	stage = clutter_stage_get_default ();

	timeline = clutter_timeline_new (1000);
	g_object_set(timeline, "loop", TRUE, NULL);

	texture = g_object_new (CLUTTER_TYPE_TEXTURE, "disable-slicing", TRUE, NULL);
	clutter_actor_set_size(stage,  1920, 1080);

	pipeline = GST_PIPELINE(gst_pipeline_new ("player"));

	src 	= gst_element_factory_make("filesrc", 		"src"	);
	g_object_set (src, "location", "/server/tmp/movies/simpsons.mp4", NULL);
	demux 	= gst_element_factory_make("qtdemux", 		"demux"	);
	queue 	= gst_element_factory_make("queue", 			"queue"	);
	decode 	= gst_element_factory_make("ffdec_h264", 	"decode");
	sink 	= gst_element_factory_make("cluttersink",	"sink"	);
	g_object_set (sink, "texture", CLUTTER_TEXTURE (texture), "qos", TRUE,	"sync", TRUE, NULL);

	gst_bin_add_many (GST_BIN (pipeline), src, demux, queue, decode, sink, NULL);
	gst_element_link (src, demux);
	gst_element_link_many (queue, decode, sink, NULL);
	g_signal_connect (demux, "pad-added", G_CALLBACK (on_pad_added), queue);

	gst_element_set_state (GST_ELEMENT(pipeline), GST_STATE_PLAYING);

	clutter_timeline_start (timeline);

	clutter_group_add (CLUTTER_GROUP (stage), texture);

	clutter_actor_show_all (stage);

	clutter_main();

	return EXIT_SUCCESS;
}
