~aleteoryx/muditaos

ref: b00548440b4d37ee2806f0760a949b23b7e14aa8 muditaos/module-audio/Audio/AbstractStream.hpp -rw-r--r-- 6.2 KiB
b0054844 — Lefucjusz [BH-2020] Fix double-free in DecoderMP3 1 year, 5 months ago
                                                                                
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "AudioFormat.hpp"

#include <cstdint>

namespace audio
{
    /**
     * @brief An abstract interface for classes implementing data connection
     * between two endpoints. The data is stored in blocks and accessed just
     * like in a regular FIFO. It provides an API to implement a zero-copy
     * mechanism.
     *
     */
    class AbstractStream
    {
      public:
        /**
         * @brief Events that are broadcasted to listeners by an AbstractStream
         *
         */
        enum class Event
        {
            NoEvent,
            StreamFull,
            StreamHalfUsed,
            StreamEmpty,
            StreamOverflow,
            StreamUnderFlow
        };

        /**
         * @brief An abstract interface for AbstractStream's event listeners
         *
         */
        class EventListener
        {
          public:
            /**
             * @brief Called on event by AbstractStream
             *
             * @param stream - stream reporting the event
             * @param event - type of event
             */
            virtual void onEvent(AbstractStream *stream, Event event) = 0;
        };

        /**
         * @brief A structure describing a range of raw data using a pair of
         * pointer and the size of data.
         */
        struct Span
        {
            /**
             * @brief Pointer to raw data.
             */
            std::uint8_t *data = nullptr;

            /**
             * @brief Size of data
             */
            std::size_t dataSize = 0;

            /**
             * @brief Calculate pointer to the end of the raw buffer
             *
             * @return pointer to the end of data.
             */
            constexpr std::uint8_t *dataEnd() const noexcept
            {
                return data + dataSize;
            }

            /**
             * @brief Resets the stored value to Null
             *
             */
            constexpr void reset() noexcept
            {
                data     = nullptr;
                dataSize = 0;
            }

            inline bool operator==(const Span &other) const noexcept
            {
                return data == other.data && dataSize == other.dataSize;
            }

            inline bool operator!=(const Span &other) const noexcept
            {
                return !operator==(other);
            }
        };

        /**
         * @brief A structure describing characteristics of an AbstractStream's
         * endpoint.
         *
         */
        struct Traits
        {
            /**
             * @brief the size of data to read/write in a single operation
             */
            std::size_t blockSize = 0;

            /**
             * @brief the format of data stored in the AbstractStream
             */
            AudioFormat format = nullFormat;
        };

        virtual ~AbstractStream() = default;

        /**
         * @brief Registers events listener
         *
         * @param listener to register
         */
        virtual void registerListener(EventListener *listener) = 0;

        /**
         * @brief Unregisters events listener
         *
         * @param listener to unregister
         */
        virtual void unregisterListeners(EventListener *listener) = 0;

        /**
         * @brief Fills single block with data provided
         *
         * @param data - a pointer to raw data to be copied from
         * @param dataSize - size of the data
         * @return true if operation succeded, false otherwise
         * @return false
         */
        virtual bool push(void *data, std::size_t dataSize) = 0;

        /**
         * @brief Fills single block with data provided
         *
         * @param span describing block of data to be copied
         *
         * @return true if operation succeded, false otherwise
         */
        virtual bool push(const Span &span) = 0;

        /**
         * @brief Fills single block with empty data
         *
         * @return true if operation succeded, false otherwise
         */
        virtual bool push() = 0;

        /**
         * @brief Copies one block of data to a buffer provided as an argument
         *
         * @param span - a place to copy stream's data to
         *
         * @return true if operation succeded, false otherwise
         */
        virtual bool pop(Span &span) = 0;

        /**
         * @brief Reserves a block in a stream for writing
         *
         * @param span - a space reserved within the stream
         * @return true if operation succeded, false otherwise
         */
        virtual bool reserve(Span &span) = 0;

        /**
         * @brief Marks reserved block as ready and writes it to the stream.
         */
        virtual void commit() = 0;

        /**
         * @brief Discards data written to the reserved block
         */
        virtual void release() = 0;

        /**
         * @brief Gets pointer and size of data ready to read without copying.
         *
         * @param span
         *
         * @return true if operation succeded, false otherwise
         */
        virtual bool peek(Span &span) = 0;

        /**
         * @brief Marks peeked data as read.
         */
        virtual void consume() = 0;

        /**
         * @brief Reset peek state without consuming it.
         */
        virtual void unpeek() = 0;

        /**
         * @brief Resets the state of a stream; discards all data
         */
        virtual void reset() = 0;

        /**
         * @brief Get the traits of the stream's input.
         *
         * @return Traits
         */
        [[nodiscard]] virtual auto getInputTraits() const noexcept -> Traits = 0;

        /**
         * @brief Get the traits of the stream's output.
         *
         * @return Traits
         */
        [[nodiscard]] virtual auto getOutputTraits() const noexcept -> Traits = 0;

        /**
         * @brief Checks if stream is empty.
         */
        [[nodiscard]] virtual bool isEmpty() const noexcept = 0;

        /**
         * @brief Checks if stream is full.
         */
        [[nodiscard]] virtual bool isFull() const noexcept = 0;
    };

}; // namespace audio