From 0f9a779f7214f22dfa4625b91dbc3e4d0fc1c122 Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Tue, 14 Apr 2015 09:32:14 +0400 Subject: [QSoundBuffer] Replace isReady() with state() states The two-state "isReady" is not enough for checking if loading was already requested. This also makes it abvious we're accepting load() after error. Change-Id: I8181f99e8b36be484ec791862941b5b2ec78eb1f Reviewed-by: Yoann Lopes --- src/imports/audioengine/qaudioengine_openal_p.cpp | 23 ++++++++++++++-------- src/imports/audioengine/qaudioengine_openal_p.h | 5 +++-- .../audioengine/qdeclarative_audiosample_p.cpp | 9 ++++----- src/imports/audioengine/qsoundbuffer_p.h | 13 +++++++++++- src/imports/audioengine/qsoundinstance_p.cpp | 2 +- src/imports/audioengine/qsoundsource_openal_p.cpp | 2 +- 6 files changed, 36 insertions(+), 18 deletions(-) (limited to 'src/imports/audioengine') diff --git a/src/imports/audioengine/qaudioengine_openal_p.cpp b/src/imports/audioengine/qaudioengine_openal_p.cpp index c71695ad0..839a584c5 100644 --- a/src/imports/audioengine/qaudioengine_openal_p.cpp +++ b/src/imports/audioengine/qaudioengine_openal_p.cpp @@ -58,7 +58,7 @@ StaticSoundBufferAL::StaticSoundBufferAL(QObject *parent, const QUrl &url, QSamp m_ref(1), m_url(url), m_alBuffer(0), - m_isReady(false), + m_state(Creating), m_sample(0), m_sampleLoader(sampleLoader) { @@ -79,10 +79,19 @@ StaticSoundBufferAL::~StaticSoundBufferAL() } } +QSoundBuffer::State StaticSoundBufferAL::state() const +{ + return m_state; +} + void StaticSoundBufferAL::load() { - if (m_sample) + if (m_state == Loading || m_state == Ready) return; + + m_state = Loading; + emit stateChanged(m_state); + m_sample = m_sampleLoader->requestSample(m_url); connect(m_sample, SIGNAL(error()), this, SLOT(decoderError())); connect(m_sample, SIGNAL(ready()), this, SLOT(sampleReady())); @@ -98,11 +107,6 @@ void StaticSoundBufferAL::load() } } -bool StaticSoundBufferAL::isReady() const -{ - return m_isReady; -} - void StaticSoundBufferAL::bindToSource(ALuint alSource) { Q_ASSERT(m_alBuffer != 0); @@ -162,7 +166,8 @@ void StaticSoundBufferAL::sampleReady() m_sample->release(); m_sample = 0; - m_isReady = true; + m_state = Ready; + emit stateChanged(m_state); emit ready(); } @@ -176,6 +181,8 @@ void StaticSoundBufferAL::decoderError() m_sample->release(); m_sample = 0; + m_state = Error; + emit stateChanged(m_state); emit error(); } diff --git a/src/imports/audioengine/qaudioengine_openal_p.h b/src/imports/audioengine/qaudioengine_openal_p.h index 9b3a7abea..f534ab8c4 100644 --- a/src/imports/audioengine/qaudioengine_openal_p.h +++ b/src/imports/audioengine/qaudioengine_openal_p.h @@ -74,8 +74,9 @@ public: StaticSoundBufferAL(QObject *parent, const QUrl &url, QSampleCache *sampleLoader); ~StaticSoundBufferAL(); + State state() const Q_DECL_OVERRIDE; + void load() Q_DECL_OVERRIDE; - bool isReady() const Q_DECL_OVERRIDE; void bindToSource(ALuint alSource) Q_DECL_OVERRIDE; void unbindFromSource(ALuint alSource) Q_DECL_OVERRIDE; @@ -92,7 +93,7 @@ private: long m_ref; QUrl m_url; ALuint m_alBuffer; - bool m_isReady; + State m_state; QSample *m_sample; QSampleCache *m_sampleLoader; }; diff --git a/src/imports/audioengine/qdeclarative_audiosample_p.cpp b/src/imports/audioengine/qdeclarative_audiosample_p.cpp index e95840711..5a9a67b71 100644 --- a/src/imports/audioengine/qdeclarative_audiosample_p.cpp +++ b/src/imports/audioengine/qdeclarative_audiosample_p.cpp @@ -153,7 +153,7 @@ bool QDeclarativeAudioSample::isLoaded() const { if (!m_soundBuffer) return false; - return m_soundBuffer->isReady(); + return m_soundBuffer->state() == QSoundBuffer::Ready; } /*! @@ -163,13 +163,12 @@ bool QDeclarativeAudioSample::isLoaded() const */ void QDeclarativeAudioSample::load() { - if (isLoaded()) - return; if (!m_soundBuffer) { m_preloaded = true; return; } - m_soundBuffer->load(); + if (m_soundBuffer->state() != QSoundBuffer::Loading && m_soundBuffer->state() != QSoundBuffer::Ready) + m_soundBuffer->load(); } void QDeclarativeAudioSample::setPreloaded(bool preloaded) @@ -218,7 +217,7 @@ void QDeclarativeAudioSample::init() } else { m_soundBuffer = qobject_cast(parent())->engine()->getStaticSoundBuffer(m_url); - if (m_soundBuffer->isReady()) { + if (m_soundBuffer->state() == QSoundBuffer::Ready) { emit loadedChanged(); } else { connect(m_soundBuffer, SIGNAL(ready()), this, SIGNAL(loadedChanged())); diff --git a/src/imports/audioengine/qsoundbuffer_p.h b/src/imports/audioengine/qsoundbuffer_p.h index e9cc4cc01..9a98b59ba 100644 --- a/src/imports/audioengine/qsoundbuffer_p.h +++ b/src/imports/audioengine/qsoundbuffer_p.h @@ -41,11 +41,22 @@ QT_BEGIN_NAMESPACE class QSoundBuffer : public QObject { Q_OBJECT + public: - virtual bool isReady() const = 0; + enum State + { + Creating, + Loading, + Error, + Ready + }; + + virtual State state() const = 0; + virtual void load() = 0; Q_SIGNALS: + void stateChanged(State state); void ready(); void error(); diff --git a/src/imports/audioengine/qsoundinstance_p.cpp b/src/imports/audioengine/qsoundinstance_p.cpp index 522c0106e..20fef6d70 100644 --- a/src/imports/audioengine/qsoundinstance_p.cpp +++ b/src/imports/audioengine/qsoundinstance_p.cpp @@ -144,7 +144,7 @@ void QSoundInstance::prepareNewVariation() detach(); m_bindBuffer = playVar->sampleObject()->soundBuffer(); - if (m_bindBuffer->isReady()) { + if (m_bindBuffer->state() == QSoundBuffer::Ready) { Q_ASSERT(m_soundSource); m_soundSource->bindBuffer(m_bindBuffer); m_isReady = true; diff --git a/src/imports/audioengine/qsoundsource_openal_p.cpp b/src/imports/audioengine/qsoundsource_openal_p.cpp index 5725a506a..d5b2be9ea 100644 --- a/src/imports/audioengine/qsoundsource_openal_p.cpp +++ b/src/imports/audioengine/qsoundsource_openal_p.cpp @@ -85,7 +85,7 @@ void QSoundSourcePrivate::release() void QSoundSourcePrivate::bindBuffer(QSoundBuffer* soundBuffer) { unbindBuffer(); - Q_ASSERT(soundBuffer->isReady()); + Q_ASSERT(soundBuffer->state() == QSoundBuffer::Ready); m_bindBuffer = qobject_cast(soundBuffer); m_bindBuffer->bindToSource(m_alSource); m_isReady = true; -- cgit v1.2.1