From 4734818b3dde6a272c8bade478ae040d9991bc31 Mon Sep 17 00:00:00 2001 From: wang-bin Date: Thu, 28 Jan 2016 01:31:43 +0800 Subject: [PATCH] ao: add drain(), reset() to clear buffers TODO: call backend api --- src/QtAV/AudioOutput.h | 4 +++- src/output/audio/AudioOutput.cpp | 26 ++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/QtAV/AudioOutput.h b/src/QtAV/AudioOutput.h index cf4424ba5..74c01e368 100644 --- a/src/QtAV/AudioOutput.h +++ b/src/QtAV/AudioOutput.h @@ -1,6 +1,6 @@ /****************************************************************************** QtAV: Media play library based on Qt and FFmpeg - Copyright (C) 2012-2015 Wang Bin + Copyright (C) 2012-2016 Wang Bin * This file is part of QtAV @@ -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; diff --git a/src/output/audio/AudioOutput.cpp b/src/output/audio/AudioOutput.cpp index 0bc114817..e83de7856 100644 --- a/src/output/audio/AudioOutput.cpp +++ b/src/output/audio/AudioOutput.cpp @@ -1,6 +1,6 @@ /****************************************************************************** QtAV: Media play library based on Qt and FFmpeg - Copyright (C) 2012-2015 Wang Bin + Copyright (C) 2012-2016 Wang Bin * This file is part of QtAV @@ -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; @@ -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); @@ -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 @@ -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(); @@ -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();