Re: getting device count breaks when linking to Foundation on 10.6
Re: getting device count breaks when linking to Foundation on 10.6
- Subject: Re: getting device count breaks when linking to Foundation on 10.6
- From: Jeff Moore <email@hidden>
- Date: Tue, 31 Aug 2010 11:42:26 -0700
In 10.6, the HAL was changed to use the main thread's run loop for handling notifications by default. In your program, the main thread's run loop is not getting a chance to run so the notifications are not making it in to the HAL.
To fix this, you need to either task the main thread's run loop or tell the HAL to use a different thread for notifications. The following snippet will accomplish the latter by telling the HAL to create it's own thread to manage notifications:
CFRunLoopRef theRunLoop = NULL;
AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
AudioObjectSetPropertyData(kAudioObjectSystemObject, &theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
You can drop this into your program as the first thing that the main() does.
--
Jeff Moore
Core Audio
Apple
On Aug 31, 2010, at 10:32 AM, Simmons, Aaron wrote:
> I have a mixed C++/Objective-C project that uses AudioObjectGetPropertyDataSize to get the number of audio devices (such as USB headsets) plugged in. This API doesn't seem to work under certain conditions. For example, on 10.5 it will work but on 10.6 it won't detect when a new USB headset is plugged in.
>
> I've pared down the problem to a small bit of code that reproduces the problem (it calls AudioObjectGetPropertyDataSize in a loop). The code will work on 10.6 (ie, it will detect when devices are plugged/unplugged) when its only linked against CoreAudio, but once you link against Foundation *it will stop working*.
>
> I don't understand how linking to a framework can break code that otherwise works.
>
> Here is the code (coreaudio-test.cpp):
>
> #include <stdio.h>
> #include <CoreAudio/AudioHardware.h>
>
> int main(int argc, char **argv) {
> printf("Press <enter> to refresh device list> \n");
> while (1) {
> getchar();
>
> // get device count
> UInt32 dataSize = 0;
> AudioObjectPropertyAddress propertyAddress;
> propertyAddress.mSelector = kAudioHardwarePropertyDevices;
> propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
> propertyAddress.mElement = kAudioObjectPropertyElementMaster;
> OSStatus result =
> AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize);
>
> int count = -1;
> if (result == noErr) {
> count = dataSize / sizeof(AudioDeviceID);
> }
> printf("num devices= %d \n", count);
> }
>
> return 0;
> }
>
> And here is the Makefile:
> LFLAGS= -framework CoreAudio
>
> all: coreaudio-test coreaudio-test.broken
>
> # create a test that works
> coreaudio-test: coreaudio-test.cpp
> g++ -o $@ $^ $(LFLAGS)
>
> # linking to foundation will break the test
> coreaudio-test.broken: coreaudio-test.cpp
> g++ -o $@ $^ $(LFLAGS) -framework Foundation
>
>
> Any thoughts on this bizarre behavior?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden