hypfvieh / dbus-java

Improved version of java DBus library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/)
https://hypfvieh.github.io/dbus-java/
MIT License
185 stars 73 forks source link

Cannot connect to pulse bus #3

Closed Silveryard closed 7 years ago

Silveryard commented 7 years ago

I've been trying to connect to the pulseaudio dbus server with this code:

DBusConnection sessionConnection = DBusConnection.getConnection(DBusConnection.SESSION);
DBus.Properties properties = sessionConnection.getRemoteObject("org.pulseaudio.Server", "/org/pulseaudio/server_lookup1", DBus.Properties.class);
String address = properties.Get("org.PulseAudio.ServerLookup1", "Address");
sessionConnection.disconnect();

connection = DBusConnection.getConnection(address);

but it keeps failing with the following exception at the last line:

org.freedesktop.dbus.exceptions.DBusException: Method "Hello" with signature "" on interface "org.freedesktop.DBus" doesn't exist
  at org.freedesktop.dbus.DBusConnection.<init>(DBusConnection.java:374)
  at org.freedesktop.dbus.DBusConnection.getConnection(DBusConnection.java:239)
hypfvieh commented 7 years ago

I'm not sure what's wrong with your code, because it works for me on Ubuntu 16.04 (except of line 6 of your sample because the returned address is not a valid file/unix-socket on the file system).

Could you verify with e.g. 'd-feet' that your used parameters are available on Dbus.

Silveryard commented 7 years ago

Interestingly pulseaudios server lookup seems to return this path even if the dbus server is not active. Make sure the pulses dbus protocol module is loaded. This can be done with 'pacmd load-module module-dbus-protocol'. After this the dbus-socket file should be avaliable

I get the same error with 17.04 and 16.04

hypfvieh commented 7 years ago

Did you try 'd-feet' (apt-get install d-feet)? Is pulseaudio visable/accessable there?

Silveryard commented 7 years ago

When I try to connect to "unix:path=/run/user/1000/pulse/dbus-socket" d-feet just does nothing. The CLI command "dbus-monitor --address "unix:path=/run/user/1000/pulse/dbus-socket"" gives me the same error as stated above.

One application that connects to the pulse dbus is qpaeq. qpaeq is used to control the pulseaudio equalizer and is essentially a python script. I read through the sourcecode and tried it myself. Connecting to the dbus-socket via python works just fine. I suspect that the python dbus library does not call the "Hello()" method when connecting

Edit: The problematic line always is the last one in my sample code

hypfvieh commented 7 years ago

I guess I have found the problematic part. In DBusConnection.getConnection(address), a new DBusConnection instance is created, which will 'self-register' the session to DBus.

This is done by calling dbus = getRemoteObject("org.freedesktop.DBus", "/org/freedesktop/DBus", DBus.class) and afterwards busnames.add(dbus.Hello()).

The call to dbus.Hello() will raise the Exception as described above.

I've added a workaround by adding a new variant of DBusConnection.getConnection which uses 2 parameters, the address and a boolean which will register or not register the session on Dbus.

In case of pulse audio, you have to use DBusConnection.getConnection(address, false) to get a working connection to pulse audio DBus address.

To test this feature, please download the source code and compile it yourself (should be working out-of-the-box using maven). Also update the referenced version in your project to 2.7.3-SNAPSHOT.

If your test is successful, please let me know, so I can create an upstream release on maven central.

After all, I'm not sure if this is a bug in dbus-java or more a bug in pulse audio which is not implementing the 'Hello()' method which seems to be called by most DBus-client application like dbus-monitor or d-feet.

Silveryard commented 7 years ago

This changes allow me to connect to the dbus. But as I cannot find any documentation on what bus names are present on this dbus server may be out of luck calling any functions. The python code in qpaeq does not expect a bus name whereas dbus-java does but neither '' nor '/' work.

You are right. In the end it's a bug in pulse audio.

hypfvieh commented 7 years ago

"" and "/" are wrong. Check the source of qpaeq, they use something like "/org/pulseaudio/equalizing1" and "org.PulseAudio.Ext.Equalizing1.Manager" or "org.PulseAudio.Core1" and "/org/pulseaudio/core1".

So the code could look like this:

public static void main(String[] _args) throws DBusException {
        DBusConnection sessionConnection = DBusConnection.getConnection(DBusConnection.SESSION);

        Properties properties = sessionConnection.getRemoteObject("org.pulseaudio.Server", "/org/pulseaudio/server_lookup1", org.freedesktop.DBus.Properties.class);
        String address =  properties.Get("org.PulseAudio.ServerLookup1", "Address");

        System.out.println("Found address: " + address + "\n");

        sessionConnection.disconnect();

        File file = new File(address.replace("unix:path=", ""));
        if (!file.exists()) {
            System.out.println("Extracted address " + address + " is not a valid file/unix-socket");
        } else {
            DBusConnection connection = DBusConnection.getConnection(address, false);

            Properties core1Props = connection.getRemoteObject("org.PulseAudio.Core1", "/org/pulseaudio/core1", org.freedesktop.DBus.Properties.class);
            System.out.println("PulseAudio Name: " + core1Props.Get("org.PulseAudio.Core1", "Name"));
            System.out.println("PulseAudio Version: " + core1Props.Get("org.PulseAudio.Core1", "Version"));

            System.out.println("-----------------------------------------");
            System.out.println("PulseAudio Hostname: " + core1Props.Get("org.PulseAudio.Core1", "Hostname"));
            System.out.println("PulseAudio Username: " + core1Props.Get("org.PulseAudio.Core1", "Username"));

            connection.disconnect();
        }

This code will print something like:

Found address: unix:path=/run/user/1000/pulse/dbus-socket

PulseAudio Name: pulseaudio
PulseAudio Version: 8.0
-----------------------------------------
PulseAudio Hostname: penguin
PulseAudio Username: hypfvieh

For more details on the DBus specs for PulseAudio, take a look here: https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/Developer/Clients/DBus/

Silveryard commented 7 years ago

Thank you very much :) I think this issue can be closed