diff options
Diffstat (limited to 'mediaplayer/mediaplayer.cpp')
-rw-r--r-- | mediaplayer/mediaplayer.cpp | 138 |
1 files changed, 115 insertions, 23 deletions
diff --git a/mediaplayer/mediaplayer.cpp b/mediaplayer/mediaplayer.cpp index f6a9cbd..f686fbb 100644 --- a/mediaplayer/mediaplayer.cpp +++ b/mediaplayer/mediaplayer.cpp @@ -15,10 +15,11 @@ */ #include <QDebug> -#include <QJsonObject> +#include <QMutexLocker> #include "mediaplayer.h" #include "MediaplayerMpdBackend.h" +#include "MediaplayerBluezBackend.h" Playlist::Playlist(QVariantMap &item) @@ -34,24 +35,37 @@ Playlist::Playlist(QVariantMap &item) Playlist::~Playlist() {} -Mediaplayer::Mediaplayer (QQmlContext *context, QObject * parent) : +Mediaplayer::Mediaplayer(QQmlContext *context, QObject * parent) : QObject(parent) { 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"); + m_mpd_backend = new MediaplayerMpdBackend(this, context); + if (!m_mpd_backend) + qFatal("Could not create MediaplayerMpdBackend"); + m_backend = m_mpd_backend; + + m_bluez_backend = new MediaplayerBluezBackend(this, context); + if (!m_bluez_backend) + qFatal("Could not create MediaplayerBluezBackend"); } Mediaplayer::~Mediaplayer() { + delete m_mpd_backend; + delete m_bluez_backend; +} + +void Mediaplayer::start() +{ + m_mpd_backend->start(); + m_bluez_backend->start(); } // Qt UI Context -void Mediaplayer::updatePlaylist(QVariantMap playlist) +void Mediaplayer::updateLocalPlaylist(QVariantMap playlist) { QVariantList list = playlist["list"].toList(); @@ -82,30 +96,44 @@ void Mediaplayer::updatePlaylist(QVariantMap playlist) m_context->setContextProperty("MediaplayerModel", QVariant::fromValue(m_playlist)); } -void Mediaplayer::updateMetadata(QVariantMap metadata) +void Mediaplayer::updateLocalMetadata(QVariantMap metadata) { - if (metadata.contains("track")) { - QVariantMap track = metadata.value("track").toMap(); + if (!m_bt_connected) + updateMetadata(metadata); +} - 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; +void Mediaplayer::updateBluetoothMetadata(QVariantMap metadata) +{ + if (m_bt_connected) + updateMetadata(metadata); +} + +void Mediaplayer::updateBluetoothMediaConnected(const bool connected) +{ + if (m_bt_connected != connected) { + QMutexLocker locker(&m_backend_mutex); + if (connected) { + qDebug() << "Mediaplayer::updateBluetoothMediaConnected: switching to BlueZ backend"; + m_backend = m_bluez_backend; + m_bt_connected = connected; + m_bluez_backend->refresh_metadata(); + } else { + qDebug() << "Mediaplayer::updateBluetoothMediaConnected: switching to MPD backend"; + m_backend = m_mpd_backend; + m_bt_connected = connected; + m_mpd_backend->refresh_metadata(); } } - emit metadataChanged(metadata); + m_bt_operation_mutex.lock(); + if (m_bt_operation) { + m_bt_operation = false; + } + // else externally driven event + m_bt_operation_mutex.unlock(); } + // Control methods // For backwards compatibility @@ -122,60 +150,124 @@ void Mediaplayer::connect() void Mediaplayer::disconnectBluetooth() { + m_bt_operation_mutex.lock(); + if (m_bt_operation) { + m_bt_operation_mutex.unlock(); + qDebug() << "Bluetooth media connection operation in progress, ignoring"; + return; + } + m_bt_operation_mutex.unlock(); + // Disconnect from Bluetooth media + if (m_bt_connected) { + // Explicitly pausing before disconnecting does not seem to be required + m_bluez_backend->disconnect_media(); + } } void Mediaplayer::connectBluetooth() { + m_bt_operation_mutex.lock(); + if (m_bt_operation) { + m_bt_operation_mutex.unlock(); + qDebug() << "Bluetooth media connection operation in progress, ignoring"; + return; + } + m_bt_operation_mutex.unlock(); + // Connect to Bluetooth media + if (!m_bt_connected) { + m_mpd_backend->pause(); + m_bluez_backend->connect_media(); + } } void Mediaplayer::play() { + QMutexLocker locker(&m_backend_mutex); m_backend->play(); } void Mediaplayer::pause() { + QMutexLocker locker(&m_backend_mutex); m_backend->pause(); } void Mediaplayer::previous() { + QMutexLocker locker(&m_backend_mutex); m_backend->previous(); } void Mediaplayer::next() { + QMutexLocker locker(&m_backend_mutex); m_backend->next(); } void Mediaplayer::seek(int milliseconds) { + QMutexLocker locker(&m_backend_mutex); m_backend->seek(milliseconds); } void Mediaplayer::fastforward(int milliseconds) { + QMutexLocker locker(&m_backend_mutex); m_backend->fastforward(milliseconds); } void Mediaplayer::rewind(int milliseconds) { + QMutexLocker locker(&m_backend_mutex); m_backend->rewind(milliseconds); } void Mediaplayer::picktrack(int track) { + QMutexLocker locker(&m_backend_mutex); m_backend->picktrack(track); } void Mediaplayer::volume(int volume) { + QMutexLocker locker(&m_backend_mutex); m_backend->volume(volume); } void Mediaplayer::loop(QString state) { + QMutexLocker locker(&m_backend_mutex); m_backend->loop(state); } + +// Private + +// Common metadata helper +void Mediaplayer::updateMetadata(QVariantMap &metadata) +{ + if (metadata.contains("track")) { + QVariantMap track = metadata.value("track").toMap(); + + 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; + } + } + + // Insert current Bluetooth status + metadata["connected"] = m_bt_connected; + + emit metadataChanged(metadata); +} |