• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 - 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 "utils_data_stub.h"
17 
18 namespace OHOS::Camera {
WriteMetadataDataToVec(const camera_metadata_item_t & entry,std::vector<uint8_t> & cameraAbility)19 bool UtilsDataStub::WriteMetadataDataToVec(const camera_metadata_item_t &entry, std::vector<uint8_t> &cameraAbility)
20 {
21     if (entry.data_type == META_TYPE_BYTE) {
22         for (size_t i = 0; i < entry.count; i++) {
23             WriteData<int8_t>(*(entry.data.u8 + i), cameraAbility);
24         }
25     } else if (entry.data_type == META_TYPE_INT32) {
26         for (size_t i = 0; i < entry.count; i++) {
27             WriteData<int32_t>(*(entry.data.i32 + i), cameraAbility);
28         }
29     } else if (entry.data_type == META_TYPE_FLOAT) {
30         for (size_t i = 0; i < entry.count; i++) {
31             WriteData<float>(*(entry.data.f + i), cameraAbility);
32         }
33     } else if (entry.data_type == META_TYPE_INT64) {
34         for (size_t i = 0; i < entry.count; i++) {
35             WriteData<int64_t>(*(entry.data.i64 + i), cameraAbility);
36         }
37     } else if (entry.data_type == META_TYPE_DOUBLE) {
38         for (size_t i = 0; i < entry.count; i++) {
39             WriteData<double>(*(entry.data.d + i), cameraAbility);
40         }
41     } else if (entry.data_type == META_TYPE_RATIONAL) {
42         for (size_t i = 0; i < entry.count; i++) {
43             WriteData<int32_t>((*(entry.data.r + i)).numerator, cameraAbility);
44             WriteData<int32_t>((*(entry.data.r + i)).denominator, cameraAbility);
45         }
46     }
47 
48     return true;
49 }
50 
ConvertMetadataToVec(const std::shared_ptr<CameraMetadata> & metadata,std::vector<uint8_t> & cameraAbility)51 bool UtilsDataStub::ConvertMetadataToVec(const std::shared_ptr<CameraMetadata> &metadata,
52     std::vector<uint8_t> &cameraAbility)
53 {
54     if (metadata == nullptr) {
55         return false;
56     }
57 
58     bool bRet = true;
59     uint32_t tagCount = 0;
60     common_metadata_header_t *meta = metadata->get();
61     if (meta != nullptr) {
62         tagCount = GetCameraMetadataItemCount(meta);
63         WriteData<uint32_t>(tagCount, cameraAbility);
64         WriteData<uint32_t>(GetCameraMetadataItemCapacity(meta), cameraAbility);
65         WriteData<uint32_t>(GetCameraMetadataDataSize(meta), cameraAbility);
66         for (uint32_t i = 0; i < tagCount; i++) {
67             camera_metadata_item_t item;
68             int ret = GetCameraMetadataItem(meta, i, &item);
69             if (ret != CAM_META_SUCCESS) {
70                 return false;
71             }
72 
73             WriteData<uint32_t>(item.index, cameraAbility);
74             WriteData<uint32_t>(item.item, cameraAbility);
75             WriteData<uint32_t>(item.data_type, cameraAbility);
76             WriteData<uint32_t>(item.count, cameraAbility);
77 
78             bRet = WriteMetadataDataToVec(item, cameraAbility);
79         }
80     } else {
81         cameraAbility.push_back(tagCount);
82     }
83 
84     return bRet;
85 }
86 
EncodeCameraMetadata(const std::shared_ptr<CameraMetadata> & metadata,MessageParcel & data)87 bool UtilsDataStub::EncodeCameraMetadata(const std::shared_ptr<CameraMetadata> &metadata,
88     MessageParcel &data)
89 {
90     if (metadata == nullptr) {
91         return false;
92     }
93 
94     bool bRet = true;
95     uint32_t tagCount = 0;
96     common_metadata_header_t *meta = metadata->get();
97     if (meta != nullptr) {
98         tagCount = Camera::GetCameraMetadataItemCount(meta);
99         bRet = (bRet && data.WriteInt32(static_cast<int32_t>(tagCount)));
100         camera_metadata_item_entry_t *item = Camera::GetMetadataItems(meta);
101         for (uint32_t i = 0; i < tagCount; i++, item++) {
102             camera_metadata_item_t entry;
103             int ret = FindCameraMetadataItem(meta, item->item, &entry);
104             if (ret == -ENOENT) {
105                 return false;
106             }
107 
108             bRet = (bRet && data.WriteUint32(static_cast<uint32_t>(entry.index)));
109             bRet = (bRet && data.WriteUint32(entry.item));
110             bRet = (bRet && data.WriteUint8(static_cast<uint8_t>(entry.data_type)));
111             bRet = (bRet && data.WriteUint32(static_cast<uint32_t>(entry.count)));
112             bRet = (bRet && UtilsDataStub::WriteMetadata(entry, data));
113         }
114     } else {
115         bRet = data.WriteInt32(tagCount);
116     }
117     return bRet;
118 }
119 
ReadMetadataDataFromVec(int32_t & index,camera_metadata_item_t & entry,const std::vector<uint8_t> & cameraAbility)120 bool UtilsDataStub::ReadMetadataDataFromVec(int32_t &index, camera_metadata_item_t &entry,
121     const std::vector<uint8_t> &cameraAbility)
122 {
123     if (entry.data_type == META_TYPE_BYTE) {
124         entry.data.u8 = new(std::nothrow) uint8_t[entry.count];
125         if (entry.data.u8 != nullptr) {
126             for (size_t i = 0; i < entry.count; i++) {
127                 ReadData<uint8_t>(entry.data.u8[i], index, cameraAbility);
128             }
129         }
130     } else if (entry.data_type == META_TYPE_INT32) {
131         entry.data.i32 = new(std::nothrow) int32_t[entry.count];
132         if (entry.data.i32 != nullptr) {
133             for (size_t i = 0; i < entry.count; i++) {
134                 ReadData<int32_t>(entry.data.i32[i], index, cameraAbility);
135             }
136         }
137     } else if (entry.data_type == META_TYPE_FLOAT) {
138         entry.data.f = new(std::nothrow) float[entry.count];
139         if (entry.data.f != nullptr) {
140             for (size_t i = 0; i < entry.count; i++) {
141                 ReadData<float>(entry.data.f[i], index, cameraAbility);
142             }
143         }
144     } else if (entry.data_type == META_TYPE_INT64) {
145         entry.data.i64 = new(std::nothrow) int64_t[entry.count];
146         if (entry.data.i64 != nullptr) {
147             for (size_t i = 0; i < entry.count; i++) {
148                 ReadData<int64_t>(entry.data.i64[i], index, cameraAbility);
149             }
150         }
151     } else if (entry.data_type == META_TYPE_DOUBLE) {
152         entry.data.d = new(std::nothrow) double[entry.count];
153         if (entry.data.d != nullptr) {
154             for (size_t i = 0; i < entry.count; i++) {
155                 ReadData<double>(entry.data.d[i], index, cameraAbility);
156             }
157         }
158     } else if (entry.data_type == META_TYPE_RATIONAL) {
159         entry.data.r = new(std::nothrow) camera_rational_t[entry.count];
160         if (entry.data.r != nullptr) {
161             for (size_t i = 0; i < entry.count; i++) {
162                 ReadData<int32_t>(entry.data.r[i].numerator, index, cameraAbility);
163                 ReadData<int32_t>(entry.data.r[i].denominator, index, cameraAbility);
164             }
165         }
166     }
167 
168     return true;
169 }
170 
ConvertVecToMetadata(const std::vector<uint8_t> & cameraAbility,std::shared_ptr<CameraMetadata> & metadata)171 void UtilsDataStub::ConvertVecToMetadata(const std::vector<uint8_t> &cameraAbility,
172     std::shared_ptr<CameraMetadata> &metadata)
173 {
174     int32_t index = 0;
175     uint32_t tagCount = 0;
176     uint32_t itemCapacity = 0;
177     uint32_t dataCapacity = 0;
178     constexpr uint32_t MAX_SUPPORTED_TAGS = 1000;
179     constexpr uint32_t MAX_SUPPORTED_ITEMS = 1000;
180     constexpr uint32_t MAX_ITEM_CAPACITY = (1000 * 10);
181     constexpr uint32_t MAX_DATA_CAPACITY = (1000 * 10 * 10);
182 
183     ReadData<uint32_t>(tagCount, index, cameraAbility);
184     if (tagCount > MAX_SUPPORTED_TAGS) {
185         tagCount = MAX_SUPPORTED_TAGS;
186         METADATA_ERR_LOG("MetadataUtils::DecodeCameraMetadata tagCount is more than supported value");
187     }
188     ReadData<uint32_t>(itemCapacity, index, cameraAbility);
189     if (itemCapacity > MAX_ITEM_CAPACITY) {
190         itemCapacity = MAX_ITEM_CAPACITY;
191         METADATA_ERR_LOG("MetadataUtils::DecodeCameraMetadata itemCapacity is more than supported value");
192     }
193     ReadData<uint32_t>(dataCapacity, index, cameraAbility);
194     if (dataCapacity > MAX_DATA_CAPACITY) {
195         dataCapacity = MAX_DATA_CAPACITY;
196         METADATA_ERR_LOG("MetadataUtils::DecodeCameraMetadata dataCapacity is more than supported value");
197     }
198 
199     std::vector<camera_metadata_item_t> items;
200     for (int32_t i = 0; i < tagCount; i++) {
201         camera_metadata_item_t item;
202         ReadData<uint32_t>(item.index, index, cameraAbility);
203         ReadData<uint32_t>(item.item, index, cameraAbility);
204         ReadData<uint32_t>(item.data_type, index, cameraAbility);
205         ReadData<uint32_t>(item.count, index, cameraAbility);
206         if (item.count > MAX_SUPPORTED_ITEMS) {
207             item.count = MAX_SUPPORTED_ITEMS;
208             METADATA_ERR_LOG("MetadataUtils::DecodeCameraMetadata item.count is more than supported value");
209         }
210         ReadMetadataDataFromVec(index, item, cameraAbility);
211         items.push_back(item);
212     }
213 
214     metadata = std::make_shared<CameraMetadata>(itemCapacity, dataCapacity);
215     CHECK_IF_PTR_NULL_RETURN_VOID(metadata);
216     common_metadata_header_t *meta = metadata->get();
217     for (auto &item_ : items) {
218         void *buffer = nullptr;
219         MetadataUtils::ItemDataToBuffer(item_, &buffer);
220         (void)AddCameraMetadataItem(meta, item_.item, buffer, item_.count);
221         MetadataUtils::FreeMetadataBuffer(item_);
222     }
223 }
224 
DecodeCameraMetadata(MessageParcel & data,std::shared_ptr<CameraMetadata> & metadata)225 void UtilsDataStub::DecodeCameraMetadata(MessageParcel &data, std::shared_ptr<CameraMetadata> &metadata)
226 {
227     int32_t tagCount = data.ReadInt32();
228     if (tagCount <= 0) {
229         return;
230     }
231 
232     int32_t metadataSize = 0;
233     std::vector<camera_metadata_item_t> entrys;
234     for (int32_t i = 0; i < tagCount; i++) {
235         camera_metadata_item_t entry;
236         entry.index = static_cast<size_t>(data.ReadUint32());
237         entry.item = static_cast<uint32_t>(data.ReadUint32());
238         entry.data_type = static_cast<uint8_t>(data.ReadUint8());
239         entry.count = static_cast<size_t>(data.ReadUint32());
240         ReadMetadata(entry, data);
241         metadataSize++;
242 
243         entrys.push_back(entry);
244     }
245 
246     metadata = std::make_shared<CameraMetadata>(tagCount, metadataSize);
247     CHECK_IF_PTR_NULL_RETURN_VOID(metadata);
248     common_metadata_header_t *meta = metadata->get();
249     for (auto &entry : entrys) {
250         void *buffer = nullptr;
251         UtilsDataStub::EntryDataToBuffer(entry, &buffer);
252         if (buffer != nullptr) {
253             (void)Camera::AddCameraMetadataItem(meta, entry.item, buffer, entry.count);
254         }
255         MetadataUtils::FreeMetadataBuffer(entry);
256     }
257 }
258 
EncodeStreamInfo(const std::shared_ptr<StreamInfo> & pInfo,MessageParcel & parcel)259 bool UtilsDataStub::EncodeStreamInfo(const std::shared_ptr<StreamInfo> &pInfo, MessageParcel &parcel)
260 {
261     bool bRet = true;
262     bRet = (bRet && parcel.WriteInt32(static_cast<int32_t>(pInfo->streamId_)));
263     bRet = (bRet && parcel.WriteInt32(static_cast<int32_t>(pInfo->width_)));
264     bRet = (bRet && parcel.WriteInt32(static_cast<int32_t>(pInfo->height_)));
265     bRet = (bRet && parcel.WriteInt32(static_cast<int32_t>(pInfo->format_)));
266     bRet = (bRet && parcel.WriteInt32(pInfo->intent_));
267     bRet = (bRet && parcel.WriteBool(pInfo->tunneledMode_));
268     bool bufferQueueFlag = (pInfo->bufferQueue_->producer_ != nullptr) ? true : false;
269     bRet = (bRet && parcel.WriteBool(bufferQueueFlag));
270     if (bufferQueueFlag) {
271         bRet = (bRet && parcel.WriteRemoteObject(pInfo->bufferQueue_->producer_->AsObject()));
272     }
273     bRet = (bRet && parcel.WriteInt32(static_cast<int32_t>(pInfo->minFrameDuration_)));
274     bRet = (bRet && parcel.WriteInt32(pInfo->encodeType_));
275     return bRet;
276 }
277 
DecodeStreamInfo(MessageParcel & parcel,std::shared_ptr<StreamInfo> & pInfo)278 void UtilsDataStub::DecodeStreamInfo(MessageParcel &parcel, std::shared_ptr<StreamInfo> &pInfo)
279 {
280     pInfo->streamId_ = static_cast<int>(parcel.ReadInt32());
281     pInfo->width_ = static_cast<int>(parcel.ReadInt32());
282     pInfo->height_ = static_cast<int>(parcel.ReadInt32());
283     pInfo->format_ = static_cast<int>(parcel.ReadInt32());
284     pInfo->intent_ = static_cast<StreamIntent>(parcel.ReadInt32());
285     pInfo->tunneledMode_ = parcel.ReadBool();
286     bool bufferQueueFlag = parcel.ReadBool();
287     if (bufferQueueFlag) {
288         sptr<IRemoteObject> remoteBufferProducer = parcel.ReadRemoteObject();
289         pInfo->bufferQueue_->producer_ = OHOS::iface_cast<OHOS::IBufferProducer>(remoteBufferProducer);
290     }
291     pInfo->minFrameDuration_ = static_cast<int>(parcel.ReadInt32());
292     pInfo->encodeType_ = static_cast<EncodeType>(parcel.ReadInt32());
293 }
294 
GetDataSize(uint8_t type)295 int32_t UtilsDataStub::GetDataSize(uint8_t type)
296 {
297     int32_t size = 0;
298     if (type == META_TYPE_BYTE) {
299         size = sizeof(uint8_t);
300     } else if (type == META_TYPE_INT32) {
301         size = sizeof(int32_t);
302     } else if (type == META_TYPE_FLOAT) {
303         size = sizeof(float);
304     } else if (type == META_TYPE_INT64) {
305         size = sizeof(int64_t);
306     } else if (type == META_TYPE_DOUBLE) {
307         size = sizeof(double);
308     } else if (type == META_TYPE_RATIONAL) {
309         size = sizeof(camera_rational_t);
310     } else {
311         size = 0;
312     }
313     return size;
314 }
315 
WriteMetadata(const camera_metadata_item_t & entry,MessageParcel & data)316 bool UtilsDataStub::WriteMetadata(const camera_metadata_item_t &entry, MessageParcel &data)
317 {
318     if (entry.data_type == META_TYPE_BYTE) {
319         std::vector<uint8_t> buffers;
320         for (size_t i = 0; i < entry.count; i++) {
321             buffers.push_back(*(entry.data.u8 + i));
322         }
323         data.WriteUInt8Vector(buffers);
324     } else if (entry.data_type == META_TYPE_INT32) {
325         std::vector<int32_t> buffers;
326         for (size_t i = 0; i < entry.count; i++) {
327             buffers.push_back(*(entry.data.i32 + i));
328         }
329         data.WriteInt32Vector(buffers);
330     } else if (entry.data_type == META_TYPE_FLOAT) {
331         std::vector<float> buffers;
332         for (size_t i = 0; i < entry.count; i++) {
333             buffers.push_back(*(entry.data.f + i));
334         }
335         data.WriteFloatVector(buffers);
336     } else if (entry.data_type == META_TYPE_INT64) {
337         std::vector<int64_t> buffers;
338         for (size_t i = 0; i < entry.count; i++) {
339             buffers.push_back(*(entry.data.i64 + i));
340         }
341         data.WriteInt64Vector(buffers);
342     } else if (entry.data_type == META_TYPE_DOUBLE) {
343         std::vector<double> buffers;
344         for (size_t i = 0; i < entry.count; i++) {
345             buffers.push_back(*(entry.data.d + i));
346         }
347         data.WriteDoubleVector(buffers);
348     } else if (entry.data_type == META_TYPE_RATIONAL) {
349         std::vector<int32_t> buffers;
350         for (size_t i = 0; i < entry.count; i++) {
351             buffers.push_back((*(entry.data.r + i)).numerator);
352             buffers.push_back((*(entry.data.r + i)).denominator);
353         }
354         data.WriteInt32Vector(buffers);
355     }
356 
357     return true;
358 }
359 
ReadMetadata(camera_metadata_item_t & entry,MessageParcel & data)360 bool UtilsDataStub::ReadMetadata(camera_metadata_item_t &entry, MessageParcel &data)
361 {
362     if (entry.data_type == META_TYPE_BYTE) {
363         std::vector<uint8_t> buffers;
364         data.ReadUInt8Vector(&buffers);
365         entry.data.u8 = new(std::nothrow) uint8_t[entry.count];
366         if (entry.data.u8 != nullptr) {
367             for (size_t i = 0; i < entry.count && i < buffers.size(); i++) {
368                 entry.data.u8[i] = buffers.at(i);
369             }
370         }
371     } else if (entry.data_type == META_TYPE_INT32) {
372         std::vector<int32_t> buffers;
373         data.ReadInt32Vector(&buffers);
374         entry.data.i32 = new(std::nothrow) int32_t[entry.count];
375         if (entry.data.i32 != nullptr) {
376             for (size_t i = 0; i < entry.count && i < buffers.size(); i++) {
377                 entry.data.i32[i] = buffers.at(i);
378             }
379         }
380     } else if (entry.data_type == META_TYPE_FLOAT) {
381         std::vector<float> buffers;
382         data.ReadFloatVector(&buffers);
383         entry.data.f = new(std::nothrow) float[entry.count];
384         if (entry.data.f != nullptr) {
385             for (size_t i = 0; i < entry.count && i < buffers.size(); i++) {
386                 entry.data.f[i] = buffers.at(i);
387             }
388         }
389     } else if (entry.data_type == META_TYPE_INT64) {
390         std::vector<int64_t> buffers;
391         data.ReadInt64Vector(&buffers);
392         entry.data.i64 = new(std::nothrow) int64_t[entry.count];
393         if (entry.data.i64 != nullptr) {
394             for (size_t i = 0; i < entry.count && i < buffers.size(); i++) {
395                 entry.data.i64[i] = buffers.at(i);
396             }
397         }
398     } else if (entry.data_type == META_TYPE_DOUBLE) {
399         std::vector<double> buffers;
400         data.ReadDoubleVector(&buffers);
401         entry.data.d = new(std::nothrow) double[entry.count];
402         if (entry.data.d != nullptr) {
403             for (size_t i = 0; i < entry.count && i < buffers.size(); i++) {
404                 entry.data.d[i] = buffers.at(i);
405             }
406         }
407     } else if (entry.data_type == META_TYPE_RATIONAL) {
408         std::vector<int32_t> buffers;
409         data.ReadInt32Vector(&buffers);
410         entry.data.r = new(std::nothrow) camera_rational_t[entry.count];
411         if (entry.data.r != nullptr) {
412             for (size_t i = 0, j = 0;
413                 i < entry.count && j < static_cast<size_t>(buffers.size() - 1);
414                 i++, j += 2) { // 2:Take two elements from the buffer vector container
415                 entry.data.r[i].numerator = buffers.at(j);
416                 entry.data.r[i].denominator = buffers.at(j + 1);
417             }
418         }
419     }
420     return true;
421 }
422 
EntryDataToBuffer(const camera_metadata_item_t & entry,void ** buffer)423 void UtilsDataStub::EntryDataToBuffer(const camera_metadata_item_t &entry, void **buffer)
424 {
425     if (entry.data_type == META_TYPE_BYTE) {
426         *buffer = static_cast<void*>(entry.data.u8);
427     } else if (entry.data_type == META_TYPE_INT32) {
428         *buffer = static_cast<void*>(entry.data.i32);
429     } else if (entry.data_type == META_TYPE_FLOAT) {
430         *buffer = static_cast<void*>(entry.data.f);
431     } else if (entry.data_type == META_TYPE_INT64) {
432         *buffer = static_cast<void*>(entry.data.i64);
433     } else if (entry.data_type == META_TYPE_DOUBLE) {
434         *buffer = static_cast<void*>(entry.data.d);
435     } else if (entry.data_type == META_TYPE_RATIONAL) {
436         *buffer = static_cast<void*>(entry.data.r);
437     }
438 }
439 }
440