2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2014, David Sansome <me@davidsansome.com>
|
|
|
|
*
|
|
|
|
* Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <CoreAudio/AudioHardware.h>
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QList>
|
|
|
|
#include <QString>
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/scoped_cftyperef.h"
|
|
|
|
|
2019-01-01 20:22:19 +01:00
|
|
|
#include "macosdevicefinder.h"
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::unique_ptr<T> GetProperty(const AudioDeviceID& device_id, const AudioObjectPropertyAddress& address, UInt32* size_bytes_out = nullptr) {
|
|
|
|
|
|
|
|
UInt32 size_bytes = 0;
|
|
|
|
OSStatus status = AudioObjectGetPropertyDataSize(device_id, &address, 0, NULL, &size_bytes);
|
|
|
|
if (status != kAudioHardwareNoError) {
|
|
|
|
qLog(Warning) << "AudioObjectGetPropertyDataSize failed:" << status;
|
|
|
|
return std::unique_ptr<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<T> ret(reinterpret_cast<T*>(malloc(size_bytes)));
|
|
|
|
|
|
|
|
status = AudioObjectGetPropertyData(device_id, &address, 0, NULL, &size_bytes, ret.get());
|
|
|
|
if (status != kAudioHardwareNoError) {
|
|
|
|
qLog(Warning) << "AudioObjectGetPropertyData failed:" << status;
|
|
|
|
return std::unique_ptr<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size_bytes_out) {
|
|
|
|
*size_bytes_out = size_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2019-01-01 20:22:19 +01:00
|
|
|
MacOsDeviceFinder::MacOsDeviceFinder()
|
2018-06-28 01:15:32 +02:00
|
|
|
: DeviceFinder("osxaudio", { "osxaudio", "osx", "osxaudiosink"} ) {
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-01 20:22:19 +01:00
|
|
|
QList<DeviceFinder::Device> MacOsDeviceFinder::ListDevices() {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QList<Device> ret;
|
|
|
|
|
|
|
|
AudioObjectPropertyAddress address = {
|
|
|
|
kAudioHardwarePropertyDevices,
|
|
|
|
kAudioObjectPropertyScopeGlobal,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
UInt32 device_size_bytes = 0;
|
|
|
|
std::unique_ptr<AudioDeviceID> devices = GetProperty<AudioDeviceID>(kAudioObjectSystemObject, address, &device_size_bytes);
|
|
|
|
if (!devices.get()) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-07-13 18:11:57 +02:00
|
|
|
const UInt32 device_count = device_size_bytes / sizeof(AudioDeviceID);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
address.mScope = kAudioDevicePropertyScopeOutput;
|
|
|
|
for (UInt32 i = 0; i < device_count; ++i) {
|
|
|
|
const AudioDeviceID id = devices.get()[i];
|
|
|
|
|
|
|
|
// Query device name
|
|
|
|
address.mSelector = kAudioDevicePropertyDeviceNameCFString;
|
|
|
|
std::unique_ptr<CFStringRef> device_name = GetProperty<CFStringRef>(id, address);
|
|
|
|
ScopedCFTypeRef<CFStringRef> scoped_device_name(*device_name.get());
|
|
|
|
if (!device_name.get()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-06-07 19:38:40 +02:00
|
|
|
// Determine if the device is an output device (it is an output device if it has output channels)
|
2018-02-27 18:06:05 +01:00
|
|
|
address.mSelector = kAudioDevicePropertyStreamConfiguration;
|
|
|
|
std::unique_ptr<AudioBufferList> buffer_list = GetProperty<AudioBufferList>(id, address);
|
|
|
|
if (!buffer_list.get() || buffer_list->mNumberBuffers == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Device dev;
|
2018-04-05 21:40:05 +02:00
|
|
|
dev.value = id;
|
2018-07-03 17:51:52 +02:00
|
|
|
dev.description = QString::fromUtf8(CFStringGetCStringPtr(*device_name, CFStringGetSystemEncoding()));
|
|
|
|
if (dev.description.isEmpty()) dev.description = QString("Unknown device " + dev.value.toString());
|
2018-04-05 21:40:05 +02:00
|
|
|
dev.iconname = GuessIconName(dev.description);
|
2018-02-27 18:06:05 +01:00
|
|
|
ret.append(dev);
|
|
|
|
}
|
|
|
|
return ret;
|
2018-07-03 17:51:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|