Audio: gracefully handle pulse disconnection

Also moved the address_lookup function for readability
This commit is contained in:
Davide Andreoli 2014-08-30 11:13:55 +02:00
parent fce3d8716c
commit 684fb7fc90
1 changed files with 42 additions and 30 deletions

View File

@ -52,8 +52,9 @@ class Gadget(e.Gadget):
super().instance_destroyed(obj)
def speaker_click_cb(self, obj, sig, source):
ch = self.pulse.channels[0]
ch.mute_toggle()
if self.pulse.channels:
ch = self.pulse.channels[0]
ch.mute_toggle()
def speaker_update(self, speaker):
if self.pulse and len(self.pulse.channels) > 0:
@ -62,15 +63,16 @@ class Gadget(e.Gadget):
speaker.message_send(0, (ch.muted, left, right))
def speaker_wheel_cb(self, obj, sig, source):
ch = self.pulse.channels[0]
vol = ch.volume
if sig == 'mouse,wheel,0,1':
new_vol = vol - 3000
elif sig == 'mouse,wheel,0,-1':
new_vol = vol + 3000
else:
return
ch.volume_set(min(max(0, new_vol), 65500))
if self.pulse.channels:
ch = self.pulse.channels[0]
vol = ch.volume
if sig == 'mouse,wheel,0,1':
new_vol = vol - 3000
elif sig == 'mouse,wheel,0,-1':
new_vol = vol + 3000
else:
return
ch.volume_set(min(max(0, new_vol), 65500))
def popup_created(self, popup):
super().popup_created(popup)
@ -348,6 +350,25 @@ class PulseAudio_Client(object):
self.try_to_connect()
def address_lookup(self):
""" Search the address of the pulse dbus socket """
# 1. try the environment var
addr = os.environ.get('PULSE_DBUS_SERVER')
if addr: return addr
# 2. well-known system-wide daemon socket
if os.access('/run/pulse/dbus-socket', os.R_OK | os.W_OK):
return 'unix:path=/run/pulse/dbus-socket'
# 3. dbus lookup on the SessionBus
try:
bus = dbus.SessionBus()
obj = bus.get_object('org.PulseAudio1', '/org/pulseaudio/server_lookup1')
return obj.Get('org.PulseAudio.ServerLookup1', 'Address',
dbus_interface=dbus.PROPERTIES_IFACE)
except:
return None
def try_to_connect(self):
if self.connect() is False:
ecore.Timer(5.0, self.try_to_connect)
@ -363,6 +384,7 @@ class PulseAudio_Client(object):
mainloop=DBusEcoreMainLoop())
except:
return False
self.conn.call_on_disconnection(self.disconnect_cb)
# get all available channels
self.all_channels_add()
@ -386,6 +408,15 @@ class PulseAudio_Client(object):
path_keyword='obj_path')
return True
def disconnect_cb(self, conn):
for ch in self.channels:
_instance.channel_removed(ch)
del ch
self.conn = None
self.srv_addr = None
self.channels = []
ecore.Timer(1.0, self.try_to_connect)
def channel_signal_cb(self, *args, signal):
obj_path = args[0]
if signal == 'NewSink':
@ -411,25 +442,6 @@ class PulseAudio_Client(object):
ch.mute_changed_signal_cb(*args)
break
def address_lookup(self):
""" Search the address of the pulse dbus socket """
# 1. try the environment var
addr = os.environ.get('PULSE_DBUS_SERVER')
if addr: return addr
# 2. well-known system-wide daemon socket
if os.access('/run/pulse/dbus-socket', os.R_OK | os.W_OK):
return 'unix:path=/run/pulse/dbus-socket'
# 3. dbus lookup on the SessionBus
try:
bus = dbus.SessionBus()
obj = bus.get_object('org.PulseAudio1', '/org/pulseaudio/server_lookup1')
return obj.Get('org.PulseAudio.ServerLookup1', 'Address',
dbus_interface=dbus.PROPERTIES_IFACE)
except:
return None
def _fuckyoupulse(self, ay):
return ''.join([ chr(byte) for byte in ay ])