1 /*
2 * Copyright (c) 2021 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 "utils_data_stub.h"
17
18 namespace OHOS::Camera {
EncodeCameraMetadata(const std::shared_ptr<CameraMetadata> & metadata,MessageParcel & data)19 bool UtilsDataStub::EncodeCameraMetadata(const std::shared_ptr<CameraMetadata> &metadata,
20 MessageParcel &data)
21 {
22 if (metadata == nullptr) {
23 return false;
24 }
25
26 bool bRet = true;
27 uint32_t tagCount = 0;
28 common_metadata_header_t *meta = metadata->get();
29 if (meta != nullptr) {
30 tagCount = Camera::GetCameraMetadataItemCount(meta);
31 bRet = (bRet && data.WriteInt32(static_cast<int32_t>(tagCount)));
32 camera_metadata_item_entry_t *item = Camera::GetMetadataItems(meta);
33 for (uint32_t i = 0; i < tagCount; i++, item++) {
34 camera_metadata_item_t entry;
35 int ret = FindCameraMetadataItem(meta, item->item, &entry);
36 if (ret == -ENOENT) {
37 return false;
38 }
39
40 bRet = (bRet && data.WriteUint32(static_cast<uint32_t>(entry.index)));
41 bRet = (bRet && data.WriteUint32(entry.item));
42 bRet = (bRet && data.WriteUint8(static_cast<uint8_t>(entry.data_type)));
43 bRet = (bRet && data.WriteUint32(static_cast<uint32_t>(entry.count)));
44 bRet = (bRet && UtilsDataStub::WriteMetadata(entry, data));
45 }
46 } else {
47 bRet = data.WriteInt32(tagCount);
48 }
49 return bRet;
50 }
51
DecodeCameraMetadata(MessageParcel & data,std::shared_ptr<CameraMetadata> & metadata)52 void UtilsDataStub::DecodeCameraMetadata(MessageParcel &data, std::shared_ptr<CameraMetadata> &metadata)
53 {
54 int32_t tagCount = data.ReadInt32();
55 if (tagCount <= 0) {
56 return;
57 }
58
59 int32_t metadataSize = 0;
60 std::vector<camera_metadata_item_t> entrys;
61 for (int32_t i = 0; i < tagCount; i++) {
62 camera_metadata_item_t entry;
63 entry.index = static_cast<size_t>(data.ReadUint32());
64 entry.item = static_cast<uint32_t>(data.ReadUint32());
65 entry.data_type = static_cast<uint8_t>(data.ReadUint8());
66 entry.count = static_cast<size_t>(data.ReadUint32());
67 ReadMetadata(entry, data);
68 metadataSize++;
69
70 entrys.push_back(entry);
71 }
72
73 metadata = std::make_shared<CameraMetadata>(tagCount, metadataSize);
74 common_metadata_header_t *meta = metadata->get();
75 for (auto &entry : entrys) {
76 void *buffer = nullptr;
77 UtilsDataStub::EntryDataToBuffer(entry, &buffer);
78 if (buffer != nullptr) {
79 (void)Camera::AddCameraMetadataItem(meta, entry.item, buffer, entry.count);
80 }
81 }
82 }
83
EncodeStreamInfo(const std::shared_ptr<StreamInfo> & pInfo,MessageParcel & parcel)84 bool UtilsDataStub::EncodeStreamInfo(const std::shared_ptr<StreamInfo> &pInfo, MessageParcel &parcel)
85 {
86 bool bRet = true;
87 bRet = (bRet && parcel.WriteInt32(static_cast<int32_t>(pInfo->streamId_)));
88 bRet = (bRet && parcel.WriteInt32(static_cast<int32_t>(pInfo->width_)));
89 bRet = (bRet && parcel.WriteInt32(static_cast<int32_t>(pInfo->height_)));
90 bRet = (bRet && parcel.WriteInt32(static_cast<int32_t>(pInfo->format_)));
91 bRet = (bRet = (bRet && parcel.WriteInt32(pInfo->intent_)));
92 bRet = (bRet && parcel.WriteBool(pInfo->tunneledMode_));
93 bool bufferQueueFlag = (pInfo->bufferQueue_ != nullptr) ? true : false;
94 bRet = (bRet && parcel.WriteBool(bufferQueueFlag));
95 if (bufferQueueFlag) {
96 bRet = (bRet && parcel.WriteRemoteObject(pInfo->bufferQueue_->AsObject()));
97 }
98 bRet = (bRet && parcel.WriteInt32(static_cast<int32_t>(pInfo->minFrameDuration_)));
99 bRet = (bRet && parcel.WriteInt32(pInfo->encodeType_));
100 return bRet;
101 }
102
DecodeStreamInfo(MessageParcel & parcel,std::shared_ptr<StreamInfo> & pInfo)103 void UtilsDataStub::DecodeStreamInfo(MessageParcel &parcel, std::shared_ptr<StreamInfo> &pInfo)
104 {
105 pInfo->streamId_ = static_cast<int>(parcel.ReadInt32());
106 pInfo->width_ = static_cast<int>(parcel.ReadInt32());
107 pInfo->height_ = static_cast<int>(parcel.ReadInt32());
108 pInfo->format_ = static_cast<int>(parcel.ReadInt32());
109 pInfo->intent_ = static_cast<StreamIntent>(parcel.ReadInt32());
110 pInfo->tunneledMode_ = parcel.ReadBool();
111 bool bufferQueueFlag = parcel.ReadBool();
112 if (bufferQueueFlag) {
113 sptr<IRemoteObject> remoteBufferProducer = parcel.ReadRemoteObject();
114 pInfo->bufferQueue_ = OHOS::iface_cast<OHOS::IBufferProducer>(remoteBufferProducer);
115 }
116 pInfo->minFrameDuration_ = static_cast<int>(parcel.ReadInt32());
117 pInfo->encodeType_ = static_cast<EncodeType>(parcel.ReadInt32());
118 }
119
GetDataSize(uint8_t type)120 int32_t UtilsDataStub::GetDataSize(uint8_t type)
121 {
122 int32_t size = 0;
123 if (type == META_TYPE_BYTE) {
124 size = sizeof(uint8_t);
125 } else if (type == META_TYPE_INT32) {
126 size = sizeof(int32_t);
127 } else if (type == META_TYPE_FLOAT) {
128 size = sizeof(float);
129 } else if (type == META_TYPE_INT64) {
130 size = sizeof(int64_t);
131 } else if (type == META_TYPE_DOUBLE) {
132 size = sizeof(double);
133 } else if (type == META_TYPE_RATIONAL) {
134 size = sizeof(camera_rational_t);
135 } else {
136 size = 0;
137 }
138 return size;
139 }
140
WriteMetadata(const camera_metadata_item_t & entry,MessageParcel & data)141 bool UtilsDataStub::WriteMetadata(const camera_metadata_item_t &entry, MessageParcel &data)
142 {
143 if (entry.data_type == META_TYPE_BYTE) {
144 std::vector<uint8_t> buffers;
145 for (size_t i = 0; i < entry.count; i++) {
146 buffers.push_back(*(entry.data.u8 + i));
147 }
148 data.WriteUInt8Vector(buffers);
149 } else if (entry.data_type == META_TYPE_INT32) {
150 std::vector<int32_t> buffers;
151 for (size_t i = 0; i < entry.count; i++) {
152 buffers.push_back(*(entry.data.i32 + i));
153 }
154 data.WriteInt32Vector(buffers);
155 } else if (entry.data_type == META_TYPE_FLOAT) {
156 std::vector<float> buffers;
157 for (size_t i = 0; i < entry.count; i++) {
158 buffers.push_back(*(entry.data.f + i));
159 }
160 data.WriteFloatVector(buffers);
161 } else if (entry.data_type == META_TYPE_INT64) {
162 std::vector<int64_t> buffers;
163 for (size_t i = 0; i < entry.count; i++) {
164 buffers.push_back(*(entry.data.i64 + i));
165 }
166 data.WriteInt64Vector(buffers);
167 } else if (entry.data_type == META_TYPE_DOUBLE) {
168 std::vector<double> buffers;
169 for (size_t i = 0; i < entry.count; i++) {
170 buffers.push_back(*(entry.data.d + i));
171 }
172 data.WriteDoubleVector(buffers);
173 } else if (entry.data_type == META_TYPE_RATIONAL) {
174 std::vector<int32_t> buffers;
175 for (size_t i = 0; i < entry.count; i++) {
176 buffers.push_back((*(entry.data.r + i)).numerator);
177 buffers.push_back((*(entry.data.r + i)).denominator);
178 }
179 data.WriteInt32Vector(buffers);
180 }
181
182 return true;
183 }
184
ReadMetadata(camera_metadata_item_t & entry,MessageParcel & data)185 bool UtilsDataStub::ReadMetadata(camera_metadata_item_t &entry, MessageParcel &data)
186 {
187 if (entry.data_type == META_TYPE_BYTE) {
188 std::vector<uint8_t> buffers;
189 data.ReadUInt8Vector(&buffers);
190 entry.data.u8 = new(std::nothrow) uint8_t[entry.count];
191 if (entry.data.u8 != nullptr) {
192 for (size_t i = 0; i < entry.count; i++) {
193 entry.data.u8[i] = buffers.at(i);
194 }
195 }
196 } else if (entry.data_type == META_TYPE_INT32) {
197 std::vector<int32_t> buffers;
198 data.ReadInt32Vector(&buffers);
199 entry.data.i32 = new(std::nothrow) int32_t[entry.count];
200 if (entry.data.i32 != nullptr) {
201 for (size_t i = 0; i < entry.count; i++) {
202 entry.data.i32[i] = buffers.at(i);
203 }
204 }
205 } else if (entry.data_type == META_TYPE_FLOAT) {
206 std::vector<float> buffers;
207 data.ReadFloatVector(&buffers);
208 entry.data.f = new(std::nothrow) float[entry.count];
209 if (entry.data.f != nullptr) {
210 for (size_t i = 0; i < entry.count; i++) {
211 entry.data.f[i] = buffers.at(i);
212 }
213 }
214 } else if (entry.data_type == META_TYPE_INT64) {
215 std::vector<int64_t> buffers;
216 data.ReadInt64Vector(&buffers);
217 entry.data.i64 = new(std::nothrow) int64_t[entry.count];
218 if (entry.data.i64 != nullptr) {
219 for (size_t i = 0; i < entry.count; i++) {
220 entry.data.i64[i] = buffers.at(i);
221 }
222 }
223 } else if (entry.data_type == META_TYPE_DOUBLE) {
224 std::vector<double> buffers;
225 data.ReadDoubleVector(&buffers);
226 entry.data.d = new(std::nothrow) double[entry.count];
227 if (entry.data.d != nullptr) {
228 for (size_t i = 0; i < entry.count; i++) {
229 entry.data.d[i] = buffers.at(i);
230 }
231 }
232 } else if (entry.data_type == META_TYPE_RATIONAL) {
233 std::vector<int32_t> buffers;
234 data.ReadInt32Vector(&buffers);
235 entry.data.r = new(std::nothrow) camera_rational_t[entry.count];
236 if (entry.data.r != nullptr) {
237 for (size_t i = 0, j = 0;
238 i < entry.count && j < static_cast<size_t>(buffers.size());
239 i++, j += 2) { // 2:Take two elements from the buffer vector container
240 entry.data.r[i].numerator = buffers.at(j);
241 entry.data.r[i].denominator = buffers.at(j + 1);
242 }
243 }
244 }
245 return true;
246 }
247
EntryDataToBuffer(const camera_metadata_item_t & entry,void ** buffer)248 void UtilsDataStub::EntryDataToBuffer(const camera_metadata_item_t &entry, void **buffer)
249 {
250 if (entry.data_type == META_TYPE_BYTE) {
251 *buffer = (void*)entry.data.u8;
252 } else if (entry.data_type == META_TYPE_INT32) {
253 *buffer = (void*)entry.data.i32;
254 } else if (entry.data_type == META_TYPE_FLOAT) {
255 *buffer = (void*)entry.data.f;
256 } else if (entry.data_type == META_TYPE_INT64) {
257 *buffer = (void*)entry.data.i64;
258 } else if (entry.data_type == META_TYPE_DOUBLE) {
259 *buffer = (void*)entry.data.d;
260 } else if (entry.data_type == META_TYPE_RATIONAL) {
261 *buffer = (void*)entry.data.r;
262 }
263 }
264 }
265