andrewrk / libsoundio

C library for cross-platform real-time audio input and output
http://libsound.io/
MIT License
1.94k stars 230 forks source link

Always false on CFStringGetCString on MacOS #157

Closed qiukeren closed 5 years ago

qiukeren commented 7 years ago

it seems always return a error here:

(System is MacOS, Mac OS X 10.12.5)

coreaudio.c

// Possible errors:
//  * SoundIoErrorNoMem
//  * SoundIoErrorEncodingString
static int from_cf_string(CFStringRef string_ref, char **out_str, int *out_str_len) {
    assert(string_ref);
    CFIndex length = CFStringGetLength(string_ref);
    CFIndex max_size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
    char *buf = ALLOCATE_NONZERO(char, max_size);
    if (!buf)
        return SoundIoErrorNoMem;
//    comment it then the demo of list devices works well other wise always return SoundIoErrorEncodingString error
//    if (!CFStringGetCString(string_ref, buf, max_size, kCFStringEncodingUTF8)) {
//        free(buf);
//        return SoundIoErrorEncodingString;
//    }

    *out_str = buf; 
    *out_str_len = strlen(buf);
    return 0;
}
qiukeren commented 7 years ago

It seems like a MacOS's bug, but i wonder if there is any to avoid this, like just uncomment this lines, or change buffer size (even it is maximum size)

ruurdadema commented 7 years ago
qiukeren commented 7 years ago

how to reproduce:

just cmake this project, and then the latency/sin demo shows it:

TERMINAL $ cmake .
Configuring libsoundio version 1.1.0
-- Could NOT find JACK (missing:  JACK_LIBRARY JACK_INCLUDE_DIR HAVE_jack_set_port_rename_callback) 
-- Could NOT find PULSEAUDIO (missing:  PULSEAUDIO_LIBRARY PULSEAUDIO_INCLUDE_DIR) 
-- Could NOT find ALSA (missing:  ALSA_LIBRARY ALSA_INCLUDE_DIR) 
-- Could NOT find WASAPI (missing:  AUDIOCLIENT_H) 

Installation Summary
--------------------
* Install Directory            : /usr/local
* Build Type                   : Debug
* Build static libs            : ON
* Build examples               : ON
* Build tests                  : ON

System Dependencies
-------------------
* threads                      : OK
* JACK       (optional)        : not found
* PulseAudio (optional)        : not found
* ALSA       (optional)        : not found
* CoreAudio  (optional)        : OK
* WASAPI     (optional)        : not found

-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mudu/1salt/vendor/libsoundio
TERMINAL $ make
Scanning dependencies of target unit_tests
[  2%] Building C object CMakeFiles/unit_tests.dir/src/coreaudio.c.o
[  4%] Linking C executable unit_tests
[ 18%] Built target unit_tests
Scanning dependencies of target libsoundio_shared
[ 20%] Building C object CMakeFiles/libsoundio_shared.dir/src/coreaudio.c.o
[ 22%] Linking C shared library libsoundio.dylib
[ 35%] Built target libsoundio_shared
[ 37%] Linking C executable backend_disconnect_recover
[ 39%] Built target backend_disconnect_recover
[ 41%] Linking C executable sio_record
[ 43%] Built target sio_record
Scanning dependencies of target libsoundio_static
[ 45%] Building C object CMakeFiles/libsoundio_static.dir/src/coreaudio.c.o
[ 47%] Linking C static library libsoundio.a
[ 60%] Built target libsoundio_static
[ 62%] Linking C executable sio_sine
[ 64%] Built target sio_sine
[ 66%] Linking C executable sio_microphone
[ 68%] Built target sio_microphone
[ 70%] Linking C executable overflow
[ 72%] Built target overflow
[ 75%] Linking C executable underflow
[ 77%] Built target underflow
[ 79%] Linking C executable sio_list_devices
[ 81%] Built target sio_list_devices
.Scanning dependencies of target latency
[ 83%] Building C object CMakeFiles/latency.dir/src/coreaudio.c.o
/[ 85%] Linking C executable latency
[100%] Built target latency
TERMINAL $ ./sio_sine 
Backend: CoreAudio
libsoundio: backend disconnected: failed to encode string
Abort trap: 6

How ever, after comment the upon four lines, every thing goes well;

