xref: /aosp_15_r20/external/oboe/include/oboe/FifoBuffer.h (revision 05767d913155b055644481607e6fa1e35e2fe72c)
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 OBOE_FIFOPROCESSOR_H
18 #define OBOE_FIFOPROCESSOR_H
19 
20 #include <memory>
21 #include <stdint.h>
22 
23 #include "oboe/Definitions.h"
24 
25 #include "oboe/FifoControllerBase.h"
26 
27 namespace oboe {
28 
29 class FifoBuffer {
30 public:
31 	/**
32 	 * Construct a `FifoBuffer`.
33 	 *
34 	 * @param bytesPerFrame amount of bytes for one frame
35 	 * @param capacityInFrames the capacity of frames in fifo
36 	 */
37     FifoBuffer(uint32_t bytesPerFrame, uint32_t capacityInFrames);
38 
39 	/**
40 	 * Construct a `FifoBuffer`.
41 	 * To be used if the storage allocation is done outside of FifoBuffer.
42 	 *
43 	 * @param bytesPerFrame amount of bytes for one frame
44 	 * @param capacityInFrames capacity of frames in fifo
45 	 * @param readCounterAddress address of read counter
46 	 * @param writeCounterAddress address of write counter
47 	 * @param dataStorageAddress address of storage
48 	 */
49     FifoBuffer(uint32_t   bytesPerFrame,
50                uint32_t   capacityInFrames,
51                std::atomic<uint64_t>   *readCounterAddress,
52                std::atomic<uint64_t>   *writeCounterAddress,
53                uint8_t   *dataStorageAddress);
54 
55     ~FifoBuffer();
56 
57 	/**
58 	 * Convert a number of frames in bytes.
59 	 *
60 	 * @return number of bytes
61 	 */
62     int32_t convertFramesToBytes(int32_t frames);
63 
64     /**
65      * Read framesToRead or, if not enough, then read as many as are available.
66      *
67      * @param destination
68      * @param framesToRead number of frames requested
69      * @return number of frames actually read
70      */
71     int32_t read(void *destination, int32_t framesToRead);
72 
73 	/**
74 	 * Write framesToWrite or, if too enough, then write as many as the fifo are not empty.
75 	 *
76 	 * @param destination
77 	 * @param framesToWrite number of frames requested
78 	 * @return number of frames actually write
79 	 */
80     int32_t write(const void *source, int32_t framesToWrite);
81 
82 	/**
83 	 * Get the buffer capacity in frames.
84 	 *
85 	 * @return number of frames
86 	 */
87     uint32_t getBufferCapacityInFrames() const;
88 
89     /**
90      * Calls read(). If all of the frames cannot be read then the remainder of the buffer
91      * is set to zero.
92      *
93      * @param destination
94      * @param framesToRead number of frames requested
95      * @return number of frames actually read
96      */
97     int32_t readNow(void *destination, int32_t numFrames);
98 
99 	/**
100 	 * Get the number of frames in the fifo.
101 	 *
102 	 * @return number of frames actually in the buffer
103 	 */
getFullFramesAvailable()104     uint32_t getFullFramesAvailable() {
105         return mFifo->getFullFramesAvailable();
106     }
107 
108 	/**
109 	 * Get the amount of bytes per frame.
110 	 *
111 	 * @return number of bytes per frame
112 	 */
getBytesPerFrame()113     uint32_t getBytesPerFrame() const {
114         return mBytesPerFrame;
115     }
116 
117 	/**
118 	 * Get the position of read counter.
119 	 *
120 	 * @return position of read counter
121 	 */
getReadCounter()122     uint64_t getReadCounter() const {
123         return mFifo->getReadCounter();
124     }
125 
126 	/**
127 	 * Set the position of read counter.
128 	 *
129 	 * @param n position of read counter
130 	 */
setReadCounter(uint64_t n)131     void setReadCounter(uint64_t n) {
132         mFifo->setReadCounter(n);
133     }
134 
135 	/**
136 	 * Get the position of write counter.
137 	 *
138 	 * @return position of write counter
139 	 */
getWriteCounter()140     uint64_t getWriteCounter() {
141         return mFifo->getWriteCounter();
142     }
143 
144     /**
145 	 * Set the position of write counter.
146 	 *
147 	 * @param n position of write counter
148 	 */
setWriteCounter(uint64_t n)149     void setWriteCounter(uint64_t n) {
150         mFifo->setWriteCounter(n);
151     }
152 
153 private:
154     uint32_t mBytesPerFrame;
155     uint8_t* mStorage;
156     bool     mStorageOwned; // did this object allocate the storage?
157     std::unique_ptr<FifoControllerBase> mFifo;
158     uint64_t mFramesReadCount;
159     uint64_t mFramesUnderrunCount;
160 };
161 
162 } // namespace oboe
163 
164 #endif //OBOE_FIFOPROCESSOR_H
165