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 #include "av_trans_buffer.h"
17
18 #include <securec.h>
19
20 namespace OHOS {
21 namespace DistributedHardware {
AVTransBuffer(MetaType type)22 AVTransBuffer::AVTransBuffer(MetaType type) : meta_()
23 {
24 meta_ = std::make_shared<BufferMeta>(type);
25 }
26
CreateBufferData(size_t capacity)27 std::shared_ptr<BufferData> AVTransBuffer::CreateBufferData(size_t capacity)
28 {
29 auto bufData = std::make_shared<BufferData>(capacity);
30 data_.push_back(bufData);
31 return bufData;
32 }
33
WrapBufferData(const uint8_t * data,size_t capacity,size_t size)34 std::shared_ptr<BufferData> AVTransBuffer::WrapBufferData(const uint8_t* data, size_t capacity, size_t size)
35 {
36 auto bufData = std::make_shared<BufferData>(capacity,
37 std::shared_ptr<uint8_t>(const_cast<uint8_t *>(data), [](void* ptr) {}));
38 bufData->SetSize(size);
39 data_.push_back(bufData);
40 return bufData;
41 }
42
GetBufferData(uint32_t index)43 std::shared_ptr<BufferData> AVTransBuffer::GetBufferData(uint32_t index)
44 {
45 if (data_.size() <= index) {
46 return nullptr;
47 }
48 return data_[index];
49 }
50
GetBufferMeta()51 std::shared_ptr<BufferMeta> AVTransBuffer::GetBufferMeta()
52 {
53 return meta_;
54 }
55
UpdateBufferMeta(std::shared_ptr<BufferMeta> bufferMeta)56 void AVTransBuffer::UpdateBufferMeta(std::shared_ptr<BufferMeta> bufferMeta)
57 {
58 meta_ = bufferMeta;
59 }
60
GetDataCount()61 uint32_t AVTransBuffer::GetDataCount()
62 {
63 return data_.size();
64 }
65
IsEmpty()66 bool AVTransBuffer::IsEmpty()
67 {
68 return data_.empty();
69 }
70
Reset()71 void AVTransBuffer::Reset()
72 {
73 data_[0]->Reset();
74 MetaType type = meta_->GetMetaType();
75 meta_.reset();
76 meta_ = std::make_shared<BufferMeta>(type);
77 }
78
BufferData(size_t capacity)79 BufferData::BufferData(size_t capacity)
80 : capacity_(capacity), size_(0), address_(nullptr)
81 {
82 address_ = std::shared_ptr<uint8_t>(new uint8_t[capacity], std::default_delete<uint8_t[]>());
83 }
84
BufferData(size_t capacity,std::shared_ptr<uint8_t> bufData)85 BufferData::BufferData(size_t capacity, std::shared_ptr<uint8_t> bufData)
86 : capacity_(capacity), size_(0), address_(std::move(bufData))
87 {
88 }
89
GetSize()90 size_t BufferData::GetSize()
91 {
92 return size_;
93 }
94
GetCapacity()95 size_t BufferData::GetCapacity()
96 {
97 return capacity_;
98 }
99
GetAddress() const100 uint8_t* BufferData::GetAddress() const
101 {
102 return address_.get();
103 }
104
Write(const uint8_t * in,size_t writeSize,size_t position)105 size_t BufferData::Write(const uint8_t* in, size_t writeSize, size_t position)
106 {
107 size_t start = 0;
108 if (position == INVALID_POSITION) {
109 start = size_;
110 } else {
111 start = std::min(position, capacity_);
112 }
113 size_t length = std::min(writeSize, capacity_ - start);
114 if (memcpy_s(GetAddress() + start, length, in, length) != EOK) {
115 return 0;
116 }
117 size_ = start + length;
118 return length;
119 }
120
Read(uint8_t * out,size_t readSize,size_t position)121 size_t BufferData::Read(uint8_t* out, size_t readSize, size_t position)
122 {
123 size_t start = 0;
124 size_t maxLength = size_;
125 if (position != INVALID_POSITION) {
126 start = std::min(position, size_);
127 maxLength = size_ - start;
128 }
129 size_t length = std::min(readSize, maxLength);
130 if (memcpy_s(out, length, GetAddress() + start, length) != EOK) {
131 return 0;
132 }
133 return length;
134 }
135
Reset()136 void BufferData::Reset()
137 {
138 this->size_ = 0;
139 }
140
SetSize(size_t size)141 void BufferData::SetSize(size_t size)
142 {
143 this->size_ = size;
144 }
145
BufferMeta(MetaType type)146 BufferMeta::BufferMeta(MetaType type) : type_(type)
147 {
148 }
149
GetMetaItem(AVTransTag tag,std::string & value)150 bool BufferMeta::GetMetaItem(AVTransTag tag, std::string& value)
151 {
152 if (tagMap_.count(tag) != 0) {
153 value = tagMap_[tag];
154 return true;
155 } else {
156 return false;
157 }
158 }
159
SetMetaItem(AVTransTag tag,const std::string & value)160 void BufferMeta::SetMetaItem(AVTransTag tag, const std::string& value)
161 {
162 tagMap_[tag] = value;
163 }
164
GetMetaType() const165 MetaType BufferMeta::GetMetaType() const
166 {
167 return type_;
168 }
169 } // namespace DistributedHardware
170 } // namespace OHOS