TERMINAL $ cmake .
Configuring libsoundio version 1.1.0
-- Could NOT find JACK (missing:  JACK_LIBRARY JACK_INCLUDE_DIR HAVE_jack_set_port_rename_callback) 
-- Could NOT find PULSEAUDIO (missing:  PULSEAUDIO_LIBRARY PULSEAUDIO_INCLUDE_DIR) 
-- Could NOT find ALSA (missing:  ALSA_LIBRARY ALSA_INCLUDE_DIR) 
-- Could NOT find WASAPI (missing:  AUDIOCLIENT_H) 

Installation Summary
--------------------
* Install Directory            : /usr/local
* Build Type                   : Debug
* Build static libs            : ON
* Build examples               : ON
* Build tests                  : ON

System Dependencies
-------------------
* threads                      : OK
* JACK       (optional)        : not found
* PulseAudio (optional)        : not found
* ALSA       (optional)        : not found
* CoreAudio  (optional)        : OK
* WASAPI     (optional)        : not found

-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mudu/1salt/vendor/libsoundio
mTERMINAL $ make
Scanning dependencies of target unit_tests
[  2%] Building C object CMakeFiles/unit_tests.dir/src/coreaudio.c.o
[  4%] Linking C executable unit_tests
[ 18%] Built target unit_tests
Scanning dependencies of target libsoundio_shared
[ 20%] Building C object CMakeFiles/libsoundio_shared.dir/src/coreaudio.c.o
[ 22%] Linking C shared library libsoundio.dylib
[ 35%] Built target libsoundio_shared
[ 37%] Linking C executable backend_disconnect_recover
[ 39%] Built target backend_disconnect_recover
[ 41%] Linking C executable sio_record
[ 43%] Built target sio_record
Scanning dependencies of target libsoundio_static
[ 45%] Building C object CMakeFiles/libsoundio_static.dir/src/coreaudio.c.o
[ 47%] Linking C static library libsoundio.a
[ 60%] Built target libsoundio_static
[ 62%] Linking C executable sio_sine
[ 64%] Built target sio_sine
[ 66%] Linking C executable sio_microphone
[ 68%] Built target sio_microphone
[ 70%] Linking C executable overflow
[ 72%] Built target overflow
[ 75%] Linking C executable underflow
[ 77%] Built target underflow
[ 79%] Linking C executable sio_list_devices
[ 81%] Built target sio_list_devices
Scanning dependencies of target latency
[ 83%] Building C object CMakeFiles/latency.dir/src/coreaudio.c.o
[ 85%] Linking C executable latency
[100%] Built target latency
TERMINAL $ ./sio_sine 
Backend: CoreAudio
Output device: 
Software latency: 0.011610
'p\n' - pause
'u\n' - unpause
'P\n' - pause from within callback
'c\n' - clear buffer
'q\n' - quit
p
pausing result: (no error)
u
unpausing result: (no error)
p
pausing result: (no error)
u
unpausing result: (no error)
o
Unrecognized command: o
p
pausing result: (no error)
u
unpausing result: (no error)
p
pausing result: (no error)
^C

//have  audio here and the pause unpause key goes well
ruurdadema commented 7 years ago

Thanks. Weird. I'm also on OS X 10.12.5 and having no problem at all. I'm thinking about your system setup.

Further:

Are you on the latest Xcode? Including the latest command line tools?

qiukeren commented 7 years ago
TERMINAL $ cmake .
-- The C compiler identification is AppleClang 8.1.0.8020042
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
Configuring libsoundio version 1.1.0
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  
-- Looking for jack_set_port_rename_callback in jack
-- Looking for jack_set_port_rename_callback in jack - not found
-- Could NOT find JACK (missing:  JACK_LIBRARY JACK_INCLUDE_DIR HAVE_jack_set_port_rename_callback) 
-- Could NOT find PULSEAUDIO (missing:  PULSEAUDIO_LIBRARY PULSEAUDIO_INCLUDE_DIR) 
-- Could NOT find ALSA (missing:  ALSA_LIBRARY ALSA_INCLUDE_DIR) 
-- Found COREAUDIO: /System/Library/Frameworks/CoreAudio.framework  
-- Could NOT find WASAPI (missing:  AUDIOCLIENT_H) 

Installation Summary
--------------------
* Install Directory            : /usr/local
* Build Type                   : Debug
* Build static libs            : ON
* Build examples               : ON
* Build tests                  : ON

System Dependencies
-------------------
* threads                      : OK
* JACK       (optional)        : not found
* PulseAudio (optional)        : not found
* ALSA       (optional)        : not found
* CoreAudio  (optional)        : OK
* WASAPI     (optional)        : not found

-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mudu/1salt/vendor/libsoundio

before comment:

