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 OHOS_CAMERA_CAMERA_DEVICE_H
17 #define OHOS_CAMERA_CAMERA_DEVICE_H
18 
19 #include <iostream>
20 #include <memory>
21 #include <refbase.h>
22 #include <type_traits>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "camera_metadata_info.h"
27 #include "output/camera_output_capability.h"
28 #include "output/capture_output.h"
29 #include "ability/camera_ability.h"
30 #include "ability/camera_ability_parse_util.h"
31 
32 namespace OHOS {
33 namespace CameraStandard {
34 enum CameraPosition {
35     CAMERA_POSITION_UNSPECIFIED = 0,
36     CAMERA_POSITION_BACK,
37     CAMERA_POSITION_FRONT,
38     CAMERA_POSITION_FOLD_INNER
39 };
40 
41 enum CameraType {
42     CAMERA_TYPE_UNSUPPORTED = -1,
43     CAMERA_TYPE_DEFAULT = 0,
44     CAMERA_TYPE_WIDE_ANGLE,
45     CAMERA_TYPE_ULTRA_WIDE,
46     CAMERA_TYPE_TELEPHOTO,
47     CAMERA_TYPE_TRUE_DEPTH
48 };
49 
50 enum ConnectionType {
51     CAMERA_CONNECTION_BUILT_IN = 0,
52     CAMERA_CONNECTION_USB_PLUGIN,
53     CAMERA_CONNECTION_REMOTE
54 };
55 
56 enum CameraFoldScreenType {
57     CAMERA_FOLDSCREEN_UNSPECIFIED = 0,
58     CAMERA_FOLDSCREEN_INNER,
59     CAMERA_FOLDSCREEN_OUTER
60 };
61 
62 typedef struct dmDeviceInfo {
63     /**
64      * Device name of the device.
65      */
66     std::string deviceName;
67     /**
68      * Device type of the device.
69      */
70     uint16_t deviceTypeId;
71     /**
72      * NetworkId of the device.
73      */
74     std::string networkId;
75 } dmDeviceInfo;
76 
77 class CameraDevice : public RefBase {
78 public:
79     explicit CameraDevice(std::string cameraID, std::shared_ptr<OHOS::Camera::CameraMetadata> metadata);
80     explicit CameraDevice(
81         std::string cameraID, std::shared_ptr<OHOS::Camera::CameraMetadata> metadata, dmDeviceInfo deviceInfo);
82     virtual ~CameraDevice() = default;
83     /**
84     * @brief Get the camera Id.
85     *
86     * @return Returns the camera Id.
87     */
88     std::string GetID();
89 
90     /**
91     * @brief Get the metadata corresponding to current camera object.
92     *
93     * @return Returns the metadata corresponding to current object.
94     */
95     std::shared_ptr<OHOS::Camera::CameraMetadata> GetMetadata();
96 
97     /**
98     * @brief Reset cachedMetadata_ to default status
99     */
100     void ResetMetadata();
101 
102     /**
103     * @brief Get the current camera static abilities.
104     *
105     * @return Returns the current camera static abilities.
106     */
107     const std::shared_ptr<OHOS::Camera::CameraMetadata> GetCameraAbility();
108 
109     /**
110     * @brief Get the position of the camera.
111     *
112     * @return Returns the position of the camera.
113     */
114     CameraPosition GetPosition();
115 
116     /**
117     * @brief Get the used as position of the camera.
118     *
119     * @return Returns the used as position of the camera.
120     */
121     CameraPosition GetUsedAsPosition();
122 
123     /**
124     * @brief Get the Camera type of the camera.
125     *
126     * @return Returns the Camera type of the camera.
127     */
128     CameraType GetCameraType();
129 
130     /**
131     * @brief Get the Camera connection type.
132     *
133     * @return Returns the Camera type of the camera.
134     */
135     ConnectionType GetConnectionType();
136 
137     /**
138     * @brief Get the facing for foldScreen device.
139     *
140     * @return Returns the Camera type of the camera.
141     */
142     CameraFoldScreenType GetCameraFoldScreenType();
143 
144     /**
145     * @brief Get the Distributed Camera Host Name.
146     *
147     * @return Returns the  Host Name of the Distributed camera.
148     */
149     std::string GetHostName();
150 
151     /**
152     * @brief Get the Distributed Camera deviceType.
153     *
154     * @return Returns the deviceType of the Distributed camera.
155     */
156     uint16_t GetDeviceType();
157 
158     /**
159     * @brief Get the Distributed Camera networkId.
160     *
161     * @return Returns the networkId of the Distributed camera.
162     */
163     std::string GetNetWorkId();
164 
165     /**
166     * @brief Get the camera orientation.
167     *
168     * @return Returns the camera orientation.
169     */
170     uint32_t GetCameraOrientation();
171 
172     // or can we move definition completely in session only?
173     /**
174     * @brief Get the supported Zoom Ratio range.
175     *
176     * @return Returns vector<float> of supported Zoom ratio range.
177     */
178     std::vector<float> GetZoomRatioRange();
179 
180     /**
181     * @brief Get the supported exposure compensation range.
182     *
183     * @return Returns vector<int32_t> of supported exposure compensation range.
184     */
185     std::vector<float> GetExposureBiasRange();
186 
187     void SetProfile(sptr<CameraOutputCapability> cameraOutputCapability);
188 
189     void SetProfile(sptr<CameraOutputCapability> cameraOutputCapability, int32_t modename);
190 
191     void SetCameraDeviceUsedAsPosition(CameraPosition usedAsPosition);
192 
193     /**
194     * @brief Get sensor module type
195     *
196     * @return moduleType sensor module type.
197     */
198     uint32_t GetModuleType();
199 
200     /**
201     * @brief Get the supported fold status of the device
202     * @return The supported fold status of the device
203     *
204     * foldStatus enum values:
205     *   0: OHOS_CAMERA_FOLD_STATUS_NONFOLDABLE
206     *   1: OHOS_CAMERA_FOLD_STATUS_EXPANDED
207     *   2: OHOS_CAMERA_FOLD_STATUS_FOLDED
208     *   3: OHOS_CAMERA_FOLD_STATUS_EXPANDED + OHOS_CAMERA_FOLD_STATUS_FOLDED
209     */
210     uint32_t GetSupportedFoldStatus();
211 
212     template<typename T, typename = std::enable_if_t<std::is_same_v<T, Profile> || std::is_same_v<T, VideoProfile>>>
GetMaxSizeProfile(std::vector<T> & profiles,float profileRatioValue,CameraFormat format)213     std::shared_ptr<T> GetMaxSizeProfile(std::vector<T>& profiles, float profileRatioValue, CameraFormat format)
214     {
215         if (profileRatioValue <= 0) {
216             return nullptr;
217         }
218         std::shared_ptr<T> maxSizeProfile = nullptr;
219         for (auto& profile : profiles) {
220             if (profile.size_.width == 0 || profile.size_.height == 0) {
221                 continue;
222             }
223             if (profile.format_ != format) {
224                 continue;
225             }
226             float ratio = ((float)profile.size_.width) / profile.size_.height;
227             if (abs(ratio - profileRatioValue) / profileRatioValue > 0.05f) { // 0.05f is 5% tolerance
228                 continue;
229             }
230             if (maxSizeProfile == nullptr || profile.size_.width > maxSizeProfile->size_.width) {
231                 maxSizeProfile = std::make_shared<T>(profile);
232             }
233         }
234         return maxSizeProfile;
235     }
236 
237     std::unordered_map<int32_t, std::vector<Profile>> modePreviewProfiles_ = {};
238     std::unordered_map<int32_t, std::vector<Profile>> modePhotoProfiles_ = {};
239     std::unordered_map<int32_t, std::vector<VideoProfile>> modeVideoProfiles_ = {};
240     std::unordered_map<int32_t, DeferredDeliveryImageType> modeDeferredType_ = {};
241     CameraPosition usedAsCameraPosition_ = CAMERA_POSITION_UNSPECIFIED;
242     std::unordered_map<int32_t, int32_t> modeVideoDeferredType_ = {};
243 private:
244     std::string cameraID_;
245     const std::shared_ptr<OHOS::Camera::CameraMetadata> baseAbility_;
246     std::mutex cachedMetadataMutex_;
247     std::shared_ptr<OHOS::Camera::CameraMetadata> cachedMetadata_;
248     CameraPosition cameraPosition_ = CAMERA_POSITION_UNSPECIFIED;
249     CameraType cameraType_ = CAMERA_TYPE_DEFAULT;
250     ConnectionType connectionType_ = CAMERA_CONNECTION_BUILT_IN;
251     CameraFoldScreenType foldScreenType_ = CAMERA_FOLDSCREEN_UNSPECIFIED;
252     uint32_t cameraOrientation_ = 0;
253     uint32_t moduleType_ = 0;
254     uint32_t foldStatus_ = 0;
255     dmDeviceInfo dmDeviceInfo_ = {};
256     std::vector<float> zoomRatioRange_;
257     std::vector<float> exposureBiasRange_;
258     static const std::unordered_map<camera_type_enum_t, CameraType> metaToFwCameraType_;
259     static const std::unordered_map<camera_position_enum_t, CameraPosition> metaToFwCameraPosition_;
260     static const std::unordered_map<camera_connection_type_t, ConnectionType> metaToFwConnectionType_;
261     static const std::unordered_map<camera_foldscreen_enum_t, CameraFoldScreenType> metaToFwCameraFoldScreenType_;
262     void init(common_metadata_header_t* metadataHeader);
263     bool isFindModuleTypeTag(uint32_t &tagId);
264 };
265 } // namespace CameraStandard
266 } // namespace OHOS
267 #endif // OHOS_CAMERA_CAMERA_DEVICE_H
268