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 | |
| 17 | #include <stdint.h> |
| 18 | #include <time.h> |
jplatipus | 6c1fc78 | 2017-11-14 11:38:28 +0000 | [diff] [blame] | 19 | #include <memory.h> |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 20 | |
| 21 | #include "common/OboeDebug.h" |
| 22 | #include "fifo/FifoControllerBase.h" |
| 23 | #include "fifo/FifoController.h" |
| 24 | #include "fifo/FifoControllerIndirect.h" |
| 25 | #include "fifo/FifoBuffer.h" |
| 26 | #include "common/AudioClock.h" |
| 27 | |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 28 | namespace oboe { |
| 29 | |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 30 | FifoBuffer::FifoBuffer(uint32_t bytesPerFrame, uint32_t capacityInFrames) |
| 31 | : mFrameCapacity(capacityInFrames) |
| 32 | , mBytesPerFrame(bytesPerFrame) |
| 33 | , mStorage(NULL) |
| 34 | , mReadAtNanoseconds(0) |
| 35 | , mFramesReadCount(0) |
| 36 | , mFramesUnderrunCount(0) |
| 37 | , mUnderrunCount(0) |
| 38 | { |
| 39 | mFifo = new FifoController(capacityInFrames, capacityInFrames); |
| 40 | // allocate buffer |
| 41 | int32_t bytesPerBuffer = bytesPerFrame * capacityInFrames; |
| 42 | mStorage = new uint8_t[bytesPerBuffer]; |
| 43 | mStorageOwned = true; |
| 44 | LOGD("FifoProcessor: numFrames = %d, bytesPerFrame = %d", capacityInFrames, bytesPerFrame); |
| 45 | } |
| 46 | |
| 47 | FifoBuffer::FifoBuffer( uint32_t bytesPerFrame, |
| 48 | uint32_t capacityInFrames, |
| 49 | int64_t * readIndexAddress, |
| 50 | int64_t * writeIndexAddress, |
| 51 | uint8_t * dataStorageAddress |
| 52 | ) |
| 53 | : mFrameCapacity(capacityInFrames) |
| 54 | , mBytesPerFrame(bytesPerFrame) |
| 55 | , mStorage(dataStorageAddress) |
| 56 | , mReadAtNanoseconds(0) |
| 57 | , mFramesReadCount(0) |
| 58 | , mFramesUnderrunCount(0) |
| 59 | , mUnderrunCount(0) |
| 60 | { |
| 61 | mFifo = new FifoControllerIndirect(capacityInFrames, |
| 62 | capacityInFrames, |
| 63 | readIndexAddress, |
| 64 | writeIndexAddress); |
| 65 | mStorage = dataStorageAddress; |
| 66 | mStorageOwned = false; |
| 67 | LOGD("FifoProcessor: capacityInFrames = %d, bytesPerFrame = %d", capacityInFrames, bytesPerFrame); |
| 68 | } |
| 69 | |
| 70 | FifoBuffer::~FifoBuffer() { |
| 71 | if (mStorageOwned) { |
| 72 | delete[] mStorage; |
| 73 | } |
| 74 | delete mFifo; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | int32_t FifoBuffer::convertFramesToBytes(int32_t frames) { |
| 79 | return frames * mBytesPerFrame; |
| 80 | } |
| 81 | |
| 82 | int32_t FifoBuffer::read(void *buffer, int32_t numFrames) { |
| 83 | size_t numBytes; |
| 84 | int32_t framesAvailable = mFifo->getFullFramesAvailable(); |
| 85 | int32_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 | uint32_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 | uint32_t frames1 = mFrameCapacity - readIndex; |
| 100 | uint32_t numBytes = convertFramesToBytes(frames1); |
| 101 | memcpy(destination, source, numBytes); |
| 102 | destination += numBytes; |
| 103 | // read second part |
| 104 | source = &mStorage[0]; |
| 105 | int 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 | |
| 118 | int32_t FifoBuffer::write(const void *buffer, int32_t framesToWrite) { |
| 119 | int32_t framesAvailable = mFifo->getEmptyFramesAvailable(); |
| 120 | // LOGD("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 | uint32_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 | int frames1 = mFrameCapacity - writeIndex; |
| 137 | numBytes = convertFramesToBytes(frames1); |
| 138 | memcpy(destination, source, numBytes); |
| 139 | // LOGD("FifoBuffer::write(%p to %p, numBytes = %d", source, destination, numBytes); |
| 140 | // read second part |
| 141 | source += convertFramesToBytes(frames1); |
| 142 | destination = &mStorage[0]; |
| 143 | int framesLeft = framesToWrite - frames1; |
| 144 | numBytes = convertFramesToBytes(framesLeft); |
| 145 | // LOGD("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 | // LOGD("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 | |
| 158 | int32_t FifoBuffer::readNow(void *buffer, int32_t numFrames) { |
| 159 | mLastReadSize = numFrames; |
| 160 | int32_t framesLeft = numFrames; |
| 161 | int32_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 | mReadAtNanoseconds = AudioClock::getNanoseconds(); |
| 172 | |
| 173 | return framesRead; |
| 174 | } |
| 175 | |
| 176 | int64_t FifoBuffer::getNextReadTime(int frameRate) { |
| 177 | if (mReadAtNanoseconds == 0) { |
| 178 | return 0; |
| 179 | } |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 180 | int64_t nanosPerBuffer = (kNanosPerSecond * mLastReadSize) / frameRate; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 181 | return mReadAtNanoseconds + nanosPerBuffer; |
| 182 | } |
| 183 | |
| 184 | uint32_t FifoBuffer::getThresholdFrames() const { |
| 185 | return mFifo->getThreshold(); |
| 186 | } |
| 187 | |
| 188 | uint32_t FifoBuffer::getBufferCapacityInFrames() const { |
| 189 | return mFifo->getFrameCapacity(); |
| 190 | } |
| 191 | |
| 192 | void FifoBuffer::setThresholdFrames(uint32_t threshold) { |
| 193 | mFifo->setThreshold(threshold); |
| 194 | } |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 195 | |
| 196 | } // namespace oboe |