// gcc -Wall -o test test.c `pkg-config --libs --cflags gtk+-2.0 gstreamer-0.10`

#define TIMEOUT 3

#include <gst/gst.h>
#include <gtk/gtk.h>
#include <gio/gio.h>

static char *uri = NULL;

static gboolean
notify_volume_idle_cb (GstElement *audiosink)
{
	gdouble vol;

	g_object_get (G_OBJECT (audiosink), "volume", &vol, NULL);
	g_message ("notify_volume_idle_cb %lf", vol);

	return FALSE;
}

static void
notify_volume_cb (GObject             *object,
		  GParamSpec          *pspec,
		  GstElement          *audiosink)
{
	g_idle_add ((GSourceFunc) notify_volume_idle_cb, audiosink);
}

static gboolean
next_file (GstElement *playbin)
{
	gst_element_set_state (playbin, GST_STATE_NULL);
	g_object_set (G_OBJECT (playbin), "uri", uri, NULL);
	gst_element_set_state (playbin, GST_STATE_PLAYING);

	return TRUE;
}

int main (int argc, char **argv)
{
	GstElement *playbin, *audiosink, *videosink;
	GFile *file;

	g_set_application_name ("Totem Movie Player");
	gtk_window_set_default_icon_name ("totem");
	g_setenv("PULSE_PROP_media.role", "video", TRUE);

	g_thread_init (NULL);
	gtk_init (&argc, &argv);
	gst_init (&argc, &argv);

	file = g_file_new_for_commandline_arg (argv[1]);
	uri = g_file_get_uri (file);
	g_object_unref (file);

	audiosink = gst_element_factory_make ("pulsesink", "audio-sink");
	g_signal_connect (G_OBJECT (audiosink), "notify::volume",
			  G_CALLBACK (notify_volume_cb), audiosink);

	videosink = gst_element_factory_make ("xvimagesink", "video-sink");
	playbin = gst_element_factory_make ("playbin", "play");
	g_object_set (playbin, "audio-sink", audiosink, "video-sink", videosink, NULL);

	g_timeout_add_seconds (TIMEOUT, (GSourceFunc) next_file, playbin);

	gst_element_set_state (playbin, GST_STATE_NULL);
	g_object_set (G_OBJECT (playbin), "uri", uri, NULL);
	gst_element_set_state (playbin, GST_STATE_PLAYING);

	gtk_main ();

	g_free (uri);

	return 0;
}

