blob: 6cced2d34ba1ebebbfe68ea54f7a055d6273f01c [file] [log] [blame]
Don Turnerca6f91a2017-09-05 14:52:39 +01001/*
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
Don Turner2786fb52017-11-27 19:08:25 +000017#ifndef OBOE_FIFOPROCESSOR_H
18#define OBOE_FIFOPROCESSOR_H
Don Turnerca6f91a2017-09-05 14:52:39 +010019
Phil Burk9446ed62020-07-13 11:26:55 -070020#include <memory>
21#include <stdint.h>
Don Turnerca6f91a2017-09-05 14:52:39 +010022
Don Turner3bf32ae2017-11-27 13:25:05 +000023#include "oboe/Definitions.h"
24
Phil Burk9446ed62020-07-13 11:26:55 -070025#include "FifoControllerBase.h"
26
Don Turner3bf32ae2017-11-27 13:25:05 +000027namespace oboe {
Don Turnerca6f91a2017-09-05 14:52:39 +010028
29class FifoBuffer {
30public:
31 FifoBuffer(uint32_t bytesPerFrame, uint32_t capacityInFrames);
32
33 FifoBuffer(uint32_t bytesPerFrame,
34 uint32_t capacityInFrames,
Phil Burk08a5e0f2019-08-23 16:28:21 -070035 std::atomic<uint64_t> *readCounterAddress,
36 std::atomic<uint64_t> *writeCounterAddress,
Phil Burkf61cea32018-12-17 17:30:21 -080037 uint8_t *dataStorageAddress);
Don Turnerca6f91a2017-09-05 14:52:39 +010038
39 ~FifoBuffer();
40
41 int32_t convertFramesToBytes(int32_t frames);
42
Phil Burkb3e90b52018-10-03 08:29:24 -070043 /**
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 */
Don Turnerca6f91a2017-09-05 14:52:39 +010049 int32_t read(void *destination, int32_t framesToRead);
50
51 int32_t write(const void *source, int32_t framesToWrite);
52
Don Turnerca6f91a2017-09-05 14:52:39 +010053 uint32_t getBufferCapacityInFrames() const;
54
Phil Burkb3e90b52018-10-03 08:29:24 -070055 /**
56 * Calls read(). If all of the frames cannot be read then the remainder of the buffer
Phil Burk919cd422019-08-12 17:26:50 -070057 * is set to zero.
Phil Burkb3e90b52018-10-03 08:29:24 -070058 *
59 * @param destination
60 * @param framesToRead number of frames requested
61 * @return number of frames actually read
62 */
Phil Burk2b9390d2018-10-03 09:06:58 -070063 int32_t readNow(void *destination, int32_t numFrames);
Don Turnerca6f91a2017-09-05 14:52:39 +010064
Phil Burk08a5e0f2019-08-23 16:28:21 -070065 uint32_t getFullFramesAvailable() {
66 return mFifo->getFullFramesAvailable();
67 }
Don Turnerca6f91a2017-09-05 14:52:39 +010068
69 uint32_t getBytesPerFrame() const {
70 return mBytesPerFrame;
71 }
72
73 uint64_t getReadCounter() const {
74 return mFifo->getReadCounter();
75 }
76
77 void setReadCounter(uint64_t n) {
78 mFifo->setReadCounter(n);
Don Turnerca6f91a2017-09-05 14:52:39 +010079 }
80
81 uint64_t getWriteCounter() {
82 return mFifo->getWriteCounter();
83 }
84 void setWriteCounter(uint64_t n) {
85 mFifo->setWriteCounter(n);
Don Turnerca6f91a2017-09-05 14:52:39 +010086 }
87
88private:
Don Turnerca6f91a2017-09-05 14:52:39 +010089 uint32_t mBytesPerFrame;
90 uint8_t* mStorage;
91 bool mStorageOwned; // did this object allocate the storage?
Phil Burk08a5e0f2019-08-23 16:28:21 -070092 std::unique_ptr<FifoControllerBase> mFifo;
Don Turnerca6f91a2017-09-05 14:52:39 +010093 uint64_t mFramesReadCount;
94 uint64_t mFramesUnderrunCount;
Don Turnerca6f91a2017-09-05 14:52:39 +010095};
96
Don Turner3bf32ae2017-11-27 13:25:05 +000097} // namespace oboe
98
Don Turner2786fb52017-11-27 19:08:25 +000099#endif //OBOE_FIFOPROCESSOR_H