2019-07-02 13:37:01 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2019 Weixuan XIAO <veyx.shaw@gmail.com>
|
2019-07-02 13:37:01 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2019-07-02 13:37:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "systemvolumeplugin-macos.h"
|
|
|
|
|
|
|
|
#include <KPluginFactory>
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "plugin_systemvolume_debug.h"
|
2019-07-02 13:37:01 +01:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
2023-07-21 11:42:38 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(SystemvolumePlugin, "kdeconnect_systemvolume.json")
|
2019-07-02 13:37:01 +01:00
|
|
|
|
|
|
|
class MacOSCoreAudioDevice
|
|
|
|
{
|
|
|
|
private:
|
2019-07-30 15:14:47 +01:00
|
|
|
AudioDeviceID m_deviceId;
|
|
|
|
QString m_description;
|
|
|
|
bool m_isStereo;
|
2019-07-02 13:37:01 +01:00
|
|
|
|
|
|
|
friend class SystemvolumePlugin;
|
2022-09-10 22:23:52 +01:00
|
|
|
|
2019-07-02 13:37:01 +01:00
|
|
|
public:
|
|
|
|
MacOSCoreAudioDevice(AudioDeviceID);
|
|
|
|
~MacOSCoreAudioDevice();
|
|
|
|
|
|
|
|
void setVolume(float volume);
|
|
|
|
float volume();
|
|
|
|
void setMuted(bool muted);
|
|
|
|
bool isMuted();
|
2020-12-07 07:55:01 +00:00
|
|
|
void setDefault(bool enabled);
|
|
|
|
bool isDefault();
|
2019-07-02 13:37:01 +01:00
|
|
|
|
|
|
|
void updateType();
|
|
|
|
};
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static const AudioObjectPropertyAddress kAudioHardwarePropertyAddress = {kAudioHardwarePropertyDevices,
|
|
|
|
kAudioObjectPropertyScopeGlobal,
|
|
|
|
kAudioObjectPropertyElementMaster};
|
2019-07-02 13:37:01 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static const AudioObjectPropertyAddress kAudioStreamPropertyAddress = {kAudioDevicePropertyStreams,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster};
|
2019-07-02 13:37:01 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static const AudioObjectPropertyAddress kAudioMasterVolumePropertyAddress = {kAudioDevicePropertyVolumeScalar,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster};
|
2019-07-02 13:37:01 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static const AudioObjectPropertyAddress kAudioLeftVolumePropertyAddress = {kAudioDevicePropertyVolumeScalar, kAudioDevicePropertyScopeOutput, 1};
|
2019-07-02 13:37:01 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static const AudioObjectPropertyAddress kAudioRightVolumePropertyAddress = {kAudioDevicePropertyVolumeScalar, kAudioDevicePropertyScopeOutput, 2};
|
2019-07-02 13:37:01 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static const AudioObjectPropertyAddress kAudioMasterMutedPropertyAddress = {kAudioDevicePropertyMute,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster};
|
2019-07-02 13:37:01 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static const AudioObjectPropertyAddress kAudioMasterDataSourcePropertyAddress = {kAudioDevicePropertyDataSource,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster};
|
2019-07-02 13:37:01 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static const AudioObjectPropertyAddress kAudioDefaultOutputDevicePropertyAddress = {kAudioHardwarePropertyDefaultOutputDevice,
|
|
|
|
kAudioObjectPropertyScopeGlobal,
|
|
|
|
kAudioObjectPropertyElementMaster};
|
2020-12-07 07:55:01 +00:00
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
OSStatus onVolumeChanged(AudioObjectID object, UInt32 numAddresses, const AudioObjectPropertyAddress addresses[], void *context)
|
2019-07-02 13:37:01 +01:00
|
|
|
{
|
2019-07-30 15:14:47 +01:00
|
|
|
Q_UNUSED(object);
|
|
|
|
Q_UNUSED(addresses);
|
|
|
|
Q_UNUSED(numAddresses);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
SystemvolumePlugin *plugin = (SystemvolumePlugin *)context;
|
2019-07-02 13:37:01 +01:00
|
|
|
plugin->updateDeviceVolume(object);
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
OSStatus onMutedChanged(AudioObjectID object, UInt32 numAddresses, const AudioObjectPropertyAddress addresses[], void *context)
|
2019-07-02 13:37:01 +01:00
|
|
|
{
|
2019-07-30 15:14:47 +01:00
|
|
|
Q_UNUSED(object);
|
|
|
|
Q_UNUSED(addresses);
|
|
|
|
Q_UNUSED(numAddresses);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
SystemvolumePlugin *plugin = (SystemvolumePlugin *)context;
|
2019-07-02 13:37:01 +01:00
|
|
|
plugin->updateDeviceMuted(object);
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
2020-12-07 07:55:01 +00:00
|
|
|
OSStatus onDefaultChanged(AudioObjectID object, UInt32 numAddresses, const AudioObjectPropertyAddress addresses[], void *context)
|
|
|
|
{
|
|
|
|
Q_UNUSED(object);
|
|
|
|
Q_UNUSED(addresses);
|
|
|
|
Q_UNUSED(numAddresses);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
SystemvolumePlugin *plugin = (SystemvolumePlugin *)context;
|
2020-12-07 07:55:01 +00:00
|
|
|
plugin->sendSinkList();
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
OSStatus onOutputSourceChanged(AudioObjectID object, UInt32 numAddresses, const AudioObjectPropertyAddress addresses[], void *context)
|
2019-07-02 13:37:01 +01:00
|
|
|
{
|
2019-07-30 15:14:47 +01:00
|
|
|
Q_UNUSED(object);
|
|
|
|
Q_UNUSED(addresses);
|
|
|
|
Q_UNUSED(numAddresses);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
SystemvolumePlugin *plugin = (SystemvolumePlugin *)context;
|
2019-07-02 13:37:01 +01:00
|
|
|
plugin->sendSinkList();
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
AudioObjectID getDefaultOutputDeviceId()
|
|
|
|
{
|
2020-12-07 07:55:01 +00:00
|
|
|
AudioObjectID dataSourceId;
|
|
|
|
UInt32 size = sizeof(dataSourceId);
|
|
|
|
OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &kAudioDefaultOutputDevicePropertyAddress, 0, NULL, &size, &dataSourceId);
|
|
|
|
if (result != noErr)
|
|
|
|
return kAudioDeviceUnknown;
|
|
|
|
|
|
|
|
return dataSourceId;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
UInt32 getDeviceSourceId(AudioObjectID deviceId)
|
|
|
|
{
|
2019-07-02 13:37:01 +01:00
|
|
|
UInt32 dataSourceId;
|
|
|
|
UInt32 size = sizeof(dataSourceId);
|
|
|
|
OSStatus result = AudioObjectGetPropertyData(deviceId, &kAudioMasterDataSourcePropertyAddress, 0, NULL, &size, &dataSourceId);
|
|
|
|
if (result != noErr)
|
|
|
|
return kAudioDeviceUnknown;
|
|
|
|
|
|
|
|
return dataSourceId;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QString translateDeviceSource(AudioObjectID deviceId)
|
|
|
|
{
|
2019-07-02 13:37:01 +01:00
|
|
|
UInt32 sourceId = getDeviceSourceId(deviceId);
|
|
|
|
|
|
|
|
if (sourceId == kAudioDeviceUnknown) {
|
|
|
|
qWarning(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Unknown data source id of device" << deviceId;
|
|
|
|
return QStringLiteral("");
|
|
|
|
}
|
|
|
|
|
|
|
|
CFStringRef sourceName = nullptr;
|
|
|
|
AudioValueTranslation translation;
|
|
|
|
translation.mInputData = &sourceId;
|
|
|
|
translation.mInputDataSize = sizeof(sourceId);
|
|
|
|
translation.mOutputData = &sourceName;
|
|
|
|
translation.mOutputDataSize = sizeof(sourceName);
|
|
|
|
|
|
|
|
UInt32 translationSize = sizeof(AudioValueTranslation);
|
2022-09-10 22:23:52 +01:00
|
|
|
AudioObjectPropertyAddress propertyAddress = {kAudioDevicePropertyDataSourceNameForIDCFString,
|
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster};
|
2019-07-02 13:37:01 +01:00
|
|
|
|
|
|
|
OSStatus result = AudioObjectGetPropertyData(deviceId, &propertyAddress, 0, NULL, &translationSize, &translation);
|
|
|
|
if (result != noErr) {
|
|
|
|
qWarning(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Cannot get description of device" << deviceId;
|
|
|
|
return QStringLiteral("");
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ret = QString::fromCFString(sourceName);
|
|
|
|
CFRelease(sourceName);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
std::vector<AudioObjectID> GetAllOutputAudioDeviceIDs()
|
|
|
|
{
|
2019-07-02 13:37:01 +01:00
|
|
|
std::vector<AudioObjectID> outputDeviceIds;
|
|
|
|
|
|
|
|
UInt32 size = 0;
|
|
|
|
OSStatus result;
|
|
|
|
|
|
|
|
result = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &kAudioHardwarePropertyAddress, 0, NULL, &size);
|
|
|
|
|
|
|
|
if (result != noErr) {
|
2022-09-10 22:23:52 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Failed to read size of property " << kAudioHardwarePropertyDevices << " for device/object "
|
|
|
|
<< kAudioObjectSystemObject;
|
2019-07-02 13:37:01 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
return {};
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
size_t deviceCount = size / sizeof(AudioObjectID);
|
|
|
|
std::vector<AudioObjectID> deviceIds(deviceCount);
|
2019-07-02 13:37:01 +01:00
|
|
|
result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &kAudioHardwarePropertyAddress, 0, NULL, &size, deviceIds.data());
|
|
|
|
if (result != noErr) {
|
2022-09-10 22:23:52 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Failed to read object IDs from property " << kAudioHardwarePropertyDevices << " for device/object "
|
|
|
|
<< kAudioObjectSystemObject;
|
2019-07-02 13:37:01 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (AudioDeviceID deviceId : deviceIds) {
|
|
|
|
UInt32 streamCount = 0;
|
|
|
|
result = AudioObjectGetPropertyDataSize(deviceId, &kAudioStreamPropertyAddress, 0, NULL, &streamCount);
|
|
|
|
|
|
|
|
if (result == noErr && streamCount > 0) {
|
|
|
|
outputDeviceIds.push_back(deviceId);
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Device" << deviceId << "added";
|
|
|
|
} else {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Device" << deviceId << "dropped";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outputDeviceIds;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
SystemvolumePlugin::SystemvolumePlugin(QObject *parent, const QVariantList &args)
|
|
|
|
: KdeConnectPlugin(parent, args)
|
|
|
|
, m_sinksMap()
|
|
|
|
{
|
|
|
|
}
|
2019-07-02 13:37:01 +01:00
|
|
|
|
2020-12-07 07:55:01 +00:00
|
|
|
SystemvolumePlugin::~SystemvolumePlugin()
|
|
|
|
{
|
|
|
|
AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &kAudioDefaultOutputDevicePropertyAddress, &onDefaultChanged, (void *)this);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool SystemvolumePlugin::receivePacket(const NetworkPacket &np)
|
2019-07-02 13:37:01 +01:00
|
|
|
{
|
|
|
|
if (np.has(QStringLiteral("requestSinks"))) {
|
|
|
|
sendSinkList();
|
|
|
|
} else {
|
|
|
|
QString name = np.get<QString>(QStringLiteral("name"));
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
if (m_sinksMap.contains(name)) {
|
2019-07-02 13:37:01 +01:00
|
|
|
if (np.has(QStringLiteral("volume"))) {
|
2019-07-30 15:14:47 +01:00
|
|
|
m_sinksMap[name]->setVolume(np.get<int>(QStringLiteral("volume")) / 100.0);
|
2019-07-02 13:37:01 +01:00
|
|
|
}
|
|
|
|
if (np.has(QStringLiteral("muted"))) {
|
2019-07-30 15:14:47 +01:00
|
|
|
m_sinksMap[name]->setMuted(np.get<bool>(QStringLiteral("muted")));
|
2019-07-02 13:37:01 +01:00
|
|
|
}
|
2020-12-07 07:55:38 +00:00
|
|
|
if (np.has(QStringLiteral("enabled"))) {
|
|
|
|
m_sinksMap[name]->setDefault(np.get<bool>(QStringLiteral("enabled")));
|
|
|
|
}
|
2019-07-02 13:37:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemvolumePlugin::sendSinkList()
|
|
|
|
{
|
|
|
|
QJsonDocument document;
|
|
|
|
QJsonArray array;
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
if (!m_sinksMap.empty()) {
|
|
|
|
for (MacOSCoreAudioDevice *sink : m_sinksMap) {
|
2019-07-02 13:37:01 +01:00
|
|
|
delete sink;
|
|
|
|
}
|
2019-07-30 15:14:47 +01:00
|
|
|
m_sinksMap.clear();
|
2019-07-02 13:37:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<AudioObjectID> deviceIds = GetAllOutputAudioDeviceIDs();
|
|
|
|
|
|
|
|
for (AudioDeviceID deviceId : deviceIds) {
|
|
|
|
MacOSCoreAudioDevice *audioDevice = new MacOSCoreAudioDevice(deviceId);
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
audioDevice->m_description = translateDeviceSource(deviceId);
|
2019-07-02 13:37:01 +01:00
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
m_sinksMap.insert(QStringLiteral("default-") + QString::number(deviceId), audioDevice);
|
2019-07-02 13:37:01 +01:00
|
|
|
|
|
|
|
// Add volume change listener
|
|
|
|
AudioObjectAddPropertyListener(deviceId, &kAudioMasterVolumePropertyAddress, &onVolumeChanged, (void *)this);
|
|
|
|
|
|
|
|
AudioObjectAddPropertyListener(deviceId, &kAudioLeftVolumePropertyAddress, &onVolumeChanged, (void *)this);
|
|
|
|
AudioObjectAddPropertyListener(deviceId, &kAudioRightVolumePropertyAddress, &onVolumeChanged, (void *)this);
|
|
|
|
|
|
|
|
// Add muted change listener
|
|
|
|
AudioObjectAddPropertyListener(deviceId, &kAudioMasterMutedPropertyAddress, &onMutedChanged, (void *)this);
|
|
|
|
|
|
|
|
// Add data source change listerner
|
|
|
|
AudioObjectAddPropertyListener(deviceId, &kAudioMasterDataSourcePropertyAddress, &onOutputSourceChanged, (void *)this);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QJsonObject sinkObject{{QStringLiteral("name"), QStringLiteral("default-") + QString::number(deviceId)},
|
|
|
|
{QStringLiteral("muted"), audioDevice->isMuted()},
|
|
|
|
{QStringLiteral("description"), audioDevice->m_description},
|
|
|
|
{QStringLiteral("volume"), audioDevice->volume() * 100},
|
|
|
|
{QStringLiteral("maxVolume"), 100},
|
|
|
|
{QStringLiteral("enabled"), audioDevice->isDefault()}};
|
2019-07-02 13:37:01 +01:00
|
|
|
|
|
|
|
array.append(sinkObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.setArray(array);
|
|
|
|
|
|
|
|
NetworkPacket np(PACKET_TYPE_SYSTEMVOLUME);
|
|
|
|
np.set<QJsonDocument>(QStringLiteral("sinkList"), document);
|
|
|
|
sendPacket(np);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemvolumePlugin::connected()
|
|
|
|
{
|
2020-12-07 07:55:01 +00:00
|
|
|
AudioObjectAddPropertyListener(kAudioObjectSystemObject, &kAudioDefaultOutputDevicePropertyAddress, &onDefaultChanged, (void *)this);
|
2019-07-02 13:37:01 +01:00
|
|
|
sendSinkList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemvolumePlugin::updateDeviceMuted(AudioDeviceID deviceId)
|
|
|
|
{
|
2019-07-30 15:14:47 +01:00
|
|
|
for (MacOSCoreAudioDevice *sink : m_sinksMap) {
|
|
|
|
if (sink->m_deviceId == deviceId) {
|
2019-07-02 13:37:01 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_SYSTEMVOLUME);
|
|
|
|
np.set<bool>(QStringLiteral("muted"), (bool)(sink->isMuted()));
|
|
|
|
np.set<int>(QStringLiteral("volume"), (int)(sink->volume() * 100));
|
|
|
|
np.set<QString>(QStringLiteral("name"), QStringLiteral("default-") + QString::number(deviceId));
|
|
|
|
sendPacket(np);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Device" << deviceId << "not found while update mute state";
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemvolumePlugin::updateDeviceVolume(AudioDeviceID deviceId)
|
|
|
|
{
|
2019-07-30 15:14:47 +01:00
|
|
|
for (MacOSCoreAudioDevice *sink : m_sinksMap) {
|
|
|
|
if (sink->m_deviceId == deviceId) {
|
2019-07-02 13:37:01 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_SYSTEMVOLUME);
|
|
|
|
np.set<int>(QStringLiteral("volume"), (int)(sink->volume() * 100));
|
|
|
|
np.set<QString>(QStringLiteral("name"), QStringLiteral("default-") + QString::number(deviceId));
|
|
|
|
sendPacket(np);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Device" << deviceId << "not found while update volume";
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
MacOSCoreAudioDevice::MacOSCoreAudioDevice(AudioDeviceID deviceId)
|
|
|
|
: m_deviceId(deviceId)
|
2019-07-02 13:37:01 +01:00
|
|
|
{
|
|
|
|
updateType();
|
|
|
|
}
|
|
|
|
|
|
|
|
MacOSCoreAudioDevice::~MacOSCoreAudioDevice()
|
|
|
|
{
|
|
|
|
// Volume listener
|
2022-09-10 22:23:52 +01:00
|
|
|
AudioObjectRemovePropertyListener(m_deviceId, &kAudioMasterVolumePropertyAddress, &onVolumeChanged, (void *)this);
|
|
|
|
AudioObjectRemovePropertyListener(m_deviceId, &kAudioLeftVolumePropertyAddress, &onVolumeChanged, (void *)this);
|
|
|
|
AudioObjectRemovePropertyListener(m_deviceId, &kAudioRightVolumePropertyAddress, &onVolumeChanged, (void *)this);
|
2019-07-02 13:37:01 +01:00
|
|
|
|
|
|
|
// Muted listener
|
2022-09-10 22:23:52 +01:00
|
|
|
AudioObjectRemovePropertyListener(m_deviceId, &kAudioMasterMutedPropertyAddress, &onMutedChanged, (void *)this);
|
2019-07-02 13:37:01 +01:00
|
|
|
|
|
|
|
// Data source listener
|
2022-09-10 22:23:52 +01:00
|
|
|
AudioObjectRemovePropertyListener(m_deviceId, &kAudioMasterDataSourcePropertyAddress, &onOutputSourceChanged, (void *)this);
|
2019-07-02 13:37:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MacOSCoreAudioDevice::setVolume(float volume)
|
|
|
|
{
|
|
|
|
OSStatus result;
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
if (m_deviceId == kAudioObjectUnknown) {
|
2019-07-02 13:37:01 +01:00
|
|
|
qWarning(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Unable to set volume of Unknown Device";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
if (m_isStereo) {
|
|
|
|
result = AudioObjectSetPropertyData(m_deviceId, &kAudioLeftVolumePropertyAddress, 0, NULL, sizeof(volume), &volume);
|
|
|
|
result = AudioObjectSetPropertyData(m_deviceId, &kAudioRightVolumePropertyAddress, 0, NULL, sizeof(volume), &volume);
|
2019-07-02 13:37:01 +01:00
|
|
|
} else {
|
2019-07-30 15:14:47 +01:00
|
|
|
result = AudioObjectSetPropertyData(m_deviceId, &kAudioMasterVolumePropertyAddress, 0, NULL, sizeof(volume), &volume);
|
2019-07-02 13:37:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result != noErr) {
|
2019-07-30 15:14:47 +01:00
|
|
|
qWarning(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Unable to set volume of Device" << m_deviceId << "to" << volume;
|
2019-07-02 13:37:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacOSCoreAudioDevice::setMuted(bool muted)
|
|
|
|
{
|
2019-07-30 15:14:47 +01:00
|
|
|
if (m_deviceId == kAudioObjectUnknown) {
|
2019-07-02 13:37:01 +01:00
|
|
|
qWarning(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Unable to mute an Unknown Device";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UInt32 mutedValue = muted ? 1 : 0;
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
OSStatus result = AudioObjectSetPropertyData(m_deviceId, &kAudioMasterMutedPropertyAddress, 0, NULL, sizeof(mutedValue), &mutedValue);
|
2019-07-02 13:37:01 +01:00
|
|
|
|
|
|
|
if (result != noErr) {
|
2019-07-30 15:14:47 +01:00
|
|
|
qWarning(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Unable to set muted state of Device" << m_deviceId << "to" << muted;
|
2019-07-02 13:37:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 07:55:01 +00:00
|
|
|
void MacOSCoreAudioDevice::setDefault(bool enabled)
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!enabled)
|
|
|
|
return;
|
2020-12-07 07:55:01 +00:00
|
|
|
|
|
|
|
if (m_deviceId == kAudioObjectUnknown) {
|
|
|
|
qWarning(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Unable to set an Unknown Device as default output";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
OSStatus result = AudioObjectSetPropertyData(kAudioObjectSystemObject, &kAudioDefaultOutputDevicePropertyAddress, 0, NULL, sizeof(m_deviceId), &m_deviceId);
|
|
|
|
|
|
|
|
if (result != noErr) {
|
|
|
|
qWarning(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Unable to set default state of Device" << m_deviceId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 13:37:01 +01:00
|
|
|
float MacOSCoreAudioDevice::volume()
|
|
|
|
{
|
|
|
|
OSStatus result;
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
if (m_deviceId == kAudioObjectUnknown) {
|
2019-07-02 13:37:01 +01:00
|
|
|
qWarning(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Unable to get volume of Unknown Device";
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float volume = 0.0;
|
|
|
|
UInt32 volumeDataSize = sizeof(volume);
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
if (m_isStereo) {
|
2019-07-02 13:37:01 +01:00
|
|
|
// Try to get steoreo device volume
|
2019-07-30 15:14:47 +01:00
|
|
|
result = AudioObjectGetPropertyData(m_deviceId, &kAudioLeftVolumePropertyAddress, 0, NULL, &volumeDataSize, &volume);
|
2019-07-02 13:37:01 +01:00
|
|
|
} else {
|
|
|
|
// Try to get master volume
|
2019-07-30 15:14:47 +01:00
|
|
|
result = AudioObjectGetPropertyData(m_deviceId, &kAudioMasterVolumePropertyAddress, 0, NULL, &volumeDataSize, &volume);
|
2019-07-02 13:37:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result != noErr) {
|
2019-07-30 15:14:47 +01:00
|
|
|
qWarning(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Unable to get volume of Device" << m_deviceId;
|
2019-07-02 13:37:01 +01:00
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MacOSCoreAudioDevice::isMuted()
|
|
|
|
{
|
2019-07-30 15:14:47 +01:00
|
|
|
if (m_deviceId == kAudioObjectUnknown) {
|
2019-07-02 13:37:01 +01:00
|
|
|
qWarning(KDECONNECT_PLUGIN_SYSTEMVOLUME) << "Unable to get muted state of an Unknown Device";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
UInt32 muted = 0;
|
|
|
|
UInt32 muteddataSize = sizeof(muted);
|
|
|
|
|
2019-07-30 15:14:47 +01:00
|
|
|
AudioObjectGetPropertyData(m_deviceId, &kAudioMasterMutedPropertyAddress, 0, NULL, &muteddataSize, &muted);
|
2019-07-02 13:37:01 +01:00
|
|
|
|
|
|
|
return muted == 1;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool MacOSCoreAudioDevice::isDefault()
|
|
|
|
{
|
2020-12-07 07:55:01 +00:00
|
|
|
AudioObjectID defaultDeviceId = getDefaultOutputDeviceId();
|
|
|
|
return m_deviceId == defaultDeviceId;
|
|
|
|
}
|
|
|
|
|
2019-07-02 13:37:01 +01:00
|
|
|
void MacOSCoreAudioDevice::updateType()
|
|
|
|
{
|
|
|
|
// Try to get volume from left channel to check if it's a stereo device
|
|
|
|
float volume = 0.0;
|
|
|
|
UInt32 volumeDataSize = sizeof(volume);
|
2019-07-30 15:14:47 +01:00
|
|
|
OSStatus result = AudioObjectGetPropertyData(m_deviceId, &kAudioLeftVolumePropertyAddress, 0, NULL, &volumeDataSize, &volume);
|
2019-07-02 13:37:01 +01:00
|
|
|
if (result == noErr) {
|
2019-07-30 15:14:47 +01:00
|
|
|
m_isStereo = true;
|
2019-07-02 13:37:01 +01:00
|
|
|
} else {
|
2019-07-30 15:14:47 +01:00
|
|
|
m_isStereo = false;
|
2019-07-02 13:37:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 09:15:11 +01:00
|
|
|
#include "moc_systemvolumeplugin-macos.cpp"
|
2019-07-02 13:37:01 +01:00
|
|
|
#include "systemvolumeplugin-macos.moc"
|