From 1b622de1f1792ab2d110fec181e408129db07253 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Sat, 28 Apr 2018 00:22:45 +0200 Subject: [PATCH] Support multiple players in MprisRemote Summary: Current code only supports one player Test Plan: Run two media sessions. Dbus inspection works fine, UI has some issues with values not updated, but out of scope for this diff Reviewers: #kde_connect, apol Reviewed By: #kde_connect, apol Subscribers: apol, #kde_connect Tags: #kde_connect Differential Revision: https://phabricator.kde.org/D12546 --- interfaces/dbusinterfaces.h | 4 + plugins/mprisremote/CMakeLists.txt | 2 +- plugins/mprisremote/mprisremoteplayer.cpp | 106 ++++++++++++++++++++ plugins/mprisremote/mprisremoteplayer.h | 53 ++++++++++ plugins/mprisremote/mprisremoteplugin.cpp | 116 +++++++++++++++------- plugins/mprisremote/mprisremoteplugin.h | 30 +++--- 6 files changed, 262 insertions(+), 49 deletions(-) create mode 100644 plugins/mprisremote/mprisremoteplayer.cpp create mode 100644 plugins/mprisremote/mprisremoteplayer.h diff --git a/interfaces/dbusinterfaces.h b/interfaces/dbusinterfaces.h index fbf2395f1..434e5981e 100644 --- a/interfaces/dbusinterfaces.h +++ b/interfaces/dbusinterfaces.h @@ -134,6 +134,10 @@ class KDECONNECTINTERFACES_EXPORT MprisDbusInterface Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY propertiesChangedProxy) Q_PROPERTY(int length READ length NOTIFY propertiesChangedProxy) Q_PROPERTY(QString nowPlaying READ nowPlaying NOTIFY propertiesChangedProxy) + Q_PROPERTY(QString title READ title NOTIFY propertiesChangedProxy) + Q_PROPERTY(QString artist READ artist NOTIFY propertiesChangedProxy) + Q_PROPERTY(QString album READ album NOTIFY propertiesChangedProxy) + Q_PROPERTY(QStringList playerList READ playerList NOTIFY propertiesChangedProxy) Q_PROPERTY(int volume READ volume WRITE setVolume NOTIFY propertiesChangedProxy) Q_PROPERTY(int position READ position WRITE setPosition NOTIFY propertiesChangedProxy) diff --git a/plugins/mprisremote/CMakeLists.txt b/plugins/mprisremote/CMakeLists.txt index b4b7a2b1d..5e2b1caab 100644 --- a/plugins/mprisremote/CMakeLists.txt +++ b/plugins/mprisremote/CMakeLists.txt @@ -1,4 +1,4 @@ -kdeconnect_add_plugin(kdeconnect_mprisremote JSON kdeconnect_mprisremote.json SOURCES mprisremoteplugin.cpp) +kdeconnect_add_plugin(kdeconnect_mprisremote JSON kdeconnect_mprisremote.json SOURCES mprisremoteplugin.cpp mprisremoteplayer.cpp) target_link_libraries(kdeconnect_mprisremote kdeconnectcore diff --git a/plugins/mprisremote/mprisremoteplayer.cpp b/plugins/mprisremote/mprisremoteplayer.cpp new file mode 100644 index 000000000..cf520d7bd --- /dev/null +++ b/plugins/mprisremote/mprisremoteplayer.cpp @@ -0,0 +1,106 @@ +/** + * Copyright 2018 Nicolas Fella + * + * This program 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 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program 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 this program. If not, see . + */ + +#include +#include + +#include "mprisremoteplayer.h" + +MprisRemotePlayer::MprisRemotePlayer() : + m_playing(false) + , m_nowPlaying() + , m_volume(50) + , m_length(0) + , m_lastPosition(0) + , m_lastPositionTime() + , m_title() + , m_artist() + , m_album() +{ +} + +MprisRemotePlayer::~MprisRemotePlayer() +{ +} + +void MprisRemotePlayer::parseNetworkPacket(const NetworkPacket& np) +{ + m_nowPlaying = np.get(QStringLiteral("nowPlaying"), m_nowPlaying); + m_title = np.get(QStringLiteral("title"), m_title); + m_artist = np.get(QStringLiteral("artist"), m_artist); + m_album = np.get(QStringLiteral("album"), m_album); + m_volume = np.get(QStringLiteral("volume"), m_volume); + m_length = np.get(QStringLiteral("length"), m_length); + if(np.has(QStringLiteral("pos"))){ + m_lastPosition = np.get(QStringLiteral("pos"), m_lastPosition); + m_lastPositionTime = QDateTime::currentMSecsSinceEpoch(); + } + m_playing = np.get(QStringLiteral("isPlaying"), m_playing); +} + +long MprisRemotePlayer::position() const +{ + if(m_playing) { + return m_lastPosition + (QDateTime::currentMSecsSinceEpoch() - m_lastPositionTime); + } else { + return m_lastPosition; + } +} + +void MprisRemotePlayer::setPosition(long position) +{ + m_lastPosition = position; + m_lastPositionTime = QDateTime::currentMSecsSinceEpoch(); +} + +int MprisRemotePlayer::volume() const +{ + return m_volume; +} + +long int MprisRemotePlayer::length() const +{ + return m_length; +} + +bool MprisRemotePlayer::playing() const +{ + return m_playing; +} + +QString MprisRemotePlayer::nowPlaying() const +{ + return m_nowPlaying; +} + +QString MprisRemotePlayer::title() const +{ + return m_title; +} + +QString MprisRemotePlayer::artist() const +{ + return m_artist; +} + +QString MprisRemotePlayer::album() const +{ + return m_album; +} diff --git a/plugins/mprisremote/mprisremoteplayer.h b/plugins/mprisremote/mprisremoteplayer.h new file mode 100644 index 000000000..5cb4a6ae5 --- /dev/null +++ b/plugins/mprisremote/mprisremoteplayer.h @@ -0,0 +1,53 @@ +/** + * Copyright 2018 Nicolas Fella + * + * This program 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 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program 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 this program. If not, see . + */ +#pragma once + +#include + +class MprisRemotePlayer { + +public: + explicit MprisRemotePlayer(); + virtual ~MprisRemotePlayer(); + + void parseNetworkPacket(const NetworkPacket& np); + long position() const; + void setPosition(long position); + int volume() const; + long length() const; + bool playing() const; + QString nowPlaying() const; + QString title() const; + QString artist() const; + QString album() const; + +private: + + QString id; + bool m_playing; + QString m_nowPlaying; + int m_volume; + long m_length; + long m_lastPosition; + qint64 m_lastPositionTime; + QString m_title; + QString m_artist; + QString m_album; +}; diff --git a/plugins/mprisremote/mprisremoteplugin.cpp b/plugins/mprisremote/mprisremoteplugin.cpp index 3f12029ac..e877e6600 100644 --- a/plugins/mprisremote/mprisremoteplugin.cpp +++ b/plugins/mprisremote/mprisremoteplugin.cpp @@ -35,14 +35,8 @@ Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_MPRISREMOTE, "kdeconnect.plugin.mprisremote MprisRemotePlugin::MprisRemotePlugin(QObject* parent, const QVariantList& args) : KdeConnectPlugin(parent, args) - , m_player() - , m_playing(false) - , m_nowPlaying() - , m_volume(50) - , m_length(0) - , m_lastPosition(0) - , m_lastPositionTime() - , m_playerList() + , m_currentPlayer() + , m_players() { } @@ -55,21 +49,24 @@ bool MprisRemotePlugin::receivePacket(const NetworkPacket& np) if (np.type() != PACKET_TYPE_MPRIS) return false; - if (np.has(QStringLiteral("nowPlaying")) || np.has(QStringLiteral("volume")) || np.has(QStringLiteral("isPlaying")) || np.has(QStringLiteral("length")) || np.has(QStringLiteral("pos"))) { - if (np.get(QStringLiteral("player")) == m_player) { - m_nowPlaying = np.get(QStringLiteral("nowPlaying"), m_nowPlaying); - m_volume = np.get(QStringLiteral("volume"), m_volume); - m_length = np.get(QStringLiteral("length"), m_length); - if(np.has(QStringLiteral("pos"))){ - m_lastPosition = np.get(QStringLiteral("pos"), m_lastPosition); - m_lastPositionTime = QDateTime::currentMSecsSinceEpoch(); - } - m_playing = np.get(QStringLiteral("isPlaying"), m_playing); - } + if (np.has(QStringLiteral("player"))) { + m_players[m_currentPlayer]->parseNetworkPacket(np); } if (np.has(QStringLiteral("playerList"))) { - m_playerList = np.get(QStringLiteral("playerList"), QStringList()); + QStringList players = np.get(QStringLiteral("playerList")); + qDeleteAll(m_players); + m_players.clear(); + for (const QString& player : players) { + m_players[player] = new MprisRemotePlayer(); + } + + if (m_players.empty()) { + m_currentPlayer = QString(); + } else if (!m_players.contains(m_currentPlayer)) { + m_currentPlayer = m_players.keys().first(); + } + } Q_EMIT propertiesChanged(); @@ -78,11 +75,8 @@ bool MprisRemotePlugin::receivePacket(const NetworkPacket& np) long MprisRemotePlugin::position() const { - if(m_playing) { - return m_lastPosition + (QDateTime::currentMSecsSinceEpoch() - m_lastPositionTime); - } else { - return m_lastPosition; - } + auto player = m_players.value(m_currentPlayer); + return player ? player->position() : 0; } QString MprisRemotePlugin::dbusPath() const @@ -93,7 +87,7 @@ QString MprisRemotePlugin::dbusPath() const void MprisRemotePlugin::requestPlayerStatus() { NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, { - {"player", m_player}, + {"player", m_currentPlayer}, {"requestNowPlaying", true}, {"requestVolume", true}} ); @@ -109,7 +103,7 @@ void MprisRemotePlugin::requestPlayerList() void MprisRemotePlugin::sendAction(const QString& action) { NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, { - {"player", m_player}, + {"player", m_currentPlayer}, {"action", action} }); sendPacket(np); @@ -118,7 +112,7 @@ void MprisRemotePlugin::sendAction(const QString& action) void MprisRemotePlugin::seek(int offset) const { NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, { - {"player", m_player}, + {"player", m_currentPlayer}, {"Seek", offset}}); sendPacket(np); } @@ -126,7 +120,7 @@ void MprisRemotePlugin::seek(int offset) const void MprisRemotePlugin::setVolume(int volume) { NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, { - {"player", m_player}, + {"player", m_currentPlayer}, {"setVolume",volume} }); sendPacket(np); @@ -135,21 +129,75 @@ void MprisRemotePlugin::setVolume(int volume) void MprisRemotePlugin::setPosition(int position) { NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, { - {"player", m_player}, + {"player", m_currentPlayer}, {"SetPosition", position} }); sendPacket(np); - m_lastPosition = position; - m_lastPositionTime = QDateTime::currentMSecsSinceEpoch(); + m_players[m_currentPlayer]->setPosition(position); } void MprisRemotePlugin::setPlayer(const QString& player) { - if (m_player != player) { - m_player = player; + if (m_currentPlayer != player) { + m_currentPlayer = player; requestPlayerStatus(); + Q_EMIT propertiesChanged(); } } +bool MprisRemotePlugin::isPlaying() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->playing() : false; +} + +int MprisRemotePlugin::length() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->length() : 0; +} + +int MprisRemotePlugin::volume() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->volume() : 0; +} + +QString MprisRemotePlugin::player() const +{ + if (m_currentPlayer.isEmpty()) + return QString(); + return m_currentPlayer; +} + +QStringList MprisRemotePlugin::playerList() const +{ + return m_players.keys(); +} + +QString MprisRemotePlugin::nowPlaying() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->nowPlaying() : QString(); +} + +QString MprisRemotePlugin::title() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->title() : QString(); +} + +QString MprisRemotePlugin::album() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->album() : QString(); +} + +QString MprisRemotePlugin::artist() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->artist() : QString(); +} + #include "mprisremoteplugin.moc" diff --git a/plugins/mprisremote/mprisremoteplugin.h b/plugins/mprisremote/mprisremoteplugin.h index 00a16f215..6d83a123e 100644 --- a/plugins/mprisremote/mprisremoteplugin.h +++ b/plugins/mprisremote/mprisremoteplugin.h @@ -25,6 +25,8 @@ #include +#include "mprisremoteplayer.h" + #define PACKET_TYPE_MPRIS_REQUEST QStringLiteral("kdeconnect.mpris.request") #define PACKET_TYPE_MPRIS QStringLiteral("kdeconnect.mpris") @@ -40,18 +42,24 @@ class Q_DECL_EXPORT MprisRemotePlugin Q_PROPERTY(QStringList playerList READ playerList NOTIFY propertiesChanged) Q_PROPERTY(QString player READ player WRITE setPlayer) Q_PROPERTY(QString nowPlaying READ nowPlaying NOTIFY propertiesChanged) + Q_PROPERTY(QString title READ title NOTIFY propertiesChanged) + Q_PROPERTY(QString artist READ artist NOTIFY propertiesChanged) + Q_PROPERTY(QString album READ album NOTIFY propertiesChanged) public: explicit MprisRemotePlugin(QObject* parent, const QVariantList &args); ~MprisRemotePlugin() override; long position() const; - int volume() const { return m_volume; } - int length() const { return m_length; } - bool isPlaying() const { return m_playing; } - QStringList playerList() const { return m_playerList; } - QString player() const { return m_player; } - QString nowPlaying() const { return m_nowPlaying; } + int volume() const; + int length() const; + bool isPlaying() const; + QStringList playerList() const; + QString player() const; + QString nowPlaying() const; + QString title() const; + QString artist() const; + QString album() const; void setVolume(int volume); void setPosition(int position); @@ -71,14 +79,8 @@ Q_SIGNALS: private: void requestPlayerStatus(); - QString m_player; - bool m_playing; - QString m_nowPlaying; - int m_volume; - long m_length; - long m_lastPosition; - qint64 m_lastPositionTime; - QStringList m_playerList; + QString m_currentPlayer; + QMap m_players; }; #endif