1/*
2 * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef MEDIA_DATA_STREAM
17#define MEDIA_DATA_STREAM
18
19#include <memory>
20
21namespace OHOS {
22namespace Media {
23/**
24 * @enum MemoryType
25 *
26 * @since 1.0
27 * @version 1.0
28 */
29enum class MemoryType {
30    VIRTUAL_ADDR = 0,  ///< Virtual address
31    SURFACE_BUFFER,    ///< Surface
32    SHARE_MEMORY,      ///< Share Memory fd
33};
34
35/**
36 * @brief Data buffer, contains the data.
37 *
38 * @since 1.0
39 * @version 1.0
40 */
41class DataBuffer {
42public:
43    virtual ~DataBuffer() = default;
44
45    /**
46     * @brief Get the EOS status.
47     *
48     * @return Returns the EOS status, true if this is the end of steam.
49     * @since 1.0
50     * @version 1.0
51     */
52    virtual bool IsEos() = 0;
53
54    /**
55    * @brief Set the EOS status.
56    *
57    * @since 1.0
58    * @version 1.0
59    */
60    virtual void SetEos(bool isEos) = 0;
61
62    /**
63    * @brief Get the buffer address.
64    *
65    * @since 1.0
66    * @version 1.0
67    */
68    virtual uint8_t* GetAddress() = 0;
69
70    /**
71    * @brief Get the buffer capacity.
72    *
73    * @since 1.0
74    * @version 1.0
75    */
76    virtual size_t GetCapacity() = 0;
77
78    /**
79    * @brief Get the size of the valid data in this data buffer.
80    *
81    * @since 1.0
82    * @version 1.0
83    */
84    virtual size_t GetSize() = 0;
85
86    /**
87    * @brief Set the size of the valid data in this data buffer.
88    *
89    * @param size Indicates the size of the valid data.
90    * @since 1.0
91    * @version 1.0
92    */
93    virtual void SetSize(size_t size) = 0;
94};
95
96/**
97 * @brief Data producer uses this interface to produce data.
98 *
99 * @since 1.0
100 * @version 1.0
101 */
102class DataProducer {
103public:
104    virtual ~DataProducer() = default;
105
106    /**
107    * @brief Get empty buffer.
108    *
109    * @param buffer Out parameter to obtain the buffer.
110    * @param timeout Indicates how much time (millisecond) to wait, default -1 means wait until buffer obtained.
111    * @since 1.0
112    * @version 1.0
113    */
114    virtual bool GetEmptyBuffer(std::shared_ptr<DataBuffer>& buffer, int timeout = -1) = 0;
115
116    /**
117    * @brief Queue data buffer.
118    *
119    * @param buffer the buffer contains data.
120    * @since 1.0
121    * @version 1.0
122    */
123    virtual bool QueueDataBuffer(const std::shared_ptr<DataBuffer>& buffer) = 0;
124};
125
126/**
127 * @brief Data consumer uses this interface to consume data.
128 *
129 * @since 1.0
130 * @version 1.0
131 */
132class DataConsumer {
133public:
134    virtual ~DataConsumer() = default;
135
136    /**
137    * @brief Get data buffer.
138    *
139    * @param buffer Out parameter to obtain the buffer contains data.
140    * @param timeout Indicates how much time (millisecond) to wait, default -1 means wait until data buffer obtained.
141    * @since 1.0
142    * @version 1.0
143    */
144    virtual bool GetDataBuffer(std::shared_ptr<DataBuffer>& buffer, int timeout = -1) = 0;
145
146    /**
147    * @brief Use the shared_ptr of buffer to queue empty buffer to data stream.
148    *
149    * @param buffer Indicates the shared_ptr of the empty buffer.
150    * @since 1.0
151    * @version 1.0
152    */
153    virtual bool QueueEmptyBuffer(const std::shared_ptr<DataBuffer>& buffer) = 0;
154
155    /**
156    * @brief Use the buffer address to queue empty buffer to data stream.
157    *
158    * @param address Indicates the address of the empty buffer.
159    * @since 1.0
160    * @version 1.0
161    */
162    virtual bool QueueEmptyBuffer(uint8_t* address) = 0;
163};
164
165/**
166 * @brief Data stream, extends DataConsumer and DataProducer.
167 *
168 * @since 1.0
169 * @version 1.0
170 */
171class DataStream : public DataConsumer, public DataProducer {
172};
173
174/**
175 * @brief The factory function to create {@link DataStream}.
176 *
177 * @param size Indicates the size of each buffer.
178 * @param count Indicates the count of buffers.
179 * @param type Indicates the memory type, default is virtual address.
180 * @since 1.0
181 * @version 1.0
182 */
183std::shared_ptr<DataStream> CreateDataStream(size_t size, size_t count, MemoryType type = MemoryType::VIRTUAL_ADDR);
184} // Media
185} // OHOS
186#endif // MEDIA_DATA_STREAM
187