Skip to content

Instantly share code, notes, and snippets.

@r10r
Last active December 16, 2015 20:59
Show Gist options
  • Save r10r/5496667 to your computer and use it in GitHub Desktop.
Save r10r/5496667 to your computer and use it in GitHub Desktop.
pulseaudio dbus
require 'dbus'
class DBus::Pulseaudio
attr_accessor :socket_path, :connection
OBJECTS = {
:core => ['org.PulseAudio.Core1','/org/pulseaudio/core1']
}
def socket_path
@socket_path ||= get_socket_path
end
def connection
@connection ||= get_connection
end
def get_socket_path
session_bus = DBus.session_bus
pulseaudio_service = session_bus.service 'org.PulseAudio1'
pulseaudio = pulseaudio_service.object '/org/pulseaudio/server_lookup1'
pulseaudio.introspect
server_lookup = pulseaudio['org.PulseAudio.ServerLookup1']
address = server_lookup['Address']
address.split('=').last
end
def get_connection
connection = DBus::Connection.new("unix:path=#{socket_path}")
connection.connect
connection
end
alias :bus :connection
def object(interface, path)
service = connection.service(interface)
object = service.object(path)
object.default_iface = interface
object.introspect
object
end
def interface(interface, path)
object(interface,path)[interface]
end
#def method_missing
# retrieve and cache objects
#end
def core
object('org.PulseAudio.Core1','/org/pulseaudio/core1')
end
def get_sources
core['Sources']
end
end
require_relative './dbus_pulseaudio'
pulseaudio = DBus::Pulseaudio.new
# pulseaudio by default does not send any signals
pulseaudio.core.ListenForSignal('org.PulseAudio.Core1.Device.StateUpdated', [])
match_rule = DBus::MatchRule.new
match_rule.member = 'StateUpdated'
match_rule.interface = 'org.PulseAudio.Core1.Device'
pulseaudio.bus.add_match(match_rule) do |state|
puts "YEAH - A Device state changed to #{state}"
end
loop = DBus::Main.new
loop << pulseaudio.connection
loop.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment