
#include "audio.h"

bool Audio::get_duration(gint64 &duration)
{
	GstFormat fmt = GST_FORMAT_TIME;
	return gst_element_query_duration(GST_ELEMENT(player),
					  &fmt,
					  &duration);
}

bool Audio::get_position(gint64 &position)
{
	GstFormat fmt = GST_FORMAT_TIME;
	return gst_element_query_position(GST_ELEMENT(player),
					  &fmt,
					  &position);
}

bool Audio::seek_to(double prcnt)
{
	bool ret;
	gint64 duration;

	ret = get_duration(duration);
	if (ret == true) {
		gst_element_seek_simple(GST_ELEMENT(player),
					GST_FORMAT_TIME,
					GST_SEEK_FLAG_FLUSH,
					duration * prcnt);
	}
	return ret;
}

/* Use to seek forward or backward in a song */
bool Audio::seek(int dt)
{
	bool ret;
	gint64 cur_pos;
	gint64 new_pos;

	ret = get_position(cur_pos);
	if (ret == true) {
		/* Convert seconds to nano-seconds */
		new_pos = cur_pos + (dt * GST_SECOND);
		if (new_pos < 0)
			new_pos = 0;
		gst_element_seek_simple(GST_ELEMENT(player),
					GST_FORMAT_TIME,
					GST_SEEK_FLAG_FLUSH,
					new_pos);
	}
	return ret;
}
