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 #ifndef DISTRIBUTED_CAMERA_METADATA_PROCESSOR_H
17 #define DISTRIBUTED_CAMERA_METADATA_PROCESSOR_H
18 
19 #include <set>
20 #include <map>
21 #include <mutex>
22 #include <vector>
23 #include "constants.h"
24 #include "dcamera.h"
25 #include "cJSON.h"
26 #include "v1_1/dcamera_types.h"
27 #include "v1_0/types.h"
28 
29 namespace OHOS {
30 namespace DistributedHardware {
31 using namespace OHOS::HDI::Camera::V1_0;
32 using namespace OHOS::HDI::DistributedCamera::V1_1;
33 class DMetadataProcessor {
34 public:
35     DMetadataProcessor() = default;
36     ~DMetadataProcessor() = default;
37     DMetadataProcessor(const DMetadataProcessor &other) = delete;
38     DMetadataProcessor(DMetadataProcessor &&other) = delete;
39     DMetadataProcessor& operator=(const DMetadataProcessor &other) = delete;
40     DMetadataProcessor& operator=(DMetadataProcessor &&other) = delete;
41 
42 public:
43     DCamRetCode InitDCameraAbility(const std::string &sinkAbilityInfo);
44     DCamRetCode GetDCameraAbility(std::shared_ptr<CameraAbility> &ability);
45     DCamRetCode SetMetadataResultMode(const ResultCallbackMode &mode);
46     DCamRetCode GetEnabledMetadataResults(std::vector<MetaType> &results);
47     DCamRetCode EnableMetadataResult(const std::vector<MetaType> &results);
48     DCamRetCode DisableMetadataResult(const std::vector<MetaType> &results);
49     DCamRetCode ResetEnableResults();
50     DCamRetCode SaveResultMetadata(std::string resultStr);
51     void UpdateResultMetadata(const uint64_t &resultTimestamp);
52     void SetResultCallback(std::function<void(uint64_t, std::shared_ptr<OHOS::Camera::CameraMetadata>)> &resultCbk);
53     void PrintDCameraMetadata(const common_metadata_header_t *metadata);
54 
55 private:
56     DCamRetCode InitDCameraDefaultAbilityKeys(const std::string &sinkAbilityInfo);
57     DCamRetCode InitDCameraOutputAbilityKeys(const std::string &sinkAbilityInfo);
58     DCamRetCode AddAbilityEntry(uint32_t tag, const void *data, size_t size);
59     DCamRetCode UpdateAbilityEntry(uint32_t tag, const void *data, size_t size);
60     void ConvertToCameraMetadata(common_metadata_header_t *&input,
61         std::shared_ptr<OHOS::Camera::CameraMetadata> &output);
62     void ResizeMetadataHeader(common_metadata_header_t *&header, uint32_t itemCapacity, uint32_t dataCapacity);
63     void UpdateAllResult(const uint64_t &resultTimestamp);
64     void UpdateOnChanged(const uint64_t &resultTimestamp);
65     uint32_t GetDataSize(uint32_t type);
66     void* GetMetadataItemData(const camera_metadata_item_t &item);
67     std::map<int, std::vector<DCResolution>> GetDCameraSupportedFormats(const std::string &abilityInfo);
68     void ParsePhotoFormats(cJSON* rootValue, std::map<int, std::vector<DCResolution>>& supportedFormats);
69     void ParsePreviewFormats(cJSON* rootValue, std::map<int, std::vector<DCResolution>>& supportedFormats);
70     void ParseVideoFormats(cJSON* rootValue, std::map<int, std::vector<DCResolution>>& supportedFormats);
71     void InitDcameraBaseAbility();
72     void GetEachNodeSupportedResolution(std::vector<int>& formats, const std::string rootNode,
73         std::map<int, std::vector<DCResolution>>& supportedFormats, cJSON* rootValue);
74     void GetNodeSupportedResolution(int format, const std::string rootNode,
75         std::map<int, std::vector<DCResolution>>& supportedFormats, cJSON* rootValue);
76     void SetFpsRanges();
77     void InitBasicConfigTag(std::map<int, std::vector<DCResolution>> &supportedFormats,
78         std::vector<int32_t> &streamConfigs);
79     void InitExtendConfigTag(std::map<int, std::vector<DCResolution>> &supportedFormats,
80         std::vector<int32_t> &extendStreamConfigs);
81     void AddConfigs(std::vector<int32_t> &sinkExtendStreamConfigs, int32_t format,
82         int32_t width, int32_t height, int32_t fps);
83     void StoreSinkAndSrcConfig(int format, const std::string rootNode, std::vector<DCResolution> &resolutionVec);
84     cJSON* GetFormatObj(const std::string rootNode, cJSON* rootValue, std::string& formatStr);
85     bool GetInfoFromJson(const std::string& sinkAbilityInfo);
86     void InitOutputAbilityWithoutMode(const std::string &sinkAbilityInfo);
87     void UpdateAbilityTag(std::vector<int32_t> &streamConfigs, std::vector<int32_t> &extendStreamConfigs);
88 
89 private:
90     constexpr static uint32_t JSON_ARRAY_MAX_SIZE = 1000;
91     constexpr static uint32_t PREVIEW_FPS = 0;
92     constexpr static uint32_t PHOTO_FPS = 0;
93     constexpr static uint32_t VIDEO_FPS = 30;
94     constexpr static uint32_t EXTEND_PREVIEW = 0;
95     constexpr static uint32_t EXTEND_VIDEO = 1;
96     constexpr static uint32_t EXTEND_PHOTO = 2;
97     constexpr static int32_t EXTEND_EOF = -1;
98     constexpr static uint32_t ADD_MODE = 3;
99     std::function<void(uint64_t, std::shared_ptr<OHOS::Camera::CameraMetadata>)> resultCallback_;
100     std::shared_ptr<CameraAbility> dCameraAbility_;
101     std::string protocolVersion_;
102     std::string dCameraPosition_;
103     DCResolution maxPreviewResolution_;
104     DCResolution maxPhotoResolution_;
105     ResultCallbackMode metaResultMode_;
106     std::set<MetaType> allResultSet_;
107     std::set<MetaType> enabledResultSet_;
108     std::mutex producerMutex_;
109     std::vector<int> sinkPhotoFormats_;
110     std::map<int, std::vector<DCResolution>> sinkPhotoProfiles_;
111     std::map<int, std::vector<DCResolution>> sinkPreviewProfiles_;
112     std::map<int, std::vector<DCResolution>> sinkVideoProfiles_;
113 
114     // The latest result metadata that received from the sink device.
115     std::shared_ptr<OHOS::Camera::CameraMetadata> latestProducerMetadataResult_;
116 
117     // The latest result metadata that replied to the camera service.
118     std::shared_ptr<OHOS::Camera::CameraMetadata> latestConsumerMetadataResult_;
119 };
120 } // namespace DistributedHardware
121 } // namespace OHOS
122 #endif // DISTRIBUTED_CAMERA_METADATA_PROCESSOR_H