[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 <yoann.lopes@theqtcompany.com>
This commit is contained in:
Konstantin Ritt
2015-04-14 09:32:14 +04:00
parent 51d6b5c811
commit 0f9a779f72
6 changed files with 36 additions and 18 deletions

View File

@@ -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();
}

View File

@@ -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;
};

View File

@@ -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<QDeclarativeAudioEngine*>(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()));

View File

@@ -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();

View File

@@ -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;

View File

@@ -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<QSoundBufferPrivateAL*>(soundBuffer);
m_bindBuffer->bindToSource(m_alSource);
m_isReady = true;