• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "input/camera_input.h"
17 
18 #include <cinttypes>
19 #include <set>
20 #include "camera_device_ability_items.h"
21 #include "camera_util.h"
22 #include "hcamera_device_callback_stub.h"
23 #include "media_log.h"
24 #include "metadata_utils.h"
25 
26 namespace OHOS {
27 namespace CameraStandard {
28 class CameraDeviceServiceCallback : public HCameraDeviceCallbackStub {
29 public:
30     sptr<CameraInput> camInput_ = nullptr;
CameraDeviceServiceCallback()31     CameraDeviceServiceCallback() : camInput_(nullptr) {
32     }
33 
CameraDeviceServiceCallback(const sptr<CameraInput> & camInput)34     explicit CameraDeviceServiceCallback(const sptr<CameraInput>& camInput) : camInput_(camInput) {
35     }
36 
~CameraDeviceServiceCallback()37     ~CameraDeviceServiceCallback()
38     {
39         camInput_ = nullptr;
40     }
41 
OnError(const int32_t errorType,const int32_t errorMsg)42     int32_t OnError(const int32_t errorType, const int32_t errorMsg) override
43     {
44         MEDIA_ERR_LOG("CameraDeviceServiceCallback::OnError() is called!, errorType: %{public}d, errorMsg: %{public}d",
45                       errorType, errorMsg);
46         if (camInput_ != nullptr && camInput_->GetErrorCallback() != nullptr) {
47             camInput_->GetErrorCallback()->OnError(errorType, errorMsg);
48         } else {
49             MEDIA_INFO_LOG("CameraDeviceServiceCallback::ErrorCallback not set!, Discarding callback");
50         }
51         return CAMERA_OK;
52     }
53 
OnResult(const uint64_t timestamp,const std::shared_ptr<Camera::CameraMetadata> & result)54     int32_t OnResult(const uint64_t timestamp, const std::shared_ptr<Camera::CameraMetadata> &result) override
55     {
56         MEDIA_INFO_LOG("CameraDeviceServiceCallback::OnResult() is called!, timestamp: %{public}" PRIu64, timestamp);
57         camera_metadata_item_t item;
58         int ret = Camera::FindCameraMetadataItem(result->get(), OHOS_CONTROL_FLASH_STATE, &item);
59         if (ret == 0) {
60             MEDIA_INFO_LOG("CameraDeviceServiceCallback::OnResult() OHOS_CONTROL_FLASH_STATE is %{public}d",
61                            item.data.u8[0]);
62         }
63         ret = Camera::FindCameraMetadataItem(result->get(), OHOS_CONTROL_FLASHMODE, &item);
64         if (ret == 0) {
65             MEDIA_INFO_LOG("CameraDeviceServiceCallback::OnResult() OHOS_CONTROL_FLASHMODE is %{public}d",
66                            item.data.u8[0]);
67         }
68         ret = Camera::FindCameraMetadataItem(result->get(), OHOS_CONTROL_AE_MODE, &item);
69         if (ret == 0) {
70             MEDIA_INFO_LOG("CameraDeviceServiceCallback::OnResult() OHOS_CONTROL_AE_MODE is %{public}d",
71                            item.data.u8[0]);
72         }
73         camInput_->ProcessAutoFocusUpdates(result);
74         return CAMERA_OK;
75     }
76 };
77 
78 const std::unordered_map<camera_af_state_t, FocusCallback::FocusState> CameraInput::mapFromMetadataFocus_ = {
79     {OHOS_CAMERA_AF_STATE_PASSIVE_SCAN, FocusCallback::SCAN},
80     {OHOS_CAMERA_AF_STATE_ACTIVE_SCAN, FocusCallback::SCAN},
81     {OHOS_CAMERA_AF_STATE_PASSIVE_FOCUSED, FocusCallback::FOCUSED},
82     {OHOS_CAMERA_AF_STATE_FOCUSED_LOCKED, FocusCallback::FOCUSED},
83     {OHOS_CAMERA_AF_STATE_PASSIVE_UNFOCUSED, FocusCallback::UNFOCUSED},
84     {OHOS_CAMERA_AF_STATE_NOT_FOCUSED_LOCKED, FocusCallback::UNFOCUSED},
85 };
86 
CameraInput(sptr<ICameraDeviceService> & deviceObj,sptr<CameraInfo> & cameraObj)87 CameraInput::CameraInput(sptr<ICameraDeviceService> &deviceObj,
88                          sptr<CameraInfo> &cameraObj) : cameraObj_(cameraObj), deviceObj_(deviceObj)
89 {
90     CameraDeviceSvcCallback_ = new CameraDeviceServiceCallback(this);
91     deviceObj_->SetCallback(CameraDeviceSvcCallback_);
92 }
93 
Release()94 void CameraInput::Release()
95 {
96     int32_t retCode = deviceObj_->Release();
97     if (retCode != CAMERA_OK) {
98         MEDIA_ERR_LOG("Failed to release Camera Input, retCode: %{public}d", retCode);
99     }
100     return;
101 }
102 
LockForControl()103 void CameraInput::LockForControl()
104 {
105     changeMetaMutex_.lock();
106     int32_t items = 10;
107     int32_t dataLength = 100;
108     changedMetadata_ = std::make_shared<Camera::CameraMetadata>(items, dataLength);
109     return;
110 }
111 
UpdateSetting(std::shared_ptr<Camera::CameraMetadata> changedMetadata)112 int32_t CameraInput::UpdateSetting(std::shared_ptr<Camera::CameraMetadata> changedMetadata)
113 {
114     if (!Camera::GetCameraMetadataItemCount(changedMetadata->get())) {
115         MEDIA_INFO_LOG("CameraInput::UpdateSetting No configuration to update");
116         return CAMERA_OK;
117     }
118 
119     int32_t ret = deviceObj_->UpdateSetting(changedMetadata);
120     if (ret != CAMERA_OK) {
121         MEDIA_ERR_LOG("CameraInput::UpdateSetting Failed to update settings");
122         return ret;
123     }
124 
125     size_t length;
126     uint32_t count = changedMetadata->get()->item_count;
127     uint8_t *data = Camera::GetMetadataData(changedMetadata->get());
128     camera_metadata_item_entry_t *itemEntry = Camera::GetMetadataItems(changedMetadata->get());
129     std::shared_ptr<Camera::CameraMetadata> baseMetadata = cameraObj_->GetMetadata();
130     for (int32_t i = 0; i < count; i++, itemEntry++) {
131         bool status = false;
132         camera_metadata_item_t item;
133         length = Camera::CalculateCameraMetadataItemDataSize(itemEntry->data_type, itemEntry->count);
134         ret = Camera::FindCameraMetadataItem(baseMetadata->get(), itemEntry->item, &item);
135         if (ret == CAM_META_SUCCESS) {
136             status = baseMetadata->updateEntry(itemEntry->item,
137                                                (length == 0) ? itemEntry->data.value : (data + itemEntry->data.offset),
138                                                itemEntry->count);
139         } else if (ret == CAM_META_ITEM_NOT_FOUND) {
140             status = baseMetadata->addEntry(itemEntry->item,
141                                             (length == 0) ? itemEntry->data.value : (data + itemEntry->data.offset),
142                                             itemEntry->count);
143         }
144         if (!status) {
145             MEDIA_ERR_LOG("CameraInput::UpdateSetting Failed to add/update metadata item: %{public}d",
146                           itemEntry->item);
147         }
148     }
149     return CAMERA_OK;
150 }
151 
UnlockForControl()152 int32_t CameraInput::UnlockForControl()
153 {
154     if (changedMetadata_ == nullptr) {
155         MEDIA_ERR_LOG("CameraInput::UnlockForControl Need to call LockForControl() before UnlockForControl()");
156         return CAMERA_INVALID_ARG;
157     }
158 
159     UpdateSetting(changedMetadata_);
160     changedMetadata_ = nullptr;
161     changeMetaMutex_.unlock();
162     return CAMERA_OK;
163 }
164 
165 template<typename DataPtr, typename Vec, typename VecType>
getVector(DataPtr data,size_t count,Vec & vect,VecType dataType)166 void CameraInput::getVector(DataPtr data, size_t count, Vec &vect, VecType dataType)
167 {
168     for (size_t index = 0; index < count; index++) {
169         vect.emplace_back(static_cast<VecType>(data[index]));
170     }
171 }
172 
GetSupportedPhotoFormats()173 std::vector<camera_format_t> CameraInput::GetSupportedPhotoFormats()
174 {
175     uint32_t unitLen = 3;
176     camera_format_t format;
177     std::set<camera_format_t> formats;
178     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
179     camera_metadata_item_t item;
180     int ret = Camera::FindCameraMetadataItem(metadata->get(),
181                                              OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS, &item);
182     if (ret != CAM_META_SUCCESS) {
183         MEDIA_ERR_LOG("Failed to get stream configuration with return code %{public}d", ret);
184         return {};
185     }
186     if (item.count % unitLen != 0) {
187         MEDIA_ERR_LOG("Invalid stream configuration count: %{public}d", item.count);
188         return {};
189     }
190     for (uint32_t index = 0; index < item.count; index += unitLen) {
191         format = static_cast<camera_format_t>(item.data.i32[index]);
192         if (format == OHOS_CAMERA_FORMAT_JPEG) {
193             formats.insert(format);
194         }
195     }
196     return std::vector<camera_format_t>(formats.begin(), formats.end());
197 }
198 
GetSupportedVideoFormats()199 std::vector<camera_format_t> CameraInput::GetSupportedVideoFormats()
200 {
201     uint32_t unitLen = 3;
202     camera_format_t format;
203     std::set<camera_format_t> formats;
204     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
205     camera_metadata_item_t item;
206     int ret = Camera::FindCameraMetadataItem(metadata->get(),
207                                              OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS, &item);
208     if (ret != CAM_META_SUCCESS) {
209         MEDIA_ERR_LOG("Failed to get stream configuration with return code %{public}d", ret);
210         return {};
211     }
212     if (item.count % unitLen != 0) {
213         MEDIA_ERR_LOG("Invalid stream configuration count: %{public}d", item.count);
214         return {};
215     }
216     for (uint32_t index = 0; index < item.count; index += unitLen) {
217         format = static_cast<camera_format_t>(item.data.i32[index]);
218         if (format != OHOS_CAMERA_FORMAT_JPEG) {
219             formats.insert(format);
220         }
221     }
222     return std::vector<camera_format_t>(formats.begin(), formats.end());
223 }
224 
GetSupportedPreviewFormats()225 std::vector<camera_format_t> CameraInput::GetSupportedPreviewFormats()
226 {
227     uint32_t unitLen = 3;
228     camera_format_t format;
229     std::set<camera_format_t> formats;
230     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
231     camera_metadata_item_t item;
232     int ret = Camera::FindCameraMetadataItem(metadata->get(),
233                                              OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS, &item);
234     if (ret != CAM_META_SUCCESS) {
235         MEDIA_ERR_LOG("Failed to get stream configuration with return code %{public}d", ret);
236         return {};
237     }
238     if (item.count % unitLen != 0) {
239         MEDIA_ERR_LOG("Invalid stream configuration count: %{public}d", item.count);
240         return {};
241     }
242     for (uint32_t index = 0; index < item.count; index += unitLen) {
243         format = static_cast<camera_format_t>(item.data.i32[index]);
244         if (format != OHOS_CAMERA_FORMAT_JPEG) {
245             formats.insert(format);
246         }
247     }
248     return std::vector<camera_format_t>(formats.begin(), formats.end());
249 }
250 
getSupportedSizes(camera_format_t format)251 std::vector<CameraPicSize> CameraInput::getSupportedSizes(camera_format_t format)
252 {
253     uint32_t unitLen = 3;
254     uint32_t widthOffset = 1;
255     uint32_t heightOffset = 2;
256     camera_metadata_item_t item;
257     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
258     int ret = Camera::FindCameraMetadataItem(metadata->get(),
259                                              OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS, &item);
260     if (ret != CAM_META_SUCCESS) {
261         MEDIA_ERR_LOG("Failed to get stream configuration with return code %{public}d", ret);
262         return {};
263     }
264     if (item.count % unitLen != 0) {
265         MEDIA_ERR_LOG("Invalid stream configuration count: %{public}d", item.count);
266         return {};
267     }
268     int32_t count = 0;
269     for (uint32_t index = 0; index < item.count; index += unitLen) {
270         if (item.data.i32[index] == format) {
271             count++;
272         }
273     }
274     if (count == 0) {
275         MEDIA_ERR_LOG("Format: %{public}d is not found in stream configuration", format);
276         return {};
277     }
278 
279     std::vector<CameraPicSize> sizes(count);
280     CameraPicSize *size = &sizes[0];
281     for (uint32_t index = 0; index < item.count; index += unitLen) {
282         if (item.data.i32[index] == format) {
283             size->width = static_cast<uint32_t>(item.data.i32[index + widthOffset]);
284             size->height = static_cast<uint32_t>(item.data.i32[index + heightOffset]);
285             size++;
286         }
287     }
288     return sizes;
289 }
290 
GetSupportedExposureModes()291 std::vector<camera_ae_mode_t> CameraInput::GetSupportedExposureModes()
292 {
293     std::vector<camera_ae_mode_t> supportedExposureModes;
294     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
295     camera_metadata_item_t item;
296     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AE_AVAILABLE_MODES, &item);
297     if (ret != CAM_META_SUCCESS) {
298         MEDIA_ERR_LOG("CameraInput::GetSupportedExposureModes Failed with return code %{public}d", ret);
299         return supportedExposureModes;
300     }
301     getVector(item.data.u8, item.count, supportedExposureModes, camera_ae_mode_t(0));
302     return supportedExposureModes;
303 }
304 
SetExposureMode(camera_ae_mode_t exposureMode)305 void CameraInput::SetExposureMode(camera_ae_mode_t exposureMode)
306 {
307     if (changedMetadata_ == nullptr) {
308         MEDIA_ERR_LOG("CameraInput::SetExposureMode Need to call LockForControl() before setting camera properties");
309         return;
310     }
311     bool status = false;
312     uint32_t count = 1;
313     uint8_t exposure = exposureMode;
314     camera_metadata_item_t item;
315     int ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_AE_MODE, &item);
316     if (ret == CAM_META_ITEM_NOT_FOUND) {
317         status = changedMetadata_->addEntry(OHOS_CONTROL_AE_MODE, &exposure, count);
318     } else if (ret == CAM_META_SUCCESS) {
319         status = changedMetadata_->updateEntry(OHOS_CONTROL_AE_MODE, &exposure, count);
320     }
321 
322     if (!status) {
323         MEDIA_ERR_LOG("CameraInput::SetExposureMode Failed to set exposure mode");
324     }
325 
326     return;
327 }
328 
GetExposureMode()329 camera_ae_mode_t CameraInput::GetExposureMode()
330 {
331     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
332     camera_metadata_item_t item;
333     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AE_MODE, &item);
334     if (ret != CAM_META_SUCCESS) {
335         MEDIA_ERR_LOG("CameraInput::GetExposureMode Failed with return code %{public}d", ret);
336         return OHOS_CAMERA_AE_MODE_OFF;
337     }
338     return static_cast<camera_ae_mode_t>(item.data.u8[0]);
339 }
340 
SetExposureCallback(std::shared_ptr<ExposureCallback> exposureCallback)341 void CameraInput::SetExposureCallback(std::shared_ptr<ExposureCallback> exposureCallback)
342 {
343     exposurecallback_ = exposureCallback;
344     return;
345 }
346 
GetSupportedFocusModes()347 std::vector<camera_af_mode_t> CameraInput::GetSupportedFocusModes()
348 {
349     std::vector<camera_af_mode_t> supportedFocusModes;
350     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
351     camera_metadata_item_t item;
352     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AF_AVAILABLE_MODES, &item);
353     if (ret != CAM_META_SUCCESS) {
354         MEDIA_ERR_LOG("CameraInput::GetSupportedFocusModes Failed with return code %{public}d", ret);
355         return supportedFocusModes;
356     }
357     getVector(item.data.u8, item.count, supportedFocusModes, camera_af_mode_t(0));
358     return supportedFocusModes;
359 }
360 
SetFocusCallback(std::shared_ptr<FocusCallback> focusCallback)361 void CameraInput::SetFocusCallback(std::shared_ptr<FocusCallback> focusCallback)
362 {
363     focusCallback_ = focusCallback;
364     return;
365 }
366 
StartFocus(camera_af_mode_t focusMode)367 int32_t CameraInput::StartFocus(camera_af_mode_t focusMode)
368 {
369     bool status = false;
370     int32_t ret;
371     static int32_t triggerId = 0;
372     uint32_t count = 1;
373     uint8_t trigger = OHOS_CAMERA_AF_TRIGGER_START;
374     camera_metadata_item_t item;
375 
376     if ((focusMode == OHOS_CAMERA_AF_MODE_OFF) || (focusMode == OHOS_CAMERA_AF_MODE_CONTINUOUS_VIDEO)
377         || (focusMode == OHOS_CAMERA_AF_MODE_CONTINUOUS_PICTURE)) {
378         return CAM_META_SUCCESS;
379     }
380 
381     ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_AF_TRIGGER, &item);
382     if (ret == CAM_META_ITEM_NOT_FOUND) {
383         status = changedMetadata_->addEntry(OHOS_CONTROL_AF_TRIGGER, &trigger, count);
384     } else if (ret == CAM_META_SUCCESS) {
385         status = changedMetadata_->updateEntry(OHOS_CONTROL_AF_TRIGGER, &trigger, count);
386     }
387 
388     if (!status) {
389         MEDIA_ERR_LOG("CameraInput::StartFocus Failed to set trigger");
390         return CAM_META_FAILURE;
391     }
392 
393     triggerId++;
394     ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_AF_TRIGGER_ID, &item);
395     if (ret == CAM_META_ITEM_NOT_FOUND) {
396         status = changedMetadata_->addEntry(OHOS_CONTROL_AF_TRIGGER_ID, &triggerId, count);
397     } else if (ret == CAM_META_SUCCESS) {
398         status = changedMetadata_->updateEntry(OHOS_CONTROL_AF_TRIGGER_ID, &triggerId, count);
399     }
400 
401     if (!status) {
402         MEDIA_ERR_LOG("CameraInput::SetFocusMode Failed to set trigger Id");
403         return CAM_META_FAILURE;
404     }
405     return CAM_META_SUCCESS;
406 }
407 
SetFocusMode(camera_af_mode_t focusMode)408 void CameraInput::SetFocusMode(camera_af_mode_t focusMode)
409 {
410     if (changedMetadata_ == nullptr) {
411         MEDIA_ERR_LOG("CameraInput::SetFocusMode Need to call LockForControl() before setting camera properties");
412         return;
413     }
414 
415     bool status = false;
416     int32_t ret;
417     uint32_t count = 1;
418     uint8_t focus = focusMode;
419     camera_metadata_item_t item;
420 
421     MEDIA_DEBUG_LOG("CameraInput::SetFocusMode Focus mode: %{public}d", focusMode);
422 
423 #ifdef BALTIMORE_CAMERA
424     ret = StartFocus(focusMode);
425     if (ret != CAM_META_SUCCESS) {
426         return;
427     }
428 #endif
429 
430     ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_AF_MODE, &item);
431     if (ret == CAM_META_ITEM_NOT_FOUND) {
432         status = changedMetadata_->addEntry(OHOS_CONTROL_AF_MODE, &focus, count);
433     } else if (ret == CAM_META_SUCCESS) {
434         status = changedMetadata_->updateEntry(OHOS_CONTROL_AF_MODE, &focus, count);
435     }
436 
437     if (!status) {
438         MEDIA_ERR_LOG("CameraInput::SetFocusMode Failed to set focus mode");
439     }
440     return;
441 }
442 
GetFocusMode()443 camera_af_mode_t CameraInput::GetFocusMode()
444 {
445     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
446     camera_metadata_item_t item;
447     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AF_MODE, &item);
448     if (ret != CAM_META_SUCCESS) {
449         MEDIA_ERR_LOG("CameraInput::GetFocusMode Failed with return code %{public}d", ret);
450         return OHOS_CAMERA_AF_MODE_OFF;
451     }
452     return static_cast<camera_af_mode_t>(item.data.u8[0]);
453 }
454 
GetSupportedZoomRatioRange()455 std::vector<float> CameraInput::GetSupportedZoomRatioRange()
456 {
457     return cameraObj_->GetZoomRatioRange();
458 }
459 
GetZoomRatio()460 float CameraInput::GetZoomRatio()
461 {
462     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
463     camera_metadata_item_t item;
464     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_ZOOM_RATIO, &item);
465     if (ret != CAM_META_SUCCESS) {
466         MEDIA_ERR_LOG("CameraInput::GetZoomRatio Failed with return code %{public}d", ret);
467         return 0;
468     }
469     return static_cast<float>(item.data.f[0]);
470 }
471 
SetCropRegion(float zoomRatio)472 int32_t CameraInput::SetCropRegion(float zoomRatio)
473 {
474     bool status = false;
475     int32_t ret;
476     int32_t leftIndex = 0;
477     int32_t topIndex = 1;
478     int32_t rightIndex = 2;
479     int32_t bottomIndex = 3;
480     int32_t factor = 2;
481     int32_t sensorRight;
482     int32_t sensorBottom;
483     const uint32_t arrayCount = 4;
484     int32_t cropRegion[arrayCount] = {};
485     camera_metadata_item_t item;
486 
487     if (zoomRatio == 0) {
488         MEDIA_ERR_LOG("CameraInput::SetCropRegion Invalid zoom ratio");
489         return CAM_META_FAILURE;
490     }
491 
492     ret = Camera::FindCameraMetadataItem(cameraObj_->GetMetadata()->get(), OHOS_SENSOR_INFO_ACTIVE_ARRAY_SIZE, &item);
493     if (ret != CAM_META_SUCCESS) {
494         MEDIA_ERR_LOG("CameraInput::SetCropRegion Failed to get sensor active array size with return code %{public}d",
495                       ret);
496         return ret;
497     }
498     if (item.count != arrayCount) {
499         MEDIA_ERR_LOG("CameraInput::SetCropRegion Invalid sensor active array size count: %{public}d", item.count);
500         return CAM_META_FAILURE;
501     }
502 
503     MEDIA_DEBUG_LOG("CameraInput::SetCropRegion Sensor active array left: %{public}d, top: %{public}d, "
504                     "right: %{public}d, bottom: %{public}d", item.data.i32[leftIndex], item.data.i32[topIndex],
505                     item.data.i32[rightIndex], item.data.i32[bottomIndex]);
506 
507     sensorRight = item.data.i32[rightIndex];
508     sensorBottom = item.data.i32[bottomIndex];
509     cropRegion[leftIndex] = (sensorRight - (sensorRight / zoomRatio)) / factor;
510     cropRegion[topIndex] = (sensorBottom - (sensorBottom / zoomRatio)) / factor;
511     cropRegion[rightIndex] = cropRegion[leftIndex] + (sensorRight / zoomRatio);
512     cropRegion[bottomIndex] = cropRegion[topIndex] + (sensorBottom / zoomRatio);
513 
514     MEDIA_DEBUG_LOG("CameraInput::SetCropRegion Crop region left: %{public}d, top: %{public}d, "
515                     "right: %{public}d, bottom: %{public}d", cropRegion[leftIndex], cropRegion[topIndex],
516                     cropRegion[rightIndex], cropRegion[bottomIndex]);
517 
518     ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_ZOOM_CROP_REGION, &item);
519     if (ret == CAM_META_ITEM_NOT_FOUND) {
520         status = changedMetadata_->addEntry(OHOS_CONTROL_ZOOM_CROP_REGION, cropRegion, arrayCount);
521     } else if (ret == CAM_META_SUCCESS) {
522         status = changedMetadata_->updateEntry(OHOS_CONTROL_ZOOM_CROP_REGION, cropRegion, arrayCount);
523     }
524 
525     if (!status) {
526         MEDIA_ERR_LOG("CameraInput::SetCropRegion Failed to set zoom crop region");
527         return CAM_META_FAILURE;
528     }
529     return CAM_META_SUCCESS;
530 }
531 
SetZoomRatio(float zoomRatio)532 void CameraInput::SetZoomRatio(float zoomRatio)
533 {
534     if (changedMetadata_ == nullptr) {
535         MEDIA_ERR_LOG("CameraInput::SetZoomRatio Need to call LockForControl() before setting camera properties");
536         return;
537     }
538 
539     bool status = false;
540     int32_t ret;
541     int32_t minIndex = 0;
542     int32_t maxIndex = 1;
543     int32_t count = 1;
544     camera_metadata_item_t item;
545 
546     MEDIA_DEBUG_LOG("CameraInput::SetZoomRatio Zoom ratio: %{public}f", zoomRatio);
547 
548     std::vector<float> zoomRange = cameraObj_->GetZoomRatioRange();
549     if (zoomRange.empty()) {
550         MEDIA_ERR_LOG("CameraInput::SetZoomRatio Zoom range is empty");
551         return;
552     }
553     if (zoomRatio < zoomRange[minIndex]) {
554         MEDIA_DEBUG_LOG("CameraInput::SetZoomRatio Zoom ratio: %{public}f is lesser than minimum zoom: %{public}f",
555                         zoomRatio, zoomRange[minIndex]);
556         zoomRatio = zoomRange[minIndex];
557     } else if (zoomRatio > zoomRange[maxIndex]) {
558         MEDIA_DEBUG_LOG("CameraInput::SetZoomRatio Zoom ratio: %{public}f is greater than maximum zoom: %{public}f",
559                         zoomRatio, zoomRange[maxIndex]);
560         zoomRatio = zoomRange[maxIndex];
561     }
562 
563     if (zoomRatio == 0) {
564         MEDIA_ERR_LOG("CameraInput::SetZoomRatio Invalid zoom ratio");
565         return;
566     }
567 
568 #ifdef BALTIMORE_CAMERA
569     ret = SetCropRegion(zoomRatio);
570     if (ret != CAM_META_SUCCESS) {
571         return;
572     }
573 #endif
574 
575     ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_ZOOM_RATIO, &item);
576     if (ret == CAM_META_ITEM_NOT_FOUND) {
577         status = changedMetadata_->addEntry(OHOS_CONTROL_ZOOM_RATIO, &zoomRatio, count);
578     } else if (ret == CAM_META_SUCCESS) {
579         status = changedMetadata_->updateEntry(OHOS_CONTROL_ZOOM_RATIO, &zoomRatio, count);
580     }
581 
582     if (!status) {
583         MEDIA_ERR_LOG("CameraInput::SetZoomRatio Failed to set zoom mode");
584     }
585     return;
586 }
587 
GetSupportedFlashModes()588 std::vector<camera_flash_mode_enum_t> CameraInput::GetSupportedFlashModes()
589 {
590     std::vector<camera_flash_mode_enum_t> supportedFlashModes;
591     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
592     camera_metadata_item_t item;
593     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_DEVICE_AVAILABLE_FLASHMODES, &item);
594     if (ret != CAM_META_SUCCESS) {
595         MEDIA_ERR_LOG("CameraInput::GetSupportedFlashModes Failed with return code %{public}d", ret);
596         return supportedFlashModes;
597     }
598     getVector(item.data.u8, item.count, supportedFlashModes, camera_flash_mode_enum_t(0));
599     return supportedFlashModes;
600 }
601 
GetFlashMode()602 camera_flash_mode_enum_t CameraInput::GetFlashMode()
603 {
604     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
605     camera_metadata_item_t item;
606     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_FLASHMODE, &item);
607     if (ret != CAM_META_SUCCESS) {
608         MEDIA_ERR_LOG("CameraInput::GetFlashMode Failed with return code %{public}d", ret);
609         return OHOS_CAMERA_FLASH_MODE_CLOSE;
610     }
611     return static_cast<camera_flash_mode_enum_t>(item.data.u8[0]);
612 }
613 
SetFlashMode(camera_flash_mode_enum_t flashMode)614 void CameraInput::SetFlashMode(camera_flash_mode_enum_t flashMode)
615 {
616     if (changedMetadata_ == nullptr) {
617         MEDIA_ERR_LOG("CameraInput::SetFlashMode Need to call LockForControl() before setting camera properties");
618         return;
619     }
620 
621     bool status = false;
622     uint32_t count = 1;
623     uint8_t flash = flashMode;
624     camera_metadata_item_t item;
625     int ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_FLASHMODE, &item);
626     if (ret == CAM_META_ITEM_NOT_FOUND) {
627         status = changedMetadata_->addEntry(OHOS_CONTROL_FLASHMODE, &flash, count);
628     } else if (ret == CAM_META_SUCCESS) {
629         status = changedMetadata_->updateEntry(OHOS_CONTROL_FLASHMODE, &flash, count);
630     }
631 
632     if (!status) {
633         MEDIA_ERR_LOG("CameraInput::SetFlashMode Failed to set flash mode");
634     }
635     return;
636 }
637 
SetErrorCallback(std::shared_ptr<ErrorCallback> errorCallback)638 void CameraInput::SetErrorCallback(std::shared_ptr<ErrorCallback> errorCallback)
639 {
640     if (errorCallback == nullptr) {
641         MEDIA_ERR_LOG("SetErrorCallback: Unregistering error callback");
642     }
643     errorCallback_ = errorCallback;
644     return;
645 }
646 
GetCameraDevice()647 sptr<ICameraDeviceService> CameraInput::GetCameraDevice()
648 {
649     return deviceObj_;
650 }
651 
GetErrorCallback()652 std::shared_ptr<ErrorCallback> CameraInput::GetErrorCallback()
653 {
654     return errorCallback_;
655 }
656 
ProcessAutoFocusUpdates(const std::shared_ptr<Camera::CameraMetadata> & result)657 void CameraInput::ProcessAutoFocusUpdates(const std::shared_ptr<Camera::CameraMetadata> &result)
658 {
659     camera_metadata_item_t item;
660     common_metadata_header_t *metadata = result->get();
661     int ret = Camera::FindCameraMetadataItem(metadata, OHOS_CONTROL_AF_MODE, &item);
662     if (ret == CAM_META_SUCCESS) {
663         MEDIA_DEBUG_LOG("Focus mode: %{public}d", item.data.u8[0]);
664     }
665     ret = Camera::FindCameraMetadataItem(metadata, OHOS_CONTROL_AF_STATE, &item);
666     if (ret == CAM_META_SUCCESS) {
667         MEDIA_INFO_LOG("Focus state: %{public}d", item.data.u8[0]);
668         if (focusCallback_ != nullptr) {
669             camera_af_state_t focusState = static_cast<camera_af_state_t>(item.data.u8[0]);
670             auto itr = mapFromMetadataFocus_.find(focusState);
671             if (itr != mapFromMetadataFocus_.end()) {
672                 focusCallback_->OnFocusState(itr->second);
673             }
674         }
675     }
676 }
677 
GetCameraSettings()678 std::string CameraInput::GetCameraSettings()
679 {
680     return Camera::MetadataUtils::EncodeToString(cameraObj_->GetMetadata());
681 }
682 
SetCameraSettings(std::string setting)683 int32_t CameraInput::SetCameraSettings(std::string setting)
684 {
685     std::shared_ptr<Camera::CameraMetadata> metadata = Camera::MetadataUtils::DecodeFromString(setting);
686     if (metadata == nullptr) {
687         MEDIA_ERR_LOG("CameraInput::SetCameraSettings Failed to decode metadata setting from string");
688         return CAMERA_INVALID_ARG;
689     }
690     return UpdateSetting(metadata);
691 }
692 } // CameraStandard
693 } // OHOS
694