Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 1 | /* |
| 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 Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 17 | #include <algorithm> |
Phil Burk | 9446ed6 | 2020-07-13 11:26:55 -0700 | [diff] [blame] | 18 | #include <memory.h> |
| 19 | #include <stdint.h> |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 20 | |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 21 | #include "fifo/FifoControllerBase.h" |
| 22 | #include "fifo/FifoController.h" |
| 23 | #include "fifo/FifoControllerIndirect.h" |
| 24 | #include "fifo/FifoBuffer.h" |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 25 | |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 26 | namespace oboe { |
| 27 | |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 28 | FifoBuffer::FifoBuffer(uint32_t bytesPerFrame, uint32_t capacityInFrames) |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 29 | : mBytesPerFrame(bytesPerFrame) |
Thomas Guilbert | b7ff43f | 2019-07-15 16:05:38 -0700 | [diff] [blame] | 30 | , mStorage(nullptr) |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 31 | , mFramesReadCount(0) |
| 32 | , mFramesUnderrunCount(0) |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 33 | { |
Phil Burk | 08a5e0f | 2019-08-23 16:28:21 -0700 | [diff] [blame] | 34 | mFifo = std::make_unique<FifoController>(capacityInFrames); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 35 | // allocate buffer |
| 36 | int32_t bytesPerBuffer = bytesPerFrame * capacityInFrames; |
| 37 | mStorage = new uint8_t[bytesPerBuffer]; |
| 38 | mStorageOwned = true; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 41 | FifoBuffer::FifoBuffer( uint32_t bytesPerFrame, |
| 42 | uint32_t capacityInFrames, |
Phil Burk | 08a5e0f | 2019-08-23 16:28:21 -0700 | [diff] [blame] | 43 | std::atomic<uint64_t> *readCounterAddress, |
| 44 | std::atomic<uint64_t> *writeCounterAddress, |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 45 | uint8_t *dataStorageAddress |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 46 | ) |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 47 | : mBytesPerFrame(bytesPerFrame) |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 48 | , mStorage(dataStorageAddress) |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 49 | , mFramesReadCount(0) |
| 50 | , mFramesUnderrunCount(0) |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 51 | { |
Phil Burk | 08a5e0f | 2019-08-23 16:28:21 -0700 | [diff] [blame] | 52 | mFifo = std::make_unique<FifoControllerIndirect>(capacityInFrames, |
| 53 | readCounterAddress, |
| 54 | writeCounterAddress); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 55 | mStorage = dataStorageAddress; |
| 56 | mStorageOwned = false; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | FifoBuffer::~FifoBuffer() { |
| 60 | if (mStorageOwned) { |
| 61 | delete[] mStorage; |
| 62 | } |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 65 | int32_t FifoBuffer::convertFramesToBytes(int32_t frames) { |
| 66 | return frames * mBytesPerFrame; |
| 67 | } |
| 68 | |
| 69 | int32_t FifoBuffer::read(void *buffer, int32_t numFrames) { |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 70 | if (numFrames <= 0) { |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 71 | return 0; |
| 72 | } |
Phil Burk | 550bf16 | 2019-08-13 15:06:41 -0700 | [diff] [blame] | 73 | // safe because numFrames is guaranteed positive |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 74 | uint32_t framesToRead = static_cast<uint32_t>(numFrames); |
| 75 | uint32_t framesAvailable = mFifo->getFullFramesAvailable(); |
| 76 | framesToRead = std::min(framesToRead, framesAvailable); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 77 | |
Phil Burk | 550bf16 | 2019-08-13 15:06:41 -0700 | [diff] [blame] | 78 | uint32_t readIndex = mFifo->getReadIndex(); // ranges 0 to capacity |
gfan | e55f983 | 2018-07-25 16:13:34 -0700 | [diff] [blame] | 79 | uint8_t *destination = reinterpret_cast<uint8_t *>(buffer); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 80 | uint8_t *source = &mStorage[convertFramesToBytes(readIndex)]; |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 81 | if ((readIndex + framesToRead) > mFifo->getFrameCapacity()) { |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 82 | // read in two parts, first part here is at the end of the mStorage buffer |
Phil Burk | 550bf16 | 2019-08-13 15:06:41 -0700 | [diff] [blame] | 83 | int32_t frames1 = static_cast<int32_t>(mFifo->getFrameCapacity() - readIndex); |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 84 | int32_t numBytes = convertFramesToBytes(frames1); |
| 85 | if (numBytes < 0) { |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 86 | return static_cast<int32_t>(Result::ErrorOutOfRange); |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 87 | } |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 88 | memcpy(destination, source, static_cast<size_t>(numBytes)); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 89 | destination += numBytes; |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 90 | // read second part, which is at the beginning of mStorage |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 91 | source = &mStorage[0]; |
Phil Burk | 550bf16 | 2019-08-13 15:06:41 -0700 | [diff] [blame] | 92 | int32_t frames2 = static_cast<uint32_t>(framesToRead - frames1); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 93 | numBytes = convertFramesToBytes(frames2); |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 94 | if (numBytes < 0) { |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 95 | return static_cast<int32_t>(Result::ErrorOutOfRange); |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 96 | } |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 97 | memcpy(destination, source, static_cast<size_t>(numBytes)); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 98 | } else { |
| 99 | // just read in one shot |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 100 | int32_t numBytes = convertFramesToBytes(framesToRead); |
| 101 | if (numBytes < 0) { |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 102 | return static_cast<int32_t>(Result::ErrorOutOfRange); |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 103 | } |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 104 | memcpy(destination, source, static_cast<size_t>(numBytes)); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 105 | } |
| 106 | mFifo->advanceReadIndex(framesToRead); |
| 107 | |
| 108 | return framesToRead; |
| 109 | } |
| 110 | |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 111 | int32_t FifoBuffer::write(const void *buffer, int32_t numFrames) { |
| 112 | if (numFrames <= 0) { |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 113 | return 0; |
| 114 | } |
Phil Burk | 550bf16 | 2019-08-13 15:06:41 -0700 | [diff] [blame] | 115 | // Guaranteed positive. |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 116 | uint32_t framesToWrite = static_cast<uint32_t>(numFrames); |
| 117 | uint32_t framesAvailable = mFifo->getEmptyFramesAvailable(); |
| 118 | framesToWrite = std::min(framesToWrite, framesAvailable); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 119 | |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 120 | uint32_t writeIndex = mFifo->getWriteIndex(); |
| 121 | int byteIndex = convertFramesToBytes(writeIndex); |
gfan | e55f983 | 2018-07-25 16:13:34 -0700 | [diff] [blame] | 122 | const uint8_t *source = reinterpret_cast<const uint8_t *>(buffer); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 123 | uint8_t *destination = &mStorage[byteIndex]; |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 124 | if ((writeIndex + framesToWrite) > mFifo->getFrameCapacity()) { |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 125 | // write in two parts, first part here |
Phil Burk | 550bf16 | 2019-08-13 15:06:41 -0700 | [diff] [blame] | 126 | int32_t frames1 = static_cast<uint32_t>(mFifo->getFrameCapacity() - writeIndex); |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 127 | int32_t numBytes = convertFramesToBytes(frames1); |
| 128 | if (numBytes < 0) { |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 129 | return static_cast<int32_t>(Result::ErrorOutOfRange); |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 130 | } |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 131 | memcpy(destination, source, static_cast<size_t>(numBytes)); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 132 | // read second part |
| 133 | source += convertFramesToBytes(frames1); |
| 134 | destination = &mStorage[0]; |
Phil Burk | 550bf16 | 2019-08-13 15:06:41 -0700 | [diff] [blame] | 135 | int frames2 = static_cast<uint32_t>(framesToWrite - frames1); |
| 136 | numBytes = convertFramesToBytes(frames2); |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 137 | if (numBytes < 0) { |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 138 | return static_cast<int32_t>(Result::ErrorOutOfRange); |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 139 | } |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 140 | memcpy(destination, source, static_cast<size_t>(numBytes)); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 141 | } else { |
| 142 | // just write in one shot |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 143 | int32_t numBytes = convertFramesToBytes(framesToWrite); |
| 144 | if (numBytes < 0) { |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 145 | return static_cast<int32_t>(Result::ErrorOutOfRange); |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 146 | } |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 147 | memcpy(destination, source, static_cast<size_t>(numBytes)); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 148 | } |
| 149 | mFifo->advanceWriteIndex(framesToWrite); |
| 150 | |
| 151 | return framesToWrite; |
| 152 | } |
| 153 | |
| 154 | int32_t FifoBuffer::readNow(void *buffer, int32_t numFrames) { |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 155 | int32_t framesRead = read(buffer, numFrames); |
Phil Burk | 550bf16 | 2019-08-13 15:06:41 -0700 | [diff] [blame] | 156 | if (framesRead < 0) { |
| 157 | return framesRead; |
| 158 | } |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 159 | int32_t framesLeft = numFrames - framesRead; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 160 | mFramesReadCount += framesRead; |
| 161 | mFramesUnderrunCount += framesLeft; |
| 162 | // Zero out any samples we could not set. |
| 163 | if (framesLeft > 0) { |
Phil Burk | b3e90b5 | 2018-10-03 08:29:24 -0700 | [diff] [blame] | 164 | uint8_t *destination = reinterpret_cast<uint8_t *>(buffer); |
Phil Burk | 2b9390d | 2018-10-03 09:06:58 -0700 | [diff] [blame] | 165 | destination += convertFramesToBytes(framesRead); // point to first byte not set |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 166 | int32_t bytesToZero = convertFramesToBytes(framesLeft); |
Don Turner | 2417f01 | 2018-12-30 21:41:41 +0000 | [diff] [blame] | 167 | memset(destination, 0, static_cast<size_t>(bytesToZero)); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 168 | } |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 169 | |
| 170 | return framesRead; |
| 171 | } |
| 172 | |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 173 | |
| 174 | uint32_t FifoBuffer::getBufferCapacityInFrames() const { |
| 175 | return mFifo->getFrameCapacity(); |
| 176 | } |
| 177 | |
Phil Burk | 08a5e0f | 2019-08-23 16:28:21 -0700 | [diff] [blame] | 178 | } // namespace oboe |