TERMINAL $ ./sio_list_devices 
libsoundio: backend disconnected: failed to encode string
Abort trap: 6

after comment it :

TERMINAL $ ./sio_list_devices 
--------Input Devices--------

 (default)
  id: ?=h???
  channel layouts:
    Stereo
  current layout: Stereo
  sample rates:
    44100 - 44100
  current sample rate: 44100
  formats: signed 16-bit LE, signed 32-bit LE, float 32-bit LE, float 64-bit LE
  min software latency: 0.00031746 sec
  max software latency: 0.09287982 sec
  current software latency: 0.01160998 sec

  id: 
  channel layouts:
    Stereo
  current layout: Stereo
  sample rates:
    44100 - 44100
  current sample rate: 44100
  formats: signed 16-bit LE, signed 32-bit LE, float 32-bit LE, float 64-bit LE
  min software latency: 0.00031746 sec
  max software latency: 0.09287982 sec
  current software latency: 0.01160998 sec

  id: 
  channel layouts:
    (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel)
  current layout: (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel)
  sample rates:
    44100 - 44100
  current sample rate: 44100
  formats: signed 16-bit LE, signed 32-bit LE, float 32-bit LE, float 64-bit LE
  min software latency: 0.00031746 sec
  max software latency: 0.09287982 sec
  current software latency: 0.01160998 sec

--------Output Devices--------

 (default)
  id: ?=h???
  channel layouts:
    Stereo
  current layout: Stereo
  sample rates:
    44100 - 44100
    48000 - 48000
    88200 - 88200
    96000 - 96000
  current sample rate: 44100
  formats: signed 16-bit LE, signed 32-bit LE, float 32-bit LE, float 64-bit LE
  min software latency: 0.00031746 sec
  max software latency: 0.09287982 sec
  current software latency: 0.01160998 sec

  id: 
  channel layouts:
    Stereo
  current layout: Stereo
  sample rates:
    44100 - 44100
    48000 - 48000
    88200 - 88200
    96000 - 96000
    176400 - 176400
    192000 - 192000
  current sample rate: 44100
  formats: signed 16-bit LE, signed 32-bit LE, float 32-bit LE, float 64-bit LE
  min software latency: 0.00031746 sec
  max software latency: 0.09287982 sec
  current software latency: 0.01160998 sec

  id: 
  channel layouts:
    (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel)
  current layout: (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel), (Invalid Channel)
  sample rates:
    44100 - 44100
    48000 - 48000
    88200 - 88200
    96000 - 96000
    176400 - 176400
    192000 - 192000
  current sample rate: 44100
  formats: signed 16-bit LE, signed 32-bit LE, float 32-bit LE, float 64-bit LE
  min software latency: 0.00031746 sec
  max software latency: 0.09287982 sec
  current software latency: 0.01160998 sec

ffidbolg
  id: 
  channel layouts:
    Stereo
  current layout: Stereo
  sample rates:
    44100 - 44100
    48000 - 48000
    88200 - 88200
    96000 - 96000
  current sample rate: 44100
  formats: signed 16-bit LE, signed 32-bit LE, float 32-bit LE, float 64-bit LE
  min software latency: 0.00031746 sec
  max software latency: 0.09287982 sec
  current software latency: 0.01160998 sec

7 devices found
ruurdadema commented 7 years ago

Ok, what's basically seems to be going on is that you probably have an audio device attached to your system with a long name, a long uid or both. The buffer provided by libsoundio to store the name and/or uid is not long enough to hold that.

So, we need to find out is there's indeed such a device in your system.

Can you run system_profiler SPAudioDataType in your terminal and post the output here?

qiukeren commented 7 years ago

Yes, here it is, system_profiler SPAudioDataType shows the hidden ghost:

image

After delete it, every thing goes well;

here is a suggesion on how to delete it:

in launchpad, search midi, then act as follows:

image

or click it twice to change to another name like 1234:

image

Thanks.

ruurdadema commented 7 years ago

Would you please be so kind and help finding the bug?

First replace the from_cf_string() method with this one:

static int from_cf_string(CFStringRef string_ref, char **out_str, int *out_str_len) {
    assert(string_ref);

    CFIndex length = CFStringGetLength(string_ref);
    CFIndex max_size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
    char *buf = ALLOCATE_NONZERO(char, max_size);
    if (!buf)
        return SoundIoErrorNoMem;

    printf("from_cf_string() string:   \"%s\"\n", CFStringGetCStringPtr(string_ref, kCFStringEncodingUTF8));
    printf("from_cf_string() length:   %lu\n", length);
    printf("from_cf_string() max_size: %lu\n", max_size);

    int result = 0;
    result = CFStringGetCString(string_ref, buf, max_size, kCFStringEncodingUTF8);

    printf("from_cf_string() result:   %d\n\n", result);
    if (!result) {
        free(buf);
        return SoundIoErrorEncodingString;
    }

    *out_str = buf;
    *out_str_len = strlen(buf);
    return 0;
}

