1 /*
2 * Copyright (c) 2021-2022 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 #include "input/camera_device.h"
16 #include <securec.h>
17 #include "camera_metadata_info.h"
18 #include "camera_log.h"
19
20 using namespace std;
21
22 namespace OHOS {
23 namespace CameraStandard {
24 const std::unordered_map<camera_type_enum_t, CameraType> CameraDevice::metaToFwCameraType_ = {
25 {OHOS_CAMERA_TYPE_WIDE_ANGLE, CAMERA_TYPE_WIDE_ANGLE},
26 {OHOS_CAMERA_TYPE_ULTRA_WIDE, CAMERA_TYPE_ULTRA_WIDE},
27 {OHOS_CAMERA_TYPE_TELTPHOTO, CAMERA_TYPE_TELEPHOTO},
28 {OHOS_CAMERA_TYPE_TRUE_DEAPTH, CAMERA_TYPE_TRUE_DEPTH},
29 {OHOS_CAMERA_TYPE_LOGICAL, CAMERA_TYPE_UNSUPPORTED},
30 {OHOS_CAMERA_TYPE_UNSPECIFIED, CAMERA_TYPE_DEFAULT}
31 };
32
33 const std::unordered_map<camera_position_enum_t, CameraPosition> CameraDevice::metaToFwCameraPosition_ = {
34 {OHOS_CAMERA_POSITION_FRONT, CAMERA_POSITION_FRONT},
35 {OHOS_CAMERA_POSITION_BACK, CAMERA_POSITION_BACK},
36 {OHOS_CAMERA_POSITION_OTHER, CAMERA_POSITION_UNSPECIFIED}
37 };
38
39 const std::unordered_map<camera_connection_type_t, ConnectionType> CameraDevice::metaToFwConnectionType_ = {
40 {OHOS_CAMERA_CONNECTION_TYPE_REMOTE, CAMERA_CONNECTION_REMOTE},
41 {OHOS_CAMERA_CONNECTION_TYPE_USB_PLUGIN, CAMERA_CONNECTION_USB_PLUGIN},
42 {OHOS_CAMERA_CONNECTION_TYPE_BUILTIN, CAMERA_CONNECTION_BUILT_IN}
43 };
44
CameraDevice(std::string cameraID,std::shared_ptr<Camera::CameraMetadata> metadata)45 CameraDevice::CameraDevice(std::string cameraID, std::shared_ptr<Camera::CameraMetadata> metadata)
46 {
47 cameraID_ = cameraID;
48 metadata_ = metadata;
49 init(metadata->get());
50 }
51
~CameraDevice()52 CameraDevice::~CameraDevice()
53 {
54 metadata_.reset();
55 metadata_ = nullptr;
56 }
57
init(common_metadata_header_t * metadata)58 void CameraDevice::init(common_metadata_header_t* metadata)
59 {
60 camera_metadata_item_t item;
61
62 int ret = Camera::FindCameraMetadataItem(metadata, OHOS_ABILITY_CAMERA_POSITION, &item);
63 if (ret == CAM_META_SUCCESS) {
64 auto itr = metaToFwCameraPosition_.find(static_cast<camera_position_enum_t>(item.data.u8[0]));
65 if (itr != metaToFwCameraPosition_.end()) {
66 cameraPosition_ = itr->second;
67 }
68 }
69
70 ret = Camera::FindCameraMetadataItem(metadata, OHOS_ABILITY_CAMERA_TYPE, &item);
71 if (ret == CAM_META_SUCCESS) {
72 auto itr = metaToFwCameraType_.find(static_cast<camera_type_enum_t>(item.data.u8[0]));
73 if (itr != metaToFwCameraType_.end()) {
74 cameraType_ = itr->second;
75 }
76 }
77
78 ret = Camera::FindCameraMetadataItem(metadata, OHOS_ABILITY_CAMERA_CONNECTION_TYPE, &item);
79 if (ret == CAM_META_SUCCESS) {
80 auto itr = metaToFwConnectionType_.find(static_cast<camera_connection_type_t>(item.data.u8[0]));
81 if (itr != metaToFwConnectionType_.end()) {
82 connectionType_ = itr->second;
83 }
84 }
85
86 ret = Camera::FindCameraMetadataItem(metadata, OHOS_CONTROL_CAPTURE_MIRROR_SUPPORTED, &item);
87 if (ret == CAM_META_SUCCESS) {
88 isMirrorSupported_ = ((item.data.u8[0] == 1) || (item.data.u8[0] == 0));
89 }
90 MEDIA_INFO_LOG("camera position: %{public}d, camera type: %{public}d, camera connection type: %{public}d, "
91 "Mirror Supported: %{public}d ",
92 cameraPosition_, cameraType_, connectionType_, isMirrorSupported_);
93 }
94
GetID()95 std::string CameraDevice::GetID()
96 {
97 return cameraID_;
98 }
99
GetMetadata()100 std::shared_ptr<Camera::CameraMetadata> CameraDevice::GetMetadata()
101 {
102 return metadata_;
103 }
104
GetPosition()105 CameraPosition CameraDevice::GetPosition()
106 {
107 return cameraPosition_;
108 }
109
GetCameraType()110 CameraType CameraDevice::GetCameraType()
111 {
112 return cameraType_;
113 }
114
GetConnectionType()115 ConnectionType CameraDevice::GetConnectionType()
116 {
117 return connectionType_;
118 }
119
IsMirrorSupported()120 bool CameraDevice::IsMirrorSupported()
121 {
122 return isMirrorSupported_;
123 }
124
CalculateZoomRange()125 std::vector<float> CameraDevice::CalculateZoomRange()
126 {
127 int32_t ret;
128 int32_t minIndex = 0;
129 int32_t maxIndex = 1;
130 uint32_t zoomRangeCount = 2;
131 constexpr float factor = 100.0;
132 float minZoom;
133 float maxZoom;
134 float tempZoom;
135 camera_metadata_item_t item;
136
137 ret = Camera::FindCameraMetadataItem(metadata_->get(), OHOS_ABILITY_ZOOM_CAP, &item);
138 if (ret != CAM_META_SUCCESS) {
139 MEDIA_ERR_LOG("Failed to get zoom cap with return code %{public}d", ret);
140 return {};
141 }
142 if (item.count != zoomRangeCount) {
143 MEDIA_ERR_LOG("Invalid zoom cap count: %{public}d", item.count);
144 return {};
145 }
146 MEDIA_DEBUG_LOG("Zoom cap min: %{public}d, max: %{public}d",
147 item.data.i32[minIndex], item.data.i32[maxIndex]);
148 minZoom = item.data.i32[minIndex] / factor;
149 maxZoom = item.data.i32[maxIndex] / factor;
150
151 ret = Camera::FindCameraMetadataItem(metadata_->get(), OHOS_ABILITY_SCENE_ZOOM_CAP, &item);
152 if (ret != CAM_META_SUCCESS) {
153 MEDIA_ERR_LOG("Failed to get scene zoom cap with return code %{public}d", ret);
154 return {};
155 }
156 if (item.count != zoomRangeCount) {
157 MEDIA_ERR_LOG("Invalid zoom cap count: %{public}d", item.count);
158 return {};
159 }
160 MEDIA_DEBUG_LOG("Scene zoom cap min: %{public}d, max: %{public}d",
161 item.data.i32[minIndex], item.data.i32[maxIndex]);
162 tempZoom = item.data.i32[minIndex] / factor;
163 if (minZoom < tempZoom) {
164 minZoom = tempZoom;
165 }
166 tempZoom = item.data.i32[maxIndex] / factor;
167 if (maxZoom > tempZoom) {
168 maxZoom = tempZoom;
169 }
170 return {minZoom, maxZoom};
171 }
172
GetZoomRatioRange()173 std::vector<float> CameraDevice::GetZoomRatioRange()
174 {
175 int32_t minIndex = 0;
176 int32_t maxIndex = 1;
177 std::vector<float> range;
178
179 if (!zoomRatioRange_.empty()) {
180 return zoomRatioRange_;
181 }
182
183 int ret;
184 uint32_t zoomRangeCount = 2;
185 camera_metadata_item_t item;
186
187 ret = Camera::FindCameraMetadataItem(metadata_->get(), OHOS_ABILITY_ZOOM_RATIO_RANGE, &item);
188 if (ret != CAM_META_SUCCESS) {
189 MEDIA_ERR_LOG("Failed to get zoom ratio range with return code %{public}d", ret);
190 return {};
191 }
192 if (item.count != zoomRangeCount) {
193 MEDIA_ERR_LOG("Invalid zoom ratio range count: %{public}d", item.count);
194 return {};
195 }
196 range = {item.data.f[minIndex], item.data.f[maxIndex]};
197
198 if (range[minIndex] > range[maxIndex]) {
199 MEDIA_ERR_LOG("Invalid zoom range. min: %{public}f, max: %{public}f", range[minIndex], range[maxIndex]);
200 return {};
201 }
202 MEDIA_DEBUG_LOG("Zoom range min: %{public}f, max: %{public}f", range[minIndex], range[maxIndex]);
203
204 zoomRatioRange_ = range;
205 return zoomRatioRange_;
206 }
207
GetExposureBiasRange()208 std::vector<int32_t> CameraDevice::GetExposureBiasRange()
209 {
210 int32_t minIndex = 0;
211 int32_t maxIndex = 1;
212 std::vector<int32_t> range;
213
214 if (!exposureBiasRange_.empty()) {
215 return exposureBiasRange_;
216 }
217
218 int ret;
219 uint32_t biasRangeCount = 2;
220 camera_metadata_item_t item;
221
222 ret = Camera::FindCameraMetadataItem(metadata_->get(), OHOS_CONTROL_AE_COMPENSATION_RANGE, &item);
223 if (ret != CAM_META_SUCCESS) {
224 MEDIA_ERR_LOG("Failed to get exposure compensation range with return code %{public}d", ret);
225 return {};
226 }
227 if (item.count != biasRangeCount) {
228 MEDIA_ERR_LOG("Invalid exposure compensation range count: %{public}d", item.count);
229 return {};
230 }
231
232 range = {item.data.i32[minIndex], item.data.i32[maxIndex]};
233 if (range[minIndex] > range[maxIndex]) {
234 MEDIA_ERR_LOG("Invalid exposure compensation range. min: %{public}d, max: %{public}d",
235 range[minIndex], range[maxIndex]);
236 return {};
237 }
238 MEDIA_DEBUG_LOG("Exposure compensation min: %{public}d, max: %{public}d", range[minIndex], range[maxIndex]);
239
240 exposureBiasRange_ = range;
241 return exposureBiasRange_;
242 }
243 } // CameraStandard
244 } // OHOS
245