mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-12-20 08:56:47 -05:00
coreaudio: Dynamically allocate C string when converting from CFString
This commit is contained in:
parent
3314dc72ea
commit
cf503f9560
1 changed files with 31 additions and 27 deletions
|
|
@ -376,23 +376,25 @@ static int ca_sink_set_state(pa_sink *s, pa_sink_state_t state) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Caveat: The caller is responsible to get rid of the CFString(Ref). */
|
/* Caveat: The caller is responsible to get rid of the CFString(Ref). */
|
||||||
static bool CFString_to_cstr_n(CFStringRef cfstr, char *buf, long n) {
|
static char * CFString_to_cstr(CFStringRef cfstr) {
|
||||||
bool ret;
|
char *ret = NULL;
|
||||||
|
|
||||||
pa_assert (buf);
|
|
||||||
|
|
||||||
ret = false;
|
ret = false;
|
||||||
|
|
||||||
if (cfstr != NULL) {
|
if (cfstr != NULL) {
|
||||||
const char *tmp = CFStringGetCStringPtr(cfstr, kCFStringEncodingUTF8);
|
const char *tmp = CFStringGetCStringPtr(cfstr, kCFStringEncodingUTF8);
|
||||||
|
CFIndex n = CFStringGetLength(cfstr) + 1 /* for the terminating NULL */;
|
||||||
|
|
||||||
|
ret = pa_xmalloc(n);
|
||||||
|
|
||||||
if (tmp == NULL) {
|
if (tmp == NULL) {
|
||||||
if (CFStringGetCString(cfstr, buf, n, kCFStringEncodingUTF8))
|
if (!CFStringGetCString(cfstr, ret, n, kCFStringEncodingUTF8)) {
|
||||||
ret = true;
|
pa_xfree(ret);
|
||||||
|
ret = NULL;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
strncpy(buf, tmp, n);
|
strncpy(ret, tmp, n - 1);
|
||||||
buf[n - 1] = 0;
|
ret[n - 1] = '\0';
|
||||||
ret = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -408,7 +410,7 @@ static int ca_device_create_sink(pa_module *m, AudioBuffer *buf, int channel_idx
|
||||||
coreaudio_sink *ca_sink;
|
coreaudio_sink *ca_sink;
|
||||||
pa_sink *sink;
|
pa_sink *sink;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
char tmp[255];
|
char *tmp;
|
||||||
pa_strbuf *strbuf;
|
pa_strbuf *strbuf;
|
||||||
AudioObjectPropertyAddress property_address;
|
AudioObjectPropertyAddress property_address;
|
||||||
CFStringRef tmp_cfstr = NULL;
|
CFStringRef tmp_cfstr = NULL;
|
||||||
|
|
@ -425,23 +427,24 @@ static int ca_device_create_sink(pa_module *m, AudioBuffer *buf, int channel_idx
|
||||||
property_address.mSelector = kAudioObjectPropertyElementName;
|
property_address.mSelector = kAudioObjectPropertyElementName;
|
||||||
property_address.mScope = kAudioDevicePropertyScopeOutput;
|
property_address.mScope = kAudioDevicePropertyScopeOutput;
|
||||||
property_address.mElement = channel_idx + i + 1;
|
property_address.mElement = channel_idx + i + 1;
|
||||||
size = sizeof(tmp);
|
size = sizeof(tmp_cfstr);
|
||||||
err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &tmp_cfstr);
|
err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &tmp_cfstr);
|
||||||
if (err == 0) {
|
if (err == 0) {
|
||||||
err = !(CFString_to_cstr_n(tmp_cfstr, tmp, sizeof(tmp)));
|
tmp = CFString_to_cstr(tmp_cfstr);
|
||||||
|
|
||||||
if (tmp_cfstr) {
|
if (tmp_cfstr)
|
||||||
CFRelease(tmp_cfstr);
|
CFRelease(tmp_cfstr);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (err || !strlen(tmp))
|
|
||||||
snprintf(tmp, sizeof(tmp), "Channel %d", (int) property_address.mElement);
|
|
||||||
|
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
pa_strbuf_puts(strbuf, ", ");
|
pa_strbuf_puts(strbuf, ", ");
|
||||||
|
|
||||||
|
if (err || !tmp || !strlen(tmp))
|
||||||
|
pa_strbuf_printf(strbuf, "Channel %d", (int) property_address.mElement);
|
||||||
|
else
|
||||||
pa_strbuf_puts(strbuf, tmp);
|
pa_strbuf_puts(strbuf, tmp);
|
||||||
|
|
||||||
|
pa_xfree(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
ca_sink->name = pa_strbuf_to_string_free(strbuf);
|
ca_sink->name = pa_strbuf_to_string_free(strbuf);
|
||||||
|
|
@ -535,7 +538,7 @@ static int ca_device_create_source(pa_module *m, AudioBuffer *buf, int channel_i
|
||||||
coreaudio_source *ca_source;
|
coreaudio_source *ca_source;
|
||||||
pa_source *source;
|
pa_source *source;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
char tmp[255];
|
char *tmp;
|
||||||
pa_strbuf *strbuf;
|
pa_strbuf *strbuf;
|
||||||
AudioObjectPropertyAddress property_address;
|
AudioObjectPropertyAddress property_address;
|
||||||
CFStringRef tmp_cfstr = NULL;
|
CFStringRef tmp_cfstr = NULL;
|
||||||
|
|
@ -552,23 +555,24 @@ static int ca_device_create_source(pa_module *m, AudioBuffer *buf, int channel_i
|
||||||
property_address.mSelector = kAudioObjectPropertyElementName;
|
property_address.mSelector = kAudioObjectPropertyElementName;
|
||||||
property_address.mScope = kAudioDevicePropertyScopeInput;
|
property_address.mScope = kAudioDevicePropertyScopeInput;
|
||||||
property_address.mElement = channel_idx + i + 1;
|
property_address.mElement = channel_idx + i + 1;
|
||||||
size = sizeof(tmp);
|
size = sizeof(tmp_cfstr);
|
||||||
err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &tmp_cfstr);
|
err = AudioObjectGetPropertyData(u->object_id, &property_address, 0, NULL, &size, &tmp_cfstr);
|
||||||
if (err == 0) {
|
if (err == 0) {
|
||||||
err = !(CFString_to_cstr_n(tmp_cfstr, tmp, sizeof(tmp)));
|
tmp = CFString_to_cstr(tmp_cfstr);
|
||||||
|
|
||||||
if (tmp_cfstr) {
|
if (tmp_cfstr)
|
||||||
CFRelease(tmp_cfstr);
|
CFRelease(tmp_cfstr);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (err || !strlen(tmp))
|
|
||||||
snprintf(tmp, sizeof(tmp), "Channel %d", (int) property_address.mElement);
|
|
||||||
|
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
pa_strbuf_puts(strbuf, ", ");
|
pa_strbuf_puts(strbuf, ", ");
|
||||||
|
|
||||||
|
if (err || !tmp || !strlen(tmp))
|
||||||
|
pa_strbuf_printf(strbuf, "Channel %d", (int) property_address.mElement);
|
||||||
|
else
|
||||||
pa_strbuf_puts(strbuf, tmp);
|
pa_strbuf_puts(strbuf, tmp);
|
||||||
|
|
||||||
|
pa_xfree(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
ca_source->name = pa_strbuf_to_string_free(strbuf);
|
ca_source->name = pa_strbuf_to_string_free(strbuf);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue