blob: b09258ea1bb511f47236bad80989e7fe278457b2 [file] [log] [blame]
Phil Burkfd911c12017-01-03 17:15:39 -08001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <cstring>
18#include <unistd.h>
19
Phil Burkc0c70e32017-02-09 13:18:38 -080020
Phil Burkfd911c12017-01-03 17:15:39 -080021#define LOG_TAG "FifoBuffer"
22//#define LOG_NDEBUG 0
23#include <utils/Log.h>
24
Phil Burkf7da0b92018-04-20 17:24:38 -070025#include <algorithm>
26
Phil Burkfd911c12017-01-03 17:15:39 -080027#include "FifoControllerBase.h"
28#include "FifoController.h"
29#include "FifoControllerIndirect.h"
30#include "FifoBuffer.h"
31
Phil Burkc0c70e32017-02-09 13:18:38 -080032using namespace android; // TODO just import names needed
33
Phil Burkfd911c12017-01-03 17:15:39 -080034FifoBuffer::FifoBuffer(int32_t bytesPerFrame, fifo_frames_t capacityInFrames)
35 : mFrameCapacity(capacityInFrames)
36 , mBytesPerFrame(bytesPerFrame)
37 , mStorage(nullptr)
38 , mFramesReadCount(0)
39 , mFramesUnderrunCount(0)
40 , mUnderrunCount(0)
41{
42 // TODO Handle possible failures to allocate. Move out of constructor?
43 mFifo = new FifoController(capacityInFrames, capacityInFrames);
44 // allocate buffer
45 int32_t bytesPerBuffer = bytesPerFrame * capacityInFrames;
46 mStorage = new uint8_t[bytesPerBuffer];
47 mStorageOwned = true;
Phil Burk55e5eab2018-04-10 15:16:38 -070048 ALOGV("capacityInFrames = %d, bytesPerFrame = %d",
Phil Burkfd911c12017-01-03 17:15:39 -080049 capacityInFrames, bytesPerFrame);
50}
51
52FifoBuffer::FifoBuffer( int32_t bytesPerFrame,
53 fifo_frames_t capacityInFrames,
54 fifo_counter_t * readIndexAddress,
55 fifo_counter_t * writeIndexAddress,
56 void * dataStorageAddress
57 )
58 : mFrameCapacity(capacityInFrames)
59 , mBytesPerFrame(bytesPerFrame)
60 , mStorage(static_cast<uint8_t *>(dataStorageAddress))
61 , mFramesReadCount(0)
62 , mFramesUnderrunCount(0)
63 , mUnderrunCount(0)
64{
Phil Burkfd911c12017-01-03 17:15:39 -080065 mFifo = new FifoControllerIndirect(capacityInFrames,
66 capacityInFrames,
67 readIndexAddress,
68 writeIndexAddress);
69 mStorageOwned = false;
Phil Burkfd911c12017-01-03 17:15:39 -080070}
71
72FifoBuffer::~FifoBuffer() {
73 if (mStorageOwned) {
74 delete[] mStorage;
75 }
76 delete mFifo;
77}
78
79
80int32_t FifoBuffer::convertFramesToBytes(fifo_frames_t frames) {
81 return frames * mBytesPerFrame;
82}
83
Phil Burkc0c70e32017-02-09 13:18:38 -080084void FifoBuffer::fillWrappingBuffer(WrappingBuffer *wrappingBuffer,
85 int32_t framesAvailable,
86 int32_t startIndex) {
87 wrappingBuffer->data[1] = nullptr;
88 wrappingBuffer->numFrames[1] = 0;
89 if (framesAvailable > 0) {
Phil Burkc0c70e32017-02-09 13:18:38 -080090 uint8_t *source = &mStorage[convertFramesToBytes(startIndex)];
91 // Does the available data cross the end of the FIFO?
92 if ((startIndex + framesAvailable) > mFrameCapacity) {
93 wrappingBuffer->data[0] = source;
Phil Burkf7da0b92018-04-20 17:24:38 -070094 fifo_frames_t firstFrames = mFrameCapacity - startIndex;
95 wrappingBuffer->numFrames[0] = firstFrames;
Phil Burkc0c70e32017-02-09 13:18:38 -080096 wrappingBuffer->data[1] = &mStorage[0];
Phil Burkf7da0b92018-04-20 17:24:38 -070097 wrappingBuffer->numFrames[1] = framesAvailable - firstFrames;
Phil Burkc0c70e32017-02-09 13:18:38 -080098 } else {
99 wrappingBuffer->data[0] = source;
100 wrappingBuffer->numFrames[0] = framesAvailable;
101 }
Phil Burkfd911c12017-01-03 17:15:39 -0800102 } else {
Phil Burkc0c70e32017-02-09 13:18:38 -0800103 wrappingBuffer->data[0] = nullptr;
104 wrappingBuffer->numFrames[0] = 0;
Phil Burkfd911c12017-01-03 17:15:39 -0800105 }
Phil Burkfd911c12017-01-03 17:15:39 -0800106}
107
Phil Burkfd34a932017-07-19 07:03:52 -0700108fifo_frames_t FifoBuffer::getFullDataAvailable(WrappingBuffer *wrappingBuffer) {
Phil Burkf7da0b92018-04-20 17:24:38 -0700109 // The FIFO might be overfull so clip to capacity.
110 fifo_frames_t framesAvailable = std::min(mFifo->getFullFramesAvailable(), mFrameCapacity);
Phil Burkc0c70e32017-02-09 13:18:38 -0800111 fifo_frames_t startIndex = mFifo->getReadIndex();
112 fillWrappingBuffer(wrappingBuffer, framesAvailable, startIndex);
Phil Burkfd34a932017-07-19 07:03:52 -0700113 return framesAvailable;
Phil Burkc0c70e32017-02-09 13:18:38 -0800114}
115
Phil Burkfd34a932017-07-19 07:03:52 -0700116fifo_frames_t FifoBuffer::getEmptyRoomAvailable(WrappingBuffer *wrappingBuffer) {
Phil Burkf7da0b92018-04-20 17:24:38 -0700117 // The FIFO might have underrun so clip to capacity.
118 fifo_frames_t framesAvailable = std::min(mFifo->getEmptyFramesAvailable(), mFrameCapacity);
Phil Burkc0c70e32017-02-09 13:18:38 -0800119 fifo_frames_t startIndex = mFifo->getWriteIndex();
120 fillWrappingBuffer(wrappingBuffer, framesAvailable, startIndex);
Phil Burkfd34a932017-07-19 07:03:52 -0700121 return framesAvailable;
Phil Burkc0c70e32017-02-09 13:18:38 -0800122}
Phil Burkfd911c12017-01-03 17:15:39 -0800123
Phil Burkc0c70e32017-02-09 13:18:38 -0800124fifo_frames_t FifoBuffer::read(void *buffer, fifo_frames_t numFrames) {
125 WrappingBuffer wrappingBuffer;
126 uint8_t *destination = (uint8_t *) buffer;
127 fifo_frames_t framesLeft = numFrames;
Phil Burkfd911c12017-01-03 17:15:39 -0800128
Phil Burkc0c70e32017-02-09 13:18:38 -0800129 getFullDataAvailable(&wrappingBuffer);
130
131 // Read data in one or two parts.
132 int partIndex = 0;
133 while (framesLeft > 0 && partIndex < WrappingBuffer::SIZE) {
134 fifo_frames_t framesToRead = framesLeft;
135 fifo_frames_t framesAvailable = wrappingBuffer.numFrames[partIndex];
Phil Burkc0c70e32017-02-09 13:18:38 -0800136 if (framesAvailable > 0) {
137 if (framesToRead > framesAvailable) {
138 framesToRead = framesAvailable;
139 }
140 int32_t numBytes = convertFramesToBytes(framesToRead);
141 memcpy(destination, wrappingBuffer.data[partIndex], numBytes);
142
143 destination += numBytes;
144 framesLeft -= framesToRead;
Phil Burk71f35bb2017-04-13 16:05:07 -0700145 } else {
146 break;
Phil Burkc0c70e32017-02-09 13:18:38 -0800147 }
148 partIndex++;
149 }
150 fifo_frames_t framesRead = numFrames - framesLeft;
151 mFifo->advanceReadIndex(framesRead);
152 return framesRead;
153}
154
155fifo_frames_t FifoBuffer::write(const void *buffer, fifo_frames_t numFrames) {
156 WrappingBuffer wrappingBuffer;
157 uint8_t *source = (uint8_t *) buffer;
158 fifo_frames_t framesLeft = numFrames;
159
160 getEmptyRoomAvailable(&wrappingBuffer);
161
162 // Read data in one or two parts.
163 int partIndex = 0;
164 while (framesLeft > 0 && partIndex < WrappingBuffer::SIZE) {
165 fifo_frames_t framesToWrite = framesLeft;
166 fifo_frames_t framesAvailable = wrappingBuffer.numFrames[partIndex];
167 if (framesAvailable > 0) {
168 if (framesToWrite > framesAvailable) {
169 framesToWrite = framesAvailable;
170 }
171 int32_t numBytes = convertFramesToBytes(framesToWrite);
172 memcpy(wrappingBuffer.data[partIndex], source, numBytes);
173
174 source += numBytes;
175 framesLeft -= framesToWrite;
Phil Burk71f35bb2017-04-13 16:05:07 -0700176 } else {
177 break;
Phil Burkc0c70e32017-02-09 13:18:38 -0800178 }
179 partIndex++;
180 }
181 fifo_frames_t framesWritten = numFrames - framesLeft;
182 mFifo->advanceWriteIndex(framesWritten);
183 return framesWritten;
Phil Burkfd911c12017-01-03 17:15:39 -0800184}
185
186fifo_frames_t FifoBuffer::readNow(void *buffer, fifo_frames_t numFrames) {
187 mLastReadSize = numFrames;
188 fifo_frames_t framesLeft = numFrames;
189 fifo_frames_t framesRead = read(buffer, numFrames);
190 framesLeft -= framesRead;
191 mFramesReadCount += framesRead;
192 mFramesUnderrunCount += framesLeft;
193 // Zero out any samples we could not set.
194 if (framesLeft > 0) {
195 mUnderrunCount++;
196 int32_t bytesToZero = convertFramesToBytes(framesLeft);
197 memset(buffer, 0, bytesToZero);
198 }
199
200 return framesRead;
201}
202
203fifo_frames_t FifoBuffer::getThreshold() {
204 return mFifo->getThreshold();
205}
206
207void FifoBuffer::setThreshold(fifo_frames_t threshold) {
208 mFifo->setThreshold(threshold);
209}
210
211fifo_frames_t FifoBuffer::getBufferCapacityInFrames() {
212 return mFifo->getCapacity();
213}
214
Phil Burkea04d972017-08-07 12:30:44 -0700215void FifoBuffer::eraseMemory() {
216 int32_t numBytes = convertFramesToBytes(getBufferCapacityInFrames());
217 if (numBytes > 0) {
218 memset(mStorage, 0, (size_t) numBytes);
219 }
220}