summaryrefslogtreecommitdiffstats
path: root/mediaplayer/mediaplayer.cpp
diff options
context:
space:
mode:
authorScott Murray <scott.murray@konsulko.com>2022-02-14 18:00:05 -0500
committerScott Murray <scott.murray@konsulko.com>2022-02-15 17:15:07 -0500
commit3f384d30d099f6eea5eb946c3cb0380f0453e2bc (patch)
tree5630474f9932b31327133351c41035b63d7dec76 /mediaplayer/mediaplayer.cpp
parent5ef90db242ad29c9772f2237b477c58ef65545c7 (diff)
Rework mediaplayer support to use mpd
Rework the mediaplayer support to replace the stubbed out app framework agl-service-mediaplayer binding use with driving mpd (Media Player Daemon) via the libmpdclient library. Local file playback behavior should be equivalent to before in an image with mpd present and suitably configured. Bluetooth AVRCP support is still stubbed out, and will be re-implemented with followup changes. Bug-AGL: SPEC-4182 Signed-off-by: Scott Murray <scott.murray@konsulko.com> Change-Id: Ifdf092b472c271460d0f5e9a8c7d2353904411b2
Diffstat (limited to 'mediaplayer/mediaplayer.cpp')
-rw-r--r--mediaplayer/mediaplayer.cpp243
1 files changed, 81 insertions, 162 deletions
diff --git a/mediaplayer/mediaplayer.cpp b/mediaplayer/mediaplayer.cpp
index c578145..f6a9cbd 100644
--- a/mediaplayer/mediaplayer.cpp
+++ b/mediaplayer/mediaplayer.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2018-2020 Konsulko Group
+ * Copyright (C) 2018-2020,2022 Konsulko Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,37 +15,34 @@
*/
#include <QDebug>
+#include <QJsonObject>
-#include "callmessage.h"
-#include "eventmessage.h"
-#include "messageengine.h"
-#include "messagefactory.h"
-#include "messageenginefactory.h"
#include "mediaplayer.h"
+#include "MediaplayerMpdBackend.h"
Playlist::Playlist(QVariantMap &item)
{
- m_index = item["index"].toInt();
- m_duration = item["duration"].toInt();
- m_genre = item["genre"].toString();
- m_path = item["path"].toString();
- m_title = item["title"].toString();
- m_album = item["album"].toString();
- m_artist = item["artist"].toString();
+ m_index = item["index"].toInt();
+ m_duration = item["duration"].toInt();
+ m_genre = item["genre"].toString();
+ m_path = item["path"].toString();
+ m_title = item["title"].toString();
+ m_album = item["album"].toString();
+ m_artist = item["artist"].toString();
}
Playlist::~Playlist() {}
-Mediaplayer::Mediaplayer (QUrl &url, QQmlContext *context, QObject * parent) :
- QObject(parent)
+Mediaplayer::Mediaplayer (QQmlContext *context, QObject * parent) :
+ QObject(parent)
{
- m_mloop = MessageEngineFactory::getInstance().getMessageEngine(url);
- m_context = context;
- m_context->setContextProperty("MediaplayerModel", QVariant::fromValue(m_playlist));
- QObject::connect(m_mloop.get(), &MessageEngine::connected, this, &Mediaplayer::onConnected);
- QObject::connect(m_mloop.get(), &MessageEngine::disconnected, this, &Mediaplayer::onDisconnected);
- QObject::connect(m_mloop.get(), &MessageEngine::messageReceived, this, &Mediaplayer::onMessageReceived);
+ m_context = context;
+ m_context->setContextProperty("MediaplayerModel", QVariant::fromValue(m_playlist));
+
+ m_backend = new MediaplayerMpdBackend(this, context);
+ if (!m_backend)
+ qFatal("Could not create MediaPlayerBackend");
}
Mediaplayer::~Mediaplayer()
@@ -56,207 +53,129 @@ Mediaplayer::~Mediaplayer()
void Mediaplayer::updatePlaylist(QVariantMap playlist)
{
- QVariantList list = playlist["list"].toList();
+ QVariantList list = playlist["list"].toList();
- m_playlist.clear();
+ m_playlist.clear();
- for (auto i : list) {
- QVariantMap item = qvariant_cast<QVariantMap>(i);
- m_playlist.append(new Playlist(item));
- }
+ for (auto i : list) {
+ QVariantMap item = qvariant_cast<QVariantMap>(i);
+ m_playlist.append(new Playlist(item));
+ }
- if (m_playlist.count() == 0) {
- QVariantMap tmp, track;
+ if (m_playlist.count() == 0) {
+ QVariantMap tmp, track;
- track.insert("title", "");
- track.insert("artist", "");
- track.insert("album", "");
- track.insert("duration", 0);
+ track.insert("title", "");
+ track.insert("artist", "");
+ track.insert("album", "");
+ track.insert("duration", 0);
- tmp.insert("position", 0);
- tmp.insert("track", track);
+ tmp.insert("position", 0);
+ tmp.insert("track", track);
- // clear metadata in UI
- m_context->setContextProperty("AlbumArt", "");
- emit metadataChanged(tmp);
- }
+ // clear metadata in UI
+ m_context->setContextProperty("AlbumArt", "");
+ emit metadataChanged(tmp);
+ }
- // Refresh model
- m_context->setContextProperty("MediaplayerModel", QVariant::fromValue(m_playlist));
+ // Refresh model
+ m_context->setContextProperty("MediaplayerModel", QVariant::fromValue(m_playlist));
}
-// Control Verb methods
-
-void Mediaplayer::control(QString control, QJsonObject parameter)
+void Mediaplayer::updateMetadata(QVariantMap metadata)
{
- std::unique_ptr<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call);
- if (!msg)
- return;
+ if (metadata.contains("track")) {
+ QVariantMap track = metadata.value("track").toMap();
- CallMessage* mpmsg = static_cast<CallMessage*>(msg.get());
- parameter.insert("value", control);
+ if (track.contains("image")) {
+ m_context->setContextProperty("AlbumArt",
+ QVariant::fromValue(track.value("image")));
+ }
+
+ if (!track.contains("artist")) {
+ track.insert("artist", "");
+ metadata["track"] = track;
+ }
+
+ if (!track.contains("album")) {
+ track.insert("album", "");
+ metadata["track"] = track;
+ }
+ }
- mpmsg->createRequest("mediaplayer", "controls", parameter);
- m_mloop->sendMessage(std::move(msg));
+ emit metadataChanged(metadata);
}
+// Control methods
-void Mediaplayer::control(QString control)
+// For backwards compatibility
+void Mediaplayer::disconnect()
{
- QJsonObject parameter;
+ disconnectBluetooth();
+}
- Mediaplayer::control(control, parameter);
+// For backwards compatibility
+void Mediaplayer::connect()
+{
+ connectBluetooth();
}
-void Mediaplayer::disconnect()
+void Mediaplayer::disconnectBluetooth()
{
- control("disconnect");
+ // Disconnect from Bluetooth media
}
-void Mediaplayer::connect()
+void Mediaplayer::connectBluetooth()
{
- control("connect");
+ // Connect to Bluetooth media
}
+
void Mediaplayer::play()
{
- control("play");
+ m_backend->play();
}
void Mediaplayer::pause()
{
- control("pause");
+ m_backend->pause();
}
void Mediaplayer::previous()
{
- control("previous");
+ m_backend->previous();
}
void Mediaplayer::next()
{
- control("next");
+ m_backend->next();
}
void Mediaplayer::seek(int milliseconds)
{
- QJsonObject parameter;
- parameter.insert("position", QString::number(milliseconds));
-
- control("seek", parameter);
+ m_backend->seek(milliseconds);
}
void Mediaplayer::fastforward(int milliseconds)
{
- QJsonObject parameter;
- parameter.insert("position", QString::number(milliseconds));
-
- control("fast-forward", parameter);
+ m_backend->fastforward(milliseconds);
}
void Mediaplayer::rewind(int milliseconds)
{
- QJsonObject parameter;
- parameter.insert("position", QString::number(milliseconds));
-
- control("rewind", parameter);
+ m_backend->rewind(milliseconds);
}
void Mediaplayer::picktrack(int track)
{
- QJsonObject parameter;
- parameter.insert("index", QString::number(track));
-
- control("pick-track", parameter);
+ m_backend->picktrack(track);
}
void Mediaplayer::volume(int volume)
{
- QJsonObject parameter;
- parameter.insert("volume", QString(volume));
-
- control("volume", parameter);
+ m_backend->volume(volume);
}
void Mediaplayer::loop(QString state)
{
- QJsonObject parameter;
- parameter.insert("state", state);
-
- control("loop", parameter);
-}
-
-void Mediaplayer::onConnected()
-{
- QStringListIterator eventIterator(events);
-
- while (eventIterator.hasNext()) {
- std::unique_ptr<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call);
- if (!msg)
- return;
-
- CallMessage* mpmsg = static_cast<CallMessage*>(msg.get());
- QJsonObject parameter;
- parameter.insert("value", eventIterator.next());
- mpmsg->createRequest("mediaplayer", "subscribe", parameter);
- m_mloop->sendMessage(std::move(msg));
- }
-}
-
-void Mediaplayer::onDisconnected()
-{
- QStringListIterator eventIterator(events);
-
- while (eventIterator.hasNext()) {
- std::unique_ptr<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call);
- if (!msg)
- return;
-
- CallMessage* mpmsg = static_cast<CallMessage*>(msg.get());
- QJsonObject parameter;
- parameter.insert("value", eventIterator.next());
- mpmsg->createRequest("mediaplayer", "unsubscribe", parameter);
- m_mloop->sendMessage(std::move(msg));
- }
-}
-
-void Mediaplayer::onMessageReceived(std::shared_ptr<Message> msg)
-{
- if (!msg)
- return;
-
- if (msg->isEvent()){
- std::shared_ptr<EventMessage> emsg = std::static_pointer_cast<EventMessage>(msg);
- QString ename = emsg->eventName();
- QString eapi = emsg->eventApi();
- QJsonObject data = emsg->eventData();
- if (eapi != "mediaplayer")
- return;
-
- if (ename == "playlist") {
- updatePlaylist(data.toVariantMap());
- } else if (ename == "metadata") {
- QVariantMap map = data.toVariantMap();
-
- if (map.contains("track")) {
- QVariantMap track = map.value("track").toMap();
-
- if (track.contains("image")) {
- m_context->setContextProperty("AlbumArt",
- QVariant::fromValue(track.value("image")));
- }
-
- if (!track.contains("artist")) {
- track.insert("artist", "");
- map["track"] = track;
- }
-
- if (!track.contains("album")) {
- track.insert("album", "");
- map["track"] = track;
- }
- }
-
- emit metadataChanged(map);
- }
- }
+ m_backend->loop(state);
}