Then compile and run the sio_list_devices example. Can you post the result of all the lines starting with "from_cf_string()".

qiukeren commented 7 years ago

with a multi output media called "1234":

mudu@mudu-PC 16:23 [~/1salt/vendor/libsoundio]: ./sio_list_devices 
from_cf_string() string:   "Built-in Microphone"
from_cf_string() length:   19
from_cf_string() max_size: 57
from_cf_string() result:   1

from_cf_string() string:   "AppleHDAEngineInput:1B,0,1,0:1"
from_cf_string() length:   30
from_cf_string() max_size: 90
from_cf_string() result:   1

from_cf_string() string:   "Built-in Output"
from_cf_string() length:   15
from_cf_string() max_size: 45
from_cf_string() result:   1

from_cf_string() string:   "AppleHDAEngineOutput:1B,0,1,1:0"
from_cf_string() length:   31
from_cf_string() max_size: 93
from_cf_string() result:   1

from_cf_string() string:   "(null)"
from_cf_string() length:   4
from_cf_string() max_size: 12
from_cf_string() result:   1

from_cf_string() string:   "~:AMS2_StackedOutput:0"
from_cf_string() length:   22
from_cf_string() max_size: 66
from_cf_string() result:   1

--------Input Devices--------

Built-in Microphone (default)
  id: AppleHDAEngineInput:1B,0,1,0:1
  channel layouts:
    Stereo
  current layout: Stereo
  sample rates:
    44100 - 44100
  current sample rate: 44100
  formats: signed 16-bit LE, signed 32-bit LE, float 32-bit LE, float 64-bit LE
  min software latency: 0.00031746 sec
  max software latency: 0.09287982 sec
  current software latency: 0.01160998 sec

--------Output Devices--------

Built-in Output (default)
  id: AppleHDAEngineOutput:1B,0,1,1:0
  channel layouts:
    Stereo
  current layout: Stereo
  sample rates:
    44100 - 44100
    48000 - 48000
    88200 - 88200
    96000 - 96000
  current sample rate: 44100
  formats: signed 16-bit LE, signed 32-bit LE, float 32-bit LE, float 64-bit LE
  min software latency: 0.00031746 sec
  max software latency: 0.09287982 sec
  current software latency: 0.01160998 sec

1234
  id: ~:AMS2_StackedOutput:0
  channel layouts:
    Stereo
  current layout: Stereo
  sample rates:
    44100 - 44100
    48000 - 48000
    88200 - 88200
    96000 - 96000
  current sample rate: 44100
  formats: signed 16-bit LE, signed 32-bit LE, float 32-bit LE, float 64-bit LE
  min software latency: 0.00031746 sec
  max software latency: 0.09287982 sec
  current software latency: 0.01160998 sec

3 devices found

with a device that must be crashed:

from_cf_string() string:   "Built-in Microphone"
from_cf_string() length:   19
from_cf_string() max_size: 57
from_cf_string() result:   1

from_cf_string() string:   "AppleHDAEngineInput:1B,0,1,0:1"
from_cf_string() length:   30
from_cf_string() max_size: 90
from_cf_string() result:   1

from_cf_string() string:   "Built-in Output"
from_cf_string() length:   15
from_cf_string() max_size: 45
from_cf_string() result:   1

from_cf_string() string:   "AppleHDAEngineOutput:1B,0,1,1:0"
from_cf_string() length:   31
from_cf_string() max_size: 93
from_cf_string() result:   1

from_cf_string() string:   "(null)"
from_cf_string() length:   5
from_cf_string() max_size: 15
from_cf_string() result:   0

libsoundio: backend disconnected: failed to encode string
Abort trap: 6

seems that the api just does not fit this language,

image

This goes well,

image

but this should crash.

ruurdadema commented 7 years ago

Thanks! What is (are) the character(s) of the device in the second screenshot?

Which language is it anyway? (I'm from NL and really have no idea :-))

qiukeren commented 7 years ago

The second is one chinese(zh-CN) character , "one" ;

In utf8, almost one chinese character use three bytes, some use four.