blob: d8314bb8c73c1a042f35c6048938a3326b1b3c48 [file] [log] [blame]
Don Turnerca6f91a2017-09-05 14:52:39 +01001/*
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
Phil Burk919cd422019-08-12 17:26:50 -070017#include <algorithm>
Phil Burk9446ed62020-07-13 11:26:55 -070018#include <memory.h>
19#include <stdint.h>
Don Turnerca6f91a2017-09-05 14:52:39 +010020
Don Turnerca6f91a2017-09-05 14:52:39 +010021#include "fifo/FifoControllerBase.h"
22#include "fifo/FifoController.h"
23#include "fifo/FifoControllerIndirect.h"
24#include "fifo/FifoBuffer.h"
Don Turnerca6f91a2017-09-05 14:52:39 +010025
Don Turner3bf32ae2017-11-27 13:25:05 +000026namespace oboe {
27
Don Turnerca6f91a2017-09-05 14:52:39 +010028FifoBuffer::FifoBuffer(uint32_t bytesPerFrame, uint32_t capacityInFrames)
Phil Burk919cd422019-08-12 17:26:50 -070029 : mBytesPerFrame(bytesPerFrame)
Thomas Guilbertb7ff43f2019-07-15 16:05:38 -070030 , mStorage(nullptr)
Don Turnerca6f91a2017-09-05 14:52:39 +010031 , mFramesReadCount(0)
32 , mFramesUnderrunCount(0)
Don Turnerca6f91a2017-09-05 14:52:39 +010033{
Phil Burk08a5e0f2019-08-23 16:28:21 -070034 mFifo = std::make_unique<FifoController>(capacityInFrames);
Don Turnerca6f91a2017-09-05 14:52:39 +010035 // allocate buffer
36 int32_t bytesPerBuffer = bytesPerFrame * capacityInFrames;
37 mStorage = new uint8_t[bytesPerBuffer];
38 mStorageOwned = true;
Don Turnerca6f91a2017-09-05 14:52:39 +010039}
40
Phil Burk919cd422019-08-12 17:26:50 -070041FifoBuffer::FifoBuffer( uint32_t bytesPerFrame,
42 uint32_t capacityInFrames,
Phil Burk08a5e0f2019-08-23 16:28:21 -070043 std::atomic<uint64_t> *readCounterAddress,
44 std::atomic<uint64_t> *writeCounterAddress,
Phil Burk919cd422019-08-12 17:26:50 -070045 uint8_t *dataStorageAddress
Don Turnerca6f91a2017-09-05 14:52:39 +010046 )
Phil Burk919cd422019-08-12 17:26:50 -070047 : mBytesPerFrame(bytesPerFrame)
Don Turnerca6f91a2017-09-05 14:52:39 +010048 , mStorage(dataStorageAddress)
Don Turnerca6f91a2017-09-05 14:52:39 +010049 , mFramesReadCount(0)
50 , mFramesUnderrunCount(0)
Don Turnerca6f91a2017-09-05 14:52:39 +010051{
Phil Burk08a5e0f2019-08-23 16:28:21 -070052 mFifo = std::make_unique<FifoControllerIndirect>(capacityInFrames,
53 readCounterAddress,
54 writeCounterAddress);
Don Turnerca6f91a2017-09-05 14:52:39 +010055 mStorage = dataStorageAddress;
56 mStorageOwned = false;
Don Turnerca6f91a2017-09-05 14:52:39 +010057}
58
59FifoBuffer::~FifoBuffer() {
60 if (mStorageOwned) {
61 delete[] mStorage;
62 }
Don Turnerca6f91a2017-09-05 14:52:39 +010063}
64
Don Turnerca6f91a2017-09-05 14:52:39 +010065int32_t FifoBuffer::convertFramesToBytes(int32_t frames) {
66 return frames * mBytesPerFrame;
67}
68
69int32_t FifoBuffer::read(void *buffer, int32_t numFrames) {
Phil Burk919cd422019-08-12 17:26:50 -070070 if (numFrames <= 0) {
Don Turnerca6f91a2017-09-05 14:52:39 +010071 return 0;
72 }
Phil Burk550bf162019-08-13 15:06:41 -070073 // safe because numFrames is guaranteed positive
Phil Burk919cd422019-08-12 17:26:50 -070074 uint32_t framesToRead = static_cast<uint32_t>(numFrames);
75 uint32_t framesAvailable = mFifo->getFullFramesAvailable();
76 framesToRead = std::min(framesToRead, framesAvailable);
Don Turnerca6f91a2017-09-05 14:52:39 +010077
Phil Burk550bf162019-08-13 15:06:41 -070078 uint32_t readIndex = mFifo->getReadIndex(); // ranges 0 to capacity
gfane55f9832018-07-25 16:13:34 -070079 uint8_t *destination = reinterpret_cast<uint8_t *>(buffer);
Don Turnerca6f91a2017-09-05 14:52:39 +010080 uint8_t *source = &mStorage[convertFramesToBytes(readIndex)];
Phil Burk919cd422019-08-12 17:26:50 -070081 if ((readIndex + framesToRead) > mFifo->getFrameCapacity()) {
Phil Burkf61cea32018-12-17 17:30:21 -080082 // read in two parts, first part here is at the end of the mStorage buffer
Phil Burk550bf162019-08-13 15:06:41 -070083 int32_t frames1 = static_cast<int32_t>(mFifo->getFrameCapacity() - readIndex);
Phil Burkf61cea32018-12-17 17:30:21 -080084 int32_t numBytes = convertFramesToBytes(frames1);
85 if (numBytes < 0) {
Don Turner2417f012018-12-30 21:41:41 +000086 return static_cast<int32_t>(Result::ErrorOutOfRange);
Phil Burkf61cea32018-12-17 17:30:21 -080087 }
Don Turner2417f012018-12-30 21:41:41 +000088 memcpy(destination, source, static_cast<size_t>(numBytes));
Don Turnerca6f91a2017-09-05 14:52:39 +010089 destination += numBytes;
Phil Burkf61cea32018-12-17 17:30:21 -080090 // read second part, which is at the beginning of mStorage
Don Turnerca6f91a2017-09-05 14:52:39 +010091 source = &mStorage[0];
Phil Burk550bf162019-08-13 15:06:41 -070092 int32_t frames2 = static_cast<uint32_t>(framesToRead - frames1);
Don Turnerca6f91a2017-09-05 14:52:39 +010093 numBytes = convertFramesToBytes(frames2);
Phil Burkf61cea32018-12-17 17:30:21 -080094 if (numBytes < 0) {
Don Turner2417f012018-12-30 21:41:41 +000095 return static_cast<int32_t>(Result::ErrorOutOfRange);
Phil Burkf61cea32018-12-17 17:30:21 -080096 }
Don Turner2417f012018-12-30 21:41:41 +000097 memcpy(destination, source, static_cast<size_t>(numBytes));
Don Turnerca6f91a2017-09-05 14:52:39 +010098 } else {
99 // just read in one shot
Phil Burkf61cea32018-12-17 17:30:21 -0800100 int32_t numBytes = convertFramesToBytes(framesToRead);
101 if (numBytes < 0) {
Don Turner2417f012018-12-30 21:41:41 +0000102 return static_cast<int32_t>(Result::ErrorOutOfRange);
Phil Burkf61cea32018-12-17 17:30:21 -0800103 }
Don Turner2417f012018-12-30 21:41:41 +0000104 memcpy(destination, source, static_cast<size_t>(numBytes));
Don Turnerca6f91a2017-09-05 14:52:39 +0100105 }
106 mFifo->advanceReadIndex(framesToRead);
107
108 return framesToRead;
109}
110
Phil Burk919cd422019-08-12 17:26:50 -0700111int32_t FifoBuffer::write(const void *buffer, int32_t numFrames) {
112 if (numFrames <= 0) {
Don Turnerca6f91a2017-09-05 14:52:39 +0100113 return 0;
114 }
Phil Burk550bf162019-08-13 15:06:41 -0700115 // Guaranteed positive.
Phil Burk919cd422019-08-12 17:26:50 -0700116 uint32_t framesToWrite = static_cast<uint32_t>(numFrames);
117 uint32_t framesAvailable = mFifo->getEmptyFramesAvailable();
118 framesToWrite = std::min(framesToWrite, framesAvailable);
Don Turnerca6f91a2017-09-05 14:52:39 +0100119
Don Turnerca6f91a2017-09-05 14:52:39 +0100120 uint32_t writeIndex = mFifo->getWriteIndex();
121 int byteIndex = convertFramesToBytes(writeIndex);
gfane55f9832018-07-25 16:13:34 -0700122 const uint8_t *source = reinterpret_cast<const uint8_t *>(buffer);
Don Turnerca6f91a2017-09-05 14:52:39 +0100123 uint8_t *destination = &mStorage[byteIndex];
Phil Burk919cd422019-08-12 17:26:50 -0700124 if ((writeIndex + framesToWrite) > mFifo->getFrameCapacity()) {
Don Turnerca6f91a2017-09-05 14:52:39 +0100125 // write in two parts, first part here
Phil Burk550bf162019-08-13 15:06:41 -0700126 int32_t frames1 = static_cast<uint32_t>(mFifo->getFrameCapacity() - writeIndex);
Phil Burkf61cea32018-12-17 17:30:21 -0800127 int32_t numBytes = convertFramesToBytes(frames1);
128 if (numBytes < 0) {
Don Turner2417f012018-12-30 21:41:41 +0000129 return static_cast<int32_t>(Result::ErrorOutOfRange);
Phil Burkf61cea32018-12-17 17:30:21 -0800130 }
Don Turner2417f012018-12-30 21:41:41 +0000131 memcpy(destination, source, static_cast<size_t>(numBytes));
Don Turnerca6f91a2017-09-05 14:52:39 +0100132 // read second part
133 source += convertFramesToBytes(frames1);
134 destination = &mStorage[0];
Phil Burk550bf162019-08-13 15:06:41 -0700135 int frames2 = static_cast<uint32_t>(framesToWrite - frames1);
136 numBytes = convertFramesToBytes(frames2);
Phil Burkf61cea32018-12-17 17:30:21 -0800137 if (numBytes < 0) {
Don Turner2417f012018-12-30 21:41:41 +0000138 return static_cast<int32_t>(Result::ErrorOutOfRange);
Phil Burkf61cea32018-12-17 17:30:21 -0800139 }
Don Turner2417f012018-12-30 21:41:41 +0000140 memcpy(destination, source, static_cast<size_t>(numBytes));
Don Turnerca6f91a2017-09-05 14:52:39 +0100141 } else {
142 // just write in one shot
Phil Burkf61cea32018-12-17 17:30:21 -0800143 int32_t numBytes = convertFramesToBytes(framesToWrite);
144 if (numBytes < 0) {
Don Turner2417f012018-12-30 21:41:41 +0000145 return static_cast<int32_t>(Result::ErrorOutOfRange);
Phil Burkf61cea32018-12-17 17:30:21 -0800146 }
Don Turner2417f012018-12-30 21:41:41 +0000147 memcpy(destination, source, static_cast<size_t>(numBytes));
Don Turnerca6f91a2017-09-05 14:52:39 +0100148 }
149 mFifo->advanceWriteIndex(framesToWrite);
150
151 return framesToWrite;
152}
153
154int32_t FifoBuffer::readNow(void *buffer, int32_t numFrames) {
Don Turnerca6f91a2017-09-05 14:52:39 +0100155 int32_t framesRead = read(buffer, numFrames);
Phil Burk550bf162019-08-13 15:06:41 -0700156 if (framesRead < 0) {
157 return framesRead;
158 }
Phil Burk919cd422019-08-12 17:26:50 -0700159 int32_t framesLeft = numFrames - framesRead;
Don Turnerca6f91a2017-09-05 14:52:39 +0100160 mFramesReadCount += framesRead;
161 mFramesUnderrunCount += framesLeft;
162 // Zero out any samples we could not set.
163 if (framesLeft > 0) {
Phil Burkb3e90b52018-10-03 08:29:24 -0700164 uint8_t *destination = reinterpret_cast<uint8_t *>(buffer);
Phil Burk2b9390d2018-10-03 09:06:58 -0700165 destination += convertFramesToBytes(framesRead); // point to first byte not set
Don Turnerca6f91a2017-09-05 14:52:39 +0100166 int32_t bytesToZero = convertFramesToBytes(framesLeft);
Don Turner2417f012018-12-30 21:41:41 +0000167 memset(destination, 0, static_cast<size_t>(bytesToZero));
Don Turnerca6f91a2017-09-05 14:52:39 +0100168 }
Don Turnerca6f91a2017-09-05 14:52:39 +0100169
170 return framesRead;
171}
172
Don Turnerca6f91a2017-09-05 14:52:39 +0100173
174uint32_t FifoBuffer::getBufferCapacityInFrames() const {
175 return mFifo->getFrameCapacity();
176}
177
Phil Burk08a5e0f2019-08-23 16:28:21 -0700178} // namespace oboe