blob: c5489f141e162e459ac290e0f84aedad7cb118e6 [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
20#define LOG_TAG "FifoBuffer"
21//#define LOG_NDEBUG 0
22#include <utils/Log.h>
23
24#include "FifoControllerBase.h"
25#include "FifoController.h"
26#include "FifoControllerIndirect.h"
27#include "FifoBuffer.h"
28
29FifoBuffer::FifoBuffer(int32_t bytesPerFrame, fifo_frames_t capacityInFrames)
30 : mFrameCapacity(capacityInFrames)
31 , mBytesPerFrame(bytesPerFrame)
32 , mStorage(nullptr)
33 , mFramesReadCount(0)
34 , mFramesUnderrunCount(0)
35 , mUnderrunCount(0)
36{
37 // TODO Handle possible failures to allocate. Move out of constructor?
38 mFifo = new FifoController(capacityInFrames, capacityInFrames);
39 // allocate buffer
40 int32_t bytesPerBuffer = bytesPerFrame * capacityInFrames;
41 mStorage = new uint8_t[bytesPerBuffer];
42 mStorageOwned = true;
43 ALOGD("FifoBuffer: capacityInFrames = %d, bytesPerFrame = %d",
44 capacityInFrames, bytesPerFrame);
45}
46
47FifoBuffer::FifoBuffer( int32_t bytesPerFrame,
48 fifo_frames_t capacityInFrames,
49 fifo_counter_t * readIndexAddress,
50 fifo_counter_t * writeIndexAddress,
51 void * dataStorageAddress
52 )
53 : mFrameCapacity(capacityInFrames)
54 , mBytesPerFrame(bytesPerFrame)
55 , mStorage(static_cast<uint8_t *>(dataStorageAddress))
56 , mFramesReadCount(0)
57 , mFramesUnderrunCount(0)
58 , mUnderrunCount(0)
59{
60 // TODO Handle possible failures to allocate. Move out of constructor?
61 mFifo = new FifoControllerIndirect(capacityInFrames,
62 capacityInFrames,
63 readIndexAddress,
64 writeIndexAddress);
65 mStorageOwned = false;
66 ALOGD("FifoProcessor: capacityInFrames = %d, bytesPerFrame = %d",
67 capacityInFrames, bytesPerFrame);
68}
69
70FifoBuffer::~FifoBuffer() {
71 if (mStorageOwned) {
72 delete[] mStorage;
73 }
74 delete mFifo;
75}
76
77
78int32_t FifoBuffer::convertFramesToBytes(fifo_frames_t frames) {
79 return frames * mBytesPerFrame;
80}
81
82fifo_frames_t FifoBuffer::read(void *buffer, fifo_frames_t numFrames) {
83 size_t numBytes;
84 fifo_frames_t framesAvailable = mFifo->getFullFramesAvailable();
85 fifo_frames_t framesToRead = numFrames;
86 // Is there enough data in the FIFO
87 if (framesToRead > framesAvailable) {
88 framesToRead = framesAvailable;
89 }
90 if (framesToRead == 0) {
91 return 0;
92 }
93
94 fifo_frames_t readIndex = mFifo->getReadIndex();
95 uint8_t *destination = (uint8_t *) buffer;
96 uint8_t *source = &mStorage[convertFramesToBytes(readIndex)];
97 if ((readIndex + framesToRead) > mFrameCapacity) {
98 // read in two parts, first part here
99 fifo_frames_t frames1 = mFrameCapacity - readIndex;
100 int32_t numBytes = convertFramesToBytes(frames1);
101 memcpy(destination, source, numBytes);
102 destination += numBytes;
103 // read second part
104 source = &mStorage[0];
105 fifo_frames_t frames2 = framesToRead - frames1;
106 numBytes = convertFramesToBytes(frames2);
107 memcpy(destination, source, numBytes);
108 } else {
109 // just read in one shot
110 numBytes = convertFramesToBytes(framesToRead);
111 memcpy(destination, source, numBytes);
112 }
113 mFifo->advanceReadIndex(framesToRead);
114
115 return framesToRead;
116}
117
118fifo_frames_t FifoBuffer::write(const void *buffer, fifo_frames_t framesToWrite) {
119 fifo_frames_t framesAvailable = mFifo->getEmptyFramesAvailable();
120// ALOGD("FifoBuffer::write() framesToWrite = %d, framesAvailable = %d",
121// framesToWrite, framesAvailable);
122 if (framesToWrite > framesAvailable) {
123 framesToWrite = framesAvailable;
124 }
125 if (framesToWrite <= 0) {
126 return 0;
127 }
128
129 size_t numBytes;
130 fifo_frames_t writeIndex = mFifo->getWriteIndex();
131 int byteIndex = convertFramesToBytes(writeIndex);
132 const uint8_t *source = (const uint8_t *) buffer;
133 uint8_t *destination = &mStorage[byteIndex];
134 if ((writeIndex + framesToWrite) > mFrameCapacity) {
135 // write in two parts, first part here
136 fifo_frames_t frames1 = mFrameCapacity - writeIndex;
137 numBytes = convertFramesToBytes(frames1);
138 memcpy(destination, source, numBytes);
139// ALOGD("FifoBuffer::write(%p to %p, numBytes = %d", source, destination, numBytes);
140 // read second part
141 source += convertFramesToBytes(frames1);
142 destination = &mStorage[0];
143 fifo_frames_t framesLeft = framesToWrite - frames1;
144 numBytes = convertFramesToBytes(framesLeft);
145// ALOGD("FifoBuffer::write(%p to %p, numBytes = %d", source, destination, numBytes);
146 memcpy(destination, source, numBytes);
147 } else {
148 // just write in one shot
149 numBytes = convertFramesToBytes(framesToWrite);
150// ALOGD("FifoBuffer::write(%p to %p, numBytes = %d", source, destination, numBytes);
151 memcpy(destination, source, numBytes);
152 }
153 mFifo->advanceWriteIndex(framesToWrite);
154
155 return framesToWrite;
156}
157
158fifo_frames_t FifoBuffer::readNow(void *buffer, fifo_frames_t numFrames) {
159 mLastReadSize = numFrames;
160 fifo_frames_t framesLeft = numFrames;
161 fifo_frames_t framesRead = read(buffer, numFrames);
162 framesLeft -= framesRead;
163 mFramesReadCount += framesRead;
164 mFramesUnderrunCount += framesLeft;
165 // Zero out any samples we could not set.
166 if (framesLeft > 0) {
167 mUnderrunCount++;
168 int32_t bytesToZero = convertFramesToBytes(framesLeft);
169 memset(buffer, 0, bytesToZero);
170 }
171
172 return framesRead;
173}
174
175fifo_frames_t FifoBuffer::getThreshold() {
176 return mFifo->getThreshold();
177}
178
179void FifoBuffer::setThreshold(fifo_frames_t threshold) {
180 mFifo->setThreshold(threshold);
181}
182
183fifo_frames_t FifoBuffer::getBufferCapacityInFrames() {
184 return mFifo->getCapacity();
185}
186