Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [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 | #ifndef FIFO_FIFO_BUFFER_H |
| 18 | #define FIFO_FIFO_BUFFER_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include "FifoControllerBase.h" |
| 23 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 24 | namespace android { |
| 25 | |
| 26 | /** |
| 27 | * Structure that represents a region in a circular buffer that might be at the |
| 28 | * end of the array and split in two. |
| 29 | */ |
| 30 | struct WrappingBuffer { |
| 31 | enum { |
| 32 | SIZE = 2 |
| 33 | }; |
| 34 | void *data[SIZE]; |
| 35 | int32_t numFrames[SIZE]; |
| 36 | }; |
| 37 | |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 38 | class FifoBuffer { |
| 39 | public: |
| 40 | FifoBuffer(int32_t bytesPerFrame, fifo_frames_t capacityInFrames); |
| 41 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 42 | FifoBuffer(int32_t bytesPerFrame, |
| 43 | fifo_frames_t capacityInFrames, |
| 44 | fifo_counter_t *readCounterAddress, |
| 45 | fifo_counter_t *writeCounterAddress, |
| 46 | void *dataStorageAddress); |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 47 | |
| 48 | ~FifoBuffer(); |
| 49 | |
| 50 | int32_t convertFramesToBytes(fifo_frames_t frames); |
| 51 | |
| 52 | fifo_frames_t read(void *destination, fifo_frames_t framesToRead); |
| 53 | |
| 54 | fifo_frames_t write(const void *source, fifo_frames_t framesToWrite); |
| 55 | |
| 56 | fifo_frames_t getThreshold(); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 57 | |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 58 | void setThreshold(fifo_frames_t threshold); |
| 59 | |
| 60 | fifo_frames_t getBufferCapacityInFrames(); |
| 61 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Return pointer to available full frames in data1 and set size in numFrames1. |
| 64 | * if the data is split across the end of the FIFO then set data2 and numFrames2. |
| 65 | * Other wise set them to null |
| 66 | * @param wrappingBuffer |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 67 | * @return total full frames available |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 68 | */ |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 69 | fifo_frames_t getFullDataAvailable(WrappingBuffer *wrappingBuffer); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Return pointer to available empty frames in data1 and set size in numFrames1. |
| 73 | * if the room is split across the end of the FIFO then set data2 and numFrames2. |
| 74 | * Other wise set them to null |
| 75 | * @param wrappingBuffer |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 76 | * @return total empty frames available |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 77 | */ |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 78 | fifo_frames_t getEmptyRoomAvailable(WrappingBuffer *wrappingBuffer); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Copy data from the FIFO into the buffer. |
| 82 | * @param buffer |
| 83 | * @param numFrames |
| 84 | * @return |
| 85 | */ |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 86 | fifo_frames_t readNow(void *buffer, fifo_frames_t numFrames); |
| 87 | |
| 88 | int64_t getNextReadTime(int32_t frameRate); |
| 89 | |
| 90 | int32_t getUnderrunCount() const { return mUnderrunCount; } |
| 91 | |
| 92 | FifoControllerBase *getFifoControllerBase() { return mFifo; } |
| 93 | |
| 94 | int32_t getBytesPerFrame() { |
| 95 | return mBytesPerFrame; |
| 96 | } |
| 97 | |
| 98 | fifo_counter_t getReadCounter() { |
| 99 | return mFifo->getReadCounter(); |
| 100 | } |
| 101 | |
| 102 | void setReadCounter(fifo_counter_t n) { |
| 103 | mFifo->setReadCounter(n); |
| 104 | } |
| 105 | |
| 106 | fifo_counter_t getWriteCounter() { |
| 107 | return mFifo->getWriteCounter(); |
| 108 | } |
| 109 | |
| 110 | void setWriteCounter(fifo_counter_t n) { |
| 111 | mFifo->setWriteCounter(n); |
| 112 | } |
| 113 | |
| 114 | private: |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 115 | |
| 116 | void fillWrappingBuffer(WrappingBuffer *wrappingBuffer, |
| 117 | int32_t framesAvailable, int32_t startIndex); |
| 118 | |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 119 | const fifo_frames_t mFrameCapacity; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 120 | const int32_t mBytesPerFrame; |
| 121 | uint8_t *mStorage; |
| 122 | bool mStorageOwned; // did this object allocate the storage? |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 123 | FifoControllerBase *mFifo; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 124 | fifo_counter_t mFramesReadCount; |
| 125 | fifo_counter_t mFramesUnderrunCount; |
| 126 | int32_t mUnderrunCount; // need? just use frames |
| 127 | int32_t mLastReadSize; |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 130 | } // android |
| 131 | |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 132 | #endif //FIFO_FIFO_BUFFER_H |