aaudio: cleanup FIFO
Remove unused methods and fields.
Better code format and alignment.
Use unique_ptr for mFifo.
Remove getFifoController() method that exposed pointer.
Updates tests and calling code as needed.
Test: test_atomic_fifo.cpp
Change-Id: I1ea66beaadeb74083307835858b757e4753affda
diff --git a/media/libaaudio/src/client/AudioEndpoint.cpp b/media/libaaudio/src/client/AudioEndpoint.cpp
index f8e34d1..214f888 100644
--- a/media/libaaudio/src/client/AudioEndpoint.cpp
+++ b/media/libaaudio/src/client/AudioEndpoint.cpp
@@ -196,7 +196,7 @@
int32_t AudioEndpoint::getEmptyFramesAvailable()
{
- return mDataQueue->getFifoControllerBase()->getEmptyFramesAvailable();
+ return mDataQueue->getEmptyFramesAvailable();
}
int32_t AudioEndpoint::getFullFramesAvailable(WrappingBuffer *wrappingBuffer)
@@ -206,15 +206,15 @@
int32_t AudioEndpoint::getFullFramesAvailable()
{
- return mDataQueue->getFifoControllerBase()->getFullFramesAvailable();
+ return mDataQueue->getFullFramesAvailable();
}
void AudioEndpoint::advanceWriteIndex(int32_t deltaFrames) {
- mDataQueue->getFifoControllerBase()->advanceWriteIndex(deltaFrames);
+ mDataQueue->advanceWriteIndex(deltaFrames);
}
void AudioEndpoint::advanceReadIndex(int32_t deltaFrames) {
- mDataQueue->getFifoControllerBase()->advanceReadIndex(deltaFrames);
+ mDataQueue->advanceReadIndex(deltaFrames);
}
void AudioEndpoint::setDataReadCounter(fifo_counter_t framesRead)
diff --git a/media/libaaudio/src/fifo/FifoBuffer.cpp b/media/libaaudio/src/fifo/FifoBuffer.cpp
index b09258e..f5113f2 100644
--- a/media/libaaudio/src/fifo/FifoBuffer.cpp
+++ b/media/libaaudio/src/fifo/FifoBuffer.cpp
@@ -23,30 +23,26 @@
#include <utils/Log.h>
#include <algorithm>
+#include <memory>
#include "FifoControllerBase.h"
#include "FifoController.h"
#include "FifoControllerIndirect.h"
#include "FifoBuffer.h"
-using namespace android; // TODO just import names needed
+using android::FifoBuffer;
+using android::fifo_frames_t;
FifoBuffer::FifoBuffer(int32_t bytesPerFrame, fifo_frames_t capacityInFrames)
- : mFrameCapacity(capacityInFrames)
- , mBytesPerFrame(bytesPerFrame)
- , mStorage(nullptr)
- , mFramesReadCount(0)
- , mFramesUnderrunCount(0)
- , mUnderrunCount(0)
+ : mBytesPerFrame(bytesPerFrame)
{
- // TODO Handle possible failures to allocate. Move out of constructor?
- mFifo = new FifoController(capacityInFrames, capacityInFrames);
+ mFifo = std::make_unique<FifoController>(capacityInFrames, capacityInFrames);
// allocate buffer
int32_t bytesPerBuffer = bytesPerFrame * capacityInFrames;
mStorage = new uint8_t[bytesPerBuffer];
mStorageOwned = true;
- ALOGV("capacityInFrames = %d, bytesPerFrame = %d",
- capacityInFrames, bytesPerFrame);
+ ALOGV("%s() capacityInFrames = %d, bytesPerFrame = %d",
+ __func__, capacityInFrames, bytesPerFrame);
}
FifoBuffer::FifoBuffer( int32_t bytesPerFrame,
@@ -55,14 +51,10 @@
fifo_counter_t * writeIndexAddress,
void * dataStorageAddress
)
- : mFrameCapacity(capacityInFrames)
- , mBytesPerFrame(bytesPerFrame)
+ : mBytesPerFrame(bytesPerFrame)
, mStorage(static_cast<uint8_t *>(dataStorageAddress))
- , mFramesReadCount(0)
- , mFramesUnderrunCount(0)
- , mUnderrunCount(0)
{
- mFifo = new FifoControllerIndirect(capacityInFrames,
+ mFifo = std::make_unique<FifoControllerIndirect>(capacityInFrames,
capacityInFrames,
readIndexAddress,
writeIndexAddress);
@@ -73,10 +65,8 @@
if (mStorageOwned) {
delete[] mStorage;
}
- delete mFifo;
}
-
int32_t FifoBuffer::convertFramesToBytes(fifo_frames_t frames) {
return frames * mBytesPerFrame;
}
@@ -87,11 +77,12 @@
wrappingBuffer->data[1] = nullptr;
wrappingBuffer->numFrames[1] = 0;
if (framesAvailable > 0) {
+ fifo_frames_t capacity = mFifo->getCapacity();
uint8_t *source = &mStorage[convertFramesToBytes(startIndex)];
// Does the available data cross the end of the FIFO?
- if ((startIndex + framesAvailable) > mFrameCapacity) {
+ if ((startIndex + framesAvailable) > capacity) {
wrappingBuffer->data[0] = source;
- fifo_frames_t firstFrames = mFrameCapacity - startIndex;
+ fifo_frames_t firstFrames = capacity - startIndex;
wrappingBuffer->numFrames[0] = firstFrames;
wrappingBuffer->data[1] = &mStorage[0];
wrappingBuffer->numFrames[1] = framesAvailable - firstFrames;
@@ -107,7 +98,8 @@
fifo_frames_t FifoBuffer::getFullDataAvailable(WrappingBuffer *wrappingBuffer) {
// The FIFO might be overfull so clip to capacity.
- fifo_frames_t framesAvailable = std::min(mFifo->getFullFramesAvailable(), mFrameCapacity);
+ fifo_frames_t framesAvailable = std::min(mFifo->getFullFramesAvailable(),
+ mFifo->getCapacity());
fifo_frames_t startIndex = mFifo->getReadIndex();
fillWrappingBuffer(wrappingBuffer, framesAvailable, startIndex);
return framesAvailable;
@@ -115,7 +107,8 @@
fifo_frames_t FifoBuffer::getEmptyRoomAvailable(WrappingBuffer *wrappingBuffer) {
// The FIFO might have underrun so clip to capacity.
- fifo_frames_t framesAvailable = std::min(mFifo->getEmptyFramesAvailable(), mFrameCapacity);
+ fifo_frames_t framesAvailable = std::min(mFifo->getEmptyFramesAvailable(),
+ mFifo->getCapacity());
fifo_frames_t startIndex = mFifo->getWriteIndex();
fillWrappingBuffer(wrappingBuffer, framesAvailable, startIndex);
return framesAvailable;
@@ -183,23 +176,6 @@
return framesWritten;
}
-fifo_frames_t FifoBuffer::readNow(void *buffer, fifo_frames_t numFrames) {
- mLastReadSize = numFrames;
- fifo_frames_t framesLeft = numFrames;
- fifo_frames_t framesRead = read(buffer, numFrames);
- framesLeft -= framesRead;
- mFramesReadCount += framesRead;
- mFramesUnderrunCount += framesLeft;
- // Zero out any samples we could not set.
- if (framesLeft > 0) {
- mUnderrunCount++;
- int32_t bytesToZero = convertFramesToBytes(framesLeft);
- memset(buffer, 0, bytesToZero);
- }
-
- return framesRead;
-}
-
fifo_frames_t FifoBuffer::getThreshold() {
return mFifo->getThreshold();
}
diff --git a/media/libaaudio/src/fifo/FifoBuffer.h b/media/libaaudio/src/fifo/FifoBuffer.h
index f5a9e27..0d188c4 100644
--- a/media/libaaudio/src/fifo/FifoBuffer.h
+++ b/media/libaaudio/src/fifo/FifoBuffer.h
@@ -17,6 +17,7 @@
#ifndef FIFO_FIFO_BUFFER_H
#define FIFO_FIFO_BUFFER_H
+#include <memory>
#include <stdint.h>
#include "FifoControllerBase.h"
@@ -77,24 +78,12 @@
*/
fifo_frames_t getEmptyRoomAvailable(WrappingBuffer *wrappingBuffer);
- /**
- * Copy data from the FIFO into the buffer.
- * @param buffer
- * @param numFrames
- * @return
- */
- fifo_frames_t readNow(void *buffer, fifo_frames_t numFrames);
-
- int64_t getNextReadTime(int32_t frameRate);
-
- int32_t getUnderrunCount() const { return mUnderrunCount; }
-
- FifoControllerBase *getFifoControllerBase() { return mFifo; }
-
int32_t getBytesPerFrame() {
return mBytesPerFrame;
}
+ // Proxy methods for the internal FifoController
+
fifo_counter_t getReadCounter() {
return mFifo->getReadCounter();
}
@@ -111,6 +100,22 @@
mFifo->setWriteCounter(n);
}
+ void advanceReadIndex(fifo_frames_t numFrames) {
+ mFifo->advanceReadIndex(numFrames);
+ }
+
+ void advanceWriteIndex(fifo_frames_t numFrames) {
+ mFifo->advanceWriteIndex(numFrames);
+ }
+
+ fifo_frames_t getEmptyFramesAvailable() {
+ return mFifo->getEmptyFramesAvailable();
+ }
+
+ fifo_frames_t getFullFramesAvailable() {
+ return mFifo->getFullFramesAvailable();
+ }
+
/*
* This is generally only called before or after the buffer is used.
*/
@@ -121,15 +126,12 @@
void fillWrappingBuffer(WrappingBuffer *wrappingBuffer,
int32_t framesAvailable, int32_t startIndex);
- const fifo_frames_t mFrameCapacity;
- const int32_t mBytesPerFrame;
- uint8_t *mStorage;
- bool mStorageOwned; // did this object allocate the storage?
- FifoControllerBase *mFifo;
- fifo_counter_t mFramesReadCount;
- fifo_counter_t mFramesUnderrunCount;
- int32_t mUnderrunCount; // need? just use frames
- int32_t mLastReadSize;
+ const int32_t mBytesPerFrame;
+ // We do not use a std::unique_ptr for mStorage because it is often a pointer to
+ // memory shared between processes and cannot be deleted trivially.
+ uint8_t *mStorage = nullptr;
+ bool mStorageOwned = false; // did this object allocate the storage?
+ std::unique_ptr<FifoControllerBase> mFifo{};
};
} // android
diff --git a/media/libaaudio/src/fifo/FifoController.h b/media/libaaudio/src/fifo/FifoController.h
index 79d98a1..057a94e 100644
--- a/media/libaaudio/src/fifo/FifoController.h
+++ b/media/libaaudio/src/fifo/FifoController.h
@@ -30,7 +30,7 @@
class FifoController : public FifoControllerBase
{
public:
- FifoController(fifo_counter_t bufferSize, fifo_counter_t threshold)
+ FifoController(fifo_frames_t bufferSize, fifo_frames_t threshold)
: FifoControllerBase(bufferSize, threshold)
, mReadCounter(0)
, mWriteCounter(0)