aboutsummaryrefslogtreecommitdiff
path: root/src/fifo/FifoControllerBase.h
blob: a4c2b41a5e5ea9adef4e44ae8264a5db9f0cd638 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef NATIVEOBOE_FIFOCONTROLLERBASE_H
#define NATIVEOBOE_FIFOCONTROLLERBASE_H

#include <stdint.h>

namespace oboe {

/**
 * Manage the read/write indices of a circular buffer.
 *
 * The caller is responsible for reading and writing the actual data.
 * Note that the span of available frames may not be contiguous. They
 * may wrap around from the end to the beginning of the buffer. In that
 * case the data must be read or written in at least two blocks of frames.
 *
 */

class FifoControllerBase {

public:
    /**
     * @param totalFrames capacity of the circular buffer in frames.
     */
    FifoControllerBase(uint32_t totalFrames);

    virtual ~FifoControllerBase() = default;

    /**
     * The frames available to read will be calculated from the read and write counters.
     * The result will be clipped to the capacity of the buffer.
     * If the buffer has underflowed then this will return zero.
     * @return number of valid frames available to read.
     */
    uint32_t getFullFramesAvailable() const;

    /**
     * The index in a circular buffer of the next frame to read.
     */
    uint32_t getReadIndex() const;

    /**
     * @param numFrames number of frames to advance the read index
     */
    void advanceReadIndex(uint32_t numFrames);

    /**
     * @return maximum number of frames that can be written without exceeding the threshold.
     */
    uint32_t getEmptyFramesAvailable() const;

    /**
     * The index in a circular buffer of the next frame to write.
     */
    uint32_t getWriteIndex() const;

    /**
     * @param numFrames number of frames to advance the write index
     */
    void advanceWriteIndex(uint32_t numFrames);

    uint32_t getFrameCapacity() const { return mTotalFrames; }

    virtual uint64_t getReadCounter() const = 0;
    virtual void setReadCounter(uint64_t n) = 0;
    virtual void incrementReadCounter(uint64_t n) = 0;
    virtual uint64_t getWriteCounter() const = 0;
    virtual void setWriteCounter(uint64_t n) = 0;
    virtual void incrementWriteCounter(uint64_t n) = 0;

private:
    uint32_t mTotalFrames;
};

} // namespace oboe

#endif //NATIVEOBOE_FIFOCONTROLLERBASE_H