Gstreamer locking
Locking for gstreamer operations may help prevent a double free error I was getting.
This commit is contained in:
parent
b000bbf808
commit
356244e3be
|
@ -1,7 +1,9 @@
|
||||||
# Bryan Schumaker (8/7/2010)
|
# Bryan Schumaker (8/7/2010)
|
||||||
|
|
||||||
import libsaria
|
import libsaria
|
||||||
|
from libsaria import threads
|
||||||
|
|
||||||
|
lock = threads.get_mutex("gstreamer")
|
||||||
gst = None
|
gst = None
|
||||||
player = None
|
player = None
|
||||||
time = None
|
time = None
|
||||||
|
@ -13,22 +15,30 @@ def init():
|
||||||
global gst
|
global gst
|
||||||
global player
|
global player
|
||||||
global time
|
global time
|
||||||
|
global lock
|
||||||
import gst
|
import gst
|
||||||
|
|
||||||
|
lock.acquire()
|
||||||
player = gst.element_factory_make("playbin2", "player")
|
player = gst.element_factory_make("playbin2", "player")
|
||||||
set_volume(libsaria.prefs["volume"])
|
set_volume_locked(libsaria.prefs["volume"])
|
||||||
time = gst.Format(gst.FORMAT_TIME)
|
time = gst.Format(gst.FORMAT_TIME)
|
||||||
bus = player.get_bus()
|
bus = player.get_bus()
|
||||||
bus.add_signal_watch()
|
bus.add_signal_watch()
|
||||||
bus.connect("message", on_message)
|
bus.connect("message", on_message)
|
||||||
|
lock.release()
|
||||||
|
|
||||||
|
def reset_locked():
|
||||||
def reset():
|
|
||||||
global player
|
global player
|
||||||
global length
|
global length
|
||||||
player.set_state(gst.STATE_NULL)
|
player.set_state(gst.STATE_NULL)
|
||||||
length = None
|
length = None
|
||||||
|
|
||||||
|
def reset():
|
||||||
|
global lock
|
||||||
|
lock.acquire()
|
||||||
|
reset_locked()
|
||||||
|
lock.release()
|
||||||
|
|
||||||
|
|
||||||
def on_message(bus, message):
|
def on_message(bus, message):
|
||||||
if message.type == gst.MESSAGE_EOS:
|
if message.type == gst.MESSAGE_EOS:
|
||||||
|
@ -36,22 +46,31 @@ def on_message(bus, message):
|
||||||
|
|
||||||
|
|
||||||
def on_quit():
|
def on_quit():
|
||||||
global player
|
global lock
|
||||||
player.set_state(gst.STATE_NULL)
|
lock.acquire()
|
||||||
|
reset_locked()
|
||||||
|
lock.release()
|
||||||
libsaria.event.invite("PREQUIT", on_quit)
|
libsaria.event.invite("PREQUIT", on_quit)
|
||||||
|
|
||||||
|
|
||||||
def get_state():
|
def get_state_locked():
|
||||||
global player
|
global player
|
||||||
return player.get_state()[1]
|
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
|
# Returns the length of the song in ms
|
||||||
def duration():
|
def duration_locked():
|
||||||
global length
|
global length
|
||||||
global player
|
global player
|
||||||
global time
|
global time
|
||||||
if get_state() == gst.STATE_NULL:
|
if get_state_locked() == gst.STATE_NULL:
|
||||||
return 0
|
return 0
|
||||||
if length == None:
|
if length == None:
|
||||||
try:
|
try:
|
||||||
|
@ -60,11 +79,18 @@ def duration():
|
||||||
length = 0
|
length = 0
|
||||||
return float(length)
|
return float(length)
|
||||||
|
|
||||||
|
def duration():
|
||||||
|
global lock
|
||||||
|
lock.acquire()
|
||||||
|
ret = duration_locked()
|
||||||
|
lock.release()
|
||||||
|
return ret
|
||||||
|
|
||||||
def position():
|
|
||||||
|
def position_locked():
|
||||||
global time
|
global time
|
||||||
global player
|
global player
|
||||||
if get_state() == gst.STATE_NULL:
|
if get_state_locked() == gst.STATE_NULL:
|
||||||
return float(0)
|
return float(0)
|
||||||
try:
|
try:
|
||||||
pos = player.query_position(time)[0]
|
pos = player.query_position(time)[0]
|
||||||
|
@ -72,46 +98,91 @@ def position():
|
||||||
pos = 0
|
pos = 0
|
||||||
return float(pos)
|
return float(pos)
|
||||||
|
|
||||||
|
def position():
|
||||||
|
global lock
|
||||||
|
lock.acquire()
|
||||||
|
ret = position_locked()
|
||||||
|
lock.release()
|
||||||
|
return ret
|
||||||
|
|
||||||
def load(file):
|
|
||||||
|
def load_locked(file):
|
||||||
global gst
|
global gst
|
||||||
global player
|
global player
|
||||||
reset()
|
reset_locked()
|
||||||
#player.set_state(gst.STATE_NULL)
|
#player.set_state(gst.STATE_NULL)
|
||||||
player.set_property("uri", "file://%s"%file)
|
player.set_property("uri", "file://%s"%file)
|
||||||
player.set_state(gst.STATE_PAUSED)
|
player.set_state(gst.STATE_PAUSED)
|
||||||
return file
|
return file
|
||||||
|
|
||||||
|
def load(file):
|
||||||
|
global lock
|
||||||
|
lock.acquire()
|
||||||
|
ret = load_locked(file)
|
||||||
|
lock.release()
|
||||||
|
return ret
|
||||||
|
|
||||||
def play():
|
|
||||||
|
def play_locked():
|
||||||
global gst
|
global gst
|
||||||
global player
|
global player
|
||||||
player.set_state(gst.STATE_PLAYING)
|
player.set_state(gst.STATE_PLAYING)
|
||||||
|
|
||||||
|
def play():
|
||||||
|
global lock
|
||||||
|
lock.acquire()
|
||||||
|
play_locked()
|
||||||
|
lock.release()
|
||||||
|
|
||||||
def pause():
|
|
||||||
|
def pause_locked():
|
||||||
global gst
|
global gst
|
||||||
global player
|
global player
|
||||||
player.set_state(gst.STATE_PAUSED)
|
player.set_state(gst.STATE_PAUSED)
|
||||||
|
|
||||||
|
def pause():
|
||||||
|
global lock
|
||||||
|
lock.acquire()
|
||||||
|
pause_locked()
|
||||||
|
lock.release()
|
||||||
|
|
||||||
|
def stop_locked():
|
||||||
|
pause_locked()
|
||||||
|
seek_locked(0)
|
||||||
|
|
||||||
def stop():
|
def stop():
|
||||||
pause()
|
global lock
|
||||||
seek(0)
|
lock.acquire()
|
||||||
|
stop_locked()
|
||||||
|
lock.release()
|
||||||
|
|
||||||
|
|
||||||
def seek(prcnt):
|
def seek_locked(prcnt):
|
||||||
global player
|
global player
|
||||||
global time
|
global time
|
||||||
global gst
|
global gst
|
||||||
try:
|
try:
|
||||||
spot = duration() * prcnt
|
spot = duration_locked() * prcnt
|
||||||
player.seek_simple(time, gst.SEEK_FLAG_FLUSH, spot)
|
player.seek_simple(time, gst.SEEK_FLAG_FLUSH, spot)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def seek(prcnt):
|
||||||
|
global lock
|
||||||
|
lock.acquire()
|
||||||
|
seek_locked(prcnt)
|
||||||
|
lock.release()
|
||||||
|
|
||||||
def set_volume(prcnt):
|
|
||||||
|
def set_volume_locked(prcnt):
|
||||||
global player
|
global player
|
||||||
player.set_property("volume", prcnt)
|
player.set_property("volume", prcnt)
|
||||||
libsaria.prefs["volume"] = prcnt
|
libsaria.prefs["volume"] = prcnt
|
||||||
return prcnt
|
return prcnt
|
||||||
|
|
||||||
|
def set_volume(prcnt):
|
||||||
|
global lock
|
||||||
|
lock.acquire()
|
||||||
|
ret = set_volume_locked(prcnt)
|
||||||
|
lock.release()
|
||||||
|
return ret
|
||||||
|
|
Loading…
Reference in New Issue