From e28e2fd5de9e2b64b68af2a7aa0bc6873c2ce71d Mon Sep 17 00:00:00 2001 From: Dominik Holland Date: Fri, 24 Nov 2017 13:31:43 +0100 Subject: ivimedia: Improve the media_simulator backend The backend uses the "qt.ivi.media.media_simulator" logging category for now to log its current state. Fix a problem where sometimes the mediaplayer stopped playing tracks when changing them too often or too fast. We now save the state internally and request QtMultimedia again once the Media was loaded. Change-Id: I8d66ee671cd6921a0d0c4a18d80649fb50311223 Reviewed-by: Robert Griebl --- .../media_simulator/mediaplayerbackend.cpp | 45 +++++++++++++++++++--- .../ivimedia/media_simulator/mediaplayerbackend.h | 4 ++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/plugins/ivimedia/media_simulator/mediaplayerbackend.cpp b/src/plugins/ivimedia/media_simulator/mediaplayerbackend.cpp index 4476679..fefabcf 100644 --- a/src/plugins/ivimedia/media_simulator/mediaplayerbackend.cpp +++ b/src/plugins/ivimedia/media_simulator/mediaplayerbackend.cpp @@ -49,21 +49,26 @@ #include #include #include +#include + +Q_LOGGING_CATEGORY(media, "qt.ivi.media.media_simulator") MediaPlayerBackend::MediaPlayerBackend(const QSqlDatabase &database, QObject *parent) : QIviMediaPlayerBackendInterface(parent) , m_count(0) , m_currentIndex(-1) , m_playMode(QIviMediaPlayer::Normal) + , m_requestedState(QIviMediaPlayer::Stopped) + , m_state(QIviMediaPlayer::Stopped) , m_threadPool(new QThreadPool(this)) , m_player(new QMediaPlayer(this)) { m_threadPool->setMaxThreadCount(1); connect(m_player, &QMediaPlayer::durationChanged, - this, &MediaPlayerBackend::durationChanged); + this, &MediaPlayerBackend::onDurationChanged); connect(m_player, &QMediaPlayer::positionChanged, - this, &MediaPlayerBackend::positionChanged); + this, &MediaPlayerBackend::onPositionChanged); connect(m_player, &QMediaPlayer::stateChanged, this, &MediaPlayerBackend::onStateChanged); connect(m_player, &QMediaPlayer::mediaStatusChanged, @@ -87,26 +92,35 @@ void MediaPlayerBackend::initialize() void MediaPlayerBackend::play() { + qCDebug(media) << Q_FUNC_INFO; + qCDebug(media) << m_player->media().canonicalUrl(); + m_requestedState = QIviMediaPlayer::Playing; m_player->play(); } void MediaPlayerBackend::pause() { + qCDebug(media) << Q_FUNC_INFO; + m_requestedState = QIviMediaPlayer::Paused; m_player->pause(); } void MediaPlayerBackend::stop() { + qCDebug(media) << Q_FUNC_INFO; + m_requestedState = QIviMediaPlayer::Stopped; m_player->stop(); } void MediaPlayerBackend::seek(qint64 offset) { + qCDebug(media) << Q_FUNC_INFO << offset; m_player->setPosition(m_player->position() + offset); } void MediaPlayerBackend::next() { + qCDebug(media) << Q_FUNC_INFO; int nextIndex = m_currentIndex + 1; if (m_playMode == QIviMediaPlayer::Shuffle) nextIndex = qrand() % m_count; @@ -120,6 +134,7 @@ void MediaPlayerBackend::next() void MediaPlayerBackend::previous() { + qCDebug(media) << Q_FUNC_INFO; int nextIndex = m_currentIndex - 1; if (m_playMode == QIviMediaPlayer::Shuffle) nextIndex = qrand() % m_count; @@ -133,12 +148,14 @@ void MediaPlayerBackend::previous() void MediaPlayerBackend::setPlayMode(QIviMediaPlayer::PlayMode playMode) { + qCDebug(media) << Q_FUNC_INFO << playMode; m_playMode = playMode; emit playModeChanged(m_playMode); } void MediaPlayerBackend::setPosition(qint64 position) { + qCDebug(media) << Q_FUNC_INFO << position; m_player->setPosition(position); } @@ -328,6 +345,7 @@ void MediaPlayerBackend::doSqlOperation(MediaPlayerBackend::OperationType type, void MediaPlayerBackend::setCurrentIndex(int index) { + qCDebug(media) << Q_FUNC_INFO << index; //If we the list is empty the current Index needs to updated to an invalid track if (m_count == 0 && index == -1) { m_currentIndex = index; @@ -357,17 +375,32 @@ void MediaPlayerBackend::setCurrentIndex(int index) void MediaPlayerBackend::onStateChanged(QMediaPlayer::State state) { - QIviMediaPlayer::PlayState iviState = QIviMediaPlayer::Stopped; + qCDebug(media) << Q_FUNC_INFO << state; if (state == QMediaPlayer::PlayingState) - iviState = QIviMediaPlayer::Playing; + m_state = QIviMediaPlayer::Playing; else if (state == QMediaPlayer::PausedState) - iviState = QIviMediaPlayer::Paused; + m_state = QIviMediaPlayer::Paused; - emit playStateChanged(iviState); + emit playStateChanged(m_state); } void MediaPlayerBackend::onMediaStatusChanged(QMediaPlayer::MediaStatus status) { + qCDebug(media) << Q_FUNC_INFO << status; if (status == QMediaPlayer::EndOfMedia) next(); + if (status == QMediaPlayer::LoadedMedia && m_requestedState == QIviMediaPlayer::Playing) + m_player->play(); +} + +void MediaPlayerBackend::onPositionChanged(qint64 position) +{ + qCDebug(media) << Q_FUNC_INFO << position; + emit positionChanged(position); +} + +void MediaPlayerBackend::onDurationChanged(qint64 duration) +{ + qCDebug(media) << Q_FUNC_INFO << duration; + emit durationChanged(duration); } diff --git a/src/plugins/ivimedia/media_simulator/mediaplayerbackend.h b/src/plugins/ivimedia/media_simulator/mediaplayerbackend.h index 054f9c4..e2db764 100644 --- a/src/plugins/ivimedia/media_simulator/mediaplayerbackend.h +++ b/src/plugins/ivimedia/media_simulator/mediaplayerbackend.h @@ -90,11 +90,15 @@ public Q_SLOTS: private Q_SLOTS: void onStateChanged(QMediaPlayer::State state); void onMediaStatusChanged(QMediaPlayer::MediaStatus status); + void onPositionChanged(qint64 position); + void onDurationChanged(qint64 duration); private: int m_count; int m_currentIndex; QIviMediaPlayer::PlayMode m_playMode; + QIviMediaPlayer::PlayState m_requestedState; + QIviMediaPlayer::PlayState m_state; QThreadPool *m_threadPool; QMediaPlayer *m_player; QSqlDatabase m_db; -- cgit v1.2.1