diff options
author | Lev Zelenskiy <lev.zelenskiy@nokia.com> | 2012-02-24 12:50:32 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-02-24 06:00:41 +0100 |
commit | b56d3e70df4a5bf25a1be97213b5dc36bc5e6dbd (patch) | |
tree | e35fd301a0e7198609679c95e2374d4b4aa28e85 /tests | |
parent | 0b8c6115cdb1ed7174b558daa00a19841a2d5a78 (diff) | |
download | qtmultimedia-b56d3e70df4a5bf25a1be97213b5dc36bc5e6dbd.tar.gz |
Changes to GStreamer backend for audio decoder.
Removed WaitingState.
New signals: finished(), positionChanged(), durationChanged().
New methods: position(), duration().
A parameter removed from read() method.
Unit tests updated.
Change-Id: Ie9d8a2804285c5542e592cce69963adbdf6ebfb8
Reviewed-by: Jonas Rabbe <jonas.rabbe@nokia.com>
Reviewed-by: Michael Goddard <michael.goddard@nokia.com>
Diffstat (limited to 'tests')
3 files changed, 146 insertions, 49 deletions
diff --git a/tests/auto/integration/qaudiodecoderbackend/tst_qaudiodecoderbackend.cpp b/tests/auto/integration/qaudiodecoderbackend/tst_qaudiodecoderbackend.cpp index f71062af4..e4370198d 100644 --- a/tests/auto/integration/qaudiodecoderbackend/tst_qaudiodecoderbackend.cpp +++ b/tests/auto/integration/qaudiodecoderbackend/tst_qaudiodecoderbackend.cpp @@ -82,7 +82,6 @@ void tst_QAudioDecoderBackend::cleanup() void tst_QAudioDecoderBackend::fileTest() { QAudioDecoder d; - bool ok; QAudioBuffer buffer; quint64 duration = 0; int byteCount = 0; @@ -104,6 +103,9 @@ void tst_QAudioDecoderBackend::fileTest() QSignalSpy bufferChangedSpy(&d, SIGNAL(bufferAvailableChanged(bool))); QSignalSpy errorSpy(&d, SIGNAL(error(QAudioDecoder::Error))); QSignalSpy stateSpy(&d, SIGNAL(stateChanged(QAudioDecoder::State))); + QSignalSpy durationSpy(&d, SIGNAL(durationChanged(qint64))); + QSignalSpy finishedSpy(&d, SIGNAL(finished())); + QSignalSpy positionSpy(&d, SIGNAL(positionChanged(qint64))); d.start(); QTRY_VERIFY(d.state() == QAudioDecoder::DecodingState); @@ -111,9 +113,10 @@ void tst_QAudioDecoderBackend::fileTest() QTRY_VERIFY(!readySpy.isEmpty()); QTRY_VERIFY(!bufferChangedSpy.isEmpty()); QVERIFY(d.bufferAvailable()); + QTRY_VERIFY(!durationSpy.isEmpty()); + QVERIFY(qAbs(d.duration() - 1000) < 20); - buffer = d.read(&ok); - QVERIFY(ok); + buffer = d.read(); QVERIFY(buffer.isValid()); // Test file is 44.1K 16bit mono, 44094 samples @@ -139,9 +142,11 @@ void tst_QAudioDecoderBackend::fileTest() } while (d.bufferAvailable()) { - buffer = d.read(&ok); - QVERIFY(ok); + buffer = d.read(); QVERIFY(buffer.isValid()); + QTRY_VERIFY(!positionSpy.isEmpty()); + QVERIFY(positionSpy.takeLast().at(0).toLongLong() == qint64(duration / 1000)); + duration += buffer.duration(); sampleCount += buffer.sampleCount(); byteCount += buffer.byteCount(); @@ -155,13 +160,22 @@ void tst_QAudioDecoderBackend::fileTest() QCOMPARE(sampleCount, 44094); QCOMPARE(byteCount, 44094 * 2); QVERIFY(qAbs(qint64(duration) - 1000000) < 20000); + QVERIFY(qAbs((d.position() + (buffer.duration() / 1000)) - 1000) < 20); + QTRY_COMPARE(finishedSpy.count(), 1); + QVERIFY(!d.bufferAvailable()); + QTRY_COMPARE(d.state(), QAudioDecoder::StoppedState); d.stop(); QTRY_COMPARE(d.state(), QAudioDecoder::StoppedState); + QTRY_COMPARE(durationSpy.count(), 2); + QCOMPARE(d.duration(), qint64(-1)); QVERIFY(!d.bufferAvailable()); readySpy.clear(); bufferChangedSpy.clear(); stateSpy.clear(); + durationSpy.clear(); + finishedSpy.clear(); + positionSpy.clear(); // change output audio format QAudioFormat format; @@ -189,9 +203,10 @@ void tst_QAudioDecoderBackend::fileTest() QTRY_VERIFY(!readySpy.isEmpty()); QTRY_VERIFY(!bufferChangedSpy.isEmpty()); QVERIFY(d.bufferAvailable()); + QTRY_VERIFY(!durationSpy.isEmpty()); + QVERIFY(qAbs(d.duration() - 1000) < 20); - buffer = d.read(&ok); - QVERIFY(ok); + buffer = d.read(); QVERIFY(buffer.isValid()); // See if we got the right format QVERIFY(buffer.format() == format); @@ -211,9 +226,11 @@ void tst_QAudioDecoderBackend::fileTest() } while (d.bufferAvailable()) { - buffer = d.read(&ok); - QVERIFY(ok); + buffer = d.read(); QVERIFY(buffer.isValid()); + QTRY_VERIFY(!positionSpy.isEmpty()); + QVERIFY(positionSpy.takeLast().at(0).toLongLong() == qint64(duration / 1000)); + duration += buffer.duration(); sampleCount += buffer.sampleCount(); byteCount += buffer.byteCount(); @@ -228,16 +245,21 @@ void tst_QAudioDecoderBackend::fileTest() QVERIFY(qAbs(sampleCount - 22047) < 100); QVERIFY(qAbs(byteCount - 22047) < 100); QVERIFY(qAbs(qint64(duration) - 1000000) < 20000); + QVERIFY(qAbs((d.position() + (buffer.duration() / 1000)) - 1000) < 20); + QTRY_COMPARE(finishedSpy.count(), 1); + QVERIFY(!d.bufferAvailable()); + QTRY_COMPARE(d.state(), QAudioDecoder::StoppedState); d.stop(); QTRY_COMPARE(d.state(), QAudioDecoder::StoppedState); + QTRY_COMPARE(durationSpy.count(), 2); + QCOMPARE(d.duration(), qint64(-1)); QVERIFY(!d.bufferAvailable()); } void tst_QAudioDecoderBackend::deviceTest() { QAudioDecoder d; - bool ok; QAudioBuffer buffer; quint64 duration = 0; int sampleCount = 0; @@ -246,6 +268,9 @@ void tst_QAudioDecoderBackend::deviceTest() QSignalSpy bufferChangedSpy(&d, SIGNAL(bufferAvailableChanged(bool))); QSignalSpy errorSpy(&d, SIGNAL(error(QAudioDecoder::Error))); QSignalSpy stateSpy(&d, SIGNAL(stateChanged(QAudioDecoder::State))); + QSignalSpy durationSpy(&d, SIGNAL(durationChanged(qint64))); + QSignalSpy finishedSpy(&d, SIGNAL(finished())); + QSignalSpy positionSpy(&d, SIGNAL(positionChanged(qint64))); QVERIFY(d.state() == QAudioDecoder::StoppedState); QVERIFY(d.bufferAvailable() == false); @@ -269,9 +294,10 @@ void tst_QAudioDecoderBackend::deviceTest() QTRY_VERIFY(!readySpy.isEmpty()); QTRY_VERIFY(!bufferChangedSpy.isEmpty()); QVERIFY(d.bufferAvailable()); + QTRY_VERIFY(!durationSpy.isEmpty()); + QVERIFY(qAbs(d.duration() - 1000) < 20); - buffer = d.read(&ok); - QVERIFY(ok); + buffer = d.read(); QVERIFY(buffer.isValid()); // Test file is 44.1K 16bit mono @@ -292,9 +318,11 @@ void tst_QAudioDecoderBackend::deviceTest() } while (d.bufferAvailable()) { - buffer = d.read(&ok); - QVERIFY(ok); + buffer = d.read(); QVERIFY(buffer.isValid()); + QTRY_VERIFY(!positionSpy.isEmpty()); + QVERIFY(positionSpy.takeLast().at(0).toLongLong() == qint64(duration / 1000)); + duration += buffer.duration(); sampleCount += buffer.sampleCount(); if (sampleCount < 44094) { @@ -305,13 +333,22 @@ void tst_QAudioDecoderBackend::deviceTest() // Make sure the duration is roughly correct (+/- 20ms) QCOMPARE(sampleCount, 44094); QVERIFY(qAbs(qint64(duration) - 1000000) < 20000); + QVERIFY(qAbs((d.position() + (buffer.duration() / 1000)) - 1000) < 20); + QTRY_COMPARE(finishedSpy.count(), 1); + QVERIFY(!d.bufferAvailable()); + QTRY_COMPARE(d.state(), QAudioDecoder::StoppedState); d.stop(); QTRY_COMPARE(d.state(), QAudioDecoder::StoppedState); QVERIFY(!d.bufferAvailable()); + QTRY_COMPARE(durationSpy.count(), 2); + QCOMPARE(d.duration(), qint64(-1)); readySpy.clear(); bufferChangedSpy.clear(); stateSpy.clear(); + durationSpy.clear(); + finishedSpy.clear(); + positionSpy.clear(); // Now try changing formats QAudioFormat format; @@ -332,9 +369,10 @@ void tst_QAudioDecoderBackend::deviceTest() QTRY_VERIFY(!readySpy.isEmpty()); QTRY_VERIFY(!bufferChangedSpy.isEmpty()); QVERIFY(d.bufferAvailable()); + QTRY_VERIFY(!durationSpy.isEmpty()); + QVERIFY(qAbs(d.duration() - 1000) < 20); - buffer = d.read(&ok); - QVERIFY(ok); + buffer = d.read(); QVERIFY(buffer.isValid()); // See if we got the right format QVERIFY(buffer.format() == format); @@ -347,7 +385,8 @@ void tst_QAudioDecoderBackend::deviceTest() d.stop(); QTRY_COMPARE(d.state(), QAudioDecoder::StoppedState); QVERIFY(!d.bufferAvailable()); - + QTRY_COMPARE(durationSpy.count(), 2); + QCOMPARE(d.duration(), qint64(-1)); } QTEST_MAIN(tst_QAudioDecoderBackend) diff --git a/tests/auto/unit/qaudiodecoder/tst_qaudiodecoder.cpp b/tests/auto/unit/qaudiodecoder/tst_qaudiodecoder.cpp index aacc607f9..edc250199 100644 --- a/tests/auto/unit/qaudiodecoder/tst_qaudiodecoder.cpp +++ b/tests/auto/unit/qaudiodecoder/tst_qaudiodecoder.cpp @@ -61,6 +61,7 @@ private Q_SLOTS: void stop(); void format(); void source(); + void readAll(); private: MockAudioDecoderService *mockAudioDecoderService; @@ -125,10 +126,7 @@ void tst_QAudioDecoder::read() QCOMPARE(d.bufferAvailable(), false); // not yet // Try to read - bool ok = false; - QAudioBuffer b = d.read(&ok); - - QVERIFY(ok == false); + QAudioBuffer b = d.read(); QVERIFY(!b.isValid()); // Read again with no parameter @@ -140,7 +138,7 @@ void tst_QAudioDecoder::read() QVERIFY(d.bufferAvailable()); - b = d.read(&ok); + b = d.read(); QVERIFY(b.format().isValid()); QVERIFY(b.isValid()); QVERIFY(b.format().channelCount() == 1); @@ -189,10 +187,7 @@ void tst_QAudioDecoder::stop() QCOMPARE(d.bufferAvailable(), false); // not yet // Try to read - bool ok = false; - QAudioBuffer b = d.read(&ok); - - QVERIFY(ok == false); + QAudioBuffer b = d.read(); QVERIFY(!b.isValid()); // Read again with no parameter @@ -234,10 +229,7 @@ void tst_QAudioDecoder::format() QCOMPARE(d.bufferAvailable(), false); // not yet // Try to read - bool ok = false; - QAudioBuffer b = d.read(&ok); - - QVERIFY(ok == false); + QAudioBuffer b = d.read(); QVERIFY(!b.isValid()); // Read again with no parameter @@ -247,7 +239,7 @@ void tst_QAudioDecoder::format() // Wait a while QTRY_COMPARE(d.bufferAvailable(), 1); - b = d.read(&ok); + b = d.read(); QVERIFY(d.audioFormat() == b.format()); // Setting format while decoding is forbidden @@ -267,7 +259,7 @@ void tst_QAudioDecoder::format() d.start(); QTRY_COMPARE(d.bufferAvailable(), 1); - b = d.read(&ok); + b = d.read(); QVERIFY(d.audioFormat() == f); QVERIFY(b.format() == f); } @@ -301,6 +293,51 @@ void tst_QAudioDecoder::source() QVERIFY(d.sourceDevice() == 0); } +void tst_QAudioDecoder::readAll() +{ + QAudioDecoder d; + d.setSourceFilename("Foo"); + QVERIFY(d.state() == QAudioDecoder::StoppedState); + + QSignalSpy durationSpy(&d, SIGNAL(durationChanged(qint64))); + QSignalSpy positionSpy(&d, SIGNAL(positionChanged(qint64))); + QSignalSpy stateSpy(&d, SIGNAL(stateChanged(QAudioDecoder::State))); + QSignalSpy finishedSpy(&d, SIGNAL(finished())); + QSignalSpy bufferAvailableSpy(&d, SIGNAL(bufferAvailableChanged(bool))); + d.start(); + int i = 0; + forever { + QVERIFY(d.state() == QAudioDecoder::DecodingState); + QCOMPARE(stateSpy.count(), 1); + QCOMPARE(durationSpy.count(), 1); + QVERIFY(finishedSpy.isEmpty()); + QTRY_VERIFY(bufferAvailableSpy.count() >= 1); + if (d.bufferAvailable()) { + QAudioBuffer b = d.read(); + QVERIFY(b.isValid()); + QCOMPARE(b.startTime() / 1000, d.position()); + QVERIFY(!positionSpy.isEmpty()); + QList<QVariant> arguments = positionSpy.takeLast(); + QCOMPARE(arguments.at(0).toLongLong(), b.startTime() / 1000); + + i++; + if (i == MOCK_DECODER_MAX_BUFFERS) { + QCOMPARE(finishedSpy.count(), 1); + QCOMPARE(stateSpy.count(), 2); + QVERIFY(d.state() == QAudioDecoder::StoppedState); + QList<QVariant> arguments = stateSpy.takeLast(); + QVERIFY(arguments.at(0).toInt() == (int)QAudioDecoder::StoppedState); + QVERIFY(!d.bufferAvailable()); + QVERIFY(!bufferAvailableSpy.isEmpty()); + arguments = bufferAvailableSpy.takeLast(); + QVERIFY(arguments.at(0).toBool() == false); + break; + } + } else + QTest::qWait(30); + } +} + QTEST_MAIN(tst_QAudioDecoder) #include "tst_qaudiodecoder.moc" diff --git a/tests/auto/unit/qmultimedia_common/mockaudiodecodercontrol.h b/tests/auto/unit/qmultimedia_common/mockaudiodecodercontrol.h index 938da31c0..f0b0adc54 100644 --- a/tests/auto/unit/qmultimedia_common/mockaudiodecodercontrol.h +++ b/tests/auto/unit/qmultimedia_common/mockaudiodecodercontrol.h @@ -51,6 +51,8 @@ #include <QTimer> #include <QIODevice> +#define MOCK_DECODER_MAX_BUFFERS 10 + QT_BEGIN_HEADER QT_BEGIN_NAMESPACE @@ -66,6 +68,7 @@ public: : QAudioDecoderControl(parent) , mState(QAudioDecoder::StoppedState) , mDevice(0) + , mPosition(-1) , mSerial(0) { mFormat.setChannels(1); @@ -126,6 +129,7 @@ public: if (!mSource.isEmpty()) { mState = QAudioDecoder::DecodingState; emit stateChanged(mState); + emit durationChanged(duration()); QTimer::singleShot(50, this, SLOT(pretendDecode())); } else { @@ -139,28 +143,33 @@ public: if (mState != QAudioDecoder::StoppedState) { mState = QAudioDecoder::StoppedState; mSerial = 0; + mPosition = 0; mBuffers.clear(); emit stateChanged(mState); emit bufferAvailableChanged(false); } } - QAudioBuffer read(bool *ok) + QAudioBuffer read() { + QAudioBuffer a; if (mBuffers.length() > 0) { - if (ok) - *ok = true; - QAudioBuffer a = mBuffers.takeFirst(); - if (mBuffers.length() == 0) + a = mBuffers.takeFirst(); + mPosition = a.startTime() / 1000; + emit positionChanged(mPosition); + + if (mBuffers.isEmpty()) emit bufferAvailableChanged(false); - QTimer::singleShot(50, this, SLOT(pretendDecode())); - return a; - } else { - // Can't do anything here :( - if (ok) - *ok = false; - return QAudioBuffer(); + + if (mBuffers.isEmpty() && mSerial >= MOCK_DECODER_MAX_BUFFERS) { + mState = QAudioDecoder::StoppedState; + emit finished(); + emit stateChanged(mState); + } else + QTimer::singleShot(50, this, SLOT(pretendDecode())); } + + return a; } bool bufferAvailable() const @@ -168,22 +177,33 @@ public: return mBuffers.length() > 0; } + qint64 position() const + { + return mPosition; + } + + qint64 duration() const + { + return (sizeof(mSerial) * MOCK_DECODER_MAX_BUFFERS * qint64(1000)) / (mFormat.sampleRate() * mFormat.channels()); + } + private slots: void pretendDecode() { + // Check if we've reached end of stream + if (mSerial >= MOCK_DECODER_MAX_BUFFERS) + return; + // We just keep the length of mBuffers to 3 or less. if (mBuffers.length() < 3) { QByteArray b(sizeof(mSerial), 0); memcpy(b.data(), &mSerial, sizeof(mSerial)); + qint64 position = (sizeof(mSerial) * mSerial * qint64(1000000)) / (mFormat.sampleRate() * mFormat.channels()); mSerial++; - mBuffers.push_back(QAudioBuffer(b, mFormat)); + mBuffers.push_back(QAudioBuffer(b, mFormat, position)); emit bufferReady(); if (mBuffers.count() == 1) emit bufferAvailableChanged(true); - } else { - // Can't do anything here, wait for a read to restart the timer - mState = QAudioDecoder::WaitingState; - emit stateChanged(mState); } } @@ -192,6 +212,7 @@ public: QString mSource; QIODevice *mDevice; QAudioFormat mFormat; + qint64 mPosition; int mSerial; QList<QAudioBuffer> mBuffers; |