Skip to content

Commit

Permalink
ao: add a simple play(data,pts) function
Browse files Browse the repository at this point in the history
waitForNextBuffer and receiveData is used internally
  • Loading branch information
wang-bin committed Feb 12, 2015
1 parent 7c94e6f commit 6f546b7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
4 changes: 1 addition & 3 deletions src/AudioThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 17 additions & 8 deletions src/QtAV/AudioOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/output/audio/AudioOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -65,6 +72,8 @@ void AudioOutput::setAudioFormat(const AudioFormat& format)
if (!isSupported(format)) {
return;
}
if (d.format == format)
return;
d.format = format;
}

Expand Down
4 changes: 1 addition & 3 deletions tests/ao/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6f546b7

Please sign in to comment.