2019-08-21 03:38:35 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
|
|
|
* SPDX-FileCopyrightText: 2019 Piyush Aggarwal <piyushaggarwal002@gmail.com>
|
2019-08-21 03:38:35 +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-08-21 03:38:35 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pausemusicplugin-win.h"
|
|
|
|
|
2020-05-26 17:55:47 +01:00
|
|
|
#include "plugin_pausemusic_debug.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <KPluginFactory>
|
2019-08-21 03:38:35 +01:00
|
|
|
|
2021-06-22 08:27:13 +01:00
|
|
|
#include <Functiondiscoverykeys_devpkey.h>
|
|
|
|
|
2019-08-21 03:38:35 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(PauseMusicPlugin, "kdeconnect_pausemusic.json")
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
PauseMusicPlugin::PauseMusicPlugin(QObject *parent, const QVariantList &args)
|
2019-08-21 03:38:35 +01:00
|
|
|
: KdeConnectPlugin(parent, args)
|
2023-07-25 16:22:20 +01:00
|
|
|
, sessionManager(GlobalSystemMediaTransportControlsSessionManager::RequestAsync().get())
|
2019-08-21 03:38:35 +01:00
|
|
|
{
|
2021-06-22 08:27:13 +01:00
|
|
|
CoInitialize(nullptr);
|
|
|
|
deviceEnumerator = nullptr;
|
|
|
|
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
|
|
|
|
valid = (hr == S_OK);
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!valid) {
|
2021-06-22 08:27:13 +01:00
|
|
|
qWarning("Initialization failed: Failed to create MMDeviceEnumerator");
|
|
|
|
qWarning("Error Code: %lx", hr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PauseMusicPlugin::updateSinksList()
|
|
|
|
{
|
|
|
|
sinksList.clear();
|
|
|
|
if (!valid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
IMMDeviceCollection *devices = nullptr;
|
|
|
|
HRESULT hr = deviceEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &devices);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (hr != S_OK) {
|
2021-06-22 08:27:13 +01:00
|
|
|
qWarning("Failed to Enumumerate AudioEndpoints");
|
|
|
|
qWarning("Error Code: %lx", hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int deviceCount;
|
|
|
|
devices->GetCount(&deviceCount);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
for (unsigned int i = 0; i < deviceCount; i++) {
|
2021-06-22 08:27:13 +01:00
|
|
|
IMMDevice *device = nullptr;
|
|
|
|
|
|
|
|
IPropertyStore *deviceProperties = nullptr;
|
|
|
|
PROPVARIANT deviceProperty;
|
|
|
|
QString name;
|
|
|
|
|
|
|
|
IAudioEndpointVolume *endpoint = nullptr;
|
|
|
|
|
|
|
|
// Get Properties
|
|
|
|
devices->Item(i, &device);
|
|
|
|
device->OpenPropertyStore(STGM_READ, &deviceProperties);
|
|
|
|
|
|
|
|
deviceProperties->GetValue(PKEY_Device_FriendlyName, &deviceProperty);
|
|
|
|
name = QString::fromWCharArray(deviceProperty.pwszVal);
|
2022-09-10 22:23:52 +01:00
|
|
|
// PropVariantClear(&deviceProperty);
|
2021-06-22 08:27:13 +01:00
|
|
|
|
|
|
|
hr = device->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void **)&endpoint);
|
2022-09-10 22:23:52 +01:00
|
|
|
if (hr != S_OK) {
|
2021-06-22 08:27:13 +01:00
|
|
|
qWarning() << "Failed to create IAudioEndpointVolume for device:" << name;
|
|
|
|
qWarning("Error Code: %lx", hr);
|
|
|
|
|
|
|
|
device->Release();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register Callback
|
|
|
|
if (!sinksList.contains(name)) {
|
|
|
|
sinksList[name] = endpoint;
|
|
|
|
}
|
|
|
|
device->Release();
|
|
|
|
}
|
|
|
|
devices->Release();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void PauseMusicPlugin::updatePlayersList()
|
|
|
|
{
|
2021-06-22 08:27:13 +01:00
|
|
|
playersList.clear();
|
2023-07-25 16:22:20 +01:00
|
|
|
auto sessions = sessionManager.GetSessions();
|
2022-09-10 22:23:52 +01:00
|
|
|
for (uint32_t i = 0; i < sessions.Size(); i++) {
|
2021-06-22 08:27:13 +01:00
|
|
|
const auto player = sessions.GetAt(i);
|
|
|
|
auto playerName = player.SourceAppUserModelId();
|
|
|
|
|
|
|
|
QString uniqueName = QString::fromWCharArray(playerName.c_str());
|
|
|
|
for (int i = 2; playersList.contains(uniqueName); ++i) {
|
|
|
|
uniqueName += QStringLiteral(" [") + QString::number(i) + QStringLiteral("]");
|
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
playersList.insert(uniqueName, player);
|
2021-06-22 08:27:13 +01:00
|
|
|
}
|
2019-08-21 03:38:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PauseMusicPlugin::~PauseMusicPlugin()
|
|
|
|
{
|
|
|
|
CoUninitialize();
|
|
|
|
}
|
|
|
|
|
2023-07-31 08:25:45 +01:00
|
|
|
void PauseMusicPlugin::receivePacket(const NetworkPacket &np)
|
2019-08-21 03:38:35 +01:00
|
|
|
{
|
2020-09-09 23:24:36 +01:00
|
|
|
bool pauseOnlyWhenTalking = config()->getBool(QStringLiteral("conditionTalking"), false);
|
2019-08-21 03:38:35 +01:00
|
|
|
|
|
|
|
if (pauseOnlyWhenTalking) {
|
2020-09-09 23:34:31 +01:00
|
|
|
if (np.get<QString>(QStringLiteral("event")) != QLatin1String("talking")) {
|
2023-07-31 08:25:45 +01:00
|
|
|
return;
|
2019-08-21 03:38:35 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-10 22:23:52 +01:00
|
|
|
if (np.get<QString>(QStringLiteral("event")) != QLatin1String("ringing") && np.get<QString>(QStringLiteral("event")) != QLatin1String("talking")) {
|
2023-07-31 08:25:45 +01:00
|
|
|
return;
|
2019-08-21 03:38:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 23:34:31 +01:00
|
|
|
bool pauseConditionFulfilled = !np.get<bool>(QStringLiteral("isCancel"));
|
2019-08-21 03:38:35 +01:00
|
|
|
|
2021-06-22 08:27:13 +01:00
|
|
|
bool pause = config()->getBool(QStringLiteral("actionPause"), true);
|
|
|
|
bool mute = config()->getBool(QStringLiteral("actionMute"), false);
|
2019-08-21 03:38:35 +01:00
|
|
|
|
2020-09-09 23:24:36 +01:00
|
|
|
const bool autoResume = config()->getBool(QStringLiteral("actionResume"), true);
|
2020-05-20 22:44:33 +01:00
|
|
|
|
2019-08-21 03:38:35 +01:00
|
|
|
if (pauseConditionFulfilled) {
|
|
|
|
if (mute) {
|
2021-06-22 08:27:13 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Muting all the unmuted sinks";
|
|
|
|
this->updateSinksList();
|
|
|
|
QHashIterator<QString, IAudioEndpointVolume *> sinksIterator(sinksList);
|
2022-09-10 22:23:52 +01:00
|
|
|
while (sinksIterator.hasNext()) {
|
2021-06-22 08:27:13 +01:00
|
|
|
sinksIterator.next();
|
|
|
|
BOOL muted;
|
|
|
|
sinksIterator.value()->GetMute(&muted);
|
|
|
|
if (!((bool)muted)) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Trying to mute " << sinksIterator.key();
|
2022-09-10 22:23:52 +01:00
|
|
|
if (sinksIterator.value()->SetMute(true, NULL) == S_OK) {
|
2021-06-22 08:27:13 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Muted " << sinksIterator.key();
|
|
|
|
mutedSinks.insert(sinksIterator.key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 03:38:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pause) {
|
2021-06-22 08:27:13 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Pausing all the playing media";
|
|
|
|
this->updatePlayersList();
|
|
|
|
QHashIterator<QString, GlobalSystemMediaTransportControlsSession> playersIterator(playersList);
|
2022-09-10 22:23:52 +01:00
|
|
|
while (playersIterator.hasNext()) {
|
2021-06-22 08:27:13 +01:00
|
|
|
playersIterator.next();
|
2022-09-10 22:23:52 +01:00
|
|
|
auto &player = playersIterator.value();
|
|
|
|
auto &playerName = playersIterator.key();
|
2021-06-22 08:27:13 +01:00
|
|
|
|
|
|
|
auto playbackInfo = player.GetPlaybackInfo();
|
|
|
|
auto playbackControls = playbackInfo.Controls();
|
|
|
|
if (playbackInfo.PlaybackStatus() == GlobalSystemMediaTransportControlsSessionPlaybackStatus::Playing) {
|
|
|
|
if (playbackControls.IsPauseEnabled()) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Trying to pause " << playerName;
|
|
|
|
if (player.TryPauseAsync().get()) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Paused " << playerName;
|
|
|
|
pausedSources.insert(playerName);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Pause not supported by the app! Trying to stop " << playerName;
|
|
|
|
if (player.TryStopAsync().get()) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Stopped " << playerName;
|
|
|
|
pausedSources.insert(playerName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 03:38:35 +01:00
|
|
|
}
|
2021-06-22 08:27:13 +01:00
|
|
|
} else if (autoResume) {
|
2019-08-21 03:38:35 +01:00
|
|
|
if (mute) {
|
2022-09-10 22:23:52 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Unmuting sinks";
|
|
|
|
QHashIterator<QString, IAudioEndpointVolume *> sinksIterator(sinksList);
|
|
|
|
while (sinksIterator.hasNext()) {
|
|
|
|
sinksIterator.next();
|
|
|
|
if (mutedSinks.contains(sinksIterator.key())) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Trying to unmute " << sinksIterator.key();
|
|
|
|
if (sinksIterator.value()->SetMute(false, NULL) == S_OK) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Unmuted " << sinksIterator.key();
|
2021-06-22 08:27:13 +01:00
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
mutedSinks.remove(sinksIterator.key());
|
2021-06-22 08:27:13 +01:00
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
}
|
2019-08-21 03:38:35 +01:00
|
|
|
}
|
|
|
|
if (pause) {
|
2021-06-22 08:27:13 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Unpausing media";
|
|
|
|
QHashIterator<QString, GlobalSystemMediaTransportControlsSession> playersIterator(playersList);
|
2022-09-10 22:23:52 +01:00
|
|
|
while (playersIterator.hasNext()) {
|
2021-06-22 08:27:13 +01:00
|
|
|
playersIterator.next();
|
2022-09-10 22:23:52 +01:00
|
|
|
auto &player = playersIterator.value();
|
|
|
|
auto &playerName = playersIterator.key();
|
2021-06-22 08:27:13 +01:00
|
|
|
|
|
|
|
auto playbackInfo = player.GetPlaybackInfo();
|
|
|
|
auto playbackControls = playbackInfo.Controls();
|
|
|
|
if (pausedSources.contains({playerName})) {
|
|
|
|
if (playbackInfo.PlaybackStatus() == GlobalSystemMediaTransportControlsSessionPlaybackStatus::Paused) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Trying to resume " << playerName;
|
|
|
|
if (player.TryPlayAsync().get()) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Resumed " << playerName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pausedSources.remove(playerName);
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 03:38:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 09:15:11 +01:00
|
|
|
#include "moc_pausemusicplugin-win.cpp"
|
2019-08-21 03:38:35 +01:00
|
|
|
#include "pausemusicplugin-win.moc"
|