diff --git a/src/apple/AudioObject.cxx b/src/apple/AudioObject.cxx new file mode 100644 index 000000000..f81f82e2f --- /dev/null +++ b/src/apple/AudioObject.cxx @@ -0,0 +1,40 @@ +/* + * Copyright 2020 Max Kellermann + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "AudioObject.hxx" +#include "StringRef.hxx" + +Apple::StringRef +AudioObjectGetStringProperty(AudioObjectID inObjectID, + const AudioObjectPropertyAddress &inAddress) +{ + auto s = AudioObjectGetPropertyDataT(inObjectID, + inAddress); + return Apple::StringRef(s); +} diff --git a/src/apple/AudioObject.hxx b/src/apple/AudioObject.hxx index 4050c2e72..ff0362dd4 100644 --- a/src/apple/AudioObject.hxx +++ b/src/apple/AudioObject.hxx @@ -37,6 +37,10 @@ #include +namespace Apple { +class StringRef; +} + inline std::size_t AudioObjectGetPropertyDataSize(AudioObjectID inObjectID, const AudioObjectPropertyAddress &inAddress) @@ -69,6 +73,10 @@ AudioObjectGetPropertyDataT(AudioObjectID inObjectID, return value; } +Apple::StringRef +AudioObjectGetStringProperty(AudioObjectID inObjectID, + const AudioObjectPropertyAddress &inAddress); + template AllocatedArray AudioObjectGetPropertyDataArray(AudioObjectID inObjectID, diff --git a/src/apple/meson.build b/src/apple/meson.build index 8f9f46484..160c62bc4 100644 --- a/src/apple/meson.build +++ b/src/apple/meson.build @@ -12,6 +12,7 @@ audiounit_dep = declare_dependency( apple = static_library( 'apple', + 'AudioObject.cxx', 'Throw.cxx', include_directories: inc, dependencies: [ diff --git a/src/output/plugins/OSXOutputPlugin.cxx b/src/output/plugins/OSXOutputPlugin.cxx index 696f5acea..b3a58f972 100644 --- a/src/output/plugins/OSXOutputPlugin.cxx +++ b/src/output/plugins/OSXOutputPlugin.cxx @@ -535,19 +535,15 @@ IsAudioDeviceName(AudioDeviceID id, const char *expected_name) noexcept kAudioObjectPropertyElementMaster, }; - CFStringRef cfname; - UInt32 size = sizeof(cfname); - - if (AudioObjectGetPropertyData(id, &aopa_name, - 0, nullptr, - &size, &cfname) != noErr) - return false; - - const Apple::StringRef cfname_(cfname); - char actual_name[256]; - if (!cfname_.GetCString(actual_name, sizeof(actual_name))) + + try { + auto cfname = AudioObjectGetStringProperty(id, aopa_name); + if (!cfname.GetCString(actual_name, sizeof(actual_name))) + return false; + } catch (...) { return false; + } return StringIsEqual(actual_name, expected_name); }