#include <gst/gst.h>
#include <stdlib.h>
#include <string.h>


typedef struct _App App;

struct _App
{
  GstElement *playbin;

  GMainLoop *loop;
};

App s_app;

static gboolean dot (gpointer data)
{
	App *app = &s_app;
	
	GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(app->playbin),GST_DEBUG_GRAPH_SHOW_ALL,"playbin_pipelinegraph");
	g_print ("dot snapshot of pipeline taken\n");
}

int
main (int argc, char *argv[])
{
	App *app = &s_app;
	GstBus *bus;
		const gchar *nano_str;
		guint major, minor, micro, nano;
		int flags;
	gst_init (&argc, &argv);
	
	if (argc < 2) {
		g_print ("usage: %s <uri>\n", argv[0]);
		return -1;
	}

	gst_version (&major, &minor, &micro, &nano);
	
	if (nano == 1)
		nano_str = "(CVS)";
	else if (nano == 2)
		nano_str = "(Prerelease)";
	else
		nano_str = "";
	
	g_print ("%s is linked against GStreamer %d.%d.%d %s for dreambox 8000\n",
		argv[0], major, minor, micro, nano_str);
		
	/* create a mainloop to get messages */
	app->loop = g_main_loop_new (NULL, TRUE);
	
	app->playbin = gst_element_factory_make ("playbin2", NULL);
	g_assert (app->playbin);
	
	/* set to read from appsrc */
	g_object_set (app->playbin, "uri", argv[1], NULL);
// 	flags = 0x03; // ( == GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO )
// 	g_object_set (G_OBJECT (app->playbin), "flags", flags, NULL);
// 	g_object_set (G_OBJECT (app->playbin), "audio-sink", app->audiosink, NULL);
// 	g_object_set (G_OBJECT (app->playbin), "video-sink", app->videosink, NULL);
	
	/* go to playing and wait in a mainloop. */
// 	mtrace();

	g_timeout_add (2000, dot, NULL);

	gst_element_set_state (app->playbin, GST_STATE_PLAYING);	

	/* this mainloop is stopped when we receive an error or EOS */
	g_main_loop_run (app->loop);
		
	g_message ("stopping");	

	gst_element_set_state (app->playbin, GST_STATE_NULL);
	
	g_main_loop_unref (app->loop);
// 	muntrace();
	return 0;
}
