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