diff --git a/src/AudioThread.cpp b/src/AudioThread.cpp index 7107a1c80..e8f6dcf1d 100644 --- a/src/AudioThread.cpp +++ b/src/AudioThread.cpp @@ -316,9 +316,7 @@ void AudioThread::run() scale(dst, dst, nb_samples, volume_i, vol); } } - ao->waitForNextBuffer(); - ao->receiveData(decodedChunk, pkt.pts); - ao->play(); + ao->play(decodedChunk, pkt.pts); d.clock->updateValue(ao->timestamp()); emit frameDelivered(); } else { diff --git a/src/QtAV/AudioOutput.h b/src/QtAV/AudioOutput.h index ed8879ca4..6859907ab 100644 --- a/src/QtAV/AudioOutput.h +++ b/src/QtAV/AudioOutput.h @@ -33,9 +33,7 @@ * ao->open(); * while (has_data) { * data = read_data(ao->bufferSize()); - * ao->waitForNextBuffer(); - * ao->receiveData(data, pts); - * ao->play(); + * ao->play(data, pts); * } * ao->close(); * See QtAV/tests/ao/main.cpp for detail @@ -78,7 +76,7 @@ class Q_AV_EXPORT AudioOutput : public QObject, public AVOutput Q_DECLARE_FLAGS(BufferControls, BufferControl) /*! * \brief The Feature enum - * features (set when playing) supported by the audio playback api. + * features (set when playing) supported by the audio playback api */ enum Feature { SetVolume = 1, @@ -94,8 +92,15 @@ class Q_AV_EXPORT AudioOutput : public QObject, public AVOutput virtual ~AudioOutput(); virtual bool open() = 0; virtual bool close() = 0; - // store and fill data to audio buffers - bool receiveData(const QByteArray &data, qreal pts = 0.0); + /*! + * \brief play + * Play out the given audio data. It may block current thread until the data can be written to audio device + * for async playback backend, or until the data is completely played for blocking playback backend. + * \param data Audio data to play + * \param pts Timestamp for this data. Useful if need A/V sync. Ignore it if only play audio + * \return true if play successfully + */ + bool play(const QByteArray& data, qreal pts = 0.0); /*! * \brief setAudioFormat * Remain the old value if not supported @@ -177,14 +182,18 @@ class Q_AV_EXPORT AudioOutput : public QObject, public AVOutput Feature features() const; void setFeature(Feature value, bool on = true); bool hasFeatures(Feature value) const; + qreal timestamp() const; + // Internal use since QtAV 1.5 + virtual bool play() = 0; //MUST /*! * \brief waitForNextBuffer * wait until you can feed more data + * Internal use since QtAV 1.5 */ virtual void waitForNextBuffer(); + // Internal use since QtAV 1.5. store and fill data to audio buffers + QTAV_DEPRECATED bool receiveData(const QByteArray &data, qreal pts = 0.0); // timestamp of current playing data - qreal timestamp() const; - virtual bool play() = 0; //MUST signals: void volumeChanged(qreal); void muteChanged(bool); diff --git a/src/output/audio/AudioOutput.cpp b/src/output/audio/AudioOutput.cpp index 2012b3ef4..cebf2b79d 100644 --- a/src/output/audio/AudioOutput.cpp +++ b/src/output/audio/AudioOutput.cpp @@ -42,6 +42,13 @@ AudioOutput::~AudioOutput() { } +bool AudioOutput::play(const QByteArray &data, qreal pts) +{ + waitForNextBuffer(); + receiveData(data, pts); + return play(); +} + bool AudioOutput::receiveData(const QByteArray &data, qreal pts) { DPTR_D(AudioOutput); @@ -65,6 +72,8 @@ void AudioOutput::setAudioFormat(const AudioFormat& format) if (!isSupported(format)) { return; } + if (d.format == format) + return; d.format = format; } diff --git a/tests/ao/main.cpp b/tests/ao/main.cpp index 935413617..1f073e030 100644 --- a/tests/ao/main.cpp +++ b/tests/ao/main.cpp @@ -87,9 +87,7 @@ int main(int argc, char** argv) left = (left+1) % kTableSize; right = (right+3)% kTableSize; } - ao->waitForNextBuffer(); - ao->receiveData(data); - ao->play(); + ao->play(data); } ao->close(); return 0;