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