Skip to content

Commit

Permalink
fix readNow buffer pointer
Browse files Browse the repository at this point in the history
Was clearing beginning of buffer instead of end of buffer.
Also document read() and readNow().
This affects OpenSL ES blocking writes when un underflow occured.

Fixes: 233
Fixes: 154
  • Loading branch information
Phil Burk committed Oct 3, 2018
1 parent f31f852 commit b3e90b5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/fifo/FifoBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ int32_t FifoBuffer::readNow(void *buffer, int32_t numFrames) {
// Zero out any samples we could not set.
if (framesLeft > 0) {
mUnderrunCount++;
uint8_t *destination = reinterpret_cast<uint8_t *>(buffer);
destination += convertFramesToBytes(framesRead); // point to first byte not set in buffer
int32_t bytesToZero = convertFramesToBytes(framesLeft);
memset(buffer, 0, bytesToZero);
memset(destination, 0, bytesToZero);
}

return framesRead;
Expand Down
14 changes: 14 additions & 0 deletions src/fifo/FifoBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class FifoBuffer {

int32_t convertFramesToBytes(int32_t frames);

/**
* Read framesToRead or, if not enough, then read as many as are available.
* @param destination
* @param framesToRead number of frames requested
* @return number of frames actually read
*/
int32_t read(void *destination, int32_t framesToRead);

int32_t write(const void *source, int32_t framesToWrite);
Expand All @@ -50,6 +56,14 @@ class FifoBuffer {

uint32_t getBufferCapacityInFrames() const;

/**
* Calls read(). If all of the frames cannot be read then the remainder of the buffer
* is set to zero and the underruncount is incremented.
*
* @param destination
* @param framesToRead number of frames requested
* @return number of frames actually read
*/
int32_t readNow(void *buffer, int32_t numFrames);

uint32_t getUnderrunCount() const { return mUnderrunCount; }
Expand Down
4 changes: 2 additions & 2 deletions src/opensles/AudioStreamBuffered.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ DataCallbackResult AudioStreamBuffered::onDefaultCallback(void *audioData, int n
int32_t framesTransferred = 0;

if (getDirection() == oboe::Direction::Output) {
// Read from the FIFO and write to audioData
// Read from the FIFO and write to audioData, clear part of buffer if not enough data.
framesTransferred = mFifoBuffer->readNow(audioData, numFrames);
} else {
// Read from audioData and write to the FIFO
framesTransferred = mFifoBuffer->write(audioData, numFrames); // FIXME writeNow????
framesTransferred = mFifoBuffer->write(audioData, numFrames); // There is no writeNow()
}

if (framesTransferred < numFrames) {
Expand Down

0 comments on commit b3e90b5

Please sign in to comment.