bool PauseMusicPlugin::receivePacket(const NetworkPacket &np)
{
bool pauseOnlyWhenTalking = config()->getBool(QStringLiteral("conditionTalking"), false);
if (pauseOnlyWhenTalking) {
if (np.get(QStringLiteral("event")) != QLatin1String("talking")) {
return true;
}
} else {
if (np.get(QStringLiteral("event")) != QLatin1String("ringing") && np.get(QStringLiteral("event")) != QLatin1String("talking")) {
return true;
}
}
bool pauseConditionFulfilled = !np.get(QStringLiteral("isCancel"));
bool pause = config()->getBool(QStringLiteral("actionPause"), true);
bool mute = config()->getBool(QStringLiteral("actionMute"), false);
const bool autoResume = config()->getBool(QStringLiteral("actionResume"), true);
if (pauseConditionFulfilled) {
if (mute) {
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Muting all the unmuted sinks";
this->updateSinksList();
QHashIterator sinksIterator(sinksList);
while (sinksIterator.hasNext()) {
sinksIterator.next();
BOOL muted;
sinksIterator.value()->GetMute(&muted);
if (!((bool)muted)) {
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Trying to mute " << sinksIterator.key();
if (sinksIterator.value()->SetMute(true, NULL) == S_OK) {
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Muted " << sinksIterator.key();
mutedSinks.insert(sinksIterator.key());
}
}
}
}
if (pause) {
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Pausing all the playing media";
this->updatePlayersList();
QHashIterator playersIterator(playersList);
while (playersIterator.hasNext()) {
playersIterator.next();
auto &player = playersIterator.value();
auto &playerName = playersIterator.key();
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);
}
}
}
}
}
} else if (autoResume) {
if (mute) {
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Unmuting sinks";
QHashIterator 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();
}
mutedSinks.remove(sinksIterator.key());
}
}
}
if (pause) {
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Unpausing media";
QHashIterator playersIterator(playersList);
while (playersIterator.hasNext()) {
playersIterator.next();
auto &player = playersIterator.value();
auto &playerName = playersIterator.key();
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);
}
}
}
}
return true;
}
This method receives a NetworkPacket object and checks if the packet is allowed to be pauseed, mute and resume.
If the condition is fulfilled and the current event is ringing, and the current event is not reachable, and the current event is not reachable, and the current event is not reachable, and the current event is not reachable, and the current event is not reachable, and the current event is ignored, and the function returns true.