CoreAudio: print error numbers

This commit is contained in:
Martin Pulec
2023-05-30 15:23:19 +02:00
parent 0f546e1f4d
commit f90214d0c9
3 changed files with 25 additions and 14 deletions

View File

@@ -59,6 +59,16 @@
#define MOD_NAME "[CoreAudio] "
/**
* @todo
* Currntly only error number is returned. Improve to return textual representation
*/
const char *get_ca_error_str(OSStatus err) {
static _Thread_local char buf[128];
snprintf(buf, sizeof buf, "0x%x (%d)", err, err);
return buf;
}
struct state_ca_capture {
#ifndef __MAC_10_6
ComponentInstance
@@ -154,7 +164,7 @@ static OSStatus InputProc(void *inRefCon,
pthread_cond_signal(&s->cv);
pthread_mutex_unlock(&s->lock);
} else {
fprintf(stderr, "[CoreAudio] writing buffer caused error %i.\n", (int) err);
log_msg(LOG_LEVEL_ERROR, MOD_NAME "writing buffer caused error %s.\n", get_ca_error_str(err));
}
return err;
@@ -182,7 +192,7 @@ static void audio_cap_ca_help()
#define CA_STRINGIFY(A) #A
#define CHECK_OK(cmd, msg, action_failed) do { OSErr ret = cmd; if (ret != noErr) {\
log_msg(strlen(CA_STRINGIFY(action_failed)) == 0 ? LOG_LEVEL_WARNING : LOG_LEVEL_ERROR, MOD_NAME "%s: %d\n", (msg), ret);\
log_msg(strlen(CA_STRINGIFY(action_failed)) == 0 ? LOG_LEVEL_WARNING : LOG_LEVEL_ERROR, MOD_NAME "%s: %s\n", (msg), get_ca_error_str(ret));\
action_failed;\
}\
} while(0)
@@ -231,7 +241,7 @@ static void * audio_cap_ca_init(struct module *parent, const char *cfg)
propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
propertyAddress.mElement = kAudioObjectPropertyElementMaster;
if ((ret = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size, &device)) != 0) {
fprintf(stderr, "Error finding default input device: %d.\n", ret);
log_msg(LOG_LEVEL_ERROR, "Error finding default input device: %s.\n", get_ca_error_str(ret));
return NULL;
}
}
@@ -255,11 +265,11 @@ static void * audio_cap_ca_init(struct module *parent, const char *cfg)
propertyAddress.mScope = kAudioDevicePropertyScopeInput;
propertyAddress.mElement = kAudioObjectPropertyElementMaster;
ret = AudioObjectGetPropertyData(device, &propertyAddress, 0, NULL, &size, &rate);
if (rate == 0.0) {
log_msg(LOG_LEVEL_ERROR, MOD_NAME "Sample rate 0.0 returned. Wrong device index?\n");
if (ret != noErr || rate == 0.0) {
log_msg(LOG_LEVEL_ERROR, MOD_NAME "Unable to get sample rate: %s. Wrong device index?\n", get_ca_error_str(ret));
return NULL;
}
if (ret != noErr || (audio_capture_sample_rate != 0 && audio_capture_sample_rate != rate)) {
if (audio_capture_sample_rate != 0 && audio_capture_sample_rate != rate) {
log_msg(LOG_LEVEL_WARNING, MOD_NAME "Requested sample rate %u, got %lf!\n", audio_capture_sample_rate, rate);
}
@@ -311,7 +321,7 @@ static void * audio_cap_ca_init(struct module *parent, const char *cfg)
#else
comp = FindNextComponent(NULL, &desc);
if(!comp) {
fprintf(stderr, "Error finding AUHAL component.\n");
log_msg(LOG_LEVEL_ERROR, MOD_NAME "Error finding AUHAL component.\n");
break;
}
CHECK_OK(OpenAComponent(comp, &s->auHALComponentInstance),

View File

@@ -168,13 +168,13 @@ static int audio_play_ca_reconfigure(void *state, struct audio_desc desc)
if (s->initialized) {
ret = AudioOutputUnitStop(s->auHALComponentInstance);
if(ret) {
LOG(LOG_LEVEL_ERROR) << MOD_NAME "Cannot stop AUHAL instance.\n";
LOG(LOG_LEVEL_ERROR) << MOD_NAME "Cannot stop AUHAL instance: " << get_ca_error_str(ret) << ".\n";
goto error;
}
ret = AudioUnitUninitialize(s->auHALComponentInstance);
if(ret) {
LOG(LOG_LEVEL_ERROR) << MOD_NAME "Cannot uninitialize AUHAL instance.\n";
LOG(LOG_LEVEL_ERROR) << MOD_NAME "Cannot uninitialize AUHAL instance: " << get_ca_error_str(ret) << ".\n";
goto error;
}
s->initialized = false;
@@ -206,7 +206,7 @@ static int audio_play_ca_reconfigure(void *state, struct audio_desc desc)
ret = AudioUnitGetProperty(s->auHALComponentInstance, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input,
0, &stream_desc, &size);
if(ret) {
LOG(LOG_LEVEL_ERROR) << MOD_NAME "Cannot get device format from AUHAL instance.\n";
LOG(LOG_LEVEL_ERROR) << MOD_NAME "Cannot get device format from AUHAL instance: " << get_ca_error_str(ret) << ".\n";
goto error;
}
stream_desc.mSampleRate = desc.sample_rate;
@@ -220,7 +220,7 @@ static int audio_play_ca_reconfigure(void *state, struct audio_desc desc)
ret = AudioUnitSetProperty(s->auHALComponentInstance, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input,
0, &stream_desc, sizeof(stream_desc));
if(ret) {
LOG(LOG_LEVEL_ERROR) << "Cannot set device format to AUHAL instance.\n";
LOG(LOG_LEVEL_ERROR) << "Cannot set device format to AUHAL instance: " << get_ca_error_str(ret) << ".\n";
goto error;
}
@@ -229,19 +229,19 @@ static int audio_play_ca_reconfigure(void *state, struct audio_desc desc)
ret = AudioUnitSetProperty(s->auHALComponentInstance, kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input, 0, &renderStruct, sizeof(AURenderCallbackStruct));
if(ret) {
LOG(LOG_LEVEL_ERROR) << MOD_NAME "Cannot register audio processing callback.\n";
LOG(LOG_LEVEL_ERROR) << MOD_NAME "Cannot register audio processing callback: " << get_ca_error_str(ret) << ".\n";
goto error;
}
ret = AudioUnitInitialize(s->auHALComponentInstance);
if(ret) {
LOG(LOG_LEVEL_ERROR) << MOD_NAME "Cannot initialize AUHAL.\n";
LOG(LOG_LEVEL_ERROR) << MOD_NAME "Cannot initialize AUHAL: " << get_ca_error_str(ret) << ".\n";
goto error;
}
ret = AudioOutputUnitStart(s->auHALComponentInstance);
if(ret) {
LOG(LOG_LEVEL_ERROR) << MOD_NAME "Cannot start AUHAL.\n";
LOG(LOG_LEVEL_ERROR) << MOD_NAME "Cannot start AUHAL: " << get_ca_error_str(ret) << ".\n";
goto error;
}

View File

@@ -46,6 +46,7 @@ struct device_info;
* @param dir -1 capture; 1 playback
*/
void audio_ca_probe(struct device_info **available_devices, int *count, int dir);
const char *get_ca_error_str(OSStatus err);
#ifdef __cplusplus
}