Commit 1b622de1 authored by Nicolas Fella's avatar Nicolas Fella

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
parent fc1335b4
......@@ -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)
......
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
......
/**
* Copyright 2018 Nicolas Fella <nicolas.fella@gmx.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <networkpacket.h>
#include <QDateTime>
#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<QString>(QStringLiteral("nowPlaying"), m_nowPlaying);
m_title = np.get<QString>(QStringLiteral("title"), m_title);
m_artist = np.get<QString>(QStringLiteral("artist"), m_artist);
m_album = np.get<QString>(QStringLiteral("album"), m_album);
m_volume = np.get<int>(QStringLiteral("volume"), m_volume);
m_length = np.get<int>(QStringLiteral("length"), m_length);
if(np.has(QStringLiteral("pos"))){
m_lastPosition = np.get<int>(QStringLiteral("pos"), m_lastPosition);
m_lastPositionTime = QDateTime::currentMSecsSinceEpoch();
}
m_playing = np.get<bool>(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;
}
/**
* Copyright 2018 Nicolas Fella <nicolas.fella@gmx.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QString>
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;
};
......@@ -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<QString>(QStringLiteral("player")) == m_player) {
m_nowPlaying = np.get<QString>(QStringLiteral("nowPlaying"), m_nowPlaying);
m_volume = np.get<int>(QStringLiteral("volume"), m_volume);
m_length = np.get<int>(QStringLiteral("length"), m_length);
if(np.has(QStringLiteral("pos"))){
m_lastPosition = np.get<int>(QStringLiteral("pos"), m_lastPosition);
m_lastPositionTime = QDateTime::currentMSecsSinceEpoch();
}
m_playing = np.get<bool>(QStringLiteral("isPlaying"), m_playing);
}
if (np.has(QStringLiteral("player"))) {
m_players[m_currentPlayer]->parseNetworkPacket(np);
}
if (np.has(QStringLiteral("playerList"))) {
m_playerList = np.get<QStringList>(QStringLiteral("playerList"), QStringList());
QStringList players = np.get<QStringList>(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"
......@@ -25,6 +25,8 @@
#include <core/kdeconnectplugin.h>
#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<QString, MprisRemotePlayer*> m_players;
};
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment