]> Repos - portaudio/commitdiff
olivier's device name patch for os x
authorbejayoharen <bejayoharen@0f58301d-fd10-0410-b4af-bbb618454e57>
Sat, 28 Jun 2014 16:28:17 +0000 (16:28 +0000)
committerbejayoharen <bejayoharen@0f58301d-fd10-0410-b4af-bbb618454e57>
Sat, 28 Jun 2014 16:28:17 +0000 (16:28 +0000)
src/hostapi/coreaudio/pa_mac_core.c

index 14632d935d63462d603f44e18ba9c03fbc71d7e8..5e4fa5131b0e16f9646c45c7aeea275ec03f08e6 100644 (file)
@@ -125,83 +125,78 @@ static bool ensureChannelNameSize( int size )
  */
 const char *PaMacCore_GetChannelName( int device, int channelIndex, bool input )
 {
-   struct PaUtilHostApiRepresentation *hostApi;
-   PaError err;
-   OSStatus error;
-   err = PaUtil_GetHostApiRepresentation( &hostApi, paCoreAudio );
-   assert(err == paNoError);
-   if( err != paNoError )
-      return NULL;
-   PaMacAUHAL *macCoreHostApi = (PaMacAUHAL*)hostApi;
-   AudioDeviceID hostApiDevice = macCoreHostApi->devIds[device];
-
-   UInt32 size = 0;
-
-   error = AudioDeviceGetPropertyInfo( hostApiDevice,
-                                       channelIndex + 1,
-                                       input,
-                                       kAudioDevicePropertyChannelName,
-                                       &size,
-                                       NULL );
-   if( error ) {
-      //try the CFString
-      CFStringRef name;
-      bool isDeviceName = false;
-      size = sizeof( name );
-      error = AudioDeviceGetProperty( hostApiDevice,
-                                      channelIndex + 1,
-                                      input,
-                                      kAudioDevicePropertyChannelNameCFString,
-                                      &size,
-                                      &name );
-      if( error ) { //as a last-ditch effort, get the device name. Later we'll append the channel number.
-         size = sizeof( name );
-         error = AudioDeviceGetProperty( hostApiDevice,
-                                      channelIndex + 1,
-                                      input,
-                                      kAudioDevicePropertyDeviceNameCFString,
-                                      &size,
-                                      &name );
-         if( error )
-            return NULL;
-         isDeviceName = true;
-      }
-      if( isDeviceName ) {
-         name = CFStringCreateWithFormat( NULL, NULL, CFSTR( "%@: %d"), name, channelIndex + 1 );
-      }
-
-      CFIndex length = CFStringGetLength(name);
-      while( ensureChannelNameSize( length * sizeof(UniChar) + 1 ) ) {
-         if( CFStringGetCString( name, channelName, channelNameSize, kCFStringEncodingUTF8 ) ) {
-            if( isDeviceName )
-               CFRelease( name );
-            return channelName;
-         }
-         if( length == 0 )
-            ++length;
-         length *= 2;
-      }
-      if( isDeviceName )
-         CFRelease( name );
-      return NULL;
-   }
-
-   //continue with C string:
-   if( !ensureChannelNameSize( size ) )
-      return NULL;
-
-   error = AudioDeviceGetProperty( hostApiDevice,
-                                   channelIndex + 1,
-                                   input,
-                                   kAudioDevicePropertyChannelName,
-                                   &size,
-                                   channelName );
-
-   if( error ) {
-      ERR( error );
-      return NULL;
-   }
-   return channelName;
+       struct PaUtilHostApiRepresentation *hostApi;
+       PaError err;
+       OSStatus error;
+       err = PaUtil_GetHostApiRepresentation( &hostApi, paCoreAudio );
+       assert(err == paNoError);
+       if( err != paNoError )
+               return NULL;
+       PaMacAUHAL *macCoreHostApi = (PaMacAUHAL*)hostApi;
+       AudioDeviceID hostApiDevice = macCoreHostApi->devIds[device];
+       CFStringRef nameRef;
+       
+       /* First try with CFString */
+       UInt32 size = sizeof(nameRef);
+       error = AudioDeviceGetProperty( hostApiDevice,
+                                                                  channelIndex + 1,
+                                                                  input,
+                                                                  kAudioDevicePropertyChannelNameCFString,
+                                                                  &size,
+                                                                  &nameRef );
+       if( error )
+       {
+               /* try the C String */
+               size = 0;
+               error = AudioDeviceGetPropertyInfo( hostApiDevice,
+                                                                                  channelIndex + 1,
+                                                                                  input,
+                                                                                  kAudioDevicePropertyChannelName,
+                                                                                  &size,
+                                                                                  NULL);
+               if( !error )
+               {
+                       if( !ensureChannelNameSize( size ) )
+                               return NULL;
+                       
+                       error = AudioDeviceGetProperty( hostApiDevice,
+                                                                                  channelIndex + 1,
+                                                                                  input,
+                                                                                  kAudioDevicePropertyChannelName,
+                                                                                  &size,
+                                                                                  channelName );
+                       
+                       
+                       if( !error )
+                               return channelName;
+               }
+               
+               /* as a last-ditch effort, we use the device name and append the channel number. */
+               nameRef = CFStringCreateWithFormat( NULL, NULL, CFSTR( "%s: %d"), hostApi->deviceInfos[device]->name, channelIndex + 1 );
+               
+               
+               size = CFStringGetMaximumSizeForEncoding(CFStringGetLength(nameRef), kCFStringEncodingUTF8);;
+               if( !ensureChannelNameSize( size ) )
+               {
+                       CFRelease( nameRef );
+                       return NULL;
+               }
+               CFStringGetCString( nameRef, channelName, size+1, kCFStringEncodingUTF8 );
+               CFRelease( nameRef );
+       }
+       else
+       {
+               size = CFStringGetMaximumSizeForEncoding(CFStringGetLength(nameRef), kCFStringEncodingUTF8);;
+               if( !ensureChannelNameSize( size ) )
+               {
+                       CFRelease( nameRef );
+                       return NULL;
+               }
+               CFStringGetCString( nameRef, channelName, size+1, kCFStringEncodingUTF8 );
+               CFRelease( nameRef );
+       }
+       
+       return channelName;
 }
 
     
