CoreAudio Enumerating HAL AudioID's Are Inconsistent

I'm doing the normal 'iterate' process to get a list of available audio devices on Cocoa, and am running into a weird case. Here's a code snippet:

AudioObjectPropertyAddress propertyAddress =
{
    kAudioHardwarePropertyDevices,
    kAudioObjectPropertyScopeGlobal,
    kAudioObjectPropertyElementMaster
};

UInt32 dataSize = 0;
OSStatus status = AudioHardwareServiceGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize);
if(status != kAudioHardwareNoError)
{
    NSLog (@"AudioObjectGetPropertyDataSize (kAudioHardwarePropertyDevices) failed: %i", status);
    return;
}

UInt32 deviceCount = (UInt32)(dataSize / sizeof(AudioDeviceID));

AudioDeviceID *audioDevices = (AudioDeviceID *)(calloc(dataSize,1));
if(NULL == audioDevices)
{
    NSLog (@"Unable to allocate memory");
    return;
}

status = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize, audioDevices);
if(status != kAudioHardwareNoError)
{
    fprintf(stderr, "AudioObjectGetPropertyData (kAudioHardwarePropertyDevices) failed: %in", status);
    free(audioDevices);
    audioDevices = NULL;
    return;
}

CFMutableArrayRef inputDeviceArray = CFArrayCreateMutable(kCFAllocatorDefault, deviceCount, &kCFTypeArrayCallBacks);
if(NULL == inputDeviceArray)
{
    NSLog (@"CFArrayCreateMutable failed");
    free(audioDevices);
    audioDevices = NULL;
    return;
}

// Iterate through all the devices and determine which are input-capable
propertyAddress.mScope = kAudioDevicePropertyScopeInput;
for(UInt32 i = 0; i < deviceCount; ++i)
{
    // Query device UID
    CFStringRef deviceUID = NULL;
    dataSize = sizeof(deviceUID);
    propertyAddress.mSelector = kAudioDevicePropertyDeviceUID;
    status = AudioHardwareServiceGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, &deviceUID);
    if(status != kAudioHardwareNoError)
    {
        NSLog (@"AudioObjectGetPropertyData (kAudioDevicePropertyDeviceUID) failed: %i", status);
        continue;
    }

    // For fun, let's do a reverse lookup for device ID

    AudioDeviceID ourDeviceID;
    UInt32 dataSize = sizeof(ourDeviceID);
    propertyAddress.mSelector = kAudioHardwarePropertyTranslateUIDToDevice;
    // Just for fun, get the default input device and compare results
    status = AudioObjectGetPropertyData(kAudioObjectSystemObject,
                                                 &propertyAddress,
                                                 sizeof(deviceUID),
                                                 &deviceUID,
                                                 &dataSize,
                                                 &ourDeviceID);
    if (kAudioHardwareNoError != status)
    {
        NSLog (@"Can't reverse lookup audio id");
    }

    if (ourDeviceID != audioDevices[i])
        NSLog (@"Reversed audio id %d Not equal to audioDevice[] of %d", ourDeviceID, audioDevices[i]);

I'm doing this to get a list of the hardware device UID's, so that I can select them for use later.

The weird part is that on my old MacPro running El Capitan, the audio device ID's in the input array do not match what is returned by the 'kAudioHardwarePropertyTranslateUIDToDevice' call. On other systems, the ID's do in fact match.

My bottom NSLog function spits out information like this:

Reversed audio id 64 Not equal to audioDevice[] of 68
Reversed audio id 92 Not equal to audioDevice[] of 96
Reversed audio id 85 Not equal to audioDevice[] of 89
Reversed audio id 76 Not equal to audioDevice[] of 80
Reversed audio id 72 Not equal to audioDevice[] of 76
Reversed audio id 56 Not equal to audioDevice[] of 60
Reversed audio id 48 Not equal to audioDevice[] of 52
Reversed audio id 99 Not equal to audioDevice[] of 103
Reversed audio id 96 Not equal to audioDevice[] of 100

This is for my 9 audio devices available on my system. This was causing a huge problem when trying to select an AudioID for my HAL output device, but ever since converting to the 'kAudioHardwarePropertyTranslateUIDToDevice' call, I seem to have fixed the problem.

The real question is: why would these numbers be different? Shouldn't they always return the same number?

链接地址: http://www.djcxy.com/p/60188.html

上一篇: 使用AUHAL音频单元将字节写入音频文件

下一篇: 枚举HAL AudioID的CoreAudio不一致