• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "FifoControllerBase.h"
26 
27 namespace oboe {
28 
29 class FifoBuffer {
30 public:
31     FifoBuffer(uint32_t bytesPerFrame, uint32_t capacityInFrames);
32 
33     FifoBuffer(uint32_t   bytesPerFrame,
34                uint32_t   capacityInFrames,
35                std::atomic<uint64_t>   *readCounterAddress,
36                std::atomic<uint64_t>   *writeCounterAddress,
37                uint8_t   *dataStorageAddress);
38 
39     ~FifoBuffer();
40 
41     int32_t convertFramesToBytes(int32_t frames);
42 
43     /**
44      * Read framesToRead or, if not enough, then read as many as are available.
45      * @param destination
46      * @param framesToRead number of frames requested
47      * @return number of frames actually read
48      */
49     int32_t read(void *destination, int32_t framesToRead);
50 
51     int32_t write(const void *source, int32_t framesToWrite);
52 
53     uint32_t getBufferCapacityInFrames() const;
54 
55     /**
56      * Calls read(). If all of the frames cannot be read then the remainder of the buffer
57      * is set to zero.
58      *
59      * @param destination
60      * @param framesToRead number of frames requested
61      * @return number of frames actually read
62      */
63     int32_t readNow(void *destination, int32_t numFrames);
64 
getFullFramesAvailable()65     uint32_t getFullFramesAvailable() {
66         return mFifo->getFullFramesAvailable();
67     }
68 
getBytesPerFrame()69     uint32_t getBytesPerFrame() const {
70         return mBytesPerFrame;
71     }
72 
getReadCounter()73     uint64_t getReadCounter() const {
74         return mFifo->getReadCounter();
75     }
76 
setReadCounter(uint64_t n)77     void setReadCounter(uint64_t n) {
78         mFifo->setReadCounter(n);
79     }
80 
getWriteCounter()81     uint64_t getWriteCounter() {
82         return mFifo->getWriteCounter();
83     }
setWriteCounter(uint64_t n)84     void setWriteCounter(uint64_t n) {
85         mFifo->setWriteCounter(n);
86     }
87 
88 private:
89     uint32_t mBytesPerFrame;
90     uint8_t* mStorage;
91     bool     mStorageOwned; // did this object allocate the storage?
92     std::unique_ptr<FifoControllerBase> mFifo;
93     uint64_t mFramesReadCount;
94     uint64_t mFramesUnderrunCount;
95 };
96 
97 } // namespace oboe
98 
99 #endif //OBOE_FIFOPROCESSOR_H
100