# Bryan Schumaker (8/7/2010)

import libsaria
from libsaria import threads

lock   = threads.get_mutex("gstreamer")
gst    = None
player = None
time   = None
bus    = None
length = None

# An init function for faster startup
def init():
	global gst
	global player
	global time
	global lock
	import gst

	lock.acquire()
	player = gst.element_factory_make("playbin2", "player")
	set_volume_locked(libsaria.prefs.get_pref("libsaria.audio.volume"))
	time = gst.Format(gst.FORMAT_TIME)
	bus = player.get_bus()
	bus.add_signal_watch()
	bus.connect("message", on_message)
	lock.release()

def reset_locked():
	global player
	global length
	player.set_state(gst.STATE_NULL)
	length = None

def reset():
	global lock
	lock.acquire()
	reset_locked()
	lock.release()


def on_message(bus, message):
	if message.type == gst.MESSAGE_EOS:
		libsaria.audio.end_of_song()


def on_quit():
	global lock
	lock.acquire()
	reset_locked()
	lock.release()
libsaria.event.invite("PREQUIT", on_quit)


def get_state_locked():
	global player
	return player.get_state()[1]

def get_state():
	global lock
	lock.acquire()
	state = get_state_locked()
	lock.release()
	return state


# Returns the length of the song in ms
def duration_locked():
	global length
	global player
	global time
	if get_state_locked() == gst.STATE_NULL:
		return 0
	if length == None:
		try:
			length = player.query_duration(time)[0]
		except:
			length = 0
	return float(length)

def duration():
	global lock
	lock.acquire()
	ret = duration_locked()
	lock.release()
	return ret


def position_locked():
	global time
	global player
	if get_state_locked() == gst.STATE_NULL:
		return float(0)
	try:
		pos = player.query_position(time)[0]
	except:
		pos = 0
	return float(pos)

def position():
	global lock
	lock.acquire()
	ret = position_locked()
	lock.release()
	return ret


def load_locked(file):
	global gst
	global player
	reset_locked()
	#player.set_state(gst.STATE_NULL)
	player.set_property("uri", "file://%s"%file)
	player.set_state(gst.STATE_PAUSED)
	return file

def load(file):
	global lock
	lock.acquire()
	ret = load_locked(file)
	lock.release()
	return ret


def play_locked():
	global gst
	global player
	player.set_state(gst.STATE_PLAYING)
	return get_state_locked() == gst.STATE_PLAYING

def play():
	global lock
	lock.acquire()
	ret = play_locked()
	lock.release()
	return ret


def pause_locked():
	global gst
	global player
	player.set_state(gst.STATE_PAUSED)
	return get_state_locked() == gst.STATE_PAUSED

def pause():
	global lock
	lock.acquire()
	ret = pause_locked()
	lock.release()
	return ret

def stop_locked():
	pause_locked()
	seek_locked(0)
	return get_state_locked() == gst.STATE_PAUSED

def stop():
	global lock
	lock.acquire()
	ret = stop_locked()
	lock.release()
	return ret


def seek_locked(prcnt):
	global player
	global time
	global gst
	try:
		spot = duration_locked() * prcnt
		player.seek_simple(time, gst.SEEK_FLAG_FLUSH, spot)
	except:
		pass

def seek(prcnt):
	global lock
	lock.acquire()
	seek_locked(prcnt)
	lock.release()


def set_volume_locked(prcnt):
	global player
	player.set_property("volume", prcnt)
	libsaria.prefs.set_pref("libsaria.audio.volume", prcnt)
	return prcnt

def set_volume(prcnt):
	global lock
	lock.acquire()
	ret = set_volume_locked(prcnt)
	lock.release()
	return ret
