Skip to content

Commit

Permalink
ao: add drain(), reset() to clear buffers
Browse files Browse the repository at this point in the history
TODO: call backend api
  • Loading branch information
wang-bin committed Jan 27, 2016
1 parent a64fab9 commit 4734818
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/QtAV/AudioOutput.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2012-2015 Wang Bin <[email protected]>
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV
Expand Down Expand Up @@ -88,6 +88,8 @@ class Q_AV_EXPORT AudioOutput : public QObject, public AVOutput
* backend name currently in use
*/
QString backend() const;
bool drain();
void reset(); //drain and set audio time 0
bool open();
bool close();
bool isOpen() const;
Expand Down
26 changes: 22 additions & 4 deletions src/output/audio/AudioOutput.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2012-2015 Wang Bin <[email protected]>
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV
Expand Down Expand Up @@ -151,7 +151,6 @@ class AudioOutputPrivate : public AVOutputPrivate
cond.wait(&mutex, (us+500LL)/1000LL);
}

int bufferSizeTotal() { return nb_buffers * buffer_size; }
struct FrameInfo {
FrameInfo(qreal t = 0, int s = 0) : timestamp(t), data_size(s) {}
qreal timestamp;
Expand Down Expand Up @@ -352,6 +351,23 @@ QString AudioOutput::backend() const
return QString();
}

bool AudioOutput::drain()
{
DPTR_D(AudioOutput);
// TODO: backend drain
while (!d.frame_infos.empty()) {
waitForNextBuffer();
}
return true;
}

void AudioOutput::reset()
{
drain();
DPTR_D(AudioOutput);
d.resetStatus();
}

bool AudioOutput::open()
{
DPTR_D(AudioOutput);
Expand Down Expand Up @@ -611,6 +627,8 @@ AudioOutput::DeviceFeatures AudioOutput::supportedDeviceFeatures() const
bool AudioOutput::waitForNextBuffer()
{
DPTR_D(AudioOutput);
if (d.frame_infos.empty())
return true;
if (!d.backend)
return false;
//don't return even if we can add buffer because we don't know when a buffer is processed and we have /to update dequeue index
Expand Down Expand Up @@ -665,7 +683,7 @@ bool AudioOutput::waitForNextBuffer()
while (!no_wait && d.processed_remain < next) {
const qint64 us = d.format.durationForBytes(next - d.processed_remain);
if (us < 1000LL)
d.uwait(10000LL);
d.uwait(1000LL);
else
d.uwait(us);
d.processed_remain = d.backend->getPlayedBytes();
Expand All @@ -689,7 +707,7 @@ bool AudioOutput::waitForNextBuffer()
if (elapsed > 0 && us > elapsed*1000LL)
us -= elapsed*1000LL;
if (us < 1000LL)
us = 10000LL; //opensl crash if 1
us = 1000LL; //opensl crash if 1ms
#endif //AO_USE_TIMER
d.uwait(us);
c = d.backend->getPlayedCount();
Expand Down

0 comments on commit 4734818

Please sign in to comment.