From 90f60bc39c0b0f05c0aad6fcf30e065c83667236 Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Fri, 24 Dec 2021 02:27:20 +0100 Subject: [PATCH] PlayerManager: Move duplicate code into function in anonymous namespace --- src/mixer/playermanager.cpp | 62 ++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/mixer/playermanager.cpp b/src/mixer/playermanager.cpp index 7377b0199e9e..bcfb9c6dbada 100644 --- a/src/mixer/playermanager.cpp +++ b/src/mixer/playermanager.cpp @@ -60,6 +60,30 @@ bool extractIntFromRegex(const QRegularExpression& regex, const QString& group, return true; } +/// Returns the first object from a list of `BaseTrackPlayer` instances where +/// the corresponding `play` CO is set to 0. +template +T* findFirstStoppedPlayerInList(const QList& players) { + for (T* pPlayer : players) { + VERIFY_OR_DEBUG_ASSERT(pPlayer != nullptr) { + continue; + } + + ControlObject* pPlayControl = ControlObject::getControl( + ConfigKey(pPlayer->getGroup(), "play")); + VERIFY_OR_DEBUG_ASSERT(pPlayControl != nullptr) { + continue; + } + + if (!pPlayControl->toBool()) { + return pPlayer; + } + } + + // There is no stopped player in the list. + return nullptr; +} + } // anonymous namespace //static @@ -658,37 +682,25 @@ void PlayerManager::slotLoadToSampler(const QString& location, int sampler) { void PlayerManager::slotLoadTrackIntoNextAvailableDeck(TrackPointer pTrack) { auto locker = lockMutex(&m_mutex); - QList::iterator it = m_decks.begin(); - while (it != m_decks.end()) { - Deck* pDeck = *it; - ControlObject* playControl = - ControlObject::getControl(ConfigKey(pDeck->getGroup(), "play")); - if (playControl && playControl->get() != 1.) { - locker.unlock(); - pDeck->slotLoadTrack(pTrack, false); - // Test for a fixed race condition with fast loads - //SleepableQThread::sleep(1); - //pDeck->slotLoadTrack(TrackPointer(), false); - return; - } - ++it; + BaseTrackPlayer* pDeck = findFirstStoppedPlayerInList(m_decks); + if (pDeck == nullptr) { + qDebug() << "PlayerManager: No stopped deck found, not loading track!"; + return; } + + pDeck->slotLoadTrack(pTrack, false); } void PlayerManager::slotLoadTrackIntoNextAvailableSampler(TrackPointer pTrack) { auto locker = lockMutex(&m_mutex); - QList::iterator it = m_samplers.begin(); - while (it != m_samplers.end()) { - Sampler* pSampler = *it; - ControlObject* playControl = - ControlObject::getControl(ConfigKey(pSampler->getGroup(), "play")); - if (playControl && playControl->get() != 1.) { - locker.unlock(); - pSampler->slotLoadTrack(pTrack, false); - return; - } - ++it; + BaseTrackPlayer* pSampler = findFirstStoppedPlayerInList(m_samplers); + if (pSampler == nullptr) { + qDebug() << "PlayerManager: No stopped sampler found, not loading track!"; + return; } + locker.unlock(); + + pSampler->slotLoadTrack(pTrack, false); } void PlayerManager::slotAnalyzeTrack(TrackPointer track) {