1 /*
2  * Copyright (C) 2023 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 AVBUFFER_H
17 #define AVBUFFER_H
18 
19 #include <memory>
20 #include <string>
21 #include "buffer/avallocator.h"
22 #include "common/status.h"
23 
24 namespace OHOS {
25 namespace Media {
26 /**
27  * @brief Class that encapsulates some types of media buffer.
28  */
29 class __attribute__((visibility("default"))) AVBuffer {
30 public:
31     ~AVBuffer();
32     /**
33      * @brief Create the AVBuffer by configuration.
34      * @param config The configuration of AVBuffer, refer to {@link AVBufferConfig}
35      * @return The shared pointer of AVBuffer.
36      * @since 4.1
37      */
38     static std::shared_ptr<AVBuffer> CreateAVBuffer(const AVBufferConfig &config);
39 
40     /**
41      * @brief Create the AVBuffer by allocator.
42      * @param allocator The instance of AVAllocator, refer to {@link AVAllocator}
43      * @param capacity The capacity of the memory, bytes.
44      * @param align The align of AVBuffer, bytes.
45      * @return The shared pointer of AVBuffer.
46      * @since 4.1
47      */
48     static std::shared_ptr<AVBuffer> CreateAVBuffer(std::shared_ptr<AVAllocator> allocator, int32_t capacity = 0,
49                                                     int32_t align = 0);
50 
51     /**
52      * @brief Create the AVBuffer by alloced memory.
53      * @param ptr The pointer of alloced memory, it requires users to manage the lifecycle.
54      * @param capacity The capacity of the memory, bytes.
55      * @param size The size of the memory, bytes. If it can not greater than capacity.
56      * @return The shared pointer of AVBuffer.
57      * @since 4.1
58      */
59     static std::shared_ptr<AVBuffer> CreateAVBuffer(uint8_t *ptr, int32_t capacity, int32_t size = 0);
60 
61     /**
62      * @brief Create the AVBuffer by surface buffer.
63      * @param surfaceBuffer The sptr of SurfaceBuffer, refer to {@link SurfaceBuffer}
64      * @return The shared pointer of AVBuffer.
65      * @since 4.1
66      */
67     static std::shared_ptr<AVBuffer> CreateAVBuffer(sptr<SurfaceBuffer> surfaceBuffer);
68 
69     /**
70      * @brief Create the AVBuffer.
71      * @return The shared pointer of AVBuffer.
72      * @since 4.1
73      */
74     static std::shared_ptr<AVBuffer> CreateAVBuffer();
75 
76     /**
77      * @brief Get the AVBufferConfig.
78      * @return The config struct of AVBuffer.
79      * @since 4.1
80      */
81     const AVBufferConfig &GetConfig();
82 
83     /**
84      * @brief Get the unique identifier of buffer.
85      * @return The unique identifier of buffer.
86      * @since 4.1
87      */
88     uint64_t GetUniqueId();
89 
90     /**
91      * @brief Wirte buffer info to MessageParcel.
92      * @param parcel The MessageParcel wirtten by buffer, refer to {@link MessageParcel}.
93      * @return Whether the writing was successful.
94      * @since 4.1
95      */
96     bool WriteToMessageParcel(MessageParcel &parcel);
97 
98     /**
99      * @brief Read buffer info from MessageParcel.
100      * @param parcel The MessageParcel that wirtten by remote buffer, refer to {@link MessageParcel}.
101      * @param isSurfaceBuffer Whether the parcel was obtained directly through SurfaceBuffer's function, {@link
102      * SurfaceBuffer}.
103      * @return Whether the reading was successful.
104      * @since 4.1
105      */
106     bool ReadFromMessageParcel(MessageParcel &parcel, bool isSurfaceBuffer = false);
107 
108     using MetaData = std::vector<uint8_t>;
109 
110     int64_t pts_;
111     int64_t dts_;
112     int64_t duration_;
113     uint32_t flag_;
114     std::shared_ptr<Meta> meta_;
115     std::shared_ptr<AVMemory> memory_;
116 
117 private:
118     AVBuffer();
119     Status Init(std::shared_ptr<AVAllocator> allocator, int32_t capacity = 0, int32_t align = 0);
120     Status Init(uint8_t *ptr, int32_t capacity, int32_t size = 0);
121     Status Init(sptr<SurfaceBuffer> surfaceBuffer);
122     AVBufferConfig config_;
123 };
124 
125 /**
126  * @brief AVBuffer's memory.
127  */
128 class __attribute__((visibility("default"))) AVMemory {
129 public:
130     friend class AVBuffer;
131     virtual ~AVMemory();
132     /**
133      * @brief Get the memory's types set by the allocator, refer to {@link MemoryType}
134      * @return the memory's types if the memory is valid, otherwise {@link VIRTUAL_MEMORY}.
135      * @since 4.1
136      */
137     virtual MemoryType GetMemoryType();
138 
139     /**
140      * @brief Get the memory's Flag set by the allocator, refer to {@link MemoryType}
141      * @return the memory's flag.
142      * @since 4.1
143      */
144     virtual MemoryFlag GetMemoryFlag();
145 
146     /**
147      * @brief Get the memory's capacity, which was set during creation and alloced by the allocator.
148      * @return The memory's capacity, bytes. If the memory is valid, otherwise -1.
149      * @since 4.1
150      */
151     virtual int32_t GetCapacity();
152 
153     /**
154      * @brief Get the memory's used size.
155      * @return The memory's size, bytes.
156      * @since 4.1
157      */
158     virtual int32_t GetSize();
159 
160     /**
161      * @brief Set the memory's used size.
162      * @param size The memory's used size. If the size is greater than the capacity, it will be set to equal the
163      * capacity.
164      * @return Returns Status::OK if the execution is successful, otherwise returns a specific error code, refer to
165      * {@link Status}
166      * @since 4.1
167      */
168     virtual Status SetSize(int32_t size);
169 
170     /**
171      * @brief Get the memory's used size.
172      * @return The memory's used size, bytes.
173      * @since 4.1
174      */
175     virtual int32_t GetOffset();
176 
177     /**
178      * @brief Set the memory's offset.
179      * @param offset The memory's offset, bytes.
180      * @return Returns Status::OK if the execution is successful, otherwise returns a specific error code, refer to
181      * {@link Status}
182      * @since 4.1
183      */
184     virtual Status SetOffset(int32_t offset);
185 
186     /**
187      * @brief Get the memory's file descriptor.
188      * @return The memory's file descriptor. If the memory type is {@link SURFACE_MEMORY} or {@link VIRTUAL_MEMORY}, it
189      * will return -1.
190      * @since 4.1
191      */
192     virtual int32_t GetFileDescriptor();
193 
194     /**
195      * @brief Get the memory's address.
196      * @return The pointer of memory's address.
197      * @since 4.1
198      */
199     virtual uint8_t *GetAddr();
200 
201     /**
202      * @brief Writing data to memory.
203      * @param in The pointer to the data being written.
204      * @param writeSize The size of writing data, bytes.
205      * @param position The position of writing data in memory, if equal to INVALID_POSITION, write continuously after
206      * existing data, bytes.
207      * @return The length of the actual written data.
208      * @since 4.1
209      */
210     virtual int32_t Write(const uint8_t *in, int32_t writeSize, int32_t position = INVALID_POSITION);
211 
212     /**
213      * @brief Reading data from memory.
214      * @param out The pointer to save the read data.
215      * @param readSize The size of reading data, bytes.
216      * @param position The position of reading data in memory, if equal to INVALID_POSITION, read from begin, bytes.
217      * @return The length of the actual read data.
218      * @since 4.1
219      */
220     virtual int32_t Read(uint8_t *out, int32_t readSize, int32_t position = INVALID_POSITION);
221 
222     /**
223      * @brief Set the memory's used size to zero.
224      * @since 4.1
225      */
226     void Reset();
227 
228     /**
229      * @brief Get the surface buffer of memory.
230      * @return Returns the surface buffer if the memory type is {@link SURFACE_MEMORY},
231      * otherwise returns nullptr.
232      * @since 4.1
233      */
234     virtual sptr<SurfaceBuffer> GetSurfaceBuffer();
235 
236 protected:
237     AVMemory();
238     virtual Status Init();
239     virtual Status Init(MessageParcel &parcel);
240     virtual Status InitSurfaceBuffer(MessageParcel &parcel);
241     virtual Status InitSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer);
242     virtual bool WriteToMessageParcel(MessageParcel &parcel);
243     virtual bool ReadFromMessageParcel(MessageParcel &parcel);
244 
245     bool ReadCommonFromMessageParcel(MessageParcel &parcel);
246     bool SkipCommonFromMessageParcel(MessageParcel &parcel);
247     bool WriteCommonToMessageParcel(MessageParcel &parcel);
248 
249     int32_t capacity_ = 0;
250     int32_t align_;
251 
252     int32_t offset_;
253     int32_t size_;
254     uint8_t *base_;
255     uint64_t uid_;
256     std::shared_ptr<AVAllocator> allocator_;
257 
258 private:
259     static std::shared_ptr<AVMemory> CreateAVMemory(std::shared_ptr<AVAllocator> allocator,
260                                                     int32_t capacity = 0, int32_t align = 0);
261     static std::shared_ptr<AVMemory> CreateAVMemory(uint8_t *ptr, int32_t capacity, int32_t size);
262     static std::shared_ptr<AVMemory> CreateAVMemory(MessageParcel &parcel, bool isSurfaceBuffer = false);
263     static std::shared_ptr<AVMemory> CreateAVMemory(sptr<SurfaceBuffer> surfaceBuffer);
264 };
265 } // namespace Media
266 } // namespace OHOS
267 #endif // AVBUFFER_H