Merge pull request #1488 from ePirat/cfstring-utils

Consolidate CFString conversions
This commit is contained in:
Jim
2018-09-16 14:42:48 -07:00
committed by GitHub
11 changed files with 141 additions and 81 deletions

View File

@@ -10,6 +10,7 @@
#ifndef _WIN32
#include <AudioToolbox/AudioToolbox.h>
#include <util/apple/cfstring-utils.h>
#endif
#define CA_LOG(level, format, ...) \
@@ -257,17 +258,8 @@ static DStr osstatus_to_dstr(OSStatus code)
kCFErrorDomainOSStatus, code, NULL)};
cf_ptr<CFStringRef> str{CFErrorCopyDescription(err.get())};
CFIndex length = CFStringGetLength(str.get());
CFIndex max_size = CFStringGetMaximumSizeForEncoding(length,
kCFStringEncodingUTF8);
dstr_ensure_capacity(result, max_size);
if (result->array && CFStringGetCString(str.get(), result->array,
max_size, kCFStringEncodingUTF8)) {
dstr_resize(result, strlen(result->array));
if (cfstr_copy_dstr(str.get(), kCFStringEncodingUTF8, result))
return result;
}
#endif
const char *code_str = code_to_str(code);

View File

@@ -1,22 +1,17 @@
#include "../platform.hpp"
#include <util/apple/cfstring-utils.h>
bool DeckLinkStringToStdString(decklink_string_t input, std::string& output)
{
const CFStringRef string = static_cast<CFStringRef>(input);
const CFIndex length = CFStringGetLength(string);
const CFIndex maxLength = CFStringGetMaximumSizeForEncoding(length,
kCFStringEncodingASCII) + 1;
char * const buffer = new char[maxLength];
char *buffer = cfstr_copy_cstr(string, kCFStringEncodingASCII);
const bool result = CFStringGetCString(string, buffer, maxLength,
kCFStringEncodingASCII);
if (result)
if (buffer)
output = std::string(buffer);
delete[] buffer;
bfree(buffer);
CFRelease(string);
return result;
return (buffer != NULL);
}

View File

@@ -14,7 +14,6 @@ include_directories(${COREAUDIO}
set(mac-capture_HEADERS
audio-device-enum.h
mac-helpers.h
window-utils.h)
set(mac-capture_SOURCES

View File

@@ -1,7 +1,8 @@
#include <CoreFoundation/CFString.h>
#include <CoreAudio/CoreAudio.h>
#include "mac-helpers.h"
#include <util/apple/cfstring-utils.h>
#include "audio-device-enum.h"
/* ugh, because mac has no means of capturing output, we have to basically
@@ -114,9 +115,9 @@ static bool coreaudio_enum_add_device(void *param, CFStringRef cf_name,
memset(&item, 0, sizeof(item));
if (!cf_to_dstr(cf_name, &item.name))
if (!cfstr_copy_dstr(cf_name, kCFStringEncodingUTF8, &item.name))
goto fail;
if (!cf_to_dstr(cf_uid, &item.value))
if (!cfstr_copy_dstr(cf_uid, kCFStringEncodingUTF8, &item.value))
goto fail;
if (data->input || !device_is_input(item.value.array))

View File

@@ -7,8 +7,8 @@
#include <obs-module.h>
#include <util/threading.h>
#include <util/c99defs.h>
#include <util/apple/cfstring-utils.h>
#include "mac-helpers.h"
#include "audio-device-enum.h"
#define PROPERTY_DEFAULT_DEVICE kAudioHardwarePropertyDefaultInputDevice
@@ -496,7 +496,7 @@ static bool coreaudio_get_device_name(struct coreaudio_data *ca)
{
CFStringRef cf_name = NULL;
UInt32 size = sizeof(CFStringRef);
char name[1024];
char *name = NULL;
const AudioObjectPropertyAddress addr = {
kAudioDevicePropertyDeviceNameCFString,
@@ -512,14 +512,15 @@ static bool coreaudio_get_device_name(struct coreaudio_data *ca)
return false;
}
if (!cf_to_cstr(cf_name, name, 1024)) {
name = cfstr_copy_cstr(cf_name, kCFStringEncodingUTF8);
if (!name) {
blog(LOG_WARNING, "[coreaudio_get_device_name] failed to "
"convert name to cstr for some reason");
return false;
}
bfree(ca->device_name);
ca->device_name = bstrdup(name);
ca->device_name = name;
if (cf_name)
CFRelease(cf_name);

View File

@@ -1,34 +0,0 @@
#pragma once
#include <util/dstr.h>
static inline bool mac_success(OSStatus stat, const char *action)
{
if (stat != noErr) {
blog(LOG_WARNING, "%s failed: %d", action, (int)stat);
return false;
}
return true;
}
static inline bool cf_to_cstr(CFStringRef ref, char *buf, size_t size)
{
if (!ref) return false;
return (bool)CFStringGetCString(ref, buf, size, kCFStringEncodingUTF8);
}
static inline bool cf_to_dstr(CFStringRef ref, struct dstr *str)
{
size_t size;
if (!ref) return false;
size = (size_t)CFStringGetLength(ref);
if (!size)
return false;
dstr_resize(str, size);
return (bool)CFStringGetCString(ref, str->array, size+1,
kCFStringEncodingUTF8);
}

View File

@@ -6,6 +6,8 @@
#include <VideoToolbox/VTVideoEncoderList.h>
#include <CoreMedia/CoreMedia.h>
#include <util/apple/cfstring-utils.h>
#include <assert.h>
#define VT_LOG(level, format, ...) \
@@ -74,23 +76,20 @@ struct vt_h264_encoder
static void log_osstatus(int log_level, struct vt_h264_encoder *enc,
const char *context, OSStatus code)
{
char *c_str = NULL;
CFErrorRef err = CFErrorCreate(kCFAllocatorDefault,
kCFErrorDomainOSStatus, code, NULL);
CFStringRef str = CFErrorCopyDescription(err);
CFIndex length = CFStringGetLength(str);
CFIndex max_size = CFStringGetMaximumSizeForEncoding(length,
kCFStringEncodingUTF8);
char *c_str = malloc(max_size);
if (CFStringGetCString(str, c_str, max_size, kCFStringEncodingUTF8)) {
c_str = cfstr_copy_cstr(str, kCFStringEncodingUTF8);
if (c_str) {
if (enc)
VT_BLOG(log_level, "Error in %s: %s", context, c_str);
else
VT_LOG(log_level, "Error in %s: %s", context, c_str);
}
free(c_str);
bfree(c_str);
CFRelease(str);
CFRelease(err);
}