diff --git a/distributed_camera/hdi_service/include/dcamera_device/dmetadata_processor.h b/distributed_camera/hdi_service/include/dcamera_device/dmetadata_processor.h index 35655c6f3ab3bde3332306222c68680c2c2e7ada..845c8dbae1fe0cef522e8b874651c7f604870931 100644 --- a/distributed_camera/hdi_service/include/dcamera_device/dmetadata_processor.h +++ b/distributed_camera/hdi_service/include/dcamera_device/dmetadata_processor.h @@ -65,6 +65,9 @@ private: uint32_t GetDataSize(uint32_t type); void* GetMetadataItemData(const camera_metadata_item_t &item); std::map> GetDCameraSupportedFormats(const std::string &abilityInfo); + void ParsePhotoFormats(Json::Value& rootValue, std::map>& supportedFormats); + void ParsePreviewFormats(Json::Value& rootValue, std::map>& supportedFormats); + void ParseVideoFormats(Json::Value& rootValue, std::map>& supportedFormats); void InitDcameraBaseAbility(); void GetEachNodeSupportedResolution(std::vector& formats, const std::string rootNode, std::map>& supportedFormats, Json::Value& rootValue); @@ -73,6 +76,7 @@ private: void SetFpsRanges(); private: + constexpr static uint32_t JSON_ARRAY_MAX_SIZE = 1000; std::function)> resultCallback_; std::shared_ptr dCameraAbility_; std::string protocolVersion_; diff --git a/distributed_camera/hdi_service/include/dstream_operator/dstream_operator.h b/distributed_camera/hdi_service/include/dstream_operator/dstream_operator.h index 880d8af09edf3a966d686091d5365327e9261873..fee4a6187ababfc14da83b5594a7b8c015bc35ec 100644 --- a/distributed_camera/hdi_service/include/dstream_operator/dstream_operator.h +++ b/distributed_camera/hdi_service/include/dstream_operator/dstream_operator.h @@ -59,6 +59,9 @@ public: const sptr &callbackObj, sptr &offlineOperator) override; DCamRetCode InitOutputConfigurations(const DHBase &dhBase, const std::string &abilityInfo); + DCamRetCode ParsePhotoFormats(Json::Value& rootValue); + DCamRetCode ParsePreviewFormats(Json::Value& rootValue); + DCamRetCode ParseVideoFormats(Json::Value& rootValue); DCamRetCode AcquireBuffer(int streamId, DCameraBuffer &buffer); DCamRetCode ShutterBuffer(int streamId, const DCameraBuffer &buffer); DCamRetCode SetCallBack(OHOS::sptr const &callback); @@ -124,6 +127,7 @@ private: int32_t DoCapture(int32_t captureId, const CaptureInfo &info, bool isStreaming); private: + constexpr static uint32_t JSON_ARRAY_MAX_SIZE = 1000; std::shared_ptr dMetadataProcessor_; OHOS::sptr dcStreamOperatorCallback_; function errorCallback_; diff --git a/distributed_camera/hdi_service/src/dcamera_device/dmetadata_processor.cpp b/distributed_camera/hdi_service/src/dcamera_device/dmetadata_processor.cpp index 9adfd9c2f9a2f58326a3633bb518bfe9e4910319..121c21c9b74e0381f9c5b5f511ef646544512b03 100644 --- a/distributed_camera/hdi_service/src/dcamera_device/dmetadata_processor.cpp +++ b/distributed_camera/hdi_service/src/dcamera_device/dmetadata_processor.cpp @@ -574,10 +574,14 @@ void DMetadataProcessor::GetEachNodeSupportedResolution(std::vector& format { for (const auto &format : formats) { std::string formatStr = std::to_string(format); - if (rootValue[rootNode]["Resolution"][formatStr].isArray() && - rootValue[rootNode]["Resolution"][formatStr].size() > 0) { - GetNodeSupportedResolution(format, formatStr, rootNode, supportedFormats, rootValue); + if (!rootValue[rootNode].isMember("Resolution") || !rootValue[rootNode]["Resolution"].isMember(formatStr) || + !rootValue[rootNode]["Resolution"][formatStr].isArray() || + rootValue[rootNode]["Resolution"][formatStr].size() == 0 || + rootValue[rootNode]["Resolution"][formatStr].size() > JSON_ARRAY_MAX_SIZE) { + DHLOGE("Resolution or %s error.", formatStr.c_str()); + continue; } + GetNodeSupportedResolution(format, formatStr, rootNode, supportedFormats, rootValue); } } @@ -587,6 +591,10 @@ void DMetadataProcessor::GetNodeSupportedResolution(int format, std::string form std::vector resolutionVec; uint32_t size = rootValue[rootNode]["Resolution"][formatStr].size(); for (uint32_t i = 0; i < size; i++) { + if (!rootValue[rootNode]["Resolution"][formatStr][i].isString()) { + DHLOGE("Resolution %s %d ,is not string.", formatStr.c_str(), i); + continue; + } std::string resoStr = rootValue[rootNode]["Resolution"][formatStr][i].asString(); std::vector reso; SplitString(resoStr, reso, STAR_SEPARATOR); @@ -631,38 +639,67 @@ std::map> DMetadataProcessor::GetDCameraSupported !rootValue.isObject()) { return supportedFormats; } + ParsePhotoFormats(rootValue, supportedFormats); + ParsePreviewFormats(rootValue, supportedFormats); + ParseVideoFormats(rootValue, supportedFormats); + return supportedFormats; +} +void DMetadataProcessor::ParsePhotoFormats(Json::Value& rootValue, + std::map>& supportedFormats) +{ + if (!rootValue.isMember("Photo") || !rootValue["Photo"].isMember("OutputFormat") || + !rootValue["Photo"]["OutputFormat"].isArray() || rootValue["Photo"]["OutputFormat"].size() == 0 || + rootValue["Photo"]["OutputFormat"].size() > JSON_ARRAY_MAX_SIZE) { + DHLOGE("Photo or photo output format error."); + return; + } std::vector photoFormats; - if (rootValue["Photo"]["OutputFormat"].isArray() && (rootValue["Photo"]["OutputFormat"].size() > 0)) { - uint32_t size = rootValue["Photo"]["OutputFormat"].size(); - for (uint32_t i = 0; i < size; i++) { + uint32_t size = rootValue["Photo"]["OutputFormat"].size(); + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["Photo"]["OutputFormat"][i]).isInt()) { photoFormats.push_back((rootValue["Photo"]["OutputFormat"][i]).asInt()); } } - GetEachNodeSupportedResolution(photoFormats, "Photo", supportedFormats, rootValue); +} +void DMetadataProcessor::ParsePreviewFormats(Json::Value& rootValue, + std::map>& supportedFormats) +{ + if (!rootValue.isMember("Preview") || !rootValue["Preview"].isMember("OutputFormat") || + !rootValue["Preview"]["OutputFormat"].isArray() || rootValue["Preview"]["OutputFormat"].size() == 0 || + rootValue["Preview"]["OutputFormat"].size() > JSON_ARRAY_MAX_SIZE) { + DHLOGE("Preview or preview output format error."); + return; + } std::vector previewFormats; - if (rootValue["Preview"]["OutputFormat"].isArray() && (rootValue["Preview"]["OutputFormat"].size() > 0)) { - uint32_t size = rootValue["Preview"]["OutputFormat"].size(); - for (uint32_t i = 0; i < size; i++) { + uint32_t size = rootValue["Preview"]["OutputFormat"].size(); + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["Preview"]["OutputFormat"][i]).isInt()) { previewFormats.push_back((rootValue["Preview"]["OutputFormat"][i]).asInt()); } } - GetEachNodeSupportedResolution(previewFormats, "Preview", supportedFormats, rootValue); +} +void DMetadataProcessor::ParseVideoFormats(Json::Value& rootValue, + std::map>& supportedFormats) +{ + if (!rootValue.isMember("Video") || !rootValue["Video"].isMember("OutputFormat") || + !rootValue["Video"]["OutputFormat"].isArray() || rootValue["Video"]["OutputFormat"].size() == 0 || + rootValue["Video"]["OutputFormat"].size() > JSON_ARRAY_MAX_SIZE) { + DHLOGE("Video or video output format error."); + return; + } std::vector videoFormats; - if (rootValue["Video"]["OutputFormat"].isArray() && (rootValue["Video"]["OutputFormat"].size() > 0)) { - uint32_t size = rootValue["Video"]["OutputFormat"].size(); - for (uint32_t i = 0; i < size; i++) { + uint32_t size = rootValue["Video"]["OutputFormat"].size(); + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["Video"]["OutputFormat"][i]).isInt()) { videoFormats.push_back((rootValue["Video"]["OutputFormat"][i]).asInt()); } } - GetEachNodeSupportedResolution(videoFormats, "Video", supportedFormats, rootValue); - - return supportedFormats; } void DMetadataProcessor::PrintDCameraMetadata(const common_metadata_header_t *metadata) diff --git a/distributed_camera/hdi_service/src/dstream_operator/dstream_operator.cpp b/distributed_camera/hdi_service/src/dstream_operator/dstream_operator.cpp index a53796f79083e947f7719cb41c0ed4ecc6d3bc7b..1976d4923379f6899d22cc350a529a66d5da0a59 100644 --- a/distributed_camera/hdi_service/src/dstream_operator/dstream_operator.cpp +++ b/distributed_camera/hdi_service/src/dstream_operator/dstream_operator.cpp @@ -488,10 +488,14 @@ void DStreamOperator::ExtractCameraAttr(Json::Value &rootValue, std::vector { for (const auto &format : formats) { std::string formatStr = std::to_string(format); - if (rootValue[rootNode]["Resolution"][formatStr].isArray() && - rootValue[rootNode]["Resolution"][formatStr].size() > 0) { - GetCameraAttr(rootValue, formatStr, rootNode, format); + if (!rootValue[rootNode].isMember("Resolution") || !rootValue[rootNode]["Resolution"].isMember(formatStr) || + !rootValue[rootNode]["Resolution"][formatStr].isArray() || + rootValue[rootNode]["Resolution"][formatStr].size() == 0 || + rootValue[rootNode]["Resolution"][formatStr].size() > JSON_ARRAY_MAX_SIZE) { + DHLOGE("Resolution or %s error.", formatStr.c_str()); + continue; } + GetCameraAttr(rootValue, formatStr, rootNode, format); } } @@ -501,6 +505,10 @@ void DStreamOperator::GetCameraAttr(Json::Value &rootValue, std::string formatSt std::vector resolutionVec; uint32_t size = rootValue[rootNode]["Resolution"][formatStr].size(); for (uint32_t i = 0; i < size; i++) { + if (!rootValue[rootNode]["Resolution"][formatStr][i].isString()) { + DHLOGE("Resolution %s %d ,is not string.", formatStr.c_str(), i); + continue; + } std::string resoStr = rootValue[rootNode]["Resolution"][formatStr][i].asString(); std::vector reso; SplitString(resoStr, reso, STAR_SEPARATOR); @@ -545,56 +553,94 @@ DCamRetCode DStreamOperator::InitOutputConfigurations(const DHBase &dhBase, cons return DCamRetCode::INVALID_ARGUMENT; } - if (rootValue["CodecType"].isArray()) { - uint32_t size = rootValue["CodecType"].size(); - for (uint32_t i = 0; i < size; i++) { + if (!rootValue.isMember("CodecType") || !rootValue["CodecType"].isArray() || + rootValue["CodecType"].size() == 0 || rootValue["CodecType"].size() > JSON_ARRAY_MAX_SIZE) { + DHLOGE("CodecType error."); + return DCamRetCode::INVALID_ARGUMENT; + } + uint32_t size = rootValue["CodecType"].size(); + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["CodecType"][i]).isString()) { std::string codeType = (rootValue["CodecType"][i]).asString(); dcSupportedCodecType_.push_back(ConvertDCEncodeType(codeType)); } } + if (ParsePhotoFormats(rootValue) != SUCCESS || ParsePreviewFormats(rootValue) != SUCCESS || + ParseVideoFormats(rootValue) != SUCCESS) { + return DCamRetCode::INVALID_ARGUMENT; + } + + bool resolutionMap = false; + if (!dcSupportedPhotoResolutionMap_.empty() || !dcSupportedPreviewResolutionMap_.empty() || + !dcSupportedVideoResolutionMap_.empty()) { + resolutionMap = true; + } + + if (dcSupportedCodecType_.empty() || dcSupportedFormatMap_.empty() || !resolutionMap) { + DHLOGE("Input ablity info is invalid."); + return DEVICE_NOT_INIT; + } + return SUCCESS; +} + +DCamRetCode DStreamOperator::ParsePhotoFormats(Json::Value& rootValue) +{ + if (!rootValue.isMember("Photo") || !rootValue["Photo"].isMember("OutputFormat") || + !rootValue["Photo"]["OutputFormat"].isArray() || rootValue["Photo"]["OutputFormat"].size() == 0 || + rootValue["Photo"]["OutputFormat"].size() > JSON_ARRAY_MAX_SIZE) { + DHLOGE("Photo or photo output format error."); + return DCamRetCode::INVALID_ARGUMENT; + } std::vector photoFormats; - if (rootValue["Photo"]["OutputFormat"].isArray() && (rootValue["Photo"]["OutputFormat"].size() > 0)) { - uint32_t size = rootValue["Photo"]["OutputFormat"].size(); - for (uint32_t i = 0; i < size; i++) { + uint32_t size = rootValue["Photo"]["OutputFormat"].size(); + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["Photo"]["OutputFormat"][i]).isInt()) { photoFormats.push_back((rootValue["Photo"]["OutputFormat"][i]).asInt()); } - dcSupportedFormatMap_[DCSceneType::PHOTO] = photoFormats; } + dcSupportedFormatMap_[DCSceneType::PHOTO] = photoFormats; ExtractCameraAttr(rootValue, photoFormats, "Photo"); + return SUCCESS; +} +DCamRetCode DStreamOperator::ParsePreviewFormats(Json::Value& rootValue) +{ + if (!rootValue.isMember("Preview") || !rootValue["Preview"].isMember("OutputFormat") || + !rootValue["Preview"]["OutputFormat"].isArray() || rootValue["Preview"]["OutputFormat"].size() == 0 || + rootValue["Preview"]["OutputFormat"].size() > JSON_ARRAY_MAX_SIZE) { + DHLOGE("Preview or preview output format error."); + return DCamRetCode::INVALID_ARGUMENT; + } std::vector previewFormats; - if (rootValue["Preview"]["OutputFormat"].isArray() && (rootValue["Preview"]["OutputFormat"].size() > 0)) { - uint32_t size = rootValue["Preview"]["OutputFormat"].size(); - for (uint32_t i = 0; i < size; i++) { + uint32_t size = rootValue["Preview"]["OutputFormat"].size(); + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["Preview"]["OutputFormat"][i]).isInt()) { previewFormats.push_back((rootValue["Preview"]["OutputFormat"][i]).asInt()); } - dcSupportedFormatMap_[DCSceneType::PREVIEW] = previewFormats; } + dcSupportedFormatMap_[DCSceneType::PREVIEW] = previewFormats; ExtractCameraAttr(rootValue, previewFormats, "Preview"); + return SUCCESS; +} +DCamRetCode DStreamOperator::ParseVideoFormats(Json::Value& rootValue) +{ + if (!rootValue.isMember("Video") || !rootValue["Video"].isMember("OutputFormat") || + !rootValue["Video"]["OutputFormat"].isArray() || rootValue["Video"]["OutputFormat"].size() == 0 || + rootValue["Video"]["OutputFormat"].size() > JSON_ARRAY_MAX_SIZE) { + DHLOGE("Video or video output format error."); + return DCamRetCode::INVALID_ARGUMENT; + } std::vector videoFormats; - if (rootValue["Video"]["OutputFormat"].isArray() && (rootValue["Video"]["OutputFormat"].size() > 0)) { - uint32_t size = rootValue["Video"]["OutputFormat"].size(); - for (uint32_t i = 0; i < size; i++) { + uint32_t size = rootValue["Video"]["OutputFormat"].size(); + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["Video"]["OutputFormat"][i]).isInt()) { videoFormats.push_back((rootValue["Video"]["OutputFormat"][i]).asInt()); } - dcSupportedFormatMap_[DCSceneType::VIDEO] = videoFormats; } + dcSupportedFormatMap_[DCSceneType::VIDEO] = videoFormats; ExtractCameraAttr(rootValue, videoFormats, "Video"); - - - bool resolutionMap = false; - if (!dcSupportedPhotoResolutionMap_.empty() || !dcSupportedPreviewResolutionMap_.empty() || - !dcSupportedVideoResolutionMap_.empty()) { - resolutionMap = true; - } - - if (dcSupportedCodecType_.empty() || dcSupportedFormatMap_.empty() || !resolutionMap) { - DHLOGE("Input ablity info is invalid."); - return DEVICE_NOT_INIT; - } - return SUCCESS; }