-
Notifications
You must be signed in to change notification settings - Fork 57
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
add basic pulseaudio support #221
Open
lrusak
wants to merge
1
commit into
LibreELEC:master
Choose a base branch
from
lrusak:pulseaudio-new
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# SPDX-License-Identifier: GPL-2.0 | ||
# Copyright (C) 2020-present Team LibreELEC | ||
|
||
import dbus_utils | ||
import dbussy | ||
import ravel | ||
|
||
BUS_NAME = 'org.pulseaudio.Server' | ||
PATH_PULSEAUDIO_CORE = '/org/pulseaudio/core1' | ||
INTERFACE_PULSEAUDIO_CORE = 'org.PulseAudio.Core1' | ||
INTERFACE_PULSEAUDIO_CARD = 'org.PulseAudio.Core1.Card' | ||
INTERFACE_PULSEAUDIO_CARDPROFILE = 'org.PulseAudio.Core1.CardProfile' | ||
INTERFACE_PULSEAUDIO_DEVICE = 'org.PulseAudio.Core1.Device' | ||
|
||
def core_get_property(name): | ||
return call_method(BUS_NAME, PATH_PULSEAUDIO_CORE, dbussy.DBUS.INTERFACE_PROPERTIES, 'Get', INTERFACE_PULSEAUDIO_CORE, name) | ||
|
||
def core_set_property(name, value): | ||
return call_method(BUS_NAME, PATH_PULSEAUDIO_CORE, dbussy.DBUS.INTERFACE_PROPERTIES, 'Set', INTERFACE_PULSEAUDIO_CORE, name, value) | ||
|
||
def core_set_fallback_sink(sink): | ||
return core_set_property('FallbackSink', (dbussy.DBUS.Signature('o'), sink)) | ||
|
||
def card_get_properties(path): | ||
return call_method(BUS_NAME, path, dbussy.DBUS.INTERFACE_PROPERTIES, 'GetAll', INTERFACE_PULSEAUDIO_CARD) | ||
|
||
def card_get_property(path, name): | ||
return call_method(BUS_NAME, path, dbussy.DBUS.INTERFACE_PROPERTIES, 'Get', INTERFACE_PULSEAUDIO_CARD, name) | ||
|
||
def card_set_property(path, name, value): | ||
return call_method(BUS_NAME, path, dbussy.DBUS.INTERFACE_PROPERTIES, 'Set', INTERFACE_PULSEAUDIO_CARD, name, value) | ||
|
||
def card_set_active_profile(path, profile): | ||
return card_set_property(path, "ActiveProfile", (dbussy.DBUS.Signature('o'), profile)) | ||
|
||
def profile_get_property(path, name): | ||
return call_method(BUS_NAME, path, dbussy.DBUS.INTERFACE_PROPERTIES, 'Get', INTERFACE_PULSEAUDIO_CARDPROFILE, name) | ||
|
||
def sink_get_property(path, name): | ||
return call_method(BUS_NAME, path, dbussy.DBUS.INTERFACE_PROPERTIES, 'Get', INTERFACE_PULSEAUDIO_DEVICE, name) | ||
|
||
def system_has_pulseaudio(): | ||
return conn is not None | ||
|
||
def call_method(bus_name, path, interface, method_name, *args, **kwargs): | ||
interface = BUS[bus_name][path].get_interface(interface) | ||
method = getattr(interface, method_name) | ||
result = method(*args, **kwargs) | ||
first = next(iter(result or []), None) | ||
return dbus_utils.convert_from_dbussy(first) | ||
|
||
try: | ||
conn = dbussy.Connection.open('unix:path=/var/run/pulse/dbus-socket', private=False) | ||
conn.bus_unique_name = 'PulseAudio' | ||
BUS = ravel.Connection(conn) | ||
except Exception as e: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
# SPDX-License-Identifier: GPL-2.0 | ||
# Copyright (C) 2021-present Team LibreELEC (https://libreelec.tv) | ||
|
||
import dbus_pulseaudio | ||
import log | ||
import modules | ||
import oe | ||
import xbmcgui | ||
import dbussy | ||
|
||
class pulseaudio(modules.Module): | ||
|
||
menu = {'7': { | ||
'name': 32500, | ||
'menuLoader': 'menu_connections', | ||
'listTyp': 'palist', | ||
'InfoText': 32501, | ||
}} | ||
ENABLED = False | ||
PULSEAUDIO_DAEMON = None | ||
|
||
@log.log_function() | ||
def __init__(self, oeMain): | ||
super().__init__() | ||
self.visible = False | ||
self.listItems = {} | ||
|
||
@log.log_function() | ||
def do_init(self): | ||
self.visible = True | ||
|
||
@log.log_function() | ||
def start_service(self): | ||
pass | ||
|
||
@log.log_function() | ||
def stop_service(self): | ||
pass | ||
|
||
@log.log_function() | ||
def exit(self): | ||
self.clear_list() | ||
self.visible = False | ||
|
||
# ################################################################### | ||
# # Pulseaudio Core | ||
# ################################################################### | ||
|
||
@log.log_function() | ||
def get_sinks(self): | ||
sinks = {} | ||
for sink in dbus_pulseaudio.core_get_property('Sinks'): | ||
sinks[sink] = {} | ||
try: | ||
sinks[sink]['Card'] = dbus_pulseaudio.sink_get_property(sink, 'Card') | ||
except dbussy.DBusError: | ||
pass | ||
sinks[sink]['Driver'] = dbus_pulseaudio.sink_get_property(sink, 'Driver') | ||
sinks[sink]['Name'] = dbus_pulseaudio.sink_get_property(sink, 'Name') | ||
sinks[sink]['PropertyList'] = dbus_pulseaudio.sink_get_property(sink, 'PropertyList') | ||
|
||
return sinks | ||
|
||
# ################################################################### | ||
# # Menu functions | ||
# ################################################################### | ||
|
||
@log.log_function() | ||
def set_fallback_sink(self, listItem=None): | ||
if listItem is None: | ||
listItem = oe.winOeMain.getControl(oe.listObject['palist']).getSelectedItem() | ||
if listItem is None: | ||
return | ||
|
||
sink = listItem.getProperty('entry') | ||
dbus_pulseaudio.core_set_fallback_sink(sink) | ||
|
||
@log.log_function() | ||
def change_profile(self, listItem=None): | ||
if listItem is None: | ||
listItem = oe.winOeMain.getControl(oe.listObject['palist']).getSelectedItem() | ||
if listItem is None: | ||
return | ||
|
||
card = listItem.getProperty('Card') | ||
profiles = dbus_pulseaudio.card_get_property(card, 'Profiles') | ||
activeProfile = dbus_pulseaudio.card_get_property(card, 'ActiveProfile') | ||
|
||
items = [] | ||
|
||
# we only want to list the available profiles | ||
profiles = [profile for profile in profiles if dbus_pulseaudio.profile_get_property(profile, 'Available') == 1] | ||
items = [dbus_pulseaudio.profile_get_property(profile, 'Description') for profile in profiles] | ||
|
||
try: | ||
active = profiles.index(activeProfile) | ||
except ValueError: | ||
active = 0 | ||
|
||
select_window = xbmcgui.Dialog() | ||
title = oe._(32502) | ||
result = select_window.select(title, items, preselect=active) | ||
if result >= 0: | ||
dbus_pulseaudio.card_set_active_profile(card, profiles[result]) | ||
|
||
# ################################################################### | ||
# # Pulseaudio GUI | ||
# ################################################################### | ||
|
||
@log.log_function() | ||
def clear_list(self): | ||
remove = [entry for entry in self.listItems] | ||
for entry in remove: | ||
del self.listItems[entry] | ||
|
||
@log.log_function() | ||
def menu_connections(self, focusItem=None): | ||
if not hasattr(oe, 'winOeMain'): | ||
return 0 | ||
if not oe.winOeMain.visible: | ||
return 0 | ||
if not dbus_pulseaudio.system_has_pulseaudio(): | ||
oe.winOeMain.getControl(1601).setLabel(oe._(32503)) | ||
self.clear_list() | ||
oe.winOeMain.getControl(int(oe.listObject['palist'])).reset() | ||
oe.dbg_log('pulseaudio::menu_connections', 'exit_function (PA Disabled)', oe.LOGDEBUG) | ||
return | ||
oe.winOeMain.getControl(1601).setLabel(oe._(32504)) | ||
dictProperties = {} | ||
|
||
# type 1=int, 2=string | ||
|
||
properties = [ | ||
{ | ||
'type': 2, | ||
'value': 'Driver', | ||
}, | ||
{ | ||
'type': 2, | ||
'value': 'Card', | ||
}, | ||
] | ||
|
||
rebuildList = 0 | ||
self.dbusDevices = self.get_sinks() | ||
for dbusDevice in self.dbusDevices: | ||
rebuildList = 1 | ||
oe.winOeMain.getControl(int(oe.listObject['palist'])).reset() | ||
self.clear_list() | ||
break | ||
|
||
fallbackSink = dbus_pulseaudio.core_get_property('FallbackSink') | ||
|
||
for dbusDevice in self.dbusDevices: | ||
dictProperties = {} | ||
sinkName = '' | ||
dictProperties['entry'] = dbusDevice | ||
dictProperties['modul'] = self.__class__.__name__ | ||
dictProperties['action'] = 'open_context_menu' | ||
dictProperties['FallbackSink'] = '0' | ||
|
||
# find the card (if available) and active profile (if available) | ||
if 'Card' in self.dbusDevices[dbusDevice]: | ||
cardPath = self.dbusDevices[dbusDevice]['Card'] | ||
cardProperties = dbus_pulseaudio.card_get_properties(cardPath) | ||
|
||
if 'ActiveProfile' in cardProperties: | ||
activeProfile = cardProperties['ActiveProfile'] | ||
dictProperties['ActiveProfileName'] = dbus_pulseaudio.profile_get_property(activeProfile, 'Name') | ||
|
||
# check if the sink is the FallbackSink (for indication) | ||
if fallbackSink is not None and dbusDevice == fallbackSink: | ||
dictProperties['FallbackSink'] = '1' | ||
|
||
if 'PropertyList' in self.dbusDevices[dbusDevice]: | ||
if 'device.description' in self.dbusDevices[dbusDevice]['PropertyList']: | ||
sinkName = bytearray(self.dbusDevices[dbusDevice]['PropertyList']['device.description']).decode().strip('\x00') | ||
|
||
# fallback to the ugly name | ||
if sinkName == '': | ||
sinkName = self.dbusDevices[dbusDevice]['Name'] | ||
|
||
for prop in properties: | ||
name = prop['value'] | ||
if name in self.dbusDevices[dbusDevice]: | ||
value = self.dbusDevices[dbusDevice][name] | ||
if prop['type'] == 1: | ||
value = str(int(value)) | ||
if prop['type'] == 2: | ||
value = str(value) | ||
if prop['type'] == 3: | ||
value = str(len(value)) | ||
if prop['type'] == 4: | ||
value = str(int(value)) | ||
dictProperties[name] = value | ||
if rebuildList == 1: | ||
self.listItems[dbusDevice] = oe.winOeMain.addConfigItem(sinkName, dictProperties, oe.listObject['palist']) | ||
else: | ||
if self.listItems[dbusDevice] != None: | ||
self.listItems[dbusDevice].setLabel(sinkName) | ||
for dictProperty in dictProperties: | ||
self.listItems[dbusDevice].setProperty(dictProperty, dictProperties[dictProperty]) | ||
|
||
@log.log_function() | ||
def open_context_menu(self, listItem): | ||
values = {} | ||
if listItem is None: | ||
listItem = oe.winOeMain.getControl(oe.listObject['palist']).getSelectedItem() | ||
if listItem.getProperty('ActiveProfileName') != '': | ||
values[1] = { | ||
'text': oe._(32505), | ||
'action': 'change_profile', | ||
} | ||
if listItem.getProperty('FallbackSink') != '1': | ||
values[2] = { | ||
'text': oe._(32508), | ||
'action': 'set_fallback_sink', | ||
} | ||
items = [] | ||
actions = [] | ||
for key in list(values.keys()): | ||
items.append(values[key]['text']) | ||
actions.append(values[key]['action']) | ||
select_window = xbmcgui.Dialog() | ||
title = oe._(32012) | ||
result = select_window.select(title, items) | ||
if result >= 0: | ||
getattr(self, actions[result])(listItem) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ | |
'list': 1100, | ||
'netlist': 1200, | ||
'btlist': 1300, | ||
'palist': 1600, | ||
'other': 1900, | ||
'test': 900, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose if this loop? From the looks of it this block is executed once if
self.dbusDevices
is not empty so it can be replaced withif self.dbusDevices:
condition. Or there is something I'm missing? I saw the same code in bluetooth module and wonder why it's implemented this way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not really sure what the question is. The loop iterates over a list of items returned from get_sinks. Then each item can be processed and added to the gui.
Is there a better way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing is that this specific loop does not iterate as such. It breaks unconditionally after the first iteration. So I'm just curious if this has any specific meaning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I didn't look at the rest of the code, yes an if is probably fine here.