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 Create the AVBuffer. 78 * @return The shared pointer of AVBuffer. 79 * @since 6.0 80 */ 81 static Status Clone(std::shared_ptr<AVBuffer>& srcBuffer, std::shared_ptr<AVBuffer>& dstBuffer); 82 83 /** 84 * @brief Get the AVBufferConfig. 85 * @return The config struct of AVBuffer. 86 * @since 4.1 87 */ 88 const AVBufferConfig &GetConfig(); 89 90 /** 91 * @brief Get the unique identifier of buffer. 92 * @return The unique identifier of buffer. 93 * @since 4.1 94 */ 95 uint64_t GetUniqueId(); 96 97 /** 98 * @brief Wirte buffer info to MessageParcel. 99 * @param parcel The MessageParcel wirtten by buffer, refer to {@link MessageParcel}. 100 * @return Whether the writing was successful. 101 * @since 4.1 102 */ 103 bool WriteToMessageParcel(MessageParcel &parcel); 104 105 /** 106 * @brief Read buffer info from MessageParcel. 107 * @param parcel The MessageParcel that wirtten by remote buffer, refer to {@link MessageParcel}. 108 * @param isSurfaceBuffer Whether the parcel was obtained directly through SurfaceBuffer's function, {@link 109 * SurfaceBuffer}. 110 * @return Whether the reading was successful. 111 * @since 4.1 112 */ 113 bool ReadFromMessageParcel(MessageParcel &parcel, bool isSurfaceBuffer = false); 114 115 using MetaData = std::vector<uint8_t>; 116 117 int64_t pts_; 118 int64_t dts_; 119 int64_t duration_; 120 uint32_t flag_; 121 std::shared_ptr<Meta> meta_; 122 std::shared_ptr<AVMemory> memory_; 123 124 private: 125 static void CopyMeta(std::shared_ptr<AVBuffer>& srcBuffer, std::shared_ptr<AVBuffer>& dstBuffer); 126 static Status CopyAVMemory(std::shared_ptr<AVBuffer>& srcBuffer, std::shared_ptr<AVBuffer>& dstBuffer); 127 128 AVBuffer(); 129 Status Init(std::shared_ptr<AVAllocator> allocator, int32_t capacity = 0, int32_t align = 0); 130 Status Init(uint8_t *ptr, int32_t capacity, int32_t size = 0); 131 Status Init(sptr<SurfaceBuffer> surfaceBuffer); 132 AVBufferConfig config_; 133 }; 134 135 /** 136 * @brief AVBuffer's memory. 137 */ 138 class __attribute__((visibility("default"))) AVMemory { 139 public: 140 friend class AVBuffer; 141 virtual ~AVMemory(); 142 /** 143 * @brief Get the memory's types set by the allocator, refer to {@link MemoryType} 144 * @return the memory's types if the memory is valid, otherwise {@link VIRTUAL_MEMORY}. 145 * @since 4.1 146 */ 147 virtual MemoryType GetMemoryType(); 148 149 /** 150 * @brief Get the memory's Flag set by the allocator, refer to {@link MemoryType} 151 * @return the memory's flag. 152 * @since 4.1 153 */ 154 virtual MemoryFlag GetMemoryFlag(); 155 156 /** 157 * @brief Get the memory's capacity, which was set during creation and alloced by the allocator. 158 * @return The memory's capacity, bytes. If the memory is valid, otherwise -1. 159 * @since 4.1 160 */ 161 virtual int32_t GetCapacity(); 162 163 /** 164 * @brief Get the memory's used size. 165 * @return The memory's size, bytes. 166 * @since 4.1 167 */ 168 virtual int32_t GetSize(); 169 170 /** 171 * @brief Set the memory's used size. 172 * @param size The memory's used size. If the size is greater than the capacity, it will be set to equal the 173 * capacity. 174 * @return Returns Status::OK if the execution is successful, otherwise returns a specific error code, refer to 175 * {@link Status} 176 * @since 4.1 177 */ 178 virtual Status SetSize(int32_t size); 179 180 /** 181 * @brief Get the memory's used size. 182 * @return The memory's used size, bytes. 183 * @since 4.1 184 */ 185 virtual int32_t GetOffset(); 186 187 /** 188 * @brief Set the memory's offset. 189 * @param offset The memory's offset, bytes. 190 * @return Returns Status::OK if the execution is successful, otherwise returns a specific error code, refer to 191 * {@link Status} 192 * @since 4.1 193 */ 194 virtual Status SetOffset(int32_t offset); 195 196 /** 197 * @brief Get the memory's file descriptor. 198 * @return The memory's file descriptor. If the memory type is {@link SURFACE_MEMORY} or {@link VIRTUAL_MEMORY}, it 199 * will return -1. 200 * @since 4.1 201 */ 202 virtual int32_t GetFileDescriptor(); 203 204 /** 205 * @brief Get the memory's address. 206 * @return The pointer of memory's address. 207 * @since 4.1 208 */ 209 virtual uint8_t *GetAddr(); 210 211 /** 212 * @brief Writing data to memory. 213 * @param in The pointer to the data being written. 214 * @param writeSize The size of writing data, bytes. 215 * @param position The position of writing data in memory, if equal to INVALID_POSITION, write continuously after 216 * existing data, bytes. 217 * @return The length of the actual written data. 218 * @since 4.1 219 */ 220 virtual int32_t Write(const uint8_t *in, int32_t writeSize, int32_t position = INVALID_POSITION); 221 222 /** 223 * @brief Reading data from memory. 224 * @param out The pointer to save the read data. 225 * @param readSize The size of reading data, bytes. 226 * @param position The position of reading data in memory, if equal to INVALID_POSITION, read from begin, bytes. 227 * @return The length of the actual read data. 228 * @since 4.1 229 */ 230 virtual int32_t Read(uint8_t *out, int32_t readSize, int32_t position = INVALID_POSITION); 231 232 /** 233 * @brief Set the memory's used size to zero. 234 * @since 4.1 235 */ 236 void Reset(); 237 238 /** 239 * @brief Get the surface buffer of memory. 240 * @return Returns the surface buffer if the memory type is {@link SURFACE_MEMORY}, 241 * otherwise returns nullptr. 242 * @since 4.1 243 */ 244 virtual sptr<SurfaceBuffer> GetSurfaceBuffer(); 245 246 protected: 247 AVMemory(); 248 virtual Status Init(); 249 virtual Status Init(MessageParcel &parcel); 250 virtual Status InitSurfaceBuffer(MessageParcel &parcel); 251 virtual Status InitSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer); 252 virtual bool WriteToMessageParcel(MessageParcel &parcel); 253 virtual bool ReadFromMessageParcel(MessageParcel &parcel); 254 255 bool ReadCommonFromMessageParcel(MessageParcel &parcel); 256 bool SkipCommonFromMessageParcel(MessageParcel &parcel); 257 bool WriteCommonToMessageParcel(MessageParcel &parcel); 258 259 int32_t capacity_ = 0; 260 int32_t align_; 261 262 int32_t offset_; 263 int32_t size_; 264 uint8_t *base_; 265 uint64_t uid_; 266 std::shared_ptr<AVAllocator> allocator_; 267 268 private: 269 static std::shared_ptr<AVMemory> CreateAVMemory(std::shared_ptr<AVAllocator> allocator, 270 int32_t capacity = 0, int32_t align = 0); 271 static std::shared_ptr<AVMemory> CreateAVMemory(uint8_t *ptr, int32_t capacity, int32_t size); 272 static std::shared_ptr<AVMemory> CreateAVMemory(MessageParcel &parcel, bool isSurfaceBuffer = false); 273 static std::shared_ptr<AVMemory> CreateAVMemory(sptr<SurfaceBuffer> surfaceBuffer); 274 }; 275 } // namespace Media 276 } // namespace OHOS 277 #endif // AVBUFFER_H