From d8894353340746ae82d85423f8c9dbed53f967e4 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 1 Jan 2018 01:04:47 -0800 Subject: Declare the total buffer time instead of buffer count --- examples/alffplay.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'examples/alffplay.cpp') diff --git a/examples/alffplay.cpp b/examples/alffplay.cpp index 6d92cdba..55605792 100644 --- a/examples/alffplay.cpp +++ b/examples/alffplay.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -53,8 +54,10 @@ const std::chrono::duration AudioSampleCorrectionMax(0.05); /* Averaging filter coefficient for audio sync. */ #define AUDIO_DIFF_AVG_NB 20 const double AudioAvgFilterCoeff = std::pow(0.01, 1.0/AUDIO_DIFF_AVG_NB); -const std::chrono::milliseconds AudioBufferTime(20); /* Per-buffer */ -#define AUDIO_BUFFER_QUEUE_SIZE 25 /* Number of buffers to queue (~500ms) */ +/* Per-buffer size, in time */ +const std::chrono::milliseconds AudioBufferTime(20); +/* Buffer total size, in time (should be divisible by the buffer time) */ +const std::chrono::milliseconds AudioBufferTotalTime(800); #define MAX_QUEUE_SIZE (15 * 1024 * 1024) /* Bytes of compressed data to keep queued */ @@ -150,18 +153,17 @@ struct AudioState { std::recursive_mutex mSrcMutex; ALuint mSource{0}; - std::array mBuffers; + std::vector mBuffers; ALsizei mBufferIdx{0}; AudioState(MovieState &movie) : mMovie(movie) - { - std::fill(mBuffers.begin(), mBuffers.end(), 0); - } + { } ~AudioState() { if(mSource) alDeleteSources(1, &mSource); - alDeleteBuffers(mBuffers.size(), mBuffers.data()); + if(!mBuffers.empty()) + alDeleteBuffers(mBuffers.size(), mBuffers.data()); av_frame_free(&mDecodedFrame); swr_free(&mSwresCtx); @@ -676,6 +678,7 @@ int AudioState::handler() goto finish; } + mBuffers.assign(AudioBufferTotalTime / AudioBufferTime, 0); alGenBuffers(mBuffers.size(), mBuffers.data()); alGenSources(1, &mSource); @@ -695,10 +698,12 @@ int AudioState::handler() /* First remove any processed buffers. */ ALint processed; alGetSourcei(mSource, AL_BUFFERS_PROCESSED, &processed); - if(processed > 0) + while(processed > 0) { - std::array tmp; - alSourceUnqueueBuffers(mSource, processed, tmp.data()); + std::array bids; + alSourceUnqueueBuffers(mSource, std::min(bids.size(), processed), + bids.data()); + processed -= std::min(bids.size(), processed); } /* Refill the buffer queue. */ -- cgit v1.2.3