dbus-native icon indicating copy to clipboard operation
dbus-native copied to clipboard

Signals in PulseAudio

Open kd496 opened this issue 1 year ago • 0 comments

Hi everyone,

I'm using this module with PulseAudio and it works well except for subscribing to a signal. The code below is annotated with approaches A and B.

A. Throws immediately.

Error: Method "AddMatch" with signature "s" on interface "org.freedesktop.DBus" doesn't exist

    at DBusInterface.<anonymous> (/home/kd/Desktop/bluetooth/samples/node_modules/dbus-native/lib/introspect.js:102:22)

B. It first logs what seems to be the same error that is thrown in A, but apart from that everything is as expected.

{
  serial: 3,
  errorName: 'org.freedesktop.DBus.Error.UnknownMethod',
  replySerial: 3,
  signature: 's',
  type: 3,
  flags: 1,
  body: [
    `Method "AddMatch" with signature "s" on interface "org.freedesktop.DBus" doesn't exist\n`
  ]
}
{
  serial: 4,
  path: '/org/pulseaudio/core1',
  interface: 'org.PulseAudio.Core1',
  member: 'NewSink',
  signature: 'o',
  type: 4,
  flags: 1,
  body: [ '/org/pulseaudio/core1/sink5' ]
}

Could someone explain what is going on here? I wrote a working python equivalent and here's a post it is based on. Might be useful.

My code:

import * as dbus from 'dbus-native';

async function getAddress(): Promise<string | null> {
    const bus = dbus.sessionBus();

    const promise = new Promise<string | null>((resolve, reject) => {
        bus.getService('org.PulseAudio1').getInterface('/org/pulseaudio/server_lookup1', 'org.freedesktop.DBus.Properties', (err, iface) => {
            if (err) {
                reject(err);
            }

            iface.Get('org.PulseAudio.ServerLookup1', 'Address', (err, result) => {
                if (err) {
                    reject(err);
                }

                let address = result[1][0];
                address = address.replace('unix:path=', '')
                resolve(address);
            })
        })
    })

    return promise;
}

async function subscribeSinkEvents(conn: any) {
    conn.getService("org.PulseAudio.Core1").getInterface('/org/pulseaudio/core1',
        'org.PulseAudio.Core1', (err, core) => {
            if (err) {
                throw new Error(err);
            }

            core.ListenForSignal('org.PulseAudio.Core1.NewSink', [], (err, result) => {
                if (err) {
                    console.error(`err: ${err}`);
                }

                // A 
                core.on('NewSink', console.log);
                //


                // B
                conn.connection.on('message', console.log);
                const match = "type='signal',destination='org.PulseAudio.Core1',path='/org/pulseaudio/core1',interface='org.PulseAudio.Core1',member='NewSink'";
                conn.addMatch(match, () => { })
                //
            })

        });
}

const address = await getAddress();

const conn = dbus.createClient({
    socket: address,
    direct: true,
})

subscribeSinkEvents(conn);

kd496 avatar Jun 28 '23 23:06 kd496