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