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 #ifndef NATIVEOBOE_FIFOCONTROLLERBASE_H 18 #define NATIVEOBOE_FIFOCONTROLLERBASE_H 19 20 #include <stdint.h> 21 22 namespace oboe { 23 24 /** 25 * Manage the read/write indices of a circular buffer. 26 * 27 * The caller is responsible for reading and writing the actual data. 28 * Note that the span of available frames may not be contiguous. They 29 * may wrap around from the end to the beginning of the buffer. In that 30 * case the data must be read or written in at least two blocks of frames. 31 * 32 */ 33 34 class FifoControllerBase { 35 36 public: 37 /** 38 * @param totalFrames capacity of the circular buffer in frames. 39 */ 40 FifoControllerBase(uint32_t totalFrames); 41 42 virtual ~FifoControllerBase() = default; 43 44 /** 45 * The frames available to read will be calculated from the read and write counters. 46 * The result will be clipped to the capacity of the buffer. 47 * If the buffer has underflowed then this will return zero. 48 * @return number of valid frames available to read. 49 */ 50 uint32_t getFullFramesAvailable() const; 51 52 /** 53 * The index in a circular buffer of the next frame to read. 54 */ 55 uint32_t getReadIndex() const; 56 57 /** 58 * @param numFrames number of frames to advance the read index 59 */ 60 void advanceReadIndex(uint32_t numFrames); 61 62 /** 63 * @return maximum number of frames that can be written without exceeding the threshold. 64 */ 65 uint32_t getEmptyFramesAvailable() const; 66 67 /** 68 * The index in a circular buffer of the next frame to write. 69 */ 70 uint32_t getWriteIndex() const; 71 72 /** 73 * @param numFrames number of frames to advance the write index 74 */ 75 void advanceWriteIndex(uint32_t numFrames); 76 getFrameCapacity()77 uint32_t getFrameCapacity() const { return mTotalFrames; } 78 79 virtual uint64_t getReadCounter() const = 0; 80 virtual void setReadCounter(uint64_t n) = 0; 81 virtual void incrementReadCounter(uint64_t n) = 0; 82 virtual uint64_t getWriteCounter() const = 0; 83 virtual void setWriteCounter(uint64_t n) = 0; 84 virtual void incrementWriteCounter(uint64_t n) = 0; 85 86 private: 87 uint32_t mTotalFrames; 88 }; 89 90 } // namespace oboe 91 92 #endif //NATIVEOBOE_FIFOCONTROLLERBASE_H 93