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 OHOS_AV_TRANSPORT_BUFFER_H 17 #define OHOS_AV_TRANSPORT_BUFFER_H 18 19 #include <map> 20 #include <memory> 21 #include <vector> 22 23 #include "av_trans_types.h" 24 25 namespace OHOS { 26 namespace DistributedHardware { 27 constexpr size_t INVALID_POSITION = -1; 28 29 enum struct MetaType : uint32_t { 30 AUDIO, 31 VIDEO, 32 }; 33 34 /** 35 * @brief BufferData description. Only manager the basic memory information. 36 */ 37 class BufferData { 38 public: 39 explicit BufferData(size_t capacity); 40 BufferData(size_t capacity, std::shared_ptr<uint8_t> bufData); 41 virtual ~BufferData() = default; 42 43 size_t GetSize(); 44 size_t GetCapacity(); 45 uint8_t *GetAddress() const; 46 size_t Write(const uint8_t* in, size_t writeSize, size_t position = INVALID_POSITION); 47 size_t Read(uint8_t* out, size_t readSize, size_t position = INVALID_POSITION); 48 void Reset(); 49 void SetSize(size_t size); 50 51 private: 52 size_t capacity_; 53 size_t size_; 54 std::shared_ptr<uint8_t> address_; 55 }; 56 57 /** 58 * @brief BufferMeta for buffer. Base class that describes various metadata. 59 */ 60 class BufferMeta { 61 public: 62 explicit BufferMeta(MetaType type); 63 virtual ~BufferMeta() = default; 64 65 MetaType GetMetaType() const; 66 bool GetMetaItem(AVTransTag tag, std::string &value); 67 void SetMetaItem(AVTransTag tag, const std::string &value); 68 69 private: 70 MetaType type_; 71 std::map<AVTransTag, std::string> tagMap_; 72 }; 73 74 /** 75 * @brief AVTransBuffer base class. 76 * Contains the data storage and metadata information of the buffer (buffer description information). 77 */ 78 class AVTransBuffer { 79 public: 80 explicit AVTransBuffer(MetaType type = MetaType::VIDEO); 81 ~AVTransBuffer() = default; 82 83 std::shared_ptr<BufferData> GetBufferData(uint32_t index = 0); 84 std::shared_ptr<BufferData> CreateBufferData(size_t capacity); 85 std::shared_ptr<BufferData> WrapBufferData(const uint8_t* data, size_t capacity, size_t size); 86 std::shared_ptr<BufferMeta> GetBufferMeta(); 87 void UpdateBufferMeta(std::shared_ptr<BufferMeta> bufferMeta); 88 uint32_t GetDataCount(); 89 void Reset(); 90 bool IsEmpty(); 91 92 private: 93 std::vector<std::shared_ptr<BufferData>> data_ {}; 94 std::shared_ptr<BufferMeta> meta_; 95 }; 96 } // namespace DistributedHardware 97 } // namespace OHOS 98 #endif // OHOS_AV_TRANSPORT_BUFFER_H