Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/volume control 2 #255

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions pulseaudio_dlna/pulseaudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def __str__(self):
self.name,
self.icon,
self.binary
)
)


class PulseModuleFactory(PulseBaseFactory):
Expand Down Expand Up @@ -374,6 +374,22 @@ def set_as_default_sink(self):
]
subprocess.check_output(cmd)

def remember_stream_volumes(self):
self.volumes = {}
for stream in self.streams:
self.volumes[stream.index] = stream.volumes

def restore_stream_volumes(self):
if hasattr(self, 'volumes'):
for index, volumes in self.volumes.items():
cmd = [
'pactl',
'set-sink-input-volume',
str(index),
str(volumes[0]),
]
subprocess.check_output(cmd)

def switch_streams_to_fallback_source(self):
if self.fallback_sink is not None:
for stream in self.streams:
Expand Down Expand Up @@ -417,6 +433,9 @@ def new(self, bus, stream_path):
device=unicode(obj.Get(
'org.PulseAudio.Core1.Stream', 'Device')),
client=PulseClientFactory.new(bus, client_path),
volumes=[
int(volume) for volume in obj.Get(
'org.PulseAudio.Core1.Stream', 'Volume')],
)
except dbus.exceptions.DBusException:
logger.debug(
Expand All @@ -430,7 +449,7 @@ class PulseStream(object):

__shared_state = {}

def __init__(self, object_path, index, device, client):
def __init__(self, object_path, index, device, client, volumes):
if object_path not in self.__shared_state:
self.__shared_state[object_path] = {}
self.__dict__ = self.__shared_state[object_path]
Expand All @@ -439,6 +458,7 @@ def __init__(self, object_path, index, device, client):
self.index = index
self.device = device
self.client = client
self.volumes = volumes

def switch_to_source(self, index):
cmd = [
Expand All @@ -449,6 +469,15 @@ def switch_to_source(self, index):
]
subprocess.check_output(cmd)

def set_volume(self, volume):
cmd = [
'pactl',
'set-sink-input-volume',
str(self.index),
str(volume),
]
subprocess.check_output(cmd)

def __eq__(self, other):
return self.object_path == other.object_path

Expand All @@ -457,11 +486,12 @@ def __gt__(self, other):

def __str__(self):
return '<PulseStream path="{}" device="{}" index="{}" ' \
'client="{}">'.format(
'client="{}" volumes="{}">'.format(
self.object_path,
self.device,
self.index,
self.client.index if self.client else None,
','.join([unicode(v) for v in self.volumes]),
)


Expand Down Expand Up @@ -524,6 +554,8 @@ def run(self):
self.on_fallback_sink_updated),
('DeviceUpdated', 'org.PulseAudio.Core1.Stream.{}',
self.on_device_updated),
('VolumeUpdated', 'org.PulseAudio.Core1.Stream.{}',
self.on_volume_updated),
)
self._connect(signals)
self.update()
Expand Down Expand Up @@ -567,6 +599,7 @@ def share_bridges(self):
def cleanup(self):
for bridge in self.bridges:
logger.info('Remove "{}" sink ...'.format(bridge.sink.name))
bridge.sink.restore_stream_volumes()
self.delete_null_sink(bridge.sink.module.index)
self.bridges = []

Expand Down Expand Up @@ -639,6 +672,11 @@ def on_device_updated(self, sink_path):
self.update()
self._delayed_handle_sink_update(sink_path)

def on_volume_updated(self, volumes):
logger.info('on_volume_updated "{volumes}"'.format(
volumes=[int(v) for v in volumes]))
self.update()

def on_fallback_sink_updated(self, sink_path):
self.default_sink = PulseSinkFactory.new(self.bus, sink_path)
self.update()
Expand All @@ -664,6 +702,10 @@ def on_playback_stream_removed(self, stream_path):
return

def _delayed_handle_sink_update(self, sink_path):
for bridge in self.bridges:
if len(bridge.sink.streams) == 0:
bridge.sink.restore_stream_volumes()

if self.signal_timers.get(sink_path, None):
GObject.source_remove(self.signal_timers[sink_path])
self.signal_timers[sink_path] = GObject.timeout_add(
Expand Down Expand Up @@ -729,6 +771,9 @@ def __handle_sink_update(self, sink_path):
logger.info(
'The device "{}" is playing.'.format(
bridge.device.label))
bridge.sink.remember_stream_volumes()
for stream in bridge.sink.streams:
stream.set_volume(65536)
else:
if not message:
message = 'Unknown reason.'
Expand Down