
#include <libsaria/audio.h>
#include <libsaria/callback.h>
#include "audio.h"

static string cur_file;

static bool change_state(GstState new_state)
{
	GstStateChangeReturn ret;
	ret = gst_element_set_state(
			GST_ELEMENT(get_player()),
			new_state
	);

	switch(ret) {
		case GST_STATE_CHANGE_SUCCESS:
		case GST_STATE_CHANGE_ASYNC:
			return true;
		default:
			return false;
	}
}

static void reset()
{
	cur_file = "";
	change_state(GST_STATE_NULL);
}

namespace libsaria
{

	void audio::play()
	{
		if (change_state(GST_STATE_PLAYING))
			trigger_callback(PLAY);
	}

	void audio::pause()
	{
		if (change_state(GST_STATE_PAUSED))
			trigger_callback(PAUSE);
	}

	void audio::stop()
	{
		pause();
		seek_to(0);
	}

	void audio::load(string file)
	{
		string uri = "file://" + file;
		reset();
		cur_file = file;
		g_object_set(G_OBJECT(get_player()), "uri", uri.c_str(), NULL);
		trigger_callback(TRACK_LOADED);
		play();
	}

	string audio::get_current_file()
	{
		return cur_file;
	}

};
