2013-07-29 17:43:13 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2013-07-29 17:43:13 +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
|
2013-07-29 17:43:13 +01:00
|
|
|
*/
|
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
#include "mpriscontrolplugin.h"
|
2013-07-29 17:43:13 +01:00
|
|
|
|
2013-09-03 01:14:27 +01:00
|
|
|
#include <QDBusArgument>
|
2013-07-29 17:43:13 +01:00
|
|
|
#include <qdbusconnectioninterface.h>
|
|
|
|
#include <QDBusReply>
|
|
|
|
#include <QDBusMessage>
|
2015-05-05 00:40:04 +01:00
|
|
|
#include <QDBusServiceWatcher>
|
2013-07-29 17:43:13 +01:00
|
|
|
|
2014-09-22 01:37:10 +01:00
|
|
|
#include <KPluginFactory>
|
|
|
|
|
2014-06-14 15:34:00 +01:00
|
|
|
#include <core/device.h>
|
2019-06-09 16:28:49 +01:00
|
|
|
#include <dbushelper.h>
|
2020-01-20 16:36:44 +00:00
|
|
|
|
|
|
|
#include "dbusproperties.h"
|
|
|
|
#include "mprisplayer.h"
|
|
|
|
#include "mprisroot.h"
|
2020-05-26 17:55:47 +01:00
|
|
|
#include "plugin_mpris_debug.h"
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2019-06-12 21:16:54 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(MprisControlPlugin, "kdeconnect_mpriscontrol.json")
|
2013-08-13 05:35:58 +01:00
|
|
|
|
2018-03-16 12:44:16 +00:00
|
|
|
MprisPlayer::MprisPlayer(const QString& serviceName, const QString& dbusObjectPath, const QDBusConnection& busConnection)
|
|
|
|
: m_serviceName(serviceName)
|
|
|
|
, m_propertiesInterface(new OrgFreedesktopDBusPropertiesInterface(serviceName, dbusObjectPath, busConnection))
|
|
|
|
, m_mediaPlayer2PlayerInterface(new OrgMprisMediaPlayer2PlayerInterface(serviceName, dbusObjectPath, busConnection))
|
|
|
|
{
|
|
|
|
m_mediaPlayer2PlayerInterface->setTimeout(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
MprisControlPlugin::MprisControlPlugin(QObject* parent, const QVariantList& args)
|
|
|
|
: KdeConnectPlugin(parent, args)
|
2015-05-05 00:40:04 +01:00
|
|
|
, prevVolume(-1)
|
2013-07-29 17:43:13 +01:00
|
|
|
{
|
2019-08-14 16:36:19 +01:00
|
|
|
m_watcher = new QDBusServiceWatcher(QString(), DBusHelper::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
|
2013-07-30 19:21:06 +01:00
|
|
|
|
2016-04-08 00:12:10 +01:00
|
|
|
// TODO: QDBusConnectionInterface::serviceOwnerChanged is deprecated, maybe query org.freedesktop.DBus directly?
|
2019-08-14 16:36:19 +01:00
|
|
|
connect(DBusHelper::sessionBus().interface(), &QDBusConnectionInterface::serviceOwnerChanged, this, &MprisControlPlugin::serviceOwnerChanged);
|
2013-07-30 19:21:06 +01:00
|
|
|
|
|
|
|
//Add existing interfaces
|
2019-08-14 16:36:19 +01:00
|
|
|
const QStringList services = DBusHelper::sessionBus().interface()->registeredServiceNames().value();
|
2017-07-20 15:14:07 +01:00
|
|
|
for (const QString& service : services) {
|
2016-04-08 00:12:10 +01:00
|
|
|
// The string doesn't matter, it just needs to be empty/non-empty
|
2016-11-26 14:38:08 +00:00
|
|
|
serviceOwnerChanged(service, QLatin1String(""), QStringLiteral("1"));
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
2015-09-11 10:27:18 +01:00
|
|
|
}
|
|
|
|
|
2016-04-08 00:12:10 +01:00
|
|
|
// Copied from the mpris2 dataengine in the plasma-workspace repository
|
|
|
|
void MprisControlPlugin::serviceOwnerChanged(const QString& serviceName, const QString& oldOwner, const QString& newOwner)
|
2015-09-11 10:27:18 +01:00
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
if (!serviceName.startsWith(QStringLiteral("org.mpris.MediaPlayer2."))) return;
|
|
|
|
if (serviceName.startsWith(QStringLiteral("org.mpris.MediaPlayer2.kdeconnect."))) return;
|
mpriscontrol plugin should ignore playerctld
playerctld (https://github.com/altdesktop/playerctl/issues/161) is a proxy daemon for the currently active player by playerctl, which facilitates managing mpris players, forwarding requests to the currently active/last active player, and sorting out troubles with selecting the correct player manually.
Unfortunately, it also creates an annoying issue with kdeconnect: when playing media on the phone, kdeconnect publishes the state to the computer through the mprisremote plugin - then, playerctld picks it up as active player, and registers its own mpris media player. As a result, the mpriscontrol plugin sees this as a running media player, and in turn, publishes the state back to the phone, essentially creating another media session on the phone, resulting in two notifications. As playerctld is _always_ only a proxy to another media player (or kdeconnect), it can safely be ignored, just like kdeconnect itself already is. This commit adds an if check doing exactly that.
2020-05-20 22:12:33 +01:00
|
|
|
// playerctld is a only a proxy to other media players, and can thus safely be ignored
|
|
|
|
if (serviceName == QStringLiteral("org.mpris.MediaPlayer2.playerctld")) return;
|
2016-04-08 00:12:10 +01:00
|
|
|
|
|
|
|
if (!oldOwner.isEmpty()) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_MPRIS) << "MPRIS service" << serviceName << "just went offline";
|
|
|
|
removePlayer(serviceName);
|
2015-09-11 10:27:18 +01:00
|
|
|
}
|
2013-07-30 19:21:06 +01:00
|
|
|
|
2016-04-08 00:12:10 +01:00
|
|
|
if (!newOwner.isEmpty()) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_MPRIS) << "MPRIS service" << serviceName << "just came online";
|
|
|
|
addPlayer(serviceName);
|
2015-09-11 10:27:18 +01:00
|
|
|
}
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
|
|
|
|
2015-09-11 10:27:18 +01:00
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
void MprisControlPlugin::addPlayer(const QString& service)
|
2013-07-30 19:21:06 +01:00
|
|
|
{
|
2018-03-16 12:44:16 +00:00
|
|
|
const QString mediaPlayerObjectPath = QStringLiteral("/org/mpris/MediaPlayer2");
|
|
|
|
|
2020-01-20 16:36:44 +00:00
|
|
|
OrgMprisMediaPlayer2Interface iface(service, mediaPlayerObjectPath, DBusHelper::sessionBus());
|
|
|
|
QString identity = iface.identity();
|
|
|
|
|
2015-11-11 19:00:59 +00:00
|
|
|
if (identity.isEmpty()) {
|
|
|
|
identity = service.mid(sizeof("org.mpris.MediaPlayer2"));
|
|
|
|
}
|
2017-08-02 14:42:01 +01:00
|
|
|
|
|
|
|
QString uniqueName = identity;
|
2018-03-17 13:37:11 +00:00
|
|
|
for (int i = 2; playerList.contains(uniqueName); ++i) {
|
|
|
|
uniqueName = identity + QLatin1String(" [") + QString::number(i) + QLatin1Char(']');
|
2017-08-02 14:42:01 +01:00
|
|
|
}
|
|
|
|
|
2019-08-14 16:36:19 +01:00
|
|
|
MprisPlayer player(service, mediaPlayerObjectPath, DBusHelper::sessionBus());
|
2018-03-16 12:44:16 +00:00
|
|
|
|
|
|
|
playerList.insert(uniqueName, player);
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2018-03-16 12:44:16 +00:00
|
|
|
connect(player.propertiesInterface(), &OrgFreedesktopDBusPropertiesInterface::PropertiesChanged,
|
|
|
|
this, &MprisControlPlugin::propertiesChanged);
|
|
|
|
connect(player.mediaPlayer2PlayerInterface(), &OrgMprisMediaPlayer2PlayerInterface::Seeked,
|
|
|
|
this, &MprisControlPlugin::seeked);
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2018-03-16 12:44:16 +00:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Mpris addPlayer" << service << "->" << uniqueName;
|
|
|
|
sendPlayerList();
|
2014-12-13 22:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MprisControlPlugin::seeked(qlonglong position){
|
2015-04-22 08:16:14 +01:00
|
|
|
//qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Seeked in player";
|
2018-03-16 12:44:16 +00:00
|
|
|
OrgMprisMediaPlayer2PlayerInterface* mediaPlayer2PlayerInterface = (OrgMprisMediaPlayer2PlayerInterface*)sender();
|
|
|
|
const auto end = playerList.constEnd();
|
|
|
|
const auto it = std::find_if(playerList.constBegin(), end, [mediaPlayer2PlayerInterface](const MprisPlayer& player) {
|
|
|
|
return (player.mediaPlayer2PlayerInterface() == mediaPlayer2PlayerInterface);
|
|
|
|
});
|
|
|
|
if (it == end) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_MPRIS) << "Seeked signal received for no longer tracked service" << mediaPlayer2PlayerInterface->service();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString& playerName = it.key();
|
2016-06-21 19:07:12 +01:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS, {
|
2019-06-10 15:40:28 +01:00
|
|
|
{QStringLiteral("pos"), position/1000}, //Send milis instead of nanos
|
|
|
|
{QStringLiteral("player"), playerName}
|
2016-06-21 19:07:12 +01:00
|
|
|
});
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
void MprisControlPlugin::propertiesChanged(const QString& propertyInterface, const QVariantMap& properties)
|
2013-07-30 19:21:06 +01:00
|
|
|
{
|
2013-08-13 22:26:42 +01:00
|
|
|
Q_UNUSED(propertyInterface);
|
2014-07-01 22:59:38 +01:00
|
|
|
|
2018-03-16 12:44:16 +00:00
|
|
|
OrgFreedesktopDBusPropertiesInterface* propertiesInterface = (OrgFreedesktopDBusPropertiesInterface*)sender();
|
|
|
|
const auto end = playerList.constEnd();
|
|
|
|
const auto it = std::find_if(playerList.constBegin(), end, [propertiesInterface](const MprisPlayer& player) {
|
|
|
|
return (player.propertiesInterface() == propertiesInterface);
|
|
|
|
});
|
|
|
|
if (it == end) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_MPRIS) << "PropertiesChanged signal received for no longer tracked service" << propertiesInterface->service();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
OrgMprisMediaPlayer2PlayerInterface* const mediaPlayer2PlayerInterface = it.value().mediaPlayer2PlayerInterface();
|
|
|
|
const QString& playerName = it.key();
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS);
|
2013-08-10 04:21:55 +01:00
|
|
|
bool somethingToSend = false;
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("Volume"))) {
|
|
|
|
int volume = (int) (properties[QStringLiteral("Volume")].toDouble()*100);
|
2013-08-10 04:21:55 +01:00
|
|
|
if (volume != prevVolume) {
|
2016-11-26 14:38:08 +00:00
|
|
|
np.set(QStringLiteral("volume"),volume);
|
2013-08-10 04:21:55 +01:00
|
|
|
prevVolume = volume;
|
|
|
|
somethingToSend = true;
|
|
|
|
}
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("Metadata"))) {
|
|
|
|
QDBusArgument bullshit = qvariant_cast<QDBusArgument>(properties[QStringLiteral("Metadata")]);
|
2013-08-10 04:21:55 +01:00
|
|
|
QVariantMap nowPlayingMap;
|
|
|
|
bullshit >> nowPlayingMap;
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
mprisPlayerMetadataToNetworkPacket(np, nowPlayingMap);
|
2017-11-22 18:13:15 +00:00
|
|
|
somethingToSend = true;
|
2013-08-10 04:21:55 +01:00
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("PlaybackStatus"))) {
|
|
|
|
bool playing = (properties[QStringLiteral("PlaybackStatus")].toString() == QLatin1String("Playing"));
|
|
|
|
np.set(QStringLiteral("isPlaying"), playing);
|
2013-08-10 04:21:55 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("CanPause"))) {
|
|
|
|
np.set(QStringLiteral("canPause"), properties[QStringLiteral("CanPause")].toBool());
|
2016-08-26 10:10:36 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("CanPlay"))) {
|
|
|
|
np.set(QStringLiteral("canPlay"), properties[QStringLiteral("CanPlay")].toBool());
|
2016-08-26 10:10:36 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("CanGoNext"))) {
|
|
|
|
np.set(QStringLiteral("canGoNext"), properties[QStringLiteral("CanGoNext")].toBool());
|
2016-08-26 10:10:36 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("CanGoPrevious"))) {
|
|
|
|
np.set(QStringLiteral("canGoPrevious"), properties[QStringLiteral("CanGoPrevious")].toBool());
|
2016-08-26 10:10:36 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("CanSeek"))) {
|
|
|
|
np.set(QStringLiteral("canSeek"), properties[QStringLiteral("CanSeek")].toBool());
|
2016-08-26 10:10:36 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2013-08-10 04:21:55 +01:00
|
|
|
|
|
|
|
if (somethingToSend) {
|
2018-03-16 12:44:16 +00:00
|
|
|
np.set(QStringLiteral("player"), playerName);
|
2014-12-13 22:41:53 +00:00
|
|
|
// Always also update the position
|
2018-03-16 12:44:16 +00:00
|
|
|
if (mediaPlayer2PlayerInterface->canSeek()) {
|
|
|
|
long long pos = mediaPlayer2PlayerInterface->position();
|
2016-11-26 14:38:08 +00:00
|
|
|
np.set(QStringLiteral("pos"), pos/1000); //Send milis instead of nanos
|
2014-12-14 02:44:20 +00:00
|
|
|
}
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2013-08-10 04:21:55 +01:00
|
|
|
}
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
|
|
|
|
2018-03-16 12:44:16 +00:00
|
|
|
void MprisControlPlugin::removePlayer(const QString& serviceName)
|
2013-07-30 19:21:06 +01:00
|
|
|
{
|
2018-03-16 12:44:16 +00:00
|
|
|
const auto end = playerList.end();
|
|
|
|
const auto it = std::find_if(playerList.begin(), end, [serviceName](const MprisPlayer& player) {
|
|
|
|
return (player.serviceName() == serviceName);
|
|
|
|
});
|
|
|
|
if (it == end) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_MPRIS) << "Could not find player for serviceName" << serviceName;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString& playerName = it.key();
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Mpris removePlayer" << serviceName << "->" << playerName;
|
|
|
|
|
|
|
|
playerList.erase(it);
|
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
sendPlayerList();
|
2013-07-29 17:43:13 +01:00
|
|
|
}
|
|
|
|
|
2018-03-04 12:47:29 +00:00
|
|
|
bool MprisControlPlugin::sendAlbumArt(const NetworkPacket& np)
|
|
|
|
{
|
|
|
|
const QString player = np.get<QString>(QStringLiteral("player"));
|
|
|
|
auto it = playerList.find(player);
|
|
|
|
bool valid_player = (it != playerList.end());
|
|
|
|
if (!valid_player) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get mpris information
|
|
|
|
auto& mprisInterface = *it.value().mediaPlayer2PlayerInterface();
|
|
|
|
QVariantMap nowPlayingMap = mprisInterface.metadata();
|
|
|
|
|
|
|
|
//Check if the supplied album art url indeed belongs to this mpris player
|
|
|
|
QUrl playerAlbumArtUrl{nowPlayingMap[QStringLiteral("mpris:artUrl")].toString()};
|
|
|
|
QString requestedAlbumArtUrl = np.get<QString>(QStringLiteral("albumArtUrl"));
|
|
|
|
if (!playerAlbumArtUrl.isValid() || playerAlbumArtUrl != QUrl(requestedAlbumArtUrl)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Only support sending local files
|
2019-06-10 15:40:28 +01:00
|
|
|
if (playerAlbumArtUrl.scheme() != QStringLiteral("file")) {
|
2018-03-04 12:47:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Open the file to send
|
|
|
|
QSharedPointer<QFile> art{new QFile(playerAlbumArtUrl.toLocalFile())};
|
|
|
|
|
|
|
|
//Send the album art as payload
|
|
|
|
NetworkPacket answer(PACKET_TYPE_MPRIS);
|
|
|
|
answer.set(QStringLiteral("transferringAlbumArt"), true);
|
|
|
|
answer.set(QStringLiteral("player"), player);
|
|
|
|
answer.set(QStringLiteral("albumArtUrl"), requestedAlbumArtUrl);
|
|
|
|
answer.setPayload(art, art->size());
|
|
|
|
sendPacket(answer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
bool MprisControlPlugin::receivePacket (const NetworkPacket& np)
|
2013-07-29 17:43:13 +01:00
|
|
|
{
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("playerList"))) {
|
2013-09-03 01:06:56 +01:00
|
|
|
return false; //Whoever sent this is an mpris client and not an mpris control!
|
|
|
|
}
|
2013-07-29 17:43:13 +01:00
|
|
|
|
2018-03-04 12:47:29 +00:00
|
|
|
if (np.has(QStringLiteral("albumArtUrl"))) {
|
|
|
|
return sendAlbumArt(np);
|
|
|
|
}
|
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
//Send the player list
|
2016-11-26 14:38:08 +00:00
|
|
|
const QString player = np.get<QString>(QStringLiteral("player"));
|
2018-03-16 12:44:16 +00:00
|
|
|
auto it = playerList.find(player);
|
|
|
|
bool valid_player = (it != playerList.end());
|
2016-11-26 14:38:08 +00:00
|
|
|
if (!valid_player || np.get<bool>(QStringLiteral("requestPlayerList"))) {
|
2013-08-13 05:35:58 +01:00
|
|
|
sendPlayerList();
|
2013-08-10 04:21:55 +01:00
|
|
|
if (!valid_player) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 17:43:13 +01:00
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
//Do something to the mpris interface
|
2018-03-16 12:44:16 +00:00
|
|
|
const QString& serviceName = it.value().serviceName();
|
|
|
|
// turn from pointer to reference to keep the patch diff small,
|
|
|
|
// actual patch would change all "mprisInterface." into "mprisInterface->"
|
|
|
|
auto& mprisInterface = *it.value().mediaPlayer2PlayerInterface();
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("action"))) {
|
|
|
|
const QString& action = np.get<QString>(QStringLiteral("action"));
|
2018-03-16 12:44:16 +00:00
|
|
|
//qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Calling action" << action << "in" << serviceName;
|
2014-10-10 19:47:35 +01:00
|
|
|
//TODO: Check for valid actions, currently we trust anything the other end sends us
|
2013-08-10 04:21:55 +01:00
|
|
|
mprisInterface.call(action);
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("setVolume"))) {
|
|
|
|
double volume = np.get<int>(QStringLiteral("setVolume"))/100.f;
|
2018-03-16 12:44:16 +00:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Setting volume" << volume << "to" << serviceName;
|
2013-08-10 04:21:55 +01:00
|
|
|
mprisInterface.setVolume(volume);
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("Seek"))) {
|
|
|
|
int offset = np.get<int>(QStringLiteral("Seek"));
|
2018-03-16 12:44:16 +00:00
|
|
|
//qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Seeking" << offset << "to" << serviceName;
|
2013-10-29 19:17:42 +00:00
|
|
|
mprisInterface.Seek(offset);
|
|
|
|
}
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("SetPosition"))){
|
|
|
|
qlonglong position = np.get<qlonglong>(QStringLiteral("SetPosition"),0)*1000;
|
2014-12-14 02:44:20 +00:00
|
|
|
qlonglong seek = position - mprisInterface.position();
|
2018-03-16 12:44:16 +00:00
|
|
|
//qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Setting position by seeking" << seek << "to" << serviceName;
|
2014-12-14 02:44:20 +00:00
|
|
|
mprisInterface.Seek(seek);
|
2014-12-13 22:41:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
//Send something read from the mpris interface
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket answer(PACKET_TYPE_MPRIS);
|
2013-08-10 04:21:55 +01:00
|
|
|
bool somethingToSend = false;
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.get<bool>(QStringLiteral("requestNowPlaying"))) {
|
2013-08-10 04:21:55 +01:00
|
|
|
QVariantMap nowPlayingMap = mprisInterface.metadata();
|
2018-03-04 19:48:51 +00:00
|
|
|
mprisPlayerMetadataToNetworkPacket(answer, nowPlayingMap);
|
2015-11-11 19:03:00 +00:00
|
|
|
|
2014-12-14 02:44:20 +00:00
|
|
|
qlonglong pos = mprisInterface.position();
|
2016-11-26 14:38:08 +00:00
|
|
|
answer.set(QStringLiteral("pos"), pos/1000);
|
2013-08-10 04:21:55 +01:00
|
|
|
|
|
|
|
bool playing = (mprisInterface.playbackStatus() == QLatin1String("Playing"));
|
2016-11-26 14:38:08 +00:00
|
|
|
answer.set(QStringLiteral("isPlaying"), playing);
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
answer.set(QStringLiteral("canPause"), mprisInterface.canPause());
|
|
|
|
answer.set(QStringLiteral("canPlay"), mprisInterface.canPlay());
|
|
|
|
answer.set(QStringLiteral("canGoNext"), mprisInterface.canGoNext());
|
|
|
|
answer.set(QStringLiteral("canGoPrevious"), mprisInterface.canGoPrevious());
|
|
|
|
answer.set(QStringLiteral("canSeek"), mprisInterface.canSeek());
|
2016-08-26 10:10:36 +01:00
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
somethingToSend = true;
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.get<bool>(QStringLiteral("requestVolume"))) {
|
2013-08-10 04:21:55 +01:00
|
|
|
int volume = (int)(mprisInterface.volume() * 100);
|
2016-11-26 14:38:08 +00:00
|
|
|
answer.set(QStringLiteral("volume"),volume);
|
2013-08-10 04:21:55 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2018-03-16 12:44:16 +00:00
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
if (somethingToSend) {
|
2016-11-26 14:38:08 +00:00
|
|
|
answer.set(QStringLiteral("player"), player);
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(answer);
|
2013-07-29 17:43:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-30 19:21:06 +01:00
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
void MprisControlPlugin::sendPlayerList()
|
2013-07-30 19:21:06 +01:00
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS);
|
2016-11-26 14:38:08 +00:00
|
|
|
np.set(QStringLiteral("playerList"),playerList.keys());
|
2018-03-04 12:47:29 +00:00
|
|
|
np.set(QStringLiteral("supportAlbumArtPayload"), true);
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
2014-06-16 19:02:07 +01:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
void MprisControlPlugin::mprisPlayerMetadataToNetworkPacket(NetworkPacket& np, const QVariantMap& nowPlayingMap) const {
|
2017-11-22 18:13:15 +00:00
|
|
|
QString title = nowPlayingMap[QStringLiteral("xesam:title")].toString();
|
|
|
|
QString artist = nowPlayingMap[QStringLiteral("xesam:artist")].toString();
|
|
|
|
QString album = nowPlayingMap[QStringLiteral("xesam:album")].toString();
|
2017-12-21 12:53:28 +00:00
|
|
|
QString albumArtUrl = nowPlayingMap[QStringLiteral("mpris:artUrl")].toString();
|
2019-12-19 17:07:32 +00:00
|
|
|
QUrl fileUrl = nowPlayingMap[QStringLiteral("xesam:url")].toUrl();
|
|
|
|
|
|
|
|
if (title.isEmpty() && artist.isEmpty() && fileUrl.isLocalFile()) {
|
|
|
|
title = fileUrl.fileName();
|
|
|
|
|
|
|
|
QStringList splitUrl = fileUrl.path().split(QDir::separator());
|
|
|
|
if (album.isEmpty() && splitUrl.size() > 1) {
|
|
|
|
album = splitUrl.at(splitUrl.size() - 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 18:13:15 +00:00
|
|
|
QString nowPlaying = title;
|
2019-12-19 17:07:32 +00:00
|
|
|
|
2017-11-22 18:13:15 +00:00
|
|
|
if (!artist.isEmpty()) {
|
2019-06-10 15:40:28 +01:00
|
|
|
nowPlaying = artist + QStringLiteral(" - ") + title;
|
2017-11-22 18:13:15 +00:00
|
|
|
}
|
2019-12-19 17:07:32 +00:00
|
|
|
|
2017-11-22 18:13:15 +00:00
|
|
|
np.set(QStringLiteral("title"), title);
|
|
|
|
np.set(QStringLiteral("artist"), artist);
|
|
|
|
np.set(QStringLiteral("album"), album);
|
2017-12-21 12:53:28 +00:00
|
|
|
np.set(QStringLiteral("albumArtUrl"), albumArtUrl);
|
2017-11-22 18:13:15 +00:00
|
|
|
np.set(QStringLiteral("nowPlaying"), nowPlaying);
|
|
|
|
|
|
|
|
bool hasLength = false;
|
|
|
|
long long length = nowPlayingMap[QStringLiteral("mpris:length")].toLongLong(&hasLength) / 1000; //nanoseconds to milliseconds
|
|
|
|
if (!hasLength) {
|
|
|
|
length = -1;
|
|
|
|
}
|
|
|
|
np.set(QStringLiteral("length"), length);
|
2020-01-18 17:26:16 +00:00
|
|
|
np.set(QStringLiteral("url"), fileUrl);
|
2017-11-22 18:13:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 19:02:07 +01:00
|
|
|
#include "mpriscontrolplugin.moc"
|