How to get PID from AudioObjectID on macOS pre Sonoma

3

I am working on an application to get when input audio device is being used. Basically I want to know the application using the microphone (built-in or external) This app runs on macOS. For Mac versions starting from Sonoma I can use this code:

int getAudioProcessPID(AudioObjectID process)
     {
       pid_t pid;
       if (@available(macOS 14.0, *)) {
        constexpr AudioObjectPropertyAddress prop {
            kAudioProcessPropertyPID,
            kAudioObjectPropertyScopeGlobal,
            kAudioObjectPropertyElementMain
        };
        UInt32 dataSize = sizeof(pid);
        OSStatus error = AudioObjectGetPropertyData(process, &prop, 0, nullptr, &dataSize, &pid);
        if (error != noErr) {
            return -1;
        }
    } else {
        // Pre sonoma code goes here
    }
    return pid;
}

which works.

However, kAudioProcessPropertyPID was added in macOS SDK 14.0. Does anyone know how to achieve the same functionality on previous versions?

I'm sorry this is off-topic but, I see your code for getting the PID from an AudioObjectID process:

int getAudioProcessPID(AudioObjectID process)

How do you obtain the AudioObjectID process parameter in the first place when an application starts using the microphone?

How to get PID from AudioObjectID on macOS pre Sonoma
 
 
Q