• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "dmetadata_processor.h"
17 
18 #include "dbuffer_manager.h"
19 #include "dcamera.h"
20 #include "distributed_hardware_log.h"
21 #include "cJSON.h"
22 #include "metadata_utils.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
InitDCameraAbility(const std::string & sinkAbilityInfo)26 DCamRetCode DMetadataProcessor::InitDCameraAbility(const std::string &sinkAbilityInfo)
27 {
28     cJSON *rootValue = cJSON_Parse(sinkAbilityInfo.c_str());
29     CHECK_NULL_RETURN_LOG(rootValue, FAILED, "The sinkAbilityInfo is null.");
30     CHECK_OBJECT_FREE_RETURN(rootValue, FAILED, "The sinkAbilityInfo is not object.");
31     cJSON *metaObj = cJSON_GetObjectItemCaseSensitive(rootValue, "MetaData");
32     if (metaObj == nullptr || !cJSON_IsString(metaObj) || (metaObj->valuestring == nullptr)) {
33         cJSON_Delete(rootValue);
34         return FAILED;
35     }
36     std::string metadataStr = std::string(metaObj->valuestring);
37     if (!metadataStr.empty()) {
38         std::hash<std::string> h;
39         DHLOGI("Decode distributed camera metadata from base64, hash: %{public}zu, length: %{public}zu",
40             h(metadataStr), metadataStr.length());
41         std::string decodeString = Base64Decode(metadataStr);
42         DHLOGI("Decode distributed camera metadata from string, hash: %{public}zu, length: %{public}zu",
43             h(decodeString), decodeString.length());
44         dCameraAbility_ = OHOS::Camera::MetadataUtils::DecodeFromString(decodeString);
45         DHLOGI("Decode distributed camera metadata from string success.");
46     }
47 
48     if (dCameraAbility_ == nullptr) {
49         DHLOGE("Metadata is null in ability set or failed to decode metadata ability from string.");
50         dCameraAbility_ = std::make_shared<CameraAbility>(DEFAULT_ENTRY_CAPACITY, DEFAULT_DATA_CAPACITY);
51     }
52 
53     if (OHOS::Camera::GetCameraMetadataItemCount(dCameraAbility_->get()) <= 0) {
54         DCamRetCode ret = InitDCameraDefaultAbilityKeys(sinkAbilityInfo);
55         if (ret != SUCCESS) {
56             DHLOGE("Init distributed camera defalult abilily keys failed.");
57             dCameraAbility_ = nullptr;
58             cJSON_Delete(rootValue);
59             return ret;
60         }
61     }
62     DCamRetCode ret = InitDCameraOutputAbilityKeys(sinkAbilityInfo);
63     if (ret != SUCCESS) {
64         DHLOGE("Init distributed camera output abilily keys failed.");
65         dCameraAbility_ = nullptr;
66         cJSON_Delete(rootValue);
67         return ret;
68     }
69 
70     camera_metadata_item_entry_t* itemEntry = OHOS::Camera::GetMetadataItems(dCameraAbility_->get());
71     CHECK_AND_FREE_RETURN_RET_LOG(itemEntry == nullptr, FAILED, rootValue, "get itemEntry failed.");
72     uint32_t count = dCameraAbility_->get()->item_count;
73     for (uint32_t i = 0; i < count; i++, itemEntry++) {
74         allResultSet_.insert((MetaType)(itemEntry->item));
75     }
76     cJSON_Delete(rootValue);
77     return SUCCESS;
78 }
79 
InitDcameraBaseAbility()80 void DMetadataProcessor::InitDcameraBaseAbility()
81 {
82     const uint8_t cameraType = OHOS_CAMERA_TYPE_LOGICAL;
83     AddAbilityEntry(OHOS_ABILITY_CAMERA_TYPE, &cameraType, 1);
84 
85     const int64_t exposureTime = 0xFFFFFFFFFFFFFFFF;
86     AddAbilityEntry(OHOS_SENSOR_EXPOSURE_TIME, &exposureTime, 1);
87 
88     const float correctionGain = 0.0;
89     AddAbilityEntry(OHOS_SENSOR_COLOR_CORRECTION_GAINS, &correctionGain, 1);
90 
91     const uint8_t faceDetectMode = OHOS_CAMERA_FACE_DETECT_MODE_OFF;
92     AddAbilityEntry(OHOS_STATISTICS_FACE_DETECT_MODE, &faceDetectMode, 1);
93 
94     const uint8_t histogramMode = OHOS_CAMERA_HISTOGRAM_MODE_OFF;
95     AddAbilityEntry(OHOS_STATISTICS_HISTOGRAM_MODE, &histogramMode, 1);
96 
97     const uint8_t aeAntibandingMode = OHOS_CAMERA_AE_ANTIBANDING_MODE_OFF;
98     AddAbilityEntry(OHOS_CONTROL_AE_ANTIBANDING_MODE, &aeAntibandingMode, 1);
99 
100     int32_t aeExposureCompensation = 0xFFFFFFFF;
101     AddAbilityEntry(OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, &aeExposureCompensation, 1);
102 
103     const uint8_t aeLock = OHOS_CAMERA_AE_LOCK_OFF;
104     AddAbilityEntry(OHOS_CONTROL_AE_LOCK, &aeLock, 1);
105 
106     const uint8_t aeMode = OHOS_CAMERA_AE_MODE_OFF;
107     AddAbilityEntry(OHOS_CONTROL_AE_MODE, &aeMode, 1);
108 
109     const uint8_t afMode = OHOS_CAMERA_AF_MODE_OFF;
110     AddAbilityEntry(OHOS_CONTROL_AF_MODE, &afMode, 1);
111 
112     const uint8_t awbLock = OHOS_CAMERA_AWB_LOCK_OFF;
113     AddAbilityEntry(OHOS_CONTROL_AWB_LOCK, &awbLock, 1);
114 
115     const uint8_t awbMode = OHOS_CAMERA_AWB_MODE_OFF;
116     AddAbilityEntry(OHOS_CONTROL_AWB_MODE, &awbMode, 1);
117 
118     const uint8_t aeAntibandingModes = OHOS_CAMERA_AE_ANTIBANDING_MODE_AUTO;
119     AddAbilityEntry(OHOS_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES, &aeAntibandingModes, 1);
120 
121     const uint8_t aeAvailableModes = OHOS_CAMERA_AE_MODE_ON;
122     AddAbilityEntry(OHOS_CONTROL_AE_AVAILABLE_MODES, &aeAvailableModes, 1);
123 
124     const int32_t compensationRange[] = { 0, 0 };
125     AddAbilityEntry(OHOS_ABILITY_AE_COMPENSATION_RANGE, compensationRange,
126         (sizeof(compensationRange) / sizeof(compensationRange[0])));
127 
128     const camera_rational_t compensationStep[] = { { 0, 1 } };
129     AddAbilityEntry(OHOS_ABILITY_AE_COMPENSATION_STEP, compensationStep,
130         (sizeof(compensationStep) / sizeof(compensationStep[0])));
131 
132     const uint8_t afAvailableModes[] = { OHOS_CAMERA_AF_MODE_AUTO, OHOS_CAMERA_AF_MODE_OFF };
133     AddAbilityEntry(OHOS_CONTROL_AF_AVAILABLE_MODES, afAvailableModes,
134         (sizeof(afAvailableModes) / sizeof(afAvailableModes[0])));
135 
136     const uint8_t awbAvailableModes = OHOS_CAMERA_AWB_MODE_AUTO;
137     AddAbilityEntry(OHOS_CONTROL_AWB_AVAILABLE_MODES, &awbAvailableModes, 1);
138 
139     const uint8_t deviceExposureMode = OHOS_CAMERA_EXPOSURE_MODE_CONTINUOUS_AUTO;
140     AddAbilityEntry(OHOS_ABILITY_DEVICE_AVAILABLE_EXPOSUREMODES, &deviceExposureMode, 1);
141 
142     const uint8_t controlExposureMode = OHOS_CAMERA_EXPOSURE_MODE_CONTINUOUS_AUTO;
143     AddAbilityEntry(OHOS_CONTROL_EXPOSUREMODE, &controlExposureMode, 1);
144 
145     const uint8_t deviceFocusModes = OHOS_CAMERA_FOCUS_MODE_AUTO;
146     AddAbilityEntry(OHOS_ABILITY_DEVICE_AVAILABLE_FOCUSMODES, &deviceFocusModes, 1);
147     SetFpsRanges();
148 }
149 
SetFpsRanges()150 void DMetadataProcessor::SetFpsRanges()
151 {
152     std::vector<int32_t> fpsRanges;
153     fpsRanges.push_back(MIN_SUPPORT_DEFAULT_FPS);
154     fpsRanges.push_back(MAX_SUPPORT_DEFAULT_FPS);
155     AddAbilityEntry(OHOS_CONTROL_AE_TARGET_FPS_RANGE, fpsRanges.data(), fpsRanges.size());
156     AddAbilityEntry(OHOS_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, fpsRanges.data(), fpsRanges.size());
157 }
158 
GetInfoFromJson(const std::string & sinkAbilityInfo)159 bool DMetadataProcessor::GetInfoFromJson(const std::string& sinkAbilityInfo)
160 {
161     cJSON *rootValue = cJSON_Parse(sinkAbilityInfo.c_str());
162     CHECK_NULL_RETURN_LOG(rootValue, false, "The sinkAbilityInfo is null.");
163     CHECK_OBJECT_FREE_RETURN(rootValue, false, "The sinkAbilityInfo is not object.");
164     cJSON *verObj = cJSON_GetObjectItemCaseSensitive(rootValue, "ProtocolVer");
165     if (verObj == nullptr || !cJSON_IsString(verObj) || (verObj->valuestring == nullptr)) {
166         cJSON_Delete(rootValue);
167         return false;
168     }
169     protocolVersion_ = std::string(verObj->valuestring);
170 
171     cJSON *positionObj = cJSON_GetObjectItemCaseSensitive(rootValue, "Position");
172     if (positionObj == nullptr || !cJSON_IsString(positionObj) || (positionObj->valuestring == nullptr)) {
173         cJSON_Delete(rootValue);
174         return false;
175     }
176     dCameraPosition_ = std::string(positionObj->valuestring);
177     cJSON_Delete(rootValue);
178     return true;
179 }
180 
InitDCameraDefaultAbilityKeys(const std::string & sinkAbilityInfo)181 DCamRetCode DMetadataProcessor::InitDCameraDefaultAbilityKeys(const std::string &sinkAbilityInfo)
182 {
183     if (!GetInfoFromJson(sinkAbilityInfo)) {
184         return FAILED;
185     }
186     if (dCameraPosition_ == "BACK") {
187         const uint8_t position = OHOS_CAMERA_POSITION_BACK;
188         AddAbilityEntry(OHOS_ABILITY_CAMERA_POSITION, &position, 1);
189     } else if (dCameraPosition_ == "FRONT") {
190         const uint8_t position = OHOS_CAMERA_POSITION_FRONT;
191         AddAbilityEntry(OHOS_ABILITY_CAMERA_POSITION, &position, 1);
192     } else {
193         const uint8_t position = OHOS_CAMERA_POSITION_OTHER;
194         AddAbilityEntry(OHOS_ABILITY_CAMERA_POSITION, &position, 1);
195     }
196 
197     InitDcameraBaseAbility();
198 
199     const uint8_t controlFocusMode = OHOS_CAMERA_FOCUS_MODE_AUTO;
200     AddAbilityEntry(OHOS_CONTROL_FOCUSMODE, &controlFocusMode, 1);
201 
202     const uint8_t deviceFlashModes = OHOS_CAMERA_FLASH_MODE_AUTO;
203     AddAbilityEntry(OHOS_ABILITY_DEVICE_AVAILABLE_FLASHMODES, &deviceFlashModes, 1);
204 
205     const uint8_t controlFlashMode = OHOS_CAMERA_FLASH_MODE_CLOSE;
206     AddAbilityEntry(OHOS_CONTROL_FLASHMODE, &controlFlashMode, 1);
207 
208     float zoomRatioRange[1] = {1.0};
209     AddAbilityEntry(OHOS_ABILITY_ZOOM_RATIO_RANGE, zoomRatioRange,
210         (sizeof(zoomRatioRange) / sizeof(zoomRatioRange[0])));
211 
212     const float zoomRatio = 1.0;
213     AddAbilityEntry(OHOS_CONTROL_ZOOM_RATIO, &zoomRatio, 1);
214 
215     int32_t activeArraySize[] = {0, 0, static_cast<int32_t>(maxPreviewResolution_.width_),
216         static_cast<int32_t>(maxPreviewResolution_.height_)};
217     AddAbilityEntry(OHOS_SENSOR_INFO_ACTIVE_ARRAY_SIZE, activeArraySize,
218         (sizeof(activeArraySize) / sizeof(activeArraySize[0])));
219 
220     int32_t pixelArraySize[] = {
221         static_cast<int32_t>(maxPreviewResolution_.width_), static_cast<int32_t>(maxPreviewResolution_.height_)
222     };
223     AddAbilityEntry(OHOS_SENSOR_INFO_PIXEL_ARRAY_SIZE, pixelArraySize,
224         (sizeof(pixelArraySize) / sizeof(pixelArraySize[0])));
225 
226     const int32_t jpegThumbnailSizes[] = {0, 0, DEGREE_240, DEGREE_180};
227     AddAbilityEntry(OHOS_JPEG_AVAILABLE_THUMBNAIL_SIZES, jpegThumbnailSizes,
228         (sizeof(jpegThumbnailSizes) / sizeof(jpegThumbnailSizes[0])));
229     return SUCCESS;
230 }
231 
InitOutputAbilityWithoutMode(const std::string & sinkAbilityInfo)232 void DMetadataProcessor::InitOutputAbilityWithoutMode(const std::string &sinkAbilityInfo)
233 {
234     DHLOGI("InitOutputAbilityWithoutMode enter.");
235     std::map<int, std::vector<DCResolution>> supportedFormats = GetDCameraSupportedFormats(sinkAbilityInfo);
236 
237     std::vector<int32_t> streamConfigs;
238     std::vector<int32_t> extendStreamConfigs;
239     for (uint32_t i = 0; i < ADD_MODE; i++) { // Compatible camera framework modification
240         camera_metadata_item_t item;
241         int32_t ret = OHOS::Camera::FindCameraMetadataItem(dCameraAbility_->get(),
242             OHOS_ABILITY_STREAM_AVAILABLE_EXTEND_CONFIGURATIONS, &item);
243         if (ret == CAM_META_SUCCESS && item.count != 0) {
244             extendStreamConfigs.push_back(i);
245         }
246         InitBasicConfigTag(supportedFormats, streamConfigs);
247         InitExtendConfigTag(supportedFormats, extendStreamConfigs);
248         extendStreamConfigs.push_back(EXTEND_EOF); // mode eof
249     }
250 
251     UpdateAbilityTag(streamConfigs, extendStreamConfigs);
252 }
253 
InitDCameraOutputAbilityKeys(const std::string & sinkAbilityInfo)254 DCamRetCode DMetadataProcessor::InitDCameraOutputAbilityKeys(const std::string &sinkAbilityInfo)
255 {
256     cJSON *rootValue = cJSON_Parse(sinkAbilityInfo.c_str());
257     CHECK_NULL_RETURN_LOG(rootValue, FAILED, "The sinkAbilityInfo is null.");
258     CHECK_OBJECT_FREE_RETURN(rootValue, FAILED, "The sinkAbilityInfo is not object.");
259 
260     cJSON *modeArray = cJSON_GetObjectItemCaseSensitive(rootValue, CAMERA_SUPPORT_MODE.c_str());
261     if (modeArray == nullptr || !cJSON_IsArray(modeArray)) {
262         InitOutputAbilityWithoutMode(sinkAbilityInfo);
263         cJSON_Delete(rootValue);
264         return SUCCESS;
265     }
266     CHECK_AND_FREE_RETURN_RET_LOG(cJSON_GetArraySize(modeArray) == 0 || static_cast<uint32_t>(
267         cJSON_GetArraySize(modeArray)) > JSON_ARRAY_MAX_SIZE, FAILED, rootValue, "modeArray create error.");
268 
269     std::vector<std::string> keys;
270     int32_t arraySize = cJSON_GetArraySize(modeArray);
271     for (int32_t i = 0; i < arraySize; ++i) {
272         cJSON *number = cJSON_GetArrayItem(modeArray, i);
273         if (number != nullptr && cJSON_IsNumber(number)) {
274             keys.push_back(std::to_string(number->valueint));
275         }
276     }
277     std::vector<int32_t> streamConfigs;
278     std::vector<int32_t> extendStreamConfigs;
279     for (std::string key : keys) {
280         cJSON *value = cJSON_GetObjectItem(rootValue, key.c_str());
281         CHECK_AND_FREE_RETURN_RET_LOG(value == nullptr || !cJSON_IsObject(value), FAILED, rootValue, "mode get error.");
282 
283         char *jsonValue = cJSON_Print(value);
284         CHECK_AND_FREE_RETURN_RET_LOG(jsonValue == nullptr, FAILED, rootValue, "cJson print value error.");
285         std::string format(jsonValue);
286         DHLOGI("the current mode :%{public}s. value :%{public}s", key.c_str(), format.c_str());
287         std::map<int, std::vector<DCResolution>> supportedFormats = GetDCameraSupportedFormats(format);
288 
289         camera_metadata_item_t item;
290         int32_t ret = OHOS::Camera::FindCameraMetadataItem(dCameraAbility_->get(),
291             OHOS_ABILITY_STREAM_AVAILABLE_EXTEND_CONFIGURATIONS, &item);
292         if (ret == CAM_META_SUCCESS && item.count != 0) {
293             extendStreamConfigs.push_back(std::stoi(key)); // mode
294         }
295 
296         InitBasicConfigTag(supportedFormats, streamConfigs);
297         InitExtendConfigTag(supportedFormats, extendStreamConfigs);
298         extendStreamConfigs.push_back(EXTEND_EOF); // mode eof
299 
300         cJSON_free(jsonValue);
301         sinkPhotoProfiles_.clear();
302         sinkPreviewProfiles_.clear();
303         sinkVideoProfiles_.clear();
304     }
305     UpdateAbilityTag(streamConfigs, extendStreamConfigs);
306 
307     cJSON_Delete(rootValue);
308     return SUCCESS;
309 }
310 
UpdateAbilityTag(std::vector<int32_t> & streamConfigs,std::vector<int32_t> & extendStreamConfigs)311 void DMetadataProcessor::UpdateAbilityTag(std::vector<int32_t> &streamConfigs,
312     std::vector<int32_t> &extendStreamConfigs)
313 {
314     UpdateAbilityEntry(OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS, streamConfigs.data(),
315         streamConfigs.size());
316 
317     UpdateAbilityEntry(OHOS_ABILITY_STREAM_AVAILABLE_EXTEND_CONFIGURATIONS, extendStreamConfigs.data(),
318         extendStreamConfigs.size());
319 
320     UpdateAbilityEntry(OHOS_SENSOR_INFO_MAX_FRAME_DURATION, &MAX_FRAME_DURATION, 1);
321 
322     const int32_t jpegMaxSize = maxPhotoResolution_.width_ * maxPhotoResolution_.height_;
323     UpdateAbilityEntry(OHOS_JPEG_MAX_SIZE, &jpegMaxSize, 1);
324 
325     const uint8_t connectionType = OHOS_CAMERA_CONNECTION_TYPE_REMOTE;
326     UpdateAbilityEntry(OHOS_ABILITY_CAMERA_CONNECTION_TYPE, &connectionType, 1);
327 }
328 
InitBasicConfigTag(std::map<int,std::vector<DCResolution>> & supportedFormats,std::vector<int32_t> & streamConfigs)329 void DMetadataProcessor::InitBasicConfigTag(std::map<int, std::vector<DCResolution>> &supportedFormats,
330     std::vector<int32_t> &streamConfigs)
331 {
332     std::map<int, std::vector<DCResolution>>::iterator iter;
333     for (iter = supportedFormats.begin(); iter != supportedFormats.end(); ++iter) {
334         std::vector<DCResolution> resolutionList = iter->second;
335         for (auto resolution : resolutionList) {
336             DHLOGI("DMetadataProcessor::sink supported formats: { format=%{public}d, width=%{public}d, height="
337                 "%{public}d }", iter->first, resolution.width_, resolution.height_);
338             streamConfigs.push_back(iter->first);
339             streamConfigs.push_back(resolution.width_);
340             streamConfigs.push_back(resolution.height_);
341         }
342     }
343 }
344 
InitExtendConfigTag(std::map<int,std::vector<DCResolution>> & supportedFormats,std::vector<int32_t> & extendStreamConfigs)345 void DMetadataProcessor::InitExtendConfigTag(std::map<int, std::vector<DCResolution>> &supportedFormats,
346     std::vector<int32_t> &extendStreamConfigs)
347 {
348     extendStreamConfigs.push_back(EXTEND_PREVIEW); // preview
349     std::map<int, std::vector<DCResolution>>::iterator previewIter;
350     for (previewIter = sinkPreviewProfiles_.begin(); previewIter != sinkPreviewProfiles_.end(); ++previewIter) {
351         std::vector<DCResolution> resolutionList = previewIter->second;
352         for (auto resolution : resolutionList) {
353             DHLOGI("sink extend supported preview formats: { format=%{public}d, width=%{public}d, height=%{public}d }",
354                 previewIter->first, resolution.width_, resolution.height_);
355             AddConfigs(extendStreamConfigs, previewIter->first, resolution.width_, resolution.height_, PREVIEW_FPS);
356         }
357     }
358     extendStreamConfigs.push_back(EXTEND_EOF); // preview eof
359 
360     extendStreamConfigs.push_back(EXTEND_VIDEO); // video
361     std::map<int, std::vector<DCResolution>>::iterator videoIter;
362     for (videoIter = sinkVideoProfiles_.begin(); videoIter != sinkVideoProfiles_.end(); ++videoIter) {
363         std::vector<DCResolution> resolutionList = videoIter->second;
364         for (auto resolution : resolutionList) {
365             DHLOGI("sink extend supported video formats: { format=%{public}d, width=%{public}d, height=%{public}d }",
366                 videoIter->first, resolution.width_, resolution.height_);
367             AddConfigs(extendStreamConfigs, videoIter->first, resolution.width_, resolution.height_, VIDEO_FPS);
368         }
369     }
370     extendStreamConfigs.push_back(EXTEND_EOF); // video eof
371 
372     if (!sinkPhotoProfiles_.empty()) {
373         extendStreamConfigs.push_back(EXTEND_PHOTO); // photo
374         std::map<int, std::vector<DCResolution>>::iterator photoIter;
375         for (photoIter = sinkPhotoProfiles_.begin(); photoIter != sinkPhotoProfiles_.end(); ++photoIter) {
376             std::vector<DCResolution> resolutionList = photoIter->second;
377             for (auto resolution : resolutionList) {
378                 DHLOGI("sink extend supported photo formats: {format=%{public}d, width=%{public}d, height=%{public}d}",
379                     photoIter->first, resolution.width_, resolution.height_);
380                 AddConfigs(extendStreamConfigs, photoIter->first, resolution.width_, resolution.height_, PHOTO_FPS);
381             }
382         }
383         extendStreamConfigs.push_back(EXTEND_EOF); // photo eof
384     }
385 }
386 
AddConfigs(std::vector<int32_t> & sinkExtendStreamConfigs,int32_t format,int32_t width,int32_t height,int32_t fps)387 void DMetadataProcessor::AddConfigs(std::vector<int32_t> &sinkExtendStreamConfigs, int32_t format,
388     int32_t width, int32_t height, int32_t fps)
389 {
390     sinkExtendStreamConfigs.push_back(format);
391     sinkExtendStreamConfigs.push_back(width);
392     sinkExtendStreamConfigs.push_back(height);
393     sinkExtendStreamConfigs.push_back(fps); // fixedfps
394     sinkExtendStreamConfigs.push_back(fps); // minfps
395     sinkExtendStreamConfigs.push_back(fps); // maxfps
396     sinkExtendStreamConfigs.push_back(EXTEND_EOF); // eof
397 }
398 
AddAbilityEntry(uint32_t tag,const void * data,size_t size)399 DCamRetCode DMetadataProcessor::AddAbilityEntry(uint32_t tag, const void *data, size_t size)
400 {
401     if (dCameraAbility_ == nullptr) {
402         DHLOGE("Distributed camera abilily is null.");
403         return DCamRetCode::INVALID_ARGUMENT;
404     }
405 
406     camera_metadata_item_t item;
407     int ret = OHOS::Camera::FindCameraMetadataItem(dCameraAbility_->get(), tag, &item);
408     if (ret != CAM_META_SUCCESS) {
409         if (!dCameraAbility_->addEntry(tag, data, size)) {
410             DHLOGE("Add tag %{public}u failed.", tag);
411             return FAILED;
412         }
413     }
414     return SUCCESS;
415 }
416 
UpdateAbilityEntry(uint32_t tag,const void * data,size_t size)417 DCamRetCode DMetadataProcessor::UpdateAbilityEntry(uint32_t tag, const void *data, size_t size)
418 {
419     if (dCameraAbility_ == nullptr) {
420         DHLOGE("Distributed camera abilily is null.");
421         return DCamRetCode::INVALID_ARGUMENT;
422     }
423 
424     camera_metadata_item_t item;
425     int ret = OHOS::Camera::FindCameraMetadataItem(dCameraAbility_->get(), tag, &item);
426     if (ret == CAM_META_SUCCESS) {
427         if (!dCameraAbility_->updateEntry(tag, data, size)) {
428             DHLOGE("Update tag %{public}u failed.", tag);
429             return FAILED;
430         }
431     }
432     return SUCCESS;
433 }
434 
GetDCameraAbility(std::shared_ptr<CameraAbility> & ability)435 DCamRetCode DMetadataProcessor::GetDCameraAbility(std::shared_ptr<CameraAbility> &ability)
436 {
437     ability = dCameraAbility_;
438     return SUCCESS;
439 }
440 
SetMetadataResultMode(const ResultCallbackMode & mode)441 DCamRetCode DMetadataProcessor::SetMetadataResultMode(const ResultCallbackMode &mode)
442 {
443     if (mode < ResultCallbackMode::PER_FRAME || mode > ResultCallbackMode::ON_CHANGED) {
444         DHLOGE("Invalid result callback mode.");
445         return DCamRetCode::INVALID_ARGUMENT;
446     }
447     metaResultMode_ = mode;
448     return SUCCESS;
449 }
450 
GetEnabledMetadataResults(std::vector<MetaType> & results)451 DCamRetCode DMetadataProcessor::GetEnabledMetadataResults(std::vector<MetaType> &results)
452 {
453     auto iter = enabledResultSet_.begin();
454     while (iter != enabledResultSet_.end()) {
455         results.push_back(*iter);
456         iter++;
457     }
458     return SUCCESS;
459 }
460 
EnableMetadataResult(const std::vector<MetaType> & results)461 DCamRetCode DMetadataProcessor::EnableMetadataResult(const std::vector<MetaType> &results)
462 {
463     if (results.size() == 0) {
464         DHLOGE("Enable metadata result list is empty.");
465         return SUCCESS;
466     }
467 
468     for (size_t i = 0; i < results.size(); i++) {
469         auto iter = allResultSet_.find(results[i]);
470         if (iter != allResultSet_.end()) {
471             auto anoIter = enabledResultSet_.find(results[i]);
472             if (anoIter == enabledResultSet_.end()) {
473                 enabledResultSet_.insert(results[i]);
474             }
475         } else {
476             DHLOGE("Cannot find match metatype.");
477             return SUCCESS;
478         }
479     }
480     return SUCCESS;
481 }
482 
DisableMetadataResult(const std::vector<MetaType> & results)483 DCamRetCode DMetadataProcessor::DisableMetadataResult(const std::vector<MetaType> &results)
484 {
485     if (results.size() == 0) {
486         DHLOGE("Disable metadata result list is empty.");
487         return SUCCESS;
488     }
489 
490     for (size_t i = 0; i < results.size(); i++) {
491         auto iter = allResultSet_.find(results[i]);
492         if (iter != allResultSet_.end()) {
493             auto anoIter = enabledResultSet_.find(results[i]);
494             if (anoIter != enabledResultSet_.end()) {
495                 enabledResultSet_.erase(*iter);
496             }
497         } else {
498             DHLOGE("Cannot find match metatype.");
499             return SUCCESS;
500         }
501     }
502     return SUCCESS;
503 }
504 
ResetEnableResults()505 DCamRetCode DMetadataProcessor::ResetEnableResults()
506 {
507     if (enabledResultSet_.size() < allResultSet_.size()) {
508         for (auto result : allResultSet_) {
509             enabledResultSet_.insert(result);
510         }
511     }
512     return SUCCESS;
513 }
514 
UpdateResultMetadata(const uint64_t & resultTimestamp)515 void DMetadataProcessor::UpdateResultMetadata(const uint64_t &resultTimestamp)
516 {
517     DHLOGD("DMetadataProcessor::UpdateResultMetadata result callback mode: %{public}d", metaResultMode_);
518     if (metaResultMode_ != ResultCallbackMode::PER_FRAME) {
519         return;
520     }
521 
522     std::lock_guard<std::mutex> autoLock(producerMutex_);
523     if (latestProducerMetadataResult_ == nullptr) {
524         DHLOGD("DMetadataProcessor::UpdateResultMetadata latest producer metadata result is null");
525         return;
526     }
527 
528     UpdateAllResult(resultTimestamp);
529 }
530 
SetResultCallback(std::function<void (uint64_t,std::shared_ptr<OHOS::Camera::CameraMetadata>)> & resultCbk)531 void DMetadataProcessor::SetResultCallback(
532     std::function<void(uint64_t, std::shared_ptr<OHOS::Camera::CameraMetadata>)> &resultCbk)
533 {
534     resultCallback_ = resultCbk;
535 }
536 
UpdateAllResult(const uint64_t & resultTimestamp)537 void DMetadataProcessor::UpdateAllResult(const uint64_t &resultTimestamp)
538 {
539     uint32_t itemCap = OHOS::Camera::GetCameraMetadataItemCapacity(latestProducerMetadataResult_->get());
540     uint32_t dataSize = OHOS::Camera::GetCameraMetadataDataSize(latestProducerMetadataResult_->get());
541     DHLOGD("DMetadataProcessor::UpdateAllResult itemCapacity: %{public}u, dataSize: %{public}u", itemCap, dataSize);
542     std::shared_ptr<OHOS::Camera::CameraMetadata> result =
543         std::make_shared<OHOS::Camera::CameraMetadata>(itemCap, dataSize);
544     int32_t ret = OHOS::Camera::CopyCameraMetadataItems(result->get(), latestProducerMetadataResult_->get());
545     if (ret != CAM_META_SUCCESS) {
546         DHLOGE("DMetadataProcessor::UpdateAllResult copy metadata item failed, ret: %{public}d", ret);
547         return;
548     }
549     resultCallback_(resultTimestamp, result);
550 }
551 
UpdateOnChanged(const uint64_t & resultTimestamp)552 void DMetadataProcessor::UpdateOnChanged(const uint64_t &resultTimestamp)
553 {
554     bool needReturn = false;
555     uint32_t itemCap = OHOS::Camera::GetCameraMetadataItemCapacity(latestProducerMetadataResult_->get());
556     uint32_t dataSize = OHOS::Camera::GetCameraMetadataDataSize(latestProducerMetadataResult_->get());
557     DHLOGD("DMetadataProcessor::UpdateOnChanged itemCapacity: %{public}u, dataSize: %{public}u", itemCap, dataSize);
558     std::shared_ptr<OHOS::Camera::CameraMetadata> result =
559         std::make_shared<OHOS::Camera::CameraMetadata>(itemCap, dataSize);
560     DHLOGD("DMetadataProcessor::UpdateOnChanged enabledResultSet size: %{public}zu", enabledResultSet_.size());
561     for (auto tag : enabledResultSet_) {
562         DHLOGD("DMetadataProcessor::UpdateOnChanged cameta device metadata tag: %{public}d", tag);
563         camera_metadata_item_t item;
564         camera_metadata_item_t anoItem;
565         int ret1 = OHOS::Camera::FindCameraMetadataItem(latestProducerMetadataResult_->get(), tag, &item);
566         int ret2 = OHOS::Camera::FindCameraMetadataItem(latestConsumerMetadataResult_->get(), tag, &anoItem);
567         DHLOGD("DMetadataProcessor::UpdateOnChanged find metadata item ret: %{public}d, %{public}d", ret1, ret2);
568         if (ret1 != CAM_META_SUCCESS) {
569             continue;
570         }
571 
572         if (ret2 == CAM_META_SUCCESS) {
573             if ((item.count != anoItem.count) || (item.data_type != anoItem.data_type)) {
574                 needReturn = true;
575                 result->addEntry(tag, GetMetadataItemData(item), item.count);
576                 continue;
577             }
578             uint32_t size = GetDataSize(item.data_type);
579             DHLOGD("DMetadataProcessor::UpdateOnChanged data size: %{public}u", size);
580             for (uint32_t i = 0; i < (size * static_cast<uint32_t>(item.count)); i++) {
581                 if (*(item.data.u8 + i) != *(anoItem.data.u8 + i)) {
582                     needReturn = true;
583                     result->addEntry(tag, GetMetadataItemData(item), item.count);
584                     break;
585                 }
586             }
587         } else {
588             needReturn = true;
589             result->addEntry(tag, GetMetadataItemData(item), item.count);
590             continue;
591         }
592     }
593 
594     if (needReturn) {
595         resultCallback_(resultTimestamp, result);
596     }
597 }
598 
SaveResultMetadata(std::string resultStr)599 DCamRetCode DMetadataProcessor::SaveResultMetadata(std::string resultStr)
600 {
601     if (resultStr.empty()) {
602         DHLOGE("Input result string is null.");
603         return DCamRetCode::INVALID_ARGUMENT;
604     }
605 
606     std::string metadataStr = Base64Decode(resultStr);
607     std::lock_guard<std::mutex> autoLock(producerMutex_);
608     latestConsumerMetadataResult_ = latestProducerMetadataResult_;
609     latestProducerMetadataResult_ = OHOS::Camera::MetadataUtils::DecodeFromString(metadataStr);
610     if (latestProducerMetadataResult_ == nullptr) {
611         DHLOGE("Failed to decode metadata setting from string.");
612         return DCamRetCode::INVALID_ARGUMENT;
613     }
614 
615     if (!OHOS::Camera::GetCameraMetadataItemCount(latestProducerMetadataResult_->get())) {
616         DHLOGE("Input result metadata item is empty.");
617         return DCamRetCode::INVALID_ARGUMENT;
618     }
619 
620     DHLOGD("DMetadataProcessor::SaveResultMetadata result callback mode: %{public}d", metaResultMode_);
621     if (metaResultMode_ != ResultCallbackMode::ON_CHANGED) {
622         return SUCCESS;
623     }
624 
625     uint64_t resultTimestamp = GetCurrentLocalTimeStamp();
626     if (latestConsumerMetadataResult_ == nullptr) {
627         UpdateAllResult(resultTimestamp);
628         return SUCCESS;
629     }
630 
631     camera_metadata_item_entry_t* itemEntry = OHOS::Camera::GetMetadataItems(latestProducerMetadataResult_->get());
632     uint32_t count = latestProducerMetadataResult_->get()->item_count;
633     for (uint32_t i = 0; i < count; i++, itemEntry++) {
634         enabledResultSet_.insert((MetaType)(itemEntry->item));
635     }
636     UpdateOnChanged(resultTimestamp);
637     return SUCCESS;
638 }
639 
ConvertToCameraMetadata(common_metadata_header_t * & input,std::shared_ptr<OHOS::Camera::CameraMetadata> & output)640 void DMetadataProcessor::ConvertToCameraMetadata(common_metadata_header_t *&input,
641     std::shared_ptr<OHOS::Camera::CameraMetadata> &output)
642 {
643     CHECK_AND_RETURN_LOG(output == nullptr, "output is nullptr");
644     auto ret = OHOS::Camera::CopyCameraMetadataItems(output->get(), input);
645     if (ret != CAM_META_SUCCESS) {
646         DHLOGE("Failed to copy the old metadata to new metadata.");
647         output = nullptr;
648     }
649 }
650 
ResizeMetadataHeader(common_metadata_header_t * & header,uint32_t itemCapacity,uint32_t dataCapacity)651 void DMetadataProcessor::ResizeMetadataHeader(common_metadata_header_t *&header,
652     uint32_t itemCapacity, uint32_t dataCapacity)
653 {
654     if (header) {
655         OHOS::Camera::FreeCameraMetadataBuffer(header);
656     }
657     header = OHOS::Camera::AllocateCameraMetadataBuffer(itemCapacity, dataCapacity);
658 }
659 
GetDataSize(uint32_t type)660 uint32_t DMetadataProcessor::GetDataSize(uint32_t type)
661 {
662     uint32_t size = 0;
663     if (type == META_TYPE_BYTE) {
664         size = sizeof(uint8_t);
665     } else if (type == META_TYPE_INT32) {
666         size = sizeof(int32_t);
667     } else if (type == META_TYPE_UINT32) {
668         size = sizeof(uint32_t);
669     } else if (type == META_TYPE_FLOAT) {
670         size = sizeof(float);
671     } else if (type == META_TYPE_INT64) {
672         size = sizeof(int64_t);
673     } else if (type == META_TYPE_DOUBLE) {
674         size = sizeof(double);
675     } else if (type == META_TYPE_RATIONAL) {
676         size = sizeof(camera_rational_t);
677     } else {
678         size = 0;
679     }
680     return size;
681 }
682 
GetMetadataItemData(const camera_metadata_item_t & item)683 void* DMetadataProcessor::GetMetadataItemData(const camera_metadata_item_t &item)
684 {
685     switch (item.data_type) {
686         case META_TYPE_BYTE: {
687             return item.data.u8;
688         }
689         case META_TYPE_INT32: {
690             return item.data.i32;
691         }
692         case META_TYPE_UINT32: {
693             return item.data.ui32;
694         }
695         case META_TYPE_FLOAT: {
696             return item.data.f;
697         }
698         case META_TYPE_INT64: {
699             return item.data.i64;
700         }
701         case META_TYPE_DOUBLE: {
702             return item.data.d;
703         }
704         case META_TYPE_RATIONAL: {
705             return item.data.r;
706         }
707         default: {
708             DHLOGE("DMetadataProcessor::GetMetadataItemData invalid data type: %{public}u", item.data_type);
709             return nullptr;
710         }
711     }
712 }
713 
GetFormatObj(const std::string rootNode,cJSON * rootValue,std::string & formatStr)714 cJSON* DMetadataProcessor::GetFormatObj(const std::string rootNode, cJSON* rootValue, std::string& formatStr)
715 {
716     cJSON* nodeObj = cJSON_GetObjectItemCaseSensitive(rootValue, rootNode.c_str());
717     if (nodeObj == nullptr || !cJSON_IsObject(nodeObj)) {
718         return nullptr;
719     }
720 
721     cJSON* resObj = cJSON_GetObjectItemCaseSensitive(nodeObj, "Resolution");
722     if (resObj == nullptr || !cJSON_IsObject(resObj)) {
723         return nullptr;
724     }
725     cJSON *formatObj = cJSON_GetObjectItemCaseSensitive(resObj, formatStr.c_str());
726     if (formatObj == nullptr || !cJSON_IsArray(formatObj) || cJSON_GetArraySize(formatObj) == 0 ||
727         static_cast<uint32_t>(cJSON_GetArraySize(formatObj)) > JSON_ARRAY_MAX_SIZE) {
728         return nullptr;
729     }
730     return formatObj;
731 }
732 
GetEachNodeSupportedResolution(std::vector<int> & formats,const std::string rootNode,std::map<int,std::vector<DCResolution>> & supportedFormats,cJSON * rootValue)733 void DMetadataProcessor::GetEachNodeSupportedResolution(std::vector<int>& formats, const std::string rootNode,
734     std::map<int, std::vector<DCResolution>>& supportedFormats, cJSON* rootValue)
735 {
736     for (const auto &format : formats) {
737         std::string formatStr = std::to_string(format);
738         cJSON *formatObj = GetFormatObj(rootNode, rootValue, formatStr);
739         if (formatObj == nullptr) {
740             DHLOGE("Resolution or %{public}s error.", formatStr.c_str());
741             continue;
742         }
743         GetNodeSupportedResolution(format, rootNode, supportedFormats, rootValue);
744     }
745 }
746 
GetNodeSupportedResolution(int format,const std::string rootNode,std::map<int,std::vector<DCResolution>> & supportedFormats,cJSON * rootValue)747 void DMetadataProcessor::GetNodeSupportedResolution(int format, const std::string rootNode,
748     std::map<int, std::vector<DCResolution>>& supportedFormats, cJSON* rootValue)
749 {
750     std::vector<DCResolution> resolutionVec;
751     std::string formatStr = std::to_string(format);
752     cJSON* formatObj = GetFormatObj(rootNode, rootValue, formatStr);
753     if (formatObj == nullptr) {
754         return;
755     }
756     int32_t size = cJSON_GetArraySize(formatObj);
757     for (int32_t i = 0; i < size; i++) {
758         cJSON *item = cJSON_GetArrayItem(formatObj, i);
759         if (item == nullptr || !cJSON_IsString(item)) {
760             DHLOGE("Resolution %{public}s %{public}d ,is not string.", formatStr.c_str(), i);
761             continue;
762         }
763         std::string resoStr = std::string(item->valuestring);
764         std::vector<std::string> reso;
765         SplitString(resoStr, reso, STAR_SEPARATOR);
766         if (reso.size() != SIZE_FMT_LEN) {
767             continue;
768         }
769         uint32_t width = static_cast<uint32_t>(std::stoi(reso[0]));
770         uint32_t height = static_cast<uint32_t>(std::stoi(reso[1]));
771         if (height == 0 || width == 0 || ((rootNode == "Photo") &&
772             ((width * height) > (MAX_SUPPORT_PHOTO_WIDTH * MAX_SUPPORT_PHOTO_HEIGHT))) ||
773             ((rootNode != "Photo") && (width > MAX_SUPPORT_PREVIEW_WIDTH || height > MAX_SUPPORT_PREVIEW_HEIGHT))) {
774             continue;
775         }
776         DCResolution resolution(width, height);
777         resolutionVec.push_back(resolution);
778     }
779     if (!resolutionVec.empty()) {
780         std::sort(resolutionVec.begin(), resolutionVec.end());
781         supportedFormats[format] = resolutionVec;
782         if ((rootNode != "Photo") && (maxPreviewResolution_ < resolutionVec[0])) {
783             maxPreviewResolution_.width_ = resolutionVec[0].width_;
784             maxPreviewResolution_.height_ = resolutionVec[0].height_;
785         }
786         if ((rootNode == "Photo") && (maxPhotoResolution_ < resolutionVec[0])) {
787             maxPhotoResolution_.width_ = resolutionVec[0].width_;
788             maxPhotoResolution_.height_ = resolutionVec[0].height_;
789         }
790         StoreSinkAndSrcConfig(format, rootNode, resolutionVec);
791     }
792 }
793 
StoreSinkAndSrcConfig(int format,const std::string rootNode,std::vector<DCResolution> & resolutionVec)794 void DMetadataProcessor::StoreSinkAndSrcConfig(int format, const std::string rootNode,
795     std::vector<DCResolution> &resolutionVec)
796 {
797     if (rootNode == "Photo") {
798         sinkPhotoProfiles_[format] = resolutionVec;
799     } else if (rootNode == "Preview") {
800         sinkPreviewProfiles_[format] = resolutionVec;
801     } else if (rootNode == "Video") {
802         sinkVideoProfiles_[format] = resolutionVec;
803     }
804 }
805 
GetDCameraSupportedFormats(const std::string & abilityInfo)806 std::map<int, std::vector<DCResolution>> DMetadataProcessor::GetDCameraSupportedFormats(
807     const std::string &abilityInfo)
808 {
809     std::map<int, std::vector<DCResolution>> supportedFormats;
810     cJSON *rootValue = cJSON_Parse(abilityInfo.c_str());
811     CHECK_NULL_RETURN_LOG(rootValue, supportedFormats, "The sinkAbilityInfo is null.");
812     CHECK_OBJECT_FREE_RETURN(rootValue, supportedFormats, "The sinkAbilityInfo is not object.");
813     ParsePhotoFormats(rootValue, supportedFormats);
814     ParsePreviewFormats(rootValue, supportedFormats);
815     ParseVideoFormats(rootValue, supportedFormats);
816     cJSON_Delete(rootValue);
817     return supportedFormats;
818 }
819 
ParsePhotoFormats(cJSON * rootValue,std::map<int,std::vector<DCResolution>> & supportedFormats)820 void DMetadataProcessor::ParsePhotoFormats(cJSON* rootValue,
821     std::map<int, std::vector<DCResolution>>& supportedFormats)
822 {
823     cJSON *photoObj = cJSON_GetObjectItemCaseSensitive(rootValue, "Photo");
824     if (photoObj == nullptr || !cJSON_IsObject(photoObj)) {
825         DHLOGE("Input Photo info is null.");
826         return;
827     }
828 
829     cJSON *formatObj = cJSON_GetObjectItemCaseSensitive(photoObj, "OutputFormat");
830     if (formatObj == nullptr || !cJSON_IsArray(formatObj) || cJSON_GetArraySize(formatObj) == 0 ||
831         static_cast<uint32_t>(cJSON_GetArraySize(formatObj)) > JSON_ARRAY_MAX_SIZE) {
832         DHLOGE("Photo output format error.");
833         return;
834     }
835 
836     std::vector<int> photoFormats;
837     int32_t size = cJSON_GetArraySize(formatObj);
838     for (int32_t i = 0; i < size; i++) {
839         cJSON *item = cJSON_GetArrayItem(formatObj, i);
840         if (item !=nullptr && cJSON_IsNumber(item)) {
841             photoFormats.push_back(item->valueint);
842         }
843     }
844     sinkPhotoFormats_ = photoFormats;
845     GetEachNodeSupportedResolution(photoFormats, "Photo", supportedFormats, rootValue);
846 }
847 
ParsePreviewFormats(cJSON * rootValue,std::map<int,std::vector<DCResolution>> & supportedFormats)848 void DMetadataProcessor::ParsePreviewFormats(cJSON* rootValue,
849     std::map<int, std::vector<DCResolution>>& supportedFormats)
850 {
851     cJSON *previewObj = cJSON_GetObjectItemCaseSensitive(rootValue, "Preview");
852     if (previewObj == nullptr || !cJSON_IsObject(previewObj)) {
853         DHLOGE("Preview error.");
854         return;
855     }
856     cJSON *formatObj = cJSON_GetObjectItemCaseSensitive(previewObj, "OutputFormat");
857     if (formatObj == nullptr || !cJSON_IsArray(formatObj) || cJSON_GetArraySize(formatObj) == 0 ||
858         static_cast<uint32_t>(cJSON_GetArraySize(formatObj)) > JSON_ARRAY_MAX_SIZE) {
859         DHLOGE("Preview output format error.");
860         return;
861     }
862     std::vector<int> previewFormats;
863     int32_t size = cJSON_GetArraySize(formatObj);
864     for (int32_t i = 0; i < size; i++) {
865         cJSON *item = cJSON_GetArrayItem(formatObj, i);
866         if (item !=nullptr && cJSON_IsNumber(item)) {
867             previewFormats.push_back(item->valueint);
868         }
869     }
870     GetEachNodeSupportedResolution(previewFormats, "Preview", supportedFormats, rootValue);
871 }
872 
ParseVideoFormats(cJSON * rootValue,std::map<int,std::vector<DCResolution>> & supportedFormats)873 void DMetadataProcessor::ParseVideoFormats(cJSON* rootValue,
874     std::map<int, std::vector<DCResolution>>& supportedFormats)
875 {
876     cJSON *videoObj = cJSON_GetObjectItemCaseSensitive(rootValue, "Video");
877     if (videoObj == nullptr || !cJSON_IsObject(videoObj)) {
878         DHLOGE("Video error.");
879         return;
880     }
881     cJSON *formatObj = cJSON_GetObjectItemCaseSensitive(videoObj, "OutputFormat");
882     if (formatObj == nullptr || !cJSON_IsArray(formatObj) || cJSON_GetArraySize(formatObj) == 0 ||
883         static_cast<uint32_t>(cJSON_GetArraySize(formatObj)) > JSON_ARRAY_MAX_SIZE) {
884         DHLOGE("Video output format error.");
885         return;
886     }
887     std::vector<int> videoFormats;
888     int32_t size = cJSON_GetArraySize(formatObj);
889     for (int32_t i = 0; i < size; i++) {
890         cJSON *item = cJSON_GetArrayItem(formatObj, i);
891         if (item !=nullptr && cJSON_IsNumber(item)) {
892             videoFormats.push_back(item->valueint);
893         }
894     }
895     GetEachNodeSupportedResolution(videoFormats, "Video", supportedFormats, rootValue);
896 }
897 
PrintDCameraMetadata(const common_metadata_header_t * metadata)898 void DMetadataProcessor::PrintDCameraMetadata(const common_metadata_header_t *metadata)
899 {
900     if (metadata == nullptr) {
901         DHLOGE("Failed to print metadata, input metadata is null.");
902         return;
903     }
904 
905     uint32_t tagCount = OHOS::Camera::GetCameraMetadataItemCount(metadata);
906     DHLOGD("DMetadataProcessor::PrintDCameraMetadata, input metadata item count = %{public}d.", tagCount);
907     for (uint32_t i = 0; i < tagCount; i++) {
908         camera_metadata_item_t item;
909         int ret = OHOS::Camera::GetCameraMetadataItem(metadata, i, &item);
910         if (ret != 0) {
911             continue;
912         }
913 
914         const char *name = OHOS::Camera::GetCameraMetadataItemName(item.item);
915         if (item.data_type == META_TYPE_BYTE) {
916             for (size_t k = 0; k < item.count; k++) {
917                 DHLOGI("tag index:%d, name:%s, value:%d", item.index, name, (uint8_t)(item.data.u8[k]));
918             }
919         } else if (item.data_type == META_TYPE_INT32) {
920             for (size_t k = 0; k < item.count; k++) {
921                 DHLOGI("tag index:%d, name:%s, value:%d", item.index, name, (int32_t)(item.data.i32[k]));
922             }
923         } else if (item.data_type == META_TYPE_UINT32) {
924             for (size_t k = 0; k < item.count; k++) {
925                 DHLOGI("tag index:%d, name:%s, value:%d", item.index, name, (uint32_t)(item.data.ui32[k]));
926             }
927         } else if (item.data_type == META_TYPE_FLOAT) {
928             for (size_t k = 0; k < item.count; k++) {
929                 DHLOGI("tag index:%d, name:%s, value:%f", item.index, name, (float)(item.data.f[k]));
930             }
931         } else if (item.data_type == META_TYPE_INT64) {
932             for (size_t k = 0; k < item.count; k++) {
933                 DHLOGI("tag index:%d, name:%s, value:%lld", item.index, name, (long long)(item.data.i64[k]));
934             }
935         } else if (item.data_type == META_TYPE_DOUBLE) {
936             for (size_t k = 0; k < item.count; k++) {
937                 DHLOGI("tag index:%d, name:%s, value:%lf", item.index, name, (double)(item.data.d[k]));
938             }
939         } else {
940             DHLOGI("tag index:%d, name:%s", item.index, name);
941         }
942     }
943 }
944 } // namespace DistributedHardware
945 } // namespace OHOS
946