@@ -657,6 +652,7 @@ static PaError InitializeDeviceInfo( PaMacAUHAL *auhalHostApi,
     Float64 sampleRate;
     char *name;
     PaError err = paNoError;
+       CFStringRef nameRef;
     UInt32 propSize;
 
     VVDBUG(("InitializeDeviceInfo(): macCoreDeviceId=%ld\n", macCoreDeviceId));
@@ -665,18 +661,37 @@ static PaError InitializeDeviceInfo( PaMacAUHAL *auhalHostApi,
 
     deviceInfo->structVersion = 2;
     deviceInfo->hostApi = hostApiIndex;
-
-    /* Get the device name.  Fail if we can't get it. */
-    err = ERR(AudioDeviceGetPropertyInfo(macCoreDeviceId, 0, 0, kAudioDevicePropertyDeviceName, &propSize, NULL));
-    if (err)
-        return err;
-
-    name = PaUtil_GroupAllocateMemory(auhalHostApi->allocations,propSize);
-    if ( !name )
-        return paInsufficientMemory;
-    err = ERR(AudioDeviceGetProperty(macCoreDeviceId, 0, 0, kAudioDevicePropertyDeviceName, &propSize, name));
+  
+    /* Get the device name using CFString */
+       propSize = sizeof(nameRef);
+    err = ERR(AudioDeviceGetProperty(macCoreDeviceId, 0, 0, kAudioDevicePropertyDeviceNameCFString, &propSize, &nameRef));
     if (err)
-        return err;
+    {
+               /* Get the device name using c string.  Fail if we can't get it. */
+               err = ERR(AudioDeviceGetPropertyInfo(macCoreDeviceId, 0, 0, kAudioDevicePropertyDeviceName, &propSize, NULL));
+               if (err)
+                       return err;
+
+               name = PaUtil_GroupAllocateMemory(auhalHostApi->allocations,propSize+1);
+               if ( !name )
+                       return paInsufficientMemory;
+               err = ERR(AudioDeviceGetProperty(macCoreDeviceId, 0, 0, kAudioDevicePropertyDeviceName, &propSize, name));
+               if (err)
+                       return err;
+       }
+       else
+       {
+               /* valid CFString so we just allocate a c string big enough to contain the data */
+               propSize = CFStringGetMaximumSizeForEncoding(CFStringGetLength(nameRef), kCFStringEncodingUTF8);
+               name = PaUtil_GroupAllocateMemory(auhalHostApi->allocations, propSize+1);
+               if ( !name )
+               {
+                       CFRelease(nameRef);
+                       return paInsufficientMemory;
+               }
+               CFStringGetCString(nameRef, name, propSize+1, kCFStringEncodingUTF8);
+               CFRelease(nameRef);
+       }
     deviceInfo->name = name;
 
     /* Try to get the default sample rate.  Don't fail if we can't get this. */