From 242ccc90c13011b22268973e9dd0dbb7a494cdf4 Mon Sep 17 00:00:00 2001 From: Henk Kalkwater Date: Mon, 2 Aug 2021 00:52:21 +0200 Subject: [PATCH] Remove complicated QMediaPlayer switching THe idea behind the switching was to provide gapless playback, but it made the code too complicated and to intertwined with QtMultiMedia. I might attempt to implement it later, but I probably will put the code in a separate class to make it easier to swap the QtMultiMedia implementation with a multimedia implementation for a player with better support for gappless playback. --- .../JellyfinQt/viewmodel/playbackmanager.h | 9 ---- core/src/viewmodel/playbackmanager.cpp | 46 ++++--------------- 2 files changed, 10 insertions(+), 45 deletions(-) diff --git a/core/include/JellyfinQt/viewmodel/playbackmanager.h b/core/include/JellyfinQt/viewmodel/playbackmanager.h index e11b552..d4703f7 100644 --- a/core/include/JellyfinQt/viewmodel/playbackmanager.h +++ b/core/include/JellyfinQt/viewmodel/playbackmanager.h @@ -226,21 +226,12 @@ private: /// Pointer to the current media player. QMediaPlayer *m_mediaPlayer = nullptr; - // There are 2 media players over here, so one is able to preload the next song - // before the other starts playing - - /// Media player 1 - QMediaPlayer *m_mediaPlayer1; - /// Media player 2 - QMediaPlayer *m_mediaPlayer2; - Model::Playlist *m_queue = nullptr; int m_queueIndex = 0; bool m_resumePlayback = true; // Helper methods void setItem(QSharedPointer newItem); - void swapMediaPlayer(); void setStreamUrl(const QUrl &streamUrl); void setPlaybackState(QMediaPlayer::State newState); diff --git a/core/src/viewmodel/playbackmanager.cpp b/core/src/viewmodel/playbackmanager.cpp index f5522f8..2d06b7e 100644 --- a/core/src/viewmodel/playbackmanager.cpp +++ b/core/src/viewmodel/playbackmanager.cpp @@ -37,12 +37,10 @@ namespace ViewModel { PlaybackManager::PlaybackManager(QObject *parent) : QObject(parent), m_item(nullptr), - m_mediaPlayer1(new QMediaPlayer(this)), - m_mediaPlayer2(new QMediaPlayer(this)), + m_mediaPlayer(new QMediaPlayer(this)), m_urlFetcherThread(new ItemUrlFetcherThread(this)), m_queue(new Model::Playlist(this)) { // Set up connections. - swapMediaPlayer(); m_updateTimer.setInterval(10000); // 10 seconds m_updateTimer.setSingleShot(false); @@ -52,6 +50,15 @@ PlaybackManager::PlaybackManager(QObject *parent) connect(&m_updateTimer, &QTimer::timeout, this, &PlaybackManager::updatePlaybackInfo); connect(m_urlFetcherThread, &ItemUrlFetcherThread::itemUrlFetched, this, &PlaybackManager::onItemExtraDataReceived); m_urlFetcherThread->start(); + + connect(m_mediaPlayer, &QMediaPlayer::stateChanged, this, &PlaybackManager::mediaPlayerStateChanged); + connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, &PlaybackManager::mediaPlayerPositionChanged); + connect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, &PlaybackManager::mediaPlayerDurationChanged); + connect(m_mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, &PlaybackManager::mediaPlayerMediaStatusChanged); + connect(m_mediaPlayer, &QMediaPlayer::videoAvailableChanged, this, &PlaybackManager::hasVideoChanged); + // I do not like the complicated overload cast + connect(m_mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(mediaPlayerError(QMediaPlayer::Error))); + } void PlaybackManager::onDestroyed() { @@ -184,7 +191,6 @@ void PlaybackManager::playItemInList(ItemModel *playlist, int index) { void PlaybackManager::next() { m_mediaPlayer->stop(); m_mediaPlayer->setMedia(QMediaContent()); - swapMediaPlayer(); if (m_nextItem.isNull()) { setItem(m_queue->nextItem()); @@ -203,7 +209,6 @@ void PlaybackManager::previous() { m_nextStreamUrl = m_streamUrl; m_streamUrl = QString(); m_nextItem = m_item; - swapMediaPlayer(); m_queue->previous(); setItem(m_queue->currentItem()); @@ -258,32 +263,6 @@ void PlaybackManager::postPlaybackInfo(PlaybackInfoType type) { m_apiClient->setDefaultErrorHandler(rep); } -void PlaybackManager::swapMediaPlayer() { - if (m_mediaPlayer != nullptr) { - disconnect(m_mediaPlayer, &QMediaPlayer::stateChanged, this, &PlaybackManager::mediaPlayerStateChanged); - disconnect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, &PlaybackManager::mediaPlayerPositionChanged); - disconnect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, &PlaybackManager::mediaPlayerDurationChanged); - disconnect(m_mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, &PlaybackManager::mediaPlayerMediaStatusChanged); - disconnect(m_mediaPlayer, &QMediaPlayer::videoAvailableChanged, this, &PlaybackManager::hasVideoChanged); - // I do not like the complicated overload cast - disconnect(m_mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(mediaPlayerError(QMediaPlayer::Error))); - } - if (m_mediaPlayer == m_mediaPlayer1) { - m_mediaPlayer = m_mediaPlayer2; - emit mediaPlayerChanged(m_mediaPlayer); - } else { - m_mediaPlayer = m_mediaPlayer1; - emit mediaPlayerChanged(m_mediaPlayer); - } - connect(m_mediaPlayer, &QMediaPlayer::stateChanged, this, &PlaybackManager::mediaPlayerStateChanged); - connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, &PlaybackManager::mediaPlayerPositionChanged); - connect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, &PlaybackManager::mediaPlayerDurationChanged); - connect(m_mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, &PlaybackManager::mediaPlayerMediaStatusChanged); - connect(m_mediaPlayer, &QMediaPlayer::videoAvailableChanged, this, &PlaybackManager::hasVideoChanged); - // I do not like the complicated overload cast - connect(m_mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(mediaPlayerError(QMediaPlayer::Error))); -} - void PlaybackManager::componentComplete() { if (m_apiClient == nullptr) qWarning() << "No ApiClient set for PlaybackManager"; m_qmlIsParsingComponent = false; @@ -410,11 +389,6 @@ void PlaybackManager::onItemExtraDataReceived(const QString &itemId, const QUrl emit playMethodChanged(m_playMethod); m_mediaPlayer->setMedia(QMediaContent(url)); m_mediaPlayer->play(); - } else if (!m_nextItem.isNull() && m_nextItem->jellyfinId() == itemId){ - QMediaPlayer *otherMediaPlayer = m_mediaPlayer == m_mediaPlayer1 ? m_mediaPlayer2 : m_mediaPlayer1; - m_nextPlaySessionId = playSession; - m_nextStreamUrl = url.toString(); - otherMediaPlayer->setMedia(QMediaContent(url)); } else { qDebug() << "Late reply for " << itemId << " received, ignoring"; }