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