• 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 #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 #include "color_space_info_parse.h"
32 
33 namespace OHOS {
34 namespace CameraStandard {
35 enum CameraPosition {
36     CAMERA_POSITION_UNSPECIFIED = 0,
37     CAMERA_POSITION_BACK,
38     CAMERA_POSITION_FRONT,
39     CAMERA_POSITION_FOLD_INNER
40 };
41 
42 enum CameraType {
43     CAMERA_TYPE_UNSUPPORTED = -1,
44     CAMERA_TYPE_DEFAULT = 0,
45     CAMERA_TYPE_WIDE_ANGLE,
46     CAMERA_TYPE_ULTRA_WIDE,
47     CAMERA_TYPE_TELEPHOTO,
48     CAMERA_TYPE_TRUE_DEPTH
49 };
50 
51 enum ConnectionType {
52     CAMERA_CONNECTION_BUILT_IN = 0,
53     CAMERA_CONNECTION_USB_PLUGIN,
54     CAMERA_CONNECTION_REMOTE
55 };
56 
57 enum CameraFoldScreenType {
58     CAMERA_FOLDSCREEN_UNSPECIFIED = 0,
59     CAMERA_FOLDSCREEN_INNER,
60     CAMERA_FOLDSCREEN_OUTER
61 };
62 
63 enum AuxiliaryType {
64     CONTRACTLENS = 0
65 };
66 
67 enum AuxiliaryStatus {
68     AUXILIARY_LOCKED = 0,
69     AUXILIARY_ON,
70     AUXILIARY_OFF
71 };
72 
73 typedef struct dmDeviceInfo {
74     /**
75      * Device name of the device.
76      */
77     std::string deviceName;
78     /**
79      * Device type of the device.
80      */
81     uint16_t deviceTypeId;
82     /**
83      * NetworkId of the device.
84      */
85     std::string networkId;
86 } dmDeviceInfo;
87 
88 struct flashmode {
89     int count = 0;
90     std::vector<int32_t> mode = {};
91 };
92 
93 struct exposuremode {
94     int count = 0;
95     std::vector<int32_t> mode = {};
96 };
97 
98 struct zoomratiorange {
99     int count = 0;
100     std::vector<int32_t> mode = {};
101     std::unordered_map<int32_t, std::pair<float, float>> range;
102 };
103 
104 struct compensationrange {
105     int count = 0;
106     std::vector<float> range = {};
107 };
108 
109 struct focusmode {
110     int count = 0;
111     std::vector<int32_t> mode = {};
112 };
113 
114 struct stabilizationmode {
115     int count = 0;
116     std::vector<int32_t> mode = {};
117 };
118 
119 struct CameraConcurrentLimtedCapability {
120     flashmode flashmodes;
121     exposuremode exposuremodes;
122     zoomratiorange ratiorange;
123     compensationrange compensation;
124     focusmode focusmodes;
125     stabilizationmode stabilizationmodes;
126     ColorSpaceInfo colorspaces;
127 };
128 
129 class CameraDevice : public RefBase {
130 public:
131     explicit CameraDevice(std::string cameraID, std::shared_ptr<OHOS::Camera::CameraMetadata> metadata);
132     [[deprecated]] explicit CameraDevice(
133         std::string cameraID, std::shared_ptr<OHOS::Camera::CameraMetadata> metadata, dmDeviceInfo deviceInfo);
134     explicit CameraDevice(
135         std::string cameraID, dmDeviceInfo deviceInfo, std::shared_ptr<OHOS::Camera::CameraMetadata> metadata);
136     virtual ~CameraDevice() = default;
137     /**
138     * @brief Get the camera Id.
139     *
140     * @return Returns the camera Id.
141     */
142     std::string GetID();
143 
144     /**
145     * @brief Get the cachedMetadata corresponding to current camera object.
146     *
147     * @return Returns the cachedMetadata corresponding to current object.
148     */
149     std::shared_ptr<OHOS::Camera::CameraMetadata> GetCachedMetadata();
150 
151     /**
152     * @brief Get the metadata corresponding to current camera object.
153     *
154     * @return Returns the metadata corresponding to current object.
155     */
156     [[deprecated]] std::shared_ptr<OHOS::Camera::CameraMetadata> GetMetadata();
157 
158     /**
159      * @brief Add metadata to the camera device.
160      *
161      * @param srcMetadata metadata got from proxy from service.
162      */
163     void AddMetadata(std::shared_ptr<OHOS::Camera::CameraMetadata> srcMetadata);
164 
165     /**
166     * @brief Reset cachedMetadata_ to default status
167     */
168     void ResetMetadata();
169 
170     /**
171     * @brief Get the current camera static abilities.
172     *
173     * @return Returns the current camera static abilities.
174     */
175     const std::shared_ptr<OHOS::Camera::CameraMetadata> GetCameraAbility();
176 
177     /**
178     * @brief Get the position of the camera.
179     *
180     * @return Returns the position of the camera.
181     */
182     CameraPosition GetPosition();
183 
184     /**
185     * @brief Get the ised as position of the camera.
186     *
187     * @return Returns the used as position of the camera.
188     */
189     CameraPosition GetUsedAsPosition();
190 
191     /**
192     * @brief Get the Camera type of the camera.
193     *
194     * @return Returns the Camera type of the camera.
195     */
196     CameraType GetCameraType();
197 
198     /**
199     * @brief Get the Camera connection type.
200     *
201     * @return Returns the Camera type of the camera.
202     */
203     ConnectionType GetConnectionType();
204 
205     /**
206     * @brief Get the facing for foldScreen device.
207     *
208     * @return Returns the Camera type of the camera.
209     */
210     CameraFoldScreenType GetCameraFoldScreenType();
211 
212     /**
213     * @brief Get the SupportedModes for device.
214     *
215     * @return Returns the SupportedModes of the camera.
216     */
217     std::vector<SceneMode> GetSupportedModes() const;
218 
219     /**
220     * @brief Get the SupportedObjectTypes for device.
221     *
222     * @return Returns the Camera SupportedObjectTypes of the camera.
223     */
224     std::vector<MetadataObjectType> GetObjectTypes() const;
225 
226     /**
227     * @brief Get the IsPrelaunch for device.
228     *
229     * @return Returns the Camera IsPrelaunch of the camera.
230     */
231     bool IsPrelaunch() const;
232 
233     /**
234     * @brief Get the Distributed Camera Host Name.
235     *
236     * @return Returns the  Host Name of the Distributed camera.
237     */
238     std::string GetHostName();
239 
240     /**
241     * @brief Get the Distributed Camera deviceType.
242     *
243     * @return Returns the deviceType of the Distributed camera.
244     */
245     uint16_t GetDeviceType();
246 
247     /**
248     * @brief Get the Distributed Camera networkId.
249     *
250     * @return Returns the networkId of the Distributed camera.
251     */
252     std::string GetNetWorkId();
253 
254     /**
255     * @brief Get the camera orientation.
256     *
257     * @return Returns the camera orientation.
258     */
259     uint32_t GetCameraOrientation();
260 
261     /**
262     * @brief Get the camera isretractable.
263     *
264     * @return Returns the camera isretractable.
265     */
266     bool GetisRetractable();
267 
268     /**
269     * @brief Get the camera lensEquivalentFocalLength.
270     *
271     * @return Returns the camera lensEquivalentFocalLength.
272     */
273     std::vector<int32_t> GetLensEquivalentFocalLength();
274 
275     // or can we move definition completely in session only?
276     /**
277     * @brief Get the supported Zoom Ratio range.
278     *
279     * @return Returns vector<float> of supported Zoom ratio range.
280     */
281     std::vector<float> GetZoomRatioRange();
282 
283     /**
284     * @brief Get the supported exposure compensation range.
285     *
286     * @return Returns vector<int32_t> of supported exposure compensation range.
287     */
288     std::vector<float> GetExposureBiasRange();
289 
290     void SetProfile(sptr<CameraOutputCapability> cameraOutputCapability);
291 
292     void SetProfile(sptr<CameraOutputCapability> cameraOutputCapability, int32_t modename);
293 
294     void SetCameraDeviceUsedAsPosition(CameraPosition usedAsPosition);
295 
296     /**
297     * @brief Get sensor module type
298     *
299     * @return moduleType sensor module type.
300     */
301     uint32_t GetModuleType();
302 
303     /**
304     * @brief Get the supported fold status of the device
305     * @return The supported fold status of the device
306     *
307     * foldStatus enum values:
308     *   0: OHOS_CAMERA_FOLD_STATUS_NONFOLDABLE
309     *   1: OHOS_CAMERA_FOLD_STATUS_EXPANDED
310     *   2: OHOS_CAMERA_FOLD_STATUS_FOLDED
311     *   3: OHOS_CAMERA_FOLD_STATUS_EXPANDED + OHOS_CAMERA_FOLD_STATUS_FOLDED
312     */
313     uint32_t GetSupportedFoldStatus();
314 
315     void SetCameraId(std::string devID);
316 
317     bool isConcurrentDeviceType();
318 
319     void SetConcurrentDeviceType(bool changeType);
320 
321     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)322     std::shared_ptr<T> GetMaxSizeProfile(std::vector<T>& profiles, float profileRatioValue, CameraFormat format)
323     {
324         if (profileRatioValue <= 0) {
325             return nullptr;
326         }
327         std::shared_ptr<T> maxSizeProfile = nullptr;
328         for (auto& profile : profiles) {
329             if (profile.size_.width == 0 || profile.size_.height == 0) {
330                 continue;
331             }
332             if (profile.format_ != format) {
333                 continue;
334             }
335             float ratio = ((float)profile.size_.width) / profile.size_.height;
336             if (abs(ratio - profileRatioValue) / profileRatioValue > 0.05f) { // 0.05f is 5% tolerance
337                 continue;
338             }
339             if (maxSizeProfile == nullptr || profile.size_.width > maxSizeProfile->size_.width) {
340                 maxSizeProfile = std::make_shared<T>(profile);
341             }
342         }
343         return maxSizeProfile;
344     }
345 
346     std::unordered_map<int32_t, std::vector<Profile>> modePreviewProfiles_ = {};
347     std::unordered_map<int32_t, std::vector<Profile>> modePhotoProfiles_ = {};
348     std::unordered_map<int32_t, std::vector<VideoProfile>> modeVideoProfiles_ = {};
349     std::unordered_map<int32_t, std::vector<DepthProfile>> modeDepthProfiles_ = {};
350     std::unordered_map<int32_t, DeferredDeliveryImageType> modeDeferredType_ = {};
351     CameraPosition usedAsCameraPosition_ = CAMERA_POSITION_UNSPECIFIED;
352     std::unordered_map<int32_t, int32_t> modeVideoDeferredType_ = {};
353     CameraConcurrentLimtedCapability limtedCapabilitySave_;
354     int32_t isConcurrentLimted_ = 0;
355 private:
356     std::string cameraID_;
357     const std::shared_ptr<OHOS::Camera::CameraMetadata> baseAbility_;
358     std::mutex cachedMetadataMutex_;
359     std::shared_ptr<OHOS::Camera::CameraMetadata> cachedMetadata_;
360     CameraPosition cameraPosition_ = CAMERA_POSITION_UNSPECIFIED;
361     CameraType cameraType_ = CAMERA_TYPE_DEFAULT;
362     ConnectionType connectionType_ = CAMERA_CONNECTION_BUILT_IN;
363     CameraFoldScreenType foldScreenType_ = CAMERA_FOLDSCREEN_UNSPECIFIED;
364     uint32_t cameraOrientation_ = 0;
365     bool isRetractable_ = false;
366     std::vector<int32_t> lensEquivalentFocalLength_ = {};
367     uint32_t moduleType_ = 0;
368     uint32_t foldStatus_ = 0;
369     std::vector<SceneMode> supportedModes_ = {};
370     std::vector<MetadataObjectType> objectTypes_ = {};
371     bool isPrelaunch_ = false;
372     dmDeviceInfo dmDeviceInfo_ = {};
373     std::vector<float> zoomRatioRange_;
374     std::vector<float> exposureBiasRange_;
375     static const std::unordered_map<camera_type_enum_t, CameraType> metaToFwCameraType_;
376     static const std::unordered_map<camera_position_enum_t, CameraPosition> metaToFwCameraPosition_;
377     static const std::unordered_map<camera_connection_type_t, ConnectionType> metaToFwConnectionType_;
378     static const std::unordered_map<camera_foldscreen_enum_t, CameraFoldScreenType> metaToFwCameraFoldScreenType_;
379     void init(common_metadata_header_t* metadataHeader);
380     void InitLensEquivalentFocalLength(common_metadata_header_t* metadata);
381     bool isFindModuleTypeTag(uint32_t &tagId);
382     bool isConcurrentDevice_ = false;
383 };
384 } // namespace CameraStandard
385 } // namespace OHOS
386 #endif // OHOS_CAMERA_CAMERA_DEVICE_H
387