• 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 "session/capture_session.h"
17 #include "camera_util.h"
18 #include "hcapture_session_callback_stub.h"
19 #include "input/camera_input.h"
20 #include "camera_log.h"
21 #include "output/photo_output.h"
22 #include "output/preview_output.h"
23 #include "output/video_output.h"
24 
25 namespace OHOS {
26 namespace CameraStandard {
27 namespace {
28     constexpr int32_t DEFAULT_ITEMS = 10;
29     constexpr int32_t DEFAULT_DATA_LENGTH = 100;
30 }
31 
32 const std::unordered_map<camera_focus_state_t, FocusCallback::FocusState> CaptureSession::metaToFwFocusState_ = {
33     {OHOS_CAMERA_FOCUS_STATE_SCAN, FocusCallback::SCAN},
34     {OHOS_CAMERA_FOCUS_STATE_FOCUSED, FocusCallback::FOCUSED},
35     {OHOS_CAMERA_FOCUS_STATE_UNFOCUSED, FocusCallback::UNFOCUSED}
36 };
37 
38 const std::unordered_map<camera_exposure_state_t,
39         ExposureCallback::ExposureState> CaptureSession::metaToFwExposureState_ = {
40     {OHOS_CAMERA_EXPOSURE_STATE_SCAN, ExposureCallback::SCAN},
41     {OHOS_CAMERA_EXPOSURE_STATE_CONVERGED, ExposureCallback::CONVERGED}
42 };
43 
44 const std::unordered_map<camera_exposure_mode_enum_t, ExposureMode> CaptureSession::metaToFwExposureMode_ = {
45     {OHOS_CAMERA_EXPOSURE_MODE_LOCKED, EXPOSURE_MODE_LOCKED},
46     {OHOS_CAMERA_EXPOSURE_MODE_AUTO, EXPOSURE_MODE_AUTO},
47     {OHOS_CAMERA_EXPOSURE_MODE_CONTINUOUS_AUTO, EXPOSURE_MODE_CONTINUOUS_AUTO}
48 };
49 
50 const std::unordered_map<ExposureMode, camera_exposure_mode_enum_t> CaptureSession::fwToMetaExposureMode_ = {
51     {EXPOSURE_MODE_LOCKED, OHOS_CAMERA_EXPOSURE_MODE_LOCKED},
52     {EXPOSURE_MODE_AUTO, OHOS_CAMERA_EXPOSURE_MODE_AUTO},
53     {EXPOSURE_MODE_CONTINUOUS_AUTO, OHOS_CAMERA_EXPOSURE_MODE_CONTINUOUS_AUTO}
54 };
55 
56 const std::unordered_map<camera_focus_mode_enum_t, FocusMode> CaptureSession::metaToFwFocusMode_ = {
57     {OHOS_CAMERA_FOCUS_MODE_MANUAL, FOCUS_MODE_MANUAL},
58     {OHOS_CAMERA_FOCUS_MODE_CONTINUOUS_AUTO, FOCUS_MODE_CONTINUOUS_AUTO},
59     {OHOS_CAMERA_FOCUS_MODE_AUTO, FOCUS_MODE_AUTO},
60     {OHOS_CAMERA_FOCUS_MODE_LOCKED, FOCUS_MODE_LOCKED}
61 };
62 
63 const std::unordered_map<FocusMode, camera_focus_mode_enum_t> CaptureSession::fwToMetaFocusMode_ = {
64     {FOCUS_MODE_MANUAL, OHOS_CAMERA_FOCUS_MODE_MANUAL},
65     {FOCUS_MODE_CONTINUOUS_AUTO, OHOS_CAMERA_FOCUS_MODE_CONTINUOUS_AUTO},
66     {FOCUS_MODE_AUTO, OHOS_CAMERA_FOCUS_MODE_AUTO},
67     {FOCUS_MODE_LOCKED, OHOS_CAMERA_FOCUS_MODE_LOCKED}
68 };
69 
70 const std::unordered_map<camera_flash_mode_enum_t, FlashMode> CaptureSession::metaToFwFlashMode_ = {
71     {OHOS_CAMERA_FLASH_MODE_CLOSE, FLASH_MODE_CLOSE},
72     {OHOS_CAMERA_FLASH_MODE_OPEN, FLASH_MODE_OPEN},
73     {OHOS_CAMERA_FLASH_MODE_AUTO, FLASH_MODE_AUTO},
74     {OHOS_CAMERA_FLASH_MODE_ALWAYS_OPEN, FLASH_MODE_ALWAYS_OPEN}
75 };
76 
77 const std::unordered_map<FlashMode, camera_flash_mode_enum_t> CaptureSession::fwToMetaFlashMode_ = {
78     {FLASH_MODE_CLOSE, OHOS_CAMERA_FLASH_MODE_CLOSE},
79     {FLASH_MODE_OPEN, OHOS_CAMERA_FLASH_MODE_OPEN},
80     {FLASH_MODE_AUTO, OHOS_CAMERA_FLASH_MODE_AUTO},
81     {FLASH_MODE_ALWAYS_OPEN, OHOS_CAMERA_FLASH_MODE_ALWAYS_OPEN}
82 };
83 
84 const std::unordered_map<CameraVideoStabilizationMode,
85 VideoStabilizationMode> CaptureSession::metaToFwVideoStabModes_ = {
86     {OHOS_CAMERA_VIDEO_STABILIZATION_OFF, OFF},
87     {OHOS_CAMERA_VIDEO_STABILIZATION_LOW, LOW},
88     {OHOS_CAMERA_VIDEO_STABILIZATION_MIDDLE, MIDDLE},
89     {OHOS_CAMERA_VIDEO_STABILIZATION_HIGH, HIGH},
90     {OHOS_CAMERA_VIDEO_STABILIZATION_AUTO, AUTO}
91 };
92 
93 const std::unordered_map<VideoStabilizationMode,
94 CameraVideoStabilizationMode> CaptureSession::fwToMetaVideoStabModes_ = {
95     {OFF, OHOS_CAMERA_VIDEO_STABILIZATION_OFF},
96     {LOW, OHOS_CAMERA_VIDEO_STABILIZATION_LOW},
97     {MIDDLE, OHOS_CAMERA_VIDEO_STABILIZATION_MIDDLE},
98     {HIGH, OHOS_CAMERA_VIDEO_STABILIZATION_HIGH},
99     {AUTO, OHOS_CAMERA_VIDEO_STABILIZATION_AUTO}
100 };
101 
OnError(int32_t errorCode)102 int32_t CaptureSessionCallback::OnError(int32_t errorCode)
103 {
104     MEDIA_INFO_LOG("CaptureSessionCallback::OnError() is called!, errorCode: %{public}d",
105                    errorCode);
106     if (captureSession_ != nullptr && captureSession_->GetApplicationCallback() != nullptr) {
107         captureSession_->GetApplicationCallback()->OnError(errorCode);
108     } else {
109         MEDIA_INFO_LOG("CaptureSessionCallback::ApplicationCallback not set!, Discarding callback");
110     }
111     return CameraErrorCode::SUCCESS;
112 }
113 
CaptureSession(sptr<ICaptureSession> & captureSession)114 CaptureSession::CaptureSession(sptr<ICaptureSession> &captureSession)
115 {
116     captureSession_ = captureSession;
117     inputDevice_ = nullptr;
118 }
119 
~CaptureSession()120 CaptureSession::~CaptureSession()
121 {
122     MEDIA_DEBUG_LOG("Enter Into CaptureSession::~CaptureSession()");
123     inputDevice_ = nullptr;
124     captureSession_ = nullptr;
125     changedMetadata_ = nullptr;
126     appCallback_ = nullptr;
127     captureSessionCallback_ = nullptr;
128     exposureCallback_ = nullptr;
129     focusCallback_ = nullptr;
130 }
131 
BeginConfig()132 int32_t CaptureSession::BeginConfig()
133 {
134     CAMERA_SYNC_TRACE;
135     MEDIA_DEBUG_LOG("Enter Into CaptureSession::BeginConfig");
136     if (IsSessionConfiged()) {
137         MEDIA_ERR_LOG("CaptureSession::BeginConfig Session is locked");
138         return CameraErrorCode::SESSION_CONFIG_LOCKED;
139     }
140     int32_t errCode = CAMERA_UNKNOWN_ERROR;
141     if (captureSession_) {
142         errCode = captureSession_->BeginConfig();
143         if (errCode != CAMERA_OK) {
144             MEDIA_ERR_LOG("Failed to BeginConfig!, %{public}d", errCode);
145         }
146     } else {
147         MEDIA_ERR_LOG("CaptureSession::BeginConfig() captureSession_ is nullptr");
148     }
149     return ServiceToCameraError(errCode);
150 }
151 
CommitConfig()152 int32_t CaptureSession::CommitConfig()
153 {
154     CAMERA_SYNC_TRACE;
155     MEDIA_DEBUG_LOG("Enter Into CaptureSession::CommitConfig");
156     if (!IsSessionConfiged()) {
157         MEDIA_ERR_LOG("CaptureSession::CommitConfig operation Not allowed!");
158         return CameraErrorCode::OPERATION_NOT_ALLOWED;
159     }
160     int32_t errCode = CAMERA_UNKNOWN_ERROR;
161     if (captureSession_) {
162         errCode = captureSession_->CommitConfig();
163         if (errCode != CAMERA_OK) {
164             MEDIA_ERR_LOG("Failed to CommitConfig!, %{public}d", errCode);
165         }
166     } else {
167         MEDIA_ERR_LOG("CaptureSession::CommitConfig() captureSession_ is nullptr");
168     }
169     return ServiceToCameraError(errCode);
170 }
171 
CanAddInput(sptr<CaptureInput> & input)172 int32_t CaptureSession::CanAddInput(sptr<CaptureInput> &input)
173 {
174     // todo: get Profile passed to createOutput and compare with OutputCapability
175     // if present in capability return ok.
176     return CameraErrorCode::SUCCESS;
177 }
178 
AddInput(sptr<CaptureInput> & input)179 int32_t CaptureSession::AddInput(sptr<CaptureInput> &input)
180 {
181     CAMERA_SYNC_TRACE;
182     MEDIA_DEBUG_LOG("Enter Into CaptureSession::AddInput");
183     if (!IsSessionConfiged()) {
184         MEDIA_ERR_LOG("CaptureSession::AddInput operation Not allowed!");
185         return CameraErrorCode::OPERATION_NOT_ALLOWED;
186     }
187     if (input == nullptr) {
188         MEDIA_ERR_LOG("CaptureSession::AddInput input is null");
189         return ServiceToCameraError(CAMERA_INVALID_ARG);
190     }
191     input->SetSession(this);
192     inputDevice_ = input;
193     int32_t errCode = CAMERA_UNKNOWN_ERROR;
194     if (captureSession_) {
195         errCode = captureSession_->AddInput(((sptr<CameraInput> &)input)->GetCameraDevice());
196         if (errCode != CAMERA_OK) {
197             MEDIA_ERR_LOG("Failed to AddInput!, %{public}d", errCode);
198         }
199     } else {
200         MEDIA_ERR_LOG("CaptureSession::AddInput() captureSession_ is nullptr");
201     }
202     return ServiceToCameraError(errCode);
203 }
204 
CanAddOutput(sptr<CaptureOutput> & output)205 int32_t CaptureSession::CanAddOutput(sptr<CaptureOutput> &output)
206 {
207     // todo: get Profile passed to createOutput and compare with OutputCapability
208     // if present in capability return ok.
209     return CameraErrorCode::SUCCESS;
210 }
211 
AddOutput(sptr<CaptureOutput> & output)212 int32_t CaptureSession::AddOutput(sptr<CaptureOutput> &output)
213 {
214     CAMERA_SYNC_TRACE;
215     MEDIA_DEBUG_LOG("Enter Into CaptureSession::AddOutput");
216     if (!IsSessionConfiged()) {
217         MEDIA_ERR_LOG("CaptureSession::AddOutput operation Not allowed!");
218         return CameraErrorCode::OPERATION_NOT_ALLOWED;
219     }
220     if (output == nullptr) {
221         MEDIA_ERR_LOG("CaptureSession::AddOutput output is null");
222         return ServiceToCameraError(CAMERA_INVALID_ARG);
223     }
224     output->SetSession(this);
225     if (output->GetOutputType() == CAPTURE_OUTPUT_TYPE_VIDEO) {
226         SetFrameRateRange(static_cast<VideoOutput *>(output.GetRefPtr())->GetFrameRateRange());
227     }
228     int32_t errCode = CAMERA_UNKNOWN_ERROR;
229     if (captureSession_) {
230         errCode = captureSession_->AddOutput(output->GetStreamType(), output->GetStream());
231         if (errCode != CAMERA_OK) {
232             MEDIA_ERR_LOG("Failed to AddOutput!, %{public}d", errCode);
233         }
234     } else {
235         MEDIA_ERR_LOG("CaptureSession::AddOutput() captureSession_ is nullptr");
236     }
237     return ServiceToCameraError(errCode);
238 }
239 
RemoveInput(sptr<CaptureInput> & input)240 int32_t CaptureSession::RemoveInput(sptr<CaptureInput> &input)
241 {
242     CAMERA_SYNC_TRACE;
243     MEDIA_DEBUG_LOG("Enter Into CaptureSession::RemoveInput");
244     if (!IsSessionConfiged()) {
245         MEDIA_ERR_LOG("CaptureSession::RemoveInput operation Not allowed!");
246         return CameraErrorCode::OPERATION_NOT_ALLOWED;
247     }
248     if (input == nullptr) {
249         MEDIA_ERR_LOG("CaptureSession::RemoveInput input is null");
250         return ServiceToCameraError(CAMERA_INVALID_ARG);
251     }
252     if (inputDevice_ != nullptr) {
253         inputDevice_ = nullptr;
254     }
255     int32_t errCode = CAMERA_UNKNOWN_ERROR;
256     if (captureSession_) {
257         errCode = captureSession_->RemoveInput(((sptr<CameraInput> &)input)->GetCameraDevice());
258         if (errCode != CAMERA_OK) {
259             MEDIA_ERR_LOG("Failed to RemoveInput!, %{public}d", errCode);
260         }
261     } else {
262         MEDIA_ERR_LOG("CaptureSession::RemoveInput() captureSession_ is nullptr");
263     }
264     return ServiceToCameraError(errCode);
265 }
266 
RemoveOutput(sptr<CaptureOutput> & output)267 int32_t CaptureSession::RemoveOutput(sptr<CaptureOutput> &output)
268 {
269     CAMERA_SYNC_TRACE;
270     MEDIA_DEBUG_LOG("Enter Into CaptureSession::RemoveOutput");
271     if (!IsSessionConfiged()) {
272         MEDIA_ERR_LOG("CaptureSession::RemoveOutput operation Not allowed!");
273         return CameraErrorCode::OPERATION_NOT_ALLOWED;
274     }
275     if (output == nullptr) {
276         MEDIA_ERR_LOG("CaptureSession::RemoveOutput output is null");
277         return ServiceToCameraError(CAMERA_INVALID_ARG);
278     }
279     output->SetSession(nullptr);
280     int32_t errCode = CAMERA_UNKNOWN_ERROR;
281     if (captureSession_) {
282         errCode = captureSession_->RemoveOutput(output->GetStreamType(), output->GetStream());
283         if (errCode != CAMERA_OK) {
284             MEDIA_ERR_LOG("Failed to RemoveOutput!, %{public}d", errCode);
285         }
286     } else {
287         MEDIA_ERR_LOG("CaptureSession::RemoveOutput() captureSession_ is nullptr");
288     }
289     return ServiceToCameraError(errCode);
290 }
291 
Start()292 int32_t CaptureSession::Start()
293 {
294     CAMERA_SYNC_TRACE;
295     MEDIA_DEBUG_LOG("Enter Into CaptureSession::Start");
296     if (!IsSessionCommited()) {
297         MEDIA_ERR_LOG("CaptureSession::Start Session not Commited");
298         return CameraErrorCode::SESSION_NOT_CONFIG;
299     }
300     int32_t errCode = CAMERA_UNKNOWN_ERROR;
301     if (captureSession_) {
302         errCode = captureSession_->Start();
303         if (errCode != CAMERA_OK) {
304             MEDIA_ERR_LOG("Failed to Start capture session!, %{public}d", errCode);
305         }
306     } else {
307         MEDIA_ERR_LOG("CaptureSession::Start() captureSession_ is nullptr");
308     }
309     return ServiceToCameraError(errCode);
310 }
311 
Stop()312 int32_t CaptureSession::Stop()
313 {
314     CAMERA_SYNC_TRACE;
315     MEDIA_DEBUG_LOG("Enter Into CaptureSession::Stop");
316     int32_t errCode = CAMERA_UNKNOWN_ERROR;
317     if (captureSession_) {
318         errCode = captureSession_->Stop();
319         if (errCode != CAMERA_OK) {
320             MEDIA_ERR_LOG("Failed to Stop capture session!, %{public}d", errCode);
321         }
322     } else {
323         MEDIA_ERR_LOG("CaptureSession::Stop() captureSession_ is nullptr");
324     }
325     return ServiceToCameraError(errCode);
326 }
327 
Release()328 int32_t CaptureSession::Release()
329 {
330     CAMERA_SYNC_TRACE;
331     MEDIA_DEBUG_LOG("Enter Into CaptureSession::Release");
332     int32_t errCode = CAMERA_UNKNOWN_ERROR;
333     if (captureSession_) {
334         errCode = captureSession_->Release(0);
335         MEDIA_DEBUG_LOG("Release capture session, %{public}d", errCode);
336     } else {
337         MEDIA_ERR_LOG("CaptureSession::Release() captureSession_ is nullptr");
338     }
339     inputDevice_ = nullptr;
340     captureSession_ = nullptr;
341     captureSessionCallback_ = nullptr;
342     changedMetadata_ = nullptr;
343     appCallback_ = nullptr;
344     exposureCallback_ = nullptr;
345     focusCallback_ = nullptr;
346     return ServiceToCameraError(errCode);
347 }
348 
SetCallback(std::shared_ptr<SessionCallback> callback)349 void CaptureSession::SetCallback(std::shared_ptr<SessionCallback> callback)
350 {
351     if (callback == nullptr) {
352         MEDIA_ERR_LOG("CaptureSession::SetCallback: Unregistering application callback!");
353     }
354     int32_t errorCode = CAMERA_OK;
355 
356     appCallback_ = callback;
357     if (appCallback_ != nullptr && captureSession_ != nullptr) {
358         if (captureSessionCallback_ == nullptr) {
359             captureSessionCallback_ = new(std::nothrow) CaptureSessionCallback(this);
360         }
361         if (captureSession_) {
362             errorCode = captureSession_->SetCallback(captureSessionCallback_);
363             if (errorCode != CAMERA_OK) {
364                 MEDIA_ERR_LOG("CaptureSession::SetCallback: Failed to register callback, errorCode: %{public}d",
365                     errorCode);
366                 captureSessionCallback_ = nullptr;
367                 appCallback_ = nullptr;
368             }
369         }
370     }
371     return;
372 }
373 
GetApplicationCallback()374 std::shared_ptr<SessionCallback> CaptureSession::GetApplicationCallback()
375 {
376     return appCallback_;
377 }
378 
UpdateSetting(std::shared_ptr<Camera::CameraMetadata> changedMetadata)379 int32_t CaptureSession::UpdateSetting(std::shared_ptr<Camera::CameraMetadata> changedMetadata)
380 {
381     CAMERA_SYNC_TRACE;
382     if (!Camera::GetCameraMetadataItemCount(changedMetadata->get())) {
383         MEDIA_INFO_LOG("CaptureSession::UpdateSetting No configuration to update");
384         return CameraErrorCode::SUCCESS;
385     }
386 
387     if (!inputDevice_ || !((sptr<CameraInput> &)inputDevice_)->GetCameraDevice()) {
388         MEDIA_ERR_LOG("CaptureSession::UpdateSetting Failed inputDevice_ is nullptr");
389         return CameraErrorCode::SUCCESS;
390     }
391     int32_t ret = ((sptr<CameraInput> &)inputDevice_)->GetCameraDevice()->UpdateSetting(changedMetadata);
392     if (ret != CAMERA_OK) {
393         MEDIA_ERR_LOG("CaptureSession::UpdateSetting Failed to update settings, errCode = %{public}d", ret);
394         return ServiceToCameraError(ret);
395     }
396 
397     uint32_t count = changedMetadata->get()->item_count;
398     uint8_t* data = Camera::GetMetadataData(changedMetadata->get());
399     camera_metadata_item_entry_t* itemEntry = Camera::GetMetadataItems(changedMetadata->get());
400     std::shared_ptr<Camera::CameraMetadata> baseMetadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
401     for (uint32_t i = 0; i < count; i++, itemEntry++) {
402         bool status = false;
403         camera_metadata_item_t item;
404         size_t length = Camera::CalculateCameraMetadataItemDataSize(itemEntry->data_type, itemEntry->count);
405         ret = Camera::FindCameraMetadataItem(baseMetadata->get(), itemEntry->item, &item);
406         if (ret == CAM_META_SUCCESS) {
407             status = baseMetadata->updateEntry(itemEntry->item,
408                                                (length == 0) ? itemEntry->data.value : (data + itemEntry->data.offset),
409                                                itemEntry->count);
410         } else if (ret == CAM_META_ITEM_NOT_FOUND) {
411             status = baseMetadata->addEntry(itemEntry->item,
412                                             (length == 0) ? itemEntry->data.value : (data + itemEntry->data.offset),
413                                             itemEntry->count);
414         }
415         if (!status) {
416             MEDIA_ERR_LOG("CaptureSession::UpdateSetting Failed to add/update metadata item: %{public}d",
417                           itemEntry->item);
418         }
419     }
420     return CameraErrorCode::SUCCESS;
421 }
422 
LockForControl()423 void CaptureSession::LockForControl()
424 {
425     changeMetaMutex_.lock();
426     MEDIA_DEBUG_LOG("CaptureSession::LockForControl Called");
427     changedMetadata_ = std::make_shared<Camera::CameraMetadata>(DEFAULT_ITEMS, DEFAULT_DATA_LENGTH);
428 }
429 
UnlockForControl()430 int32_t CaptureSession::UnlockForControl()
431 {
432     if (changedMetadata_ == nullptr) {
433         MEDIA_ERR_LOG("CaptureSession::UnlockForControl Need to call LockForControl() before UnlockForControl()");
434         return ServiceToCameraError(CAMERA_INVALID_ARG);
435     }
436     MEDIA_DEBUG_LOG("CaptureSession::UnlockForControl Called");
437     UpdateSetting(changedMetadata_);
438     changedMetadata_ = nullptr;
439     changeMetaMutex_.unlock();
440     return CameraErrorCode::SUCCESS;
441 }
442 
GetActiveVideoStabilizationMode()443 VideoStabilizationMode CaptureSession::GetActiveVideoStabilizationMode()
444 {
445     sptr<CameraDevice> cameraObj_;
446     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
447         MEDIA_ERR_LOG("CaptureSession::GetActiveVideoStabilizationMode camera device is null");
448         return OFF;
449     }
450     cameraObj_ = inputDevice_->GetCameraDeviceInfo();
451     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
452     camera_metadata_item_t item;
453     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_VIDEO_STABILIZATION_MODE, &item);
454     if (ret == CAM_META_SUCCESS) {
455         auto itr = metaToFwVideoStabModes_.find(static_cast<CameraVideoStabilizationMode>(item.data.u8[0]));
456         if (itr != metaToFwVideoStabModes_.end()) {
457             return itr->second;
458         }
459     }
460     return OFF;
461 }
462 
GetActiveVideoStabilizationMode(VideoStabilizationMode & mode)463 int32_t CaptureSession::GetActiveVideoStabilizationMode(VideoStabilizationMode &mode)
464 {
465     if (!IsSessionCommited()) {
466         MEDIA_ERR_LOG("CaptureSession::GetActiveVideoStabilizationMode Session is not Commited");
467         return CameraErrorCode::SESSION_NOT_CONFIG;
468     }
469     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
470         MEDIA_ERR_LOG("CaptureSession::GetActiveVideoStabilizationMode camera device is null");
471         return CameraErrorCode::SUCCESS;
472     }
473     mode = OFF;
474     bool isSupported = false;
475     sptr<CameraDevice> cameraObj_;
476     cameraObj_ = inputDevice_->GetCameraDeviceInfo();
477     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
478     camera_metadata_item_t item;
479     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_VIDEO_STABILIZATION_MODE, &item);
480     if (ret == CAM_META_SUCCESS) {
481         auto itr = metaToFwVideoStabModes_.find(static_cast<CameraVideoStabilizationMode>(item.data.u8[0]));
482         if (itr != metaToFwVideoStabModes_.end()) {
483             mode = itr->second;
484             isSupported = true;
485         }
486     }
487     if (!isSupported || ret != CAM_META_SUCCESS) {
488         MEDIA_ERR_LOG("CaptureSession::GetActiveVideoStabilizationMode Failed with return code %{public}d", ret);
489     }
490     return CameraErrorCode::SUCCESS;
491 }
492 
SetVideoStabilizationMode(VideoStabilizationMode stabilizationMode)493 int32_t CaptureSession::SetVideoStabilizationMode(VideoStabilizationMode stabilizationMode)
494 {
495     if (!IsSessionCommited()) {
496         MEDIA_ERR_LOG("CaptureSession::SetVideoStabilizationMode Session is not Commited");
497         return CameraErrorCode::SESSION_NOT_CONFIG;
498     }
499     auto itr = fwToMetaVideoStabModes_.find(stabilizationMode);
500     if ((itr == fwToMetaVideoStabModes_.end()) || !IsVideoStabilizationModeSupported(stabilizationMode)) {
501         MEDIA_ERR_LOG("CaptureSession::SetVideoStabilizationMode Mode: %{public}d not supported", stabilizationMode);
502         stabilizationMode = OFF;
503     }
504 
505     uint32_t count = 1;
506     uint8_t stabilizationMode_ = stabilizationMode;
507 
508     this->LockForControl();
509     MEDIA_DEBUG_LOG("CaptureSession::SetVideoStabilizingMode StabilizationMode : %{public}d", stabilizationMode_);
510     if (!(this->changedMetadata_->addEntry(OHOS_CONTROL_VIDEO_STABILIZATION_MODE, &stabilizationMode_, count))) {
511         MEDIA_DEBUG_LOG("CaptureSession::SetVideoStabilizingMode Failed to set video stabilization mode");
512     }
513 
514     int32_t errCode = this->UnlockForControl();
515     if (errCode != CameraErrorCode::SUCCESS) {
516         MEDIA_DEBUG_LOG("CaptureSession::SetVideoStabilizingMode Failed to set video stabilization mode");
517     }
518     return CameraErrorCode::SUCCESS;
519 }
520 
IsVideoStabilizationModeSupported(VideoStabilizationMode stabilizationMode)521 bool CaptureSession::IsVideoStabilizationModeSupported(VideoStabilizationMode stabilizationMode)
522 {
523     std::vector<VideoStabilizationMode> stabilizationModes = GetSupportedStabilizationMode();
524     if (std::find(stabilizationModes.begin(), stabilizationModes.end(), stabilizationMode)
525        != stabilizationModes.end()) {
526         return true;
527     }
528     return false;
529 }
530 
IsVideoStabilizationModeSupported(VideoStabilizationMode stabilizationMode,bool & isSupported)531 int32_t CaptureSession::IsVideoStabilizationModeSupported(VideoStabilizationMode stabilizationMode, bool &isSupported)
532 {
533     if (!IsSessionCommited()) {
534         MEDIA_ERR_LOG("CaptureSession::IsVideoStabilizationModeSupported Session is not Commited");
535         return CameraErrorCode::SESSION_NOT_CONFIG;
536     }
537     std::vector<VideoStabilizationMode> stabilizationModes = GetSupportedStabilizationMode();
538     if (std::find(stabilizationModes.begin(), stabilizationModes.end(), stabilizationMode)
539        != stabilizationModes.end()) {
540         isSupported = true;
541         return CameraErrorCode::SUCCESS;
542     }
543     isSupported = false;
544     return CameraErrorCode::SUCCESS;
545 }
546 
GetSupportedStabilizationMode()547 std::vector<VideoStabilizationMode> CaptureSession::GetSupportedStabilizationMode()
548 {
549     std::vector<VideoStabilizationMode> stabilizationModes;
550 
551     sptr<CameraDevice> cameraObj_;
552     if (!IsSessionCommited()) {
553         MEDIA_ERR_LOG("CaptureSession::GetSupportedStabilizationMode Session is not Commited");
554         return stabilizationModes;
555     }
556     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
557         MEDIA_ERR_LOG("CaptureSession::GetSupportedStabilizationMode camera device is null");
558         return stabilizationModes;
559     }
560     cameraObj_ = inputDevice_->GetCameraDeviceInfo();
561     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
562     camera_metadata_item_t item;
563     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_VIDEO_STABILIZATION_MODES, &item);
564     if (ret != CAM_META_SUCCESS) {
565         MEDIA_ERR_LOG("CaptureSession::GetSupporteStabilizationModes Failed with return code %{public}d", ret);
566         return stabilizationModes;
567     }
568 
569     for (uint32_t i = 0; i < item.count; i++) {
570         auto itr = metaToFwVideoStabModes_.find(static_cast<CameraVideoStabilizationMode>(item.data.u8[i]));
571         if (itr != metaToFwVideoStabModes_.end()) {
572             stabilizationModes.emplace_back(itr->second);
573         }
574     }
575     return stabilizationModes;
576 }
577 
GetSupportedStabilizationMode(std::vector<VideoStabilizationMode> & stabilizationModes)578 int32_t CaptureSession::GetSupportedStabilizationMode(std::vector<VideoStabilizationMode> &stabilizationModes)
579 {
580     sptr<CameraDevice> cameraObj_;
581     stabilizationModes.clear();
582     if (!IsSessionCommited()) {
583         MEDIA_ERR_LOG("CaptureSession::GetSupportedStabilizationMode Session is not Commited");
584         return CameraErrorCode::SESSION_NOT_CONFIG;
585     }
586     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
587         MEDIA_ERR_LOG("CaptureSession::GetSupportedStabilizationMode camera device is null");
588         return CameraErrorCode::SUCCESS;
589     }
590     cameraObj_ = inputDevice_->GetCameraDeviceInfo();
591     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj_->GetMetadata();
592     camera_metadata_item_t item;
593     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_VIDEO_STABILIZATION_MODES, &item);
594     if (ret != CAM_META_SUCCESS) {
595         MEDIA_ERR_LOG("CaptureSession::GetSupporteStabilizationModes Failed with return code %{public}d", ret);
596         return CameraErrorCode::SUCCESS;
597     }
598 
599     for (uint32_t i = 0; i < item.count; i++) {
600         auto itr = metaToFwVideoStabModes_.find(static_cast<CameraVideoStabilizationMode>(item.data.u8[i]));
601         if (itr != metaToFwVideoStabModes_.end()) {
602             stabilizationModes.emplace_back(itr->second);
603         }
604     }
605     return CameraErrorCode::SUCCESS;
606 }
607 
IsExposureModeSupported(ExposureMode exposureMode)608 bool CaptureSession::IsExposureModeSupported(ExposureMode exposureMode)
609 {
610     std::vector<ExposureMode> vecSupportedExposureModeList;
611     vecSupportedExposureModeList = this->GetSupportedExposureModes();
612     if (find(vecSupportedExposureModeList.begin(), vecSupportedExposureModeList.end(),
613         exposureMode) != vecSupportedExposureModeList.end()) {
614         return true;
615     }
616 
617     return false;
618 }
619 
IsExposureModeSupported(ExposureMode exposureMode,bool & isSupported)620 int32_t CaptureSession::IsExposureModeSupported(ExposureMode exposureMode, bool &isSupported)
621 {
622     if (!IsSessionCommited()) {
623         MEDIA_ERR_LOG("CaptureSession::IsExposureModeSupported Session is not Commited");
624         return CameraErrorCode::SESSION_NOT_CONFIG;
625     }
626     std::vector<ExposureMode> vecSupportedExposureModeList;
627     vecSupportedExposureModeList = this->GetSupportedExposureModes();
628     if (find(vecSupportedExposureModeList.begin(), vecSupportedExposureModeList.end(),
629         exposureMode) != vecSupportedExposureModeList.end()) {
630         isSupported = true;
631         return CameraErrorCode::SUCCESS;
632     }
633     isSupported = false;
634     return CameraErrorCode::SUCCESS;
635 }
636 
GetSupportedExposureModes()637 std::vector<ExposureMode> CaptureSession::GetSupportedExposureModes()
638 {
639     if (!IsSessionCommited()) {
640         MEDIA_ERR_LOG("CaptureSession::GetSupportedExposureModes Session is not Commited");
641         return {};
642     }
643     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
644         MEDIA_ERR_LOG("CaptureSession::GetSupportedExposureModes camera device is null");
645         return {};
646     }
647     std::vector<ExposureMode> supportedExposureModes;
648     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
649     camera_metadata_item_t item;
650     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_EXPOSURE_MODES, &item);
651     if (ret != CAM_META_SUCCESS) {
652         MEDIA_ERR_LOG("CaptureSession::GetSupportedExposureModes Failed with return code %{public}d", ret);
653         return supportedExposureModes;
654     }
655 
656     for (uint32_t i = 0; i < item.count; i++) {
657         auto itr = metaToFwExposureMode_.find(static_cast<camera_exposure_mode_enum_t>(item.data.u8[i]));
658         if (itr != metaToFwExposureMode_.end()) {
659             supportedExposureModes.emplace_back(itr->second);
660         }
661     }
662     return supportedExposureModes;
663 }
664 
GetSupportedExposureModes(std::vector<ExposureMode> & supportedExposureModes)665 int32_t CaptureSession::GetSupportedExposureModes(std::vector<ExposureMode> &supportedExposureModes)
666 {
667     supportedExposureModes.clear();
668     if (!IsSessionCommited()) {
669         MEDIA_ERR_LOG("CaptureSession::GetSupportedExposureModes Session is not Commited");
670         return CameraErrorCode::SESSION_NOT_CONFIG;
671     }
672     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
673         MEDIA_ERR_LOG("CaptureSession::GetSupportedExposureModes camera device is null");
674         return CameraErrorCode::SUCCESS;
675     }
676     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
677     camera_metadata_item_t item;
678     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_EXPOSURE_MODES, &item);
679     if (ret != CAM_META_SUCCESS) {
680         MEDIA_ERR_LOG("CaptureSession::GetSupportedExposureModes Failed with return code %{public}d", ret);
681         return CameraErrorCode::SUCCESS;
682     }
683 
684     for (uint32_t i = 0; i < item.count; i++) {
685         auto itr = metaToFwExposureMode_.find(static_cast<camera_exposure_mode_enum_t>(item.data.u8[i]));
686         if (itr != metaToFwExposureMode_.end()) {
687             supportedExposureModes.emplace_back(itr->second);
688         }
689     }
690     return CameraErrorCode::SUCCESS;
691 }
692 
SetExposureMode(ExposureMode exposureMode)693 int32_t CaptureSession::SetExposureMode(ExposureMode exposureMode)
694 {
695     CAMERA_SYNC_TRACE;
696     if (!IsSessionCommited()) {
697         MEDIA_ERR_LOG("CaptureSession::SetExposureMode Session is not Commited");
698         return CameraErrorCode::SESSION_NOT_CONFIG;
699     }
700 
701     if (changedMetadata_ == nullptr) {
702         MEDIA_ERR_LOG("CaptureSession::SetExposureMode Need to call LockForControl() "
703                       "before setting camera properties");
704         return CameraErrorCode::SUCCESS;
705     }
706     uint8_t exposure = fwToMetaExposureMode_.at(EXPOSURE_MODE_LOCKED);
707     auto itr = fwToMetaExposureMode_.find(exposureMode);
708     if (itr == fwToMetaExposureMode_.end()) {
709         MEDIA_ERR_LOG("CaptureSession::SetExposureMode Unknown exposure mode");
710     } else {
711         exposure = itr->second;
712     }
713 
714     bool status = false;
715     uint32_t count = 1;
716     camera_metadata_item_t item;
717     int ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_EXPOSURE_MODE, &item);
718     if (ret == CAM_META_ITEM_NOT_FOUND) {
719         status = changedMetadata_->addEntry(OHOS_CONTROL_EXPOSURE_MODE, &exposure, count);
720     } else if (ret == CAM_META_SUCCESS) {
721         status = changedMetadata_->updateEntry(OHOS_CONTROL_EXPOSURE_MODE, &exposure, count);
722     }
723 
724     if (!status) {
725         MEDIA_ERR_LOG("CaptureSession::SetExposureMode Failed to set exposure mode");
726     }
727 
728     return CameraErrorCode::SUCCESS;
729 }
730 
GetExposureMode()731 ExposureMode CaptureSession::GetExposureMode()
732 {
733     if (!IsSessionCommited()) {
734         MEDIA_ERR_LOG("CaptureSession::GetExposureMode Session is not Commited");
735         return EXPOSURE_MODE_UNSUPPORTED;
736     }
737     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
738         MEDIA_ERR_LOG("CaptureSession::GetExposureMode camera device is null");
739         return EXPOSURE_MODE_UNSUPPORTED;
740     }
741     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
742     camera_metadata_item_t item;
743     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_EXPOSURE_MODE, &item);
744     if (ret != CAM_META_SUCCESS) {
745         MEDIA_ERR_LOG("CaptureSession::GetExposureMode Failed with return code %{public}d", ret);
746         return EXPOSURE_MODE_UNSUPPORTED;
747     }
748     auto itr = metaToFwExposureMode_.find(static_cast<camera_exposure_mode_enum_t>(item.data.u8[0]));
749     if (itr != metaToFwExposureMode_.end()) {
750         return itr->second;
751     }
752 
753     return EXPOSURE_MODE_UNSUPPORTED;
754 }
755 
GetExposureMode(ExposureMode & exposureMode)756 int32_t CaptureSession::GetExposureMode(ExposureMode &exposureMode)
757 {
758     exposureMode = EXPOSURE_MODE_UNSUPPORTED;
759     if (!IsSessionCommited()) {
760         MEDIA_ERR_LOG("CaptureSession::GetExposureMode Session is not Commited");
761         return CameraErrorCode::SESSION_NOT_CONFIG;
762     }
763     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
764         MEDIA_ERR_LOG("CaptureSession::GetExposureMode camera device is null");
765         return CameraErrorCode::SUCCESS;
766     }
767     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
768     camera_metadata_item_t item;
769     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_EXPOSURE_MODE, &item);
770     if (ret != CAM_META_SUCCESS) {
771         MEDIA_ERR_LOG("CaptureSession::GetExposureMode Failed with return code %{public}d", ret);
772         return CameraErrorCode::SUCCESS;
773     }
774     auto itr = metaToFwExposureMode_.find(static_cast<camera_exposure_mode_enum_t>(item.data.u8[0]));
775     if (itr != metaToFwExposureMode_.end()) {
776         exposureMode = itr->second;
777         return CameraErrorCode::SUCCESS;
778     }
779     return CameraErrorCode::SUCCESS;
780 }
781 
782 
SetMeteringPoint(Point exposurePoint)783 int32_t CaptureSession::SetMeteringPoint(Point exposurePoint)
784 {
785     if (!IsSessionCommited()) {
786         MEDIA_ERR_LOG("CaptureSession::SetMeteringPoint Session is not Commited");
787         return CameraErrorCode::SESSION_NOT_CONFIG;
788     }
789 
790     if (changedMetadata_ == nullptr) {
791         MEDIA_ERR_LOG("CaptureSession::SetExposurePoint Need to call LockForControl() "
792             "before setting camera properties");
793         return CameraErrorCode::SUCCESS;
794     }
795     Point exposureVerifyPoint = VerifyFocusCorrectness(exposurePoint);
796     Point unifyExposurePoint = CoordinateTransform(exposureVerifyPoint);
797     bool status = false;
798     float exposureArea[2] = {unifyExposurePoint.x, unifyExposurePoint.y};
799     camera_metadata_item_t item;
800 
801     int ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_AE_REGIONS, &item);
802     if (ret == CAM_META_ITEM_NOT_FOUND) {
803         status = changedMetadata_->addEntry(OHOS_CONTROL_AE_REGIONS, exposureArea,
804             sizeof(exposureArea) / sizeof(exposureArea[0]));
805     } else if (ret == CAM_META_SUCCESS) {
806         status = changedMetadata_->updateEntry(OHOS_CONTROL_AE_REGIONS, exposureArea,
807             sizeof(exposureArea) / sizeof(exposureArea[0]));
808     }
809 
810     if (!status) {
811         MEDIA_ERR_LOG("CaptureSession::SetExposurePoint Failed to set exposure Area");
812     }
813     return CameraErrorCode::SUCCESS;
814 }
815 
GetMeteringPoint()816 Point CaptureSession::GetMeteringPoint()
817 {
818     Point exposurePoint = {0, 0};
819     if (!IsSessionCommited()) {
820         MEDIA_ERR_LOG("CaptureSession::GetMeteringPoint Session is not Commited");
821         return exposurePoint;
822     }
823     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
824         MEDIA_ERR_LOG("CaptureSession::GetMeteringPoint camera device is null");
825         return exposurePoint;
826     }
827     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
828     camera_metadata_item_t item;
829     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AE_REGIONS, &item);
830     if (ret != CAM_META_SUCCESS) {
831         MEDIA_ERR_LOG("CaptureSession::GetExposurePoint Failed with return code %{public}d", ret);
832         return exposurePoint;
833     }
834     exposurePoint.x = item.data.f[0];
835     exposurePoint.y = item.data.f[1];
836     Point unifyExposurePoint = CoordinateTransform(exposurePoint);
837     return unifyExposurePoint;
838 }
839 
GetMeteringPoint(Point & exposurePoint)840 int32_t CaptureSession::GetMeteringPoint(Point &exposurePoint)
841 {
842     exposurePoint.x = 0;
843     exposurePoint.y = 0;
844     if (!IsSessionCommited()) {
845         MEDIA_ERR_LOG("CaptureSession::GetMeteringPoint Session is not Commited");
846         return CameraErrorCode::SESSION_NOT_CONFIG;
847     }
848     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
849         MEDIA_ERR_LOG("CaptureSession::GetMeteringPoint camera device is null");
850         return CameraErrorCode::SUCCESS;
851     }
852     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
853     camera_metadata_item_t item;
854     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AE_REGIONS, &item);
855     if (ret != CAM_META_SUCCESS) {
856         MEDIA_ERR_LOG("CaptureSession::GetExposurePoint Failed with return code %{public}d", ret);
857         return CameraErrorCode::SUCCESS;
858     }
859     exposurePoint.x = item.data.f[0];
860     exposurePoint.y = item.data.f[1];
861     exposurePoint = CoordinateTransform(exposurePoint);
862     return CameraErrorCode::SUCCESS;
863 }
864 
GetExposureBiasRange()865 std::vector<float> CaptureSession::GetExposureBiasRange()
866 {
867     if (!IsSessionCommited()) {
868         MEDIA_ERR_LOG("CaptureSession::GetExposureBiasRange Session is not Commited");
869         return {};
870     }
871     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
872         MEDIA_ERR_LOG("CaptureSession::GetExposureBiasRange camera device is null");
873         return {};
874     }
875     return inputDevice_->GetCameraDeviceInfo()->GetExposureBiasRange();
876 }
877 
GetExposureBiasRange(std::vector<float> & exposureBiasRange)878 int32_t CaptureSession::GetExposureBiasRange(std::vector<float> &exposureBiasRange)
879 {
880     if (!IsSessionCommited()) {
881         MEDIA_ERR_LOG("CaptureSession::GetExposureBiasRange Session is not Commited");
882         return CameraErrorCode::SESSION_NOT_CONFIG;
883     }
884     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
885         MEDIA_ERR_LOG("CaptureSession::GetExposureBiasRange camera device is null");
886         return CameraErrorCode::SUCCESS;
887     }
888     exposureBiasRange = inputDevice_->GetCameraDeviceInfo()->GetExposureBiasRange();
889     return CameraErrorCode::SUCCESS;
890 }
891 
892 
SetExposureBias(float exposureValue)893 int32_t CaptureSession::SetExposureBias(float exposureValue)
894 {
895     if (!IsSessionCommited()) {
896         MEDIA_ERR_LOG("CaptureSession::SetExposureBias Session is not Commited");
897         return CameraErrorCode::SESSION_NOT_CONFIG;
898     }
899     if (changedMetadata_ == nullptr) {
900         MEDIA_ERR_LOG("CaptureSession::SetExposureValue Need to call LockForControl() "
901             "before setting camera properties");
902         return CameraErrorCode::SUCCESS;
903     }
904     bool status = false;
905     int32_t minIndex = 0;
906     int32_t maxIndex = 1;
907     int32_t count = 1;
908     camera_metadata_item_t item;
909     MEDIA_DEBUG_LOG("CaptureSession::SetExposureValue exposure compensation: %{public}f", exposureValue);
910     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
911         MEDIA_ERR_LOG("CaptureSession::SetExposureBias camera device is null");
912         return CameraErrorCode::OPERATION_NOT_ALLOWED;
913     }
914     std::vector<float> biasRange = inputDevice_->GetCameraDeviceInfo()->GetExposureBiasRange();
915     if (biasRange.empty()) {
916         MEDIA_ERR_LOG("CaptureSession::SetExposureValue Bias range is empty");
917         return CameraErrorCode::OPERATION_NOT_ALLOWED;
918     }
919     if (exposureValue < biasRange[minIndex]) {
920         MEDIA_DEBUG_LOG("CaptureSession::SetExposureValue bias value:"
921                         "%{public}f is lesser than minimum bias: %{public}f", exposureValue, biasRange[minIndex]);
922         exposureValue = biasRange[minIndex];
923     } else if (exposureValue > biasRange[maxIndex]) {
924         MEDIA_DEBUG_LOG("CaptureSession::SetExposureValue bias value: "
925                         "%{public}f is greater than maximum bias: %{public}f", exposureValue, biasRange[maxIndex]);
926         exposureValue = biasRange[maxIndex];
927     }
928     if (std::abs(exposureValue) <= 1e-6) {
929         MEDIA_ERR_LOG("CaptureSession::SetExposureValue exposure compensation value no need to change");
930         return CameraErrorCode::SUCCESS;
931     }
932 
933     int32_t exposureCompensation = CalculateExposureValue(exposureValue);
934 
935     int ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, &item);
936     if (ret == CAM_META_ITEM_NOT_FOUND) {
937         status = changedMetadata_->addEntry(OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, &exposureCompensation, count);
938     } else if (ret == CAM_META_SUCCESS) {
939         status = changedMetadata_->updateEntry(OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, &exposureCompensation, count);
940     }
941     if (!status) {
942         MEDIA_ERR_LOG("CaptureSession::SetExposureValue Failed to set exposure compensation");
943     }
944     return CameraErrorCode::SUCCESS;
945 }
946 
GetExposureValue()947 float CaptureSession::GetExposureValue()
948 {
949     if (!IsSessionCommited()) {
950         MEDIA_ERR_LOG("CaptureSession::GetExposureValue Session is not Commited");
951         return 0;
952     }
953     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
954         MEDIA_ERR_LOG("CaptureSession::GetExposureValue camera device is null");
955         return 0;
956     }
957     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
958     camera_metadata_item_t item;
959     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, &item);
960     if (ret != CAM_META_SUCCESS) {
961         MEDIA_ERR_LOG("CaptureSession::GetExposureValue Failed with return code %{public}d", ret);
962         return 0;
963     }
964     int32_t exposureCompensation = item.data.i32[0];
965 
966     ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AE_COMPENSATION_STEP, &item);
967     if (ret != CAM_META_SUCCESS) {
968         MEDIA_ERR_LOG("CaptureSession::GetExposureValue Failed with return code %{public}d", ret);
969         return 0;
970     }
971     int32_t stepNumerator = item.data.r->numerator;
972     int32_t stepDenominator = item.data.r->denominator;
973     float exposureValue = 0;
974     if (stepDenominator != 0) {
975         float step = static_cast<float>(stepNumerator) / static_cast<float>(stepDenominator);
976         exposureValue = step * exposureCompensation;
977     } else {
978         MEDIA_ERR_LOG("stepDenominator: %{public}d", stepDenominator);
979     }
980     MEDIA_DEBUG_LOG("exposureValue: %{public}f", exposureValue);
981 
982     return exposureValue;
983 }
984 
GetExposureValue(float & exposureValue)985 int32_t CaptureSession::GetExposureValue(float &exposureValue)
986 {
987     if (!IsSessionCommited()) {
988         MEDIA_ERR_LOG("CaptureSession::GetExposureValue Session is not Commited");
989         return CameraErrorCode::SESSION_NOT_CONFIG;
990     }
991     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
992         MEDIA_ERR_LOG("CaptureSession::GetExposureValue camera device is null");
993         return CameraErrorCode::SUCCESS;
994     }
995     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
996     camera_metadata_item_t item;
997     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, &item);
998     if (ret != CAM_META_SUCCESS) {
999         MEDIA_ERR_LOG("CaptureSession::GetExposureValue Failed with return code %{public}d", ret);
1000         return CameraErrorCode::SUCCESS;
1001     }
1002     int32_t exposureCompensation = item.data.i32[0];
1003 
1004     ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AE_COMPENSATION_STEP, &item);
1005     if (ret != CAM_META_SUCCESS) {
1006         MEDIA_ERR_LOG("CaptureSession::GetExposureValue Failed with return code %{public}d", ret);
1007         return 0;
1008     }
1009     int32_t stepNumerator = item.data.r->numerator;
1010     int32_t stepDenominator = item.data.r->denominator;
1011     float step = static_cast<float>(stepNumerator) / static_cast<float>(stepDenominator);
1012 
1013     exposureValue = step * exposureCompensation;
1014     MEDIA_DEBUG_LOG("exposureValue: %{public}f", exposureValue);
1015     return CameraErrorCode::SUCCESS;
1016 }
1017 
SetExposureCallback(std::shared_ptr<ExposureCallback> exposureCallback)1018 void CaptureSession::SetExposureCallback(std::shared_ptr<ExposureCallback> exposureCallback)
1019 {
1020     exposureCallback_ = exposureCallback;
1021 }
1022 
ProcessAutoExposureUpdates(const std::shared_ptr<Camera::CameraMetadata> & result)1023 void CaptureSession::ProcessAutoExposureUpdates(const std::shared_ptr<Camera::CameraMetadata> &result)
1024 {
1025     camera_metadata_item_t item;
1026     common_metadata_header_t* metadata = result->get();
1027 
1028     int ret = Camera::FindCameraMetadataItem(metadata, OHOS_CONTROL_EXPOSURE_MODE, &item);
1029     if (ret == CAM_META_SUCCESS) {
1030         MEDIA_DEBUG_LOG("exposure mode: %{public}d", item.data.u8[0]);
1031     }
1032 
1033     ret = Camera::FindCameraMetadataItem(metadata, OHOS_CONTROL_EXPOSURE_STATE, &item);
1034     if (ret == CAM_META_SUCCESS) {
1035         MEDIA_INFO_LOG("Exposure state: %{public}d", item.data.u8[0]);
1036         if (exposureCallback_ != nullptr) {
1037             auto itr = metaToFwExposureState_.find(static_cast<camera_exposure_state_t>(item.data.u8[0]));
1038             if (itr != metaToFwExposureState_.end()) {
1039                 exposureCallback_->OnExposureState(itr->second);
1040             }
1041         }
1042     }
1043 }
1044 
GetSupportedFocusModes()1045 std::vector<FocusMode> CaptureSession::GetSupportedFocusModes()
1046 {
1047     std::vector<FocusMode> supportedFocusModes = {};
1048     if (!IsSessionCommited()) {
1049         MEDIA_ERR_LOG("CaptureSession::SetExposureBias Session is not Commited");
1050         return supportedFocusModes;
1051     }
1052     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1053         MEDIA_ERR_LOG("CaptureSession::GetSupportedFocusModes camera device is null");
1054         return supportedFocusModes;
1055     }
1056     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1057     camera_metadata_item_t item;
1058     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_FOCUS_MODES, &item);
1059     if (ret != CAM_META_SUCCESS) {
1060         MEDIA_ERR_LOG("CaptureSession::GetSupportedFocusModes Failed with return code %{public}d", ret);
1061         return supportedFocusModes;
1062     }
1063     for (uint32_t i = 0; i < item.count; i++) {
1064         auto itr = metaToFwFocusMode_.find(static_cast<camera_focus_mode_enum_t>(item.data.u8[i]));
1065         if (itr != metaToFwFocusMode_.end()) {
1066             supportedFocusModes.emplace_back(itr->second);
1067         }
1068     }
1069     return supportedFocusModes;
1070 }
1071 
GetSupportedFocusModes(std::vector<FocusMode> & supportedFocusModes)1072 int32_t CaptureSession::GetSupportedFocusModes(std::vector<FocusMode> &supportedFocusModes)
1073 {
1074     supportedFocusModes.clear();
1075     if (!IsSessionCommited()) {
1076         MEDIA_ERR_LOG("CaptureSession::SetExposureBias Session is not Commited");
1077         return CameraErrorCode::SESSION_NOT_CONFIG;
1078     }
1079     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1080         MEDIA_ERR_LOG("CaptureSession::GetSupportedFocusModes camera device is null");
1081         return CameraErrorCode::SUCCESS;
1082     }
1083     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1084     camera_metadata_item_t item;
1085     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_FOCUS_MODES, &item);
1086     if (ret != CAM_META_SUCCESS) {
1087         MEDIA_ERR_LOG("CaptureSession::GetSupportedFocusModes Failed with return code %{public}d", ret);
1088         return CameraErrorCode::SUCCESS;
1089     }
1090     for (uint32_t i = 0; i < item.count; i++) {
1091         auto itr = metaToFwFocusMode_.find(static_cast<camera_focus_mode_enum_t>(item.data.u8[i]));
1092         if (itr != metaToFwFocusMode_.end()) {
1093             supportedFocusModes.emplace_back(itr->second);
1094             return CameraErrorCode::SUCCESS;
1095         }
1096     }
1097     return CameraErrorCode::SUCCESS;
1098 }
1099 
SetFocusCallback(std::shared_ptr<FocusCallback> focusCallback)1100 void CaptureSession::SetFocusCallback(std::shared_ptr<FocusCallback> focusCallback)
1101 {
1102     focusCallback_ = focusCallback;
1103     return;
1104 }
1105 
IsFocusModeSupported(FocusMode focusMode)1106 bool CaptureSession::IsFocusModeSupported(FocusMode focusMode)
1107 {
1108     std::vector<FocusMode> vecSupportedFocusModeList;
1109     vecSupportedFocusModeList = this->GetSupportedFocusModes();
1110     if (find(vecSupportedFocusModeList.begin(), vecSupportedFocusModeList.end(),
1111         focusMode) != vecSupportedFocusModeList.end()) {
1112         return true;
1113     }
1114 
1115     return false;
1116 }
1117 
IsFocusModeSupported(FocusMode focusMode,bool & isSupported)1118 int32_t CaptureSession::IsFocusModeSupported(FocusMode focusMode, bool &isSupported)
1119 {
1120     if (!IsSessionCommited()) {
1121         MEDIA_ERR_LOG("CaptureSession::SetExposureBias Session is not Commited");
1122         return CameraErrorCode::SESSION_NOT_CONFIG;
1123     }
1124     std::vector<FocusMode> vecSupportedFocusModeList;
1125     vecSupportedFocusModeList = this->GetSupportedFocusModes();
1126     if (find(vecSupportedFocusModeList.begin(), vecSupportedFocusModeList.end(),
1127         focusMode) != vecSupportedFocusModeList.end()) {
1128         isSupported = true;
1129         return CameraErrorCode::SUCCESS;
1130     }
1131     isSupported = false;
1132     return CameraErrorCode::SUCCESS;
1133 }
1134 
SetFocusMode(FocusMode focusMode)1135 int32_t CaptureSession::SetFocusMode(FocusMode focusMode)
1136 {
1137     CAMERA_SYNC_TRACE;
1138     if (!IsSessionCommited()) {
1139         MEDIA_ERR_LOG("CaptureSession::SetFocusMode Session is not Commited");
1140         return CameraErrorCode::SESSION_NOT_CONFIG;
1141     }
1142     if (changedMetadata_ == nullptr) {
1143         MEDIA_ERR_LOG("CaptureSession::SetFocusMode Need to call LockForControl() before setting camera properties");
1144         return CameraErrorCode::SUCCESS;
1145     }
1146     uint8_t focus = FOCUS_MODE_LOCKED;
1147     auto itr = fwToMetaFocusMode_.find(focusMode);
1148     if (itr == fwToMetaFocusMode_.end()) {
1149         MEDIA_ERR_LOG("CaptureSession::SetExposureMode Unknown exposure mode");
1150     } else {
1151         focus = itr->second;
1152     }
1153     bool status = false;
1154     int32_t ret;
1155     uint32_t count = 1;
1156     camera_metadata_item_t item;
1157 
1158     MEDIA_DEBUG_LOG("CaptureSession::SetFocusMode Focus mode: %{public}d", focusMode);
1159 
1160     ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_FOCUS_MODE, &item);
1161     if (ret == CAM_META_ITEM_NOT_FOUND) {
1162         status = changedMetadata_->addEntry(OHOS_CONTROL_FOCUS_MODE, &focus, count);
1163     } else if (ret == CAM_META_SUCCESS) {
1164         status = changedMetadata_->updateEntry(OHOS_CONTROL_FOCUS_MODE, &focus, count);
1165     }
1166 
1167     if (!status) {
1168         MEDIA_ERR_LOG("CaptureSession::SetFocusMode Failed to set focus mode");
1169     }
1170     return CameraErrorCode::SUCCESS;
1171 }
1172 
GetFocusMode()1173 FocusMode CaptureSession::GetFocusMode()
1174 {
1175     if (!IsSessionCommited()) {
1176         MEDIA_ERR_LOG("CaptureSession::GetFocusMode Session is not Commited");
1177         return FOCUS_MODE_MANUAL;
1178     }
1179     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1180         MEDIA_ERR_LOG("CaptureSession::GetFocusMode camera device is null");
1181         return FOCUS_MODE_MANUAL;
1182     }
1183     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1184     camera_metadata_item_t item;
1185     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_FOCUS_MODE, &item);
1186     if (ret != CAM_META_SUCCESS) {
1187         MEDIA_ERR_LOG("CaptureSession::GetFocusMode Failed with return code %{public}d", ret);
1188         return FOCUS_MODE_MANUAL;
1189     }
1190     auto itr = metaToFwFocusMode_.find(static_cast<camera_focus_mode_enum_t>(item.data.u8[0]));
1191     if (itr != metaToFwFocusMode_.end()) {
1192         return itr->second;
1193     }
1194     return FOCUS_MODE_MANUAL;
1195 }
1196 
GetFocusMode(FocusMode & focusMode)1197 int32_t CaptureSession::GetFocusMode(FocusMode &focusMode)
1198 {
1199     focusMode = FOCUS_MODE_MANUAL;
1200     if (!IsSessionCommited()) {
1201         MEDIA_ERR_LOG("CaptureSession::GetFocusMode Session is not Commited");
1202         return CameraErrorCode::SESSION_NOT_CONFIG;
1203     }
1204     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1205         MEDIA_ERR_LOG("CaptureSession::GetFocusMode camera device is null");
1206         return CameraErrorCode::SUCCESS;
1207     }
1208     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1209     camera_metadata_item_t item;
1210     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_FOCUS_MODE, &item);
1211     if (ret != CAM_META_SUCCESS) {
1212         MEDIA_ERR_LOG("CaptureSession::GetFocusMode Failed with return code %{public}d", ret);
1213         return CameraErrorCode::SUCCESS;
1214     }
1215     auto itr = metaToFwFocusMode_.find(static_cast<camera_focus_mode_enum_t>(item.data.u8[0]));
1216     if (itr != metaToFwFocusMode_.end()) {
1217         focusMode = itr->second;
1218         return CameraErrorCode::SUCCESS;
1219     }
1220     return CameraErrorCode::SUCCESS;
1221 }
1222 
SetFocusPoint(Point focusPoint)1223 int32_t CaptureSession::SetFocusPoint(Point focusPoint)
1224 {
1225     if (!IsSessionCommited()) {
1226         MEDIA_ERR_LOG("CaptureSession::SetFocusPoint Session is not Commited");
1227         return CameraErrorCode::SESSION_NOT_CONFIG;
1228     }
1229     if (changedMetadata_ == nullptr) {
1230         MEDIA_ERR_LOG("CaptureSession::SetFocusPoint Need to call LockForControl() before setting camera properties");
1231         return CameraErrorCode::SUCCESS;
1232     }
1233     Point focusVerifyPoint = VerifyFocusCorrectness(focusPoint);
1234     Point unifyFocusPoint = CoordinateTransform(focusVerifyPoint);
1235     bool status = false;
1236     float FocusArea[2] = {unifyFocusPoint.x, unifyFocusPoint.y};
1237     camera_metadata_item_t item;
1238 
1239     int ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_AF_REGIONS, &item);
1240     if (ret == CAM_META_ITEM_NOT_FOUND) {
1241         status = changedMetadata_->addEntry(OHOS_CONTROL_AF_REGIONS, FocusArea,
1242             sizeof(FocusArea) / sizeof(FocusArea[0]));
1243     } else if (ret == CAM_META_SUCCESS) {
1244         status = changedMetadata_->updateEntry(OHOS_CONTROL_AF_REGIONS, FocusArea,
1245             sizeof(FocusArea) / sizeof(FocusArea[0]));
1246     }
1247 
1248     if (!status) {
1249         MEDIA_ERR_LOG("CaptureSession::SetFocusPoint Failed to set Focus Area");
1250     }
1251     return CameraErrorCode::SUCCESS;
1252 }
1253 
CoordinateTransform(Point point)1254 Point CaptureSession::CoordinateTransform(Point point)
1255 {
1256     MEDIA_DEBUG_LOG("CaptureSession::CoordinateTransform begin x: %{public}f, y: %{public}f", point.x, point.y);
1257     Point unifyPoint = point;
1258     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1259         MEDIA_ERR_LOG("CaptureSession::CoordinateTransform cameraInput is nullptr");
1260         return unifyPoint;
1261     }
1262     if (inputDevice_->GetCameraDeviceInfo()->GetPosition() == CAMERA_POSITION_FRONT) {
1263         unifyPoint.x = 1 - unifyPoint.x; // flip horizontally
1264     }
1265     MEDIA_DEBUG_LOG("CaptureSession::CoordinateTransform end x: %{public}f, y: %{public}f",
1266                     unifyPoint.x, unifyPoint.y);
1267     return unifyPoint;
1268 }
1269 
VerifyFocusCorrectness(Point point)1270 Point CaptureSession::VerifyFocusCorrectness(Point point)
1271 {
1272     MEDIA_DEBUG_LOG("CaptureSession::VerifyFocusCorrectness begin x: %{public}f, y: %{public}f", point.x, point.y);
1273     float minPoint = 0.0000001;
1274     float maxPoint = 1;
1275     Point VerifyPoint = point;
1276     if (VerifyPoint.x >= -minPoint && VerifyPoint.x <= minPoint) {
1277         VerifyPoint.x = minPoint;
1278     } else if (VerifyPoint.x > maxPoint) {
1279         VerifyPoint.x = maxPoint;
1280     }
1281     if (VerifyPoint.y >= -minPoint && VerifyPoint.y <= minPoint) {
1282         VerifyPoint.y = minPoint;
1283     } else if (VerifyPoint.y > maxPoint) {
1284         VerifyPoint.y = maxPoint;
1285     }
1286     MEDIA_DEBUG_LOG("CaptureSession::VerifyFocusCorrectness end x: %{public}f, y: %{public}f",
1287                     VerifyPoint.x, VerifyPoint.y);
1288     return VerifyPoint;
1289 }
1290 
GetFocusPoint()1291 Point CaptureSession::GetFocusPoint()
1292 {
1293     Point focusPoint = {0, 0};
1294     if (!IsSessionCommited()) {
1295         MEDIA_ERR_LOG("CaptureSession::GetFocusPoint Session is not Commited");
1296         return focusPoint;
1297     }
1298     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1299         MEDIA_ERR_LOG("CaptureSession::GetFocusPoint camera device is null");
1300         return focusPoint;
1301     }
1302     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1303     camera_metadata_item_t item;
1304     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AF_REGIONS, &item);
1305     if (ret != CAM_META_SUCCESS) {
1306         MEDIA_ERR_LOG("CaptureSession::GetFocusPoint Failed with return code %{public}d", ret);
1307         return focusPoint;
1308     }
1309     focusPoint.x = item.data.f[0];
1310     focusPoint.y = item.data.f[1];
1311     Point unifyFocusPoint = CoordinateTransform(focusPoint);
1312     return unifyFocusPoint;
1313 }
1314 
GetFocusPoint(Point & focusPoint)1315 int32_t CaptureSession::GetFocusPoint(Point &focusPoint)
1316 {
1317     focusPoint.x = 0;
1318     focusPoint.y = 0;
1319     if (!IsSessionCommited()) {
1320         MEDIA_ERR_LOG("CaptureSession::GetFocusPoint Session is not Commited");
1321         return CameraErrorCode::SESSION_NOT_CONFIG;
1322     }
1323     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1324         MEDIA_ERR_LOG("CaptureSession::GetFocusPoint camera device is null");
1325         return CameraErrorCode::SUCCESS;
1326     }
1327     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1328     camera_metadata_item_t item;
1329     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AF_REGIONS, &item);
1330     if (ret != CAM_META_SUCCESS) {
1331         MEDIA_ERR_LOG("CaptureSession::GetFocusPoint Failed with return code %{public}d", ret);
1332         return CameraErrorCode::SUCCESS;
1333     }
1334     focusPoint.x = item.data.f[0];
1335     focusPoint.y = item.data.f[1];
1336     focusPoint = CoordinateTransform(focusPoint);
1337     return CameraErrorCode::SUCCESS;
1338 }
1339 
GetFocalLength()1340 float CaptureSession::GetFocalLength()
1341 {
1342     if (!IsSessionCommited()) {
1343         MEDIA_ERR_LOG("CaptureSession::GetFocalLength Session is not Commited");
1344         return 0;
1345     }
1346     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1347         MEDIA_ERR_LOG("CaptureSession::GetFocalLength camera device is null");
1348         return 0;
1349     }
1350     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1351     camera_metadata_item_t item;
1352     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_FOCAL_LENGTH, &item);
1353     if (ret != CAM_META_SUCCESS) {
1354         MEDIA_ERR_LOG("CaptureSession::GetFocalLength Failed with return code %{public}d", ret);
1355         return 0;
1356     }
1357     return static_cast<float>(item.data.f[0]);
1358 }
1359 
GetFocalLength(float & focalLength)1360 int32_t CaptureSession::GetFocalLength(float &focalLength)
1361 {
1362     focalLength = 0;
1363     if (!IsSessionCommited()) {
1364         MEDIA_ERR_LOG("CaptureSession::GetFocalLength Session is not Commited");
1365         return CameraErrorCode::SESSION_NOT_CONFIG;
1366     }
1367     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1368         MEDIA_ERR_LOG("CaptureSession::GetFocalLength camera device is null");
1369         return CameraErrorCode::SUCCESS;
1370     }
1371     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1372     camera_metadata_item_t item;
1373     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_FOCAL_LENGTH, &item);
1374     if (ret != CAM_META_SUCCESS) {
1375         MEDIA_ERR_LOG("CaptureSession::GetFocalLength Failed with return code %{public}d", ret);
1376         return CameraErrorCode::SUCCESS;
1377     }
1378     focalLength = static_cast<float>(item.data.f[0]);
1379     return CameraErrorCode::SUCCESS;
1380 }
1381 
ProcessAutoFocusUpdates(const std::shared_ptr<Camera::CameraMetadata> & result)1382 void CaptureSession::ProcessAutoFocusUpdates(const std::shared_ptr<Camera::CameraMetadata> &result)
1383 {
1384     camera_metadata_item_t item;
1385     common_metadata_header_t* metadata = result->get();
1386     int ret = Camera::FindCameraMetadataItem(metadata, OHOS_CONTROL_FOCUS_MODE, &item);
1387     if (ret != CAM_META_SUCCESS) {
1388         MEDIA_DEBUG_LOG("Camera not support Focus mode");
1389         return;
1390     }
1391     MEDIA_DEBUG_LOG("Focus mode: %{public}d", item.data.u8[0]);
1392     auto it = metaToFwFocusMode_.find(static_cast<camera_focus_mode_enum_t>(item.data.u8[0]));
1393     // continuous focus mode do not callback focusStateChange
1394     if (it == metaToFwFocusMode_.end() || it->second != FOCUS_MODE_AUTO) {
1395         return;
1396     }
1397     ret = Camera::FindCameraMetadataItem(metadata, OHOS_CONTROL_FOCUS_STATE, &item);
1398     if (ret == CAM_META_SUCCESS) {
1399         MEDIA_DEBUG_LOG("Focus state: %{public}d", item.data.u8[0]);
1400         if (focusCallback_ != nullptr) {
1401             auto itr = metaToFwFocusState_.find(static_cast<camera_focus_state_t>(item.data.u8[0]));
1402             if (itr != metaToFwFocusState_.end() && itr->second != focusCallback_->currentState) {
1403                 focusCallback_->OnFocusState(itr->second);
1404                 focusCallback_->currentState = itr->second;
1405             }
1406         }
1407     }
1408 }
1409 
GetSupportedFlashModes()1410 std::vector<FlashMode> CaptureSession::GetSupportedFlashModes()
1411 {
1412     std::vector<FlashMode> supportedFlashModes = {};
1413     if (!IsSessionCommited()) {
1414         MEDIA_ERR_LOG("CaptureSession::GetSupportedFlashModes Session is not Commited");
1415         return supportedFlashModes;
1416     }
1417     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1418         MEDIA_ERR_LOG("CaptureSession::GetSupportedFlashModes camera device is null");
1419         return supportedFlashModes;
1420     }
1421     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1422     camera_metadata_item_t item;
1423     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_FLASH_MODES, &item);
1424     if (ret != CAM_META_SUCCESS) {
1425         MEDIA_ERR_LOG("CaptureSession::GetSupportedFlashModes Failed with return code %{public}d", ret);
1426         return supportedFlashModes;
1427     }
1428     for (uint32_t i = 0; i < item.count; i++) {
1429         auto itr = metaToFwFlashMode_.find(static_cast<camera_flash_mode_enum_t>(item.data.u8[i]));
1430         if (itr != metaToFwFlashMode_.end()) {
1431             supportedFlashModes.emplace_back(itr->second);
1432         }
1433     }
1434     return supportedFlashModes;
1435 }
1436 
GetSupportedFlashModes(std::vector<FlashMode> & supportedFlashModes)1437 int32_t CaptureSession::GetSupportedFlashModes(std::vector<FlashMode> &supportedFlashModes)
1438 {
1439     supportedFlashModes.clear();
1440     if (!IsSessionCommited()) {
1441         MEDIA_ERR_LOG("CaptureSession::GetSupportedFlashModes Session is not Commited");
1442         return CameraErrorCode::SESSION_NOT_CONFIG;
1443     }
1444     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1445         MEDIA_ERR_LOG("CaptureSession::GetSupportedFlashModes camera device is null");
1446         return CameraErrorCode::SUCCESS;
1447     }
1448     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1449     camera_metadata_item_t item;
1450     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_FLASH_MODES, &item);
1451     if (ret != CAM_META_SUCCESS) {
1452         MEDIA_ERR_LOG("CaptureSession::GetSupportedFlashModes Failed with return code %{public}d", ret);
1453         return CameraErrorCode::SUCCESS;
1454     }
1455     for (uint32_t i = 0; i < item.count; i++) {
1456         auto itr = metaToFwFlashMode_.find(static_cast<camera_flash_mode_enum_t>(item.data.u8[i]));
1457         if (itr != metaToFwFlashMode_.end()) {
1458             supportedFlashModes.emplace_back(itr->second);
1459         }
1460     }
1461     return CameraErrorCode::SUCCESS;
1462 }
1463 
GetFlashMode()1464 FlashMode CaptureSession::GetFlashMode()
1465 {
1466     if (!IsSessionCommited()) {
1467         MEDIA_ERR_LOG("CaptureSession::GetFlashMode Session is not Commited");
1468         return FLASH_MODE_CLOSE;
1469     }
1470     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1471         MEDIA_ERR_LOG("CaptureSession::GetFlashMode camera device is null");
1472         return FLASH_MODE_CLOSE;
1473     }
1474     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1475     camera_metadata_item_t item;
1476     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_FLASH_MODE, &item);
1477     if (ret != CAM_META_SUCCESS) {
1478         MEDIA_ERR_LOG("CaptureSession::GetFlashMode Failed with return code %{public}d", ret);
1479         return FLASH_MODE_CLOSE;
1480     }
1481     auto itr = metaToFwFlashMode_.find(static_cast<camera_flash_mode_enum_t>(item.data.u8[0]));
1482     if (itr != metaToFwFlashMode_.end()) {
1483         return itr->second;
1484     }
1485 
1486     return FLASH_MODE_CLOSE;
1487 }
1488 
GetFlashMode(FlashMode & flashMode)1489 int32_t CaptureSession::GetFlashMode(FlashMode &flashMode)
1490 {
1491     flashMode = FLASH_MODE_CLOSE;
1492     if (!IsSessionCommited()) {
1493         MEDIA_ERR_LOG("CaptureSession::GetFlashMode Session is not Commited");
1494         return CameraErrorCode::SESSION_NOT_CONFIG;
1495     }
1496     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1497         MEDIA_ERR_LOG("CaptureSession::GetFlashMode camera device is null");
1498         return CameraErrorCode::SUCCESS;
1499     }
1500     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1501     camera_metadata_item_t item;
1502     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_FLASH_MODE, &item);
1503     if (ret != CAM_META_SUCCESS) {
1504         MEDIA_ERR_LOG("CaptureSession::GetFlashMode Failed with return code %{public}d", ret);
1505         return CameraErrorCode::SUCCESS;
1506     }
1507     auto itr = metaToFwFlashMode_.find(static_cast<camera_flash_mode_enum_t>(item.data.u8[0]));
1508     if (itr != metaToFwFlashMode_.end()) {
1509         flashMode = itr->second;
1510         return CameraErrorCode::SUCCESS;
1511     }
1512 
1513     return CameraErrorCode::SUCCESS;
1514 }
1515 
SetFlashMode(FlashMode flashMode)1516 int32_t CaptureSession::SetFlashMode(FlashMode flashMode)
1517 {
1518     CAMERA_SYNC_TRACE;
1519     if (!IsSessionCommited()) {
1520         MEDIA_ERR_LOG("CaptureSession::SetFlashMode Session is not Commited");
1521         return CameraErrorCode::SESSION_NOT_CONFIG;
1522     }
1523     if (changedMetadata_ == nullptr) {
1524         MEDIA_ERR_LOG("CaptureSession::SetFlashMode Need to call LockForControl() before setting camera properties");
1525         return CameraErrorCode::SUCCESS;
1526     }
1527     uint8_t flash = fwToMetaFlashMode_.at(FLASH_MODE_CLOSE);
1528     auto itr = fwToMetaFlashMode_.find(flashMode);
1529     if (itr == fwToMetaFlashMode_.end()) {
1530         MEDIA_ERR_LOG("CaptureSession::SetExposureMode Unknown exposure mode");
1531     } else {
1532         flash = itr->second;
1533     }
1534 
1535     bool status = false;
1536     uint32_t count = 1;
1537     camera_metadata_item_t item;
1538     int ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_FLASH_MODE, &item);
1539     if (ret == CAM_META_ITEM_NOT_FOUND) {
1540         status = changedMetadata_->addEntry(OHOS_CONTROL_FLASH_MODE, &flash, count);
1541     } else if (ret == CAM_META_SUCCESS) {
1542         status = changedMetadata_->updateEntry(OHOS_CONTROL_FLASH_MODE, &flash, count);
1543     }
1544 
1545     if (!status) {
1546         MEDIA_ERR_LOG("CaptureSession::SetFlashMode Failed to set flash mode");
1547         return CameraErrorCode::SUCCESS;
1548     }
1549 
1550     if (flashMode == FLASH_MODE_CLOSE) {
1551         POWERMGR_SYSEVENT_FLASH_OFF();
1552     } else {
1553         POWERMGR_SYSEVENT_FLASH_ON();
1554     }
1555     return CameraErrorCode::SUCCESS;
1556 }
1557 
IsFlashModeSupported(FlashMode flashMode)1558 bool CaptureSession::IsFlashModeSupported(FlashMode flashMode)
1559 {
1560     std::vector<FlashMode> vecSupportedFlashModeList;
1561     vecSupportedFlashModeList = this->GetSupportedFlashModes();
1562     if (find(vecSupportedFlashModeList.begin(), vecSupportedFlashModeList.end(), flashMode) !=
1563         vecSupportedFlashModeList.end()) {
1564         return true;
1565     }
1566 
1567     return false;
1568 }
1569 
IsFlashModeSupported(FlashMode flashMode,bool & isSupported)1570 int32_t CaptureSession::IsFlashModeSupported(FlashMode flashMode, bool &isSupported)
1571 {
1572     if (!IsSessionCommited()) {
1573         MEDIA_ERR_LOG("CaptureSession::IsFlashModeSupported Session is not Commited");
1574         return CameraErrorCode::SESSION_NOT_CONFIG;
1575     }
1576     std::vector<FlashMode> vecSupportedFlashModeList;
1577     vecSupportedFlashModeList = this->GetSupportedFlashModes();
1578     if (find(vecSupportedFlashModeList.begin(), vecSupportedFlashModeList.end(), flashMode) !=
1579         vecSupportedFlashModeList.end()) {
1580         isSupported = true;
1581         return CameraErrorCode::SUCCESS;
1582     }
1583     isSupported = false;
1584     return CameraErrorCode::SUCCESS;
1585 }
1586 
HasFlash()1587 bool CaptureSession::HasFlash()
1588 {
1589     std::vector<FlashMode> vecSupportedFlashModeList;
1590     vecSupportedFlashModeList = this->GetSupportedFlashModes();
1591     if (vecSupportedFlashModeList.empty()) {
1592         return false;
1593     }
1594     return true;
1595 }
1596 
HasFlash(bool & hasFlash)1597 int32_t CaptureSession::HasFlash(bool &hasFlash)
1598 {
1599     if (!IsSessionCommited()) {
1600         MEDIA_ERR_LOG("CaptureSession::HasFlash Session is not Commited");
1601         return CameraErrorCode::SESSION_NOT_CONFIG;
1602     }
1603     std::vector<FlashMode> vecSupportedFlashModeList;
1604     vecSupportedFlashModeList = this->GetSupportedFlashModes();
1605     if (vecSupportedFlashModeList.empty()) {
1606         hasFlash = false;
1607         return CameraErrorCode::SUCCESS;
1608     }
1609     hasFlash = true;
1610     return CameraErrorCode::SUCCESS;
1611 }
1612 
GetZoomRatioRange()1613 std::vector<float> CaptureSession::GetZoomRatioRange()
1614 {
1615     if (!IsSessionCommited()) {
1616         MEDIA_ERR_LOG("CaptureSession::GetZoomRatioRange Session is not Commited");
1617         return {};
1618     }
1619     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1620         MEDIA_ERR_LOG("CaptureSession::GetZoomRatioRange camera device is null");
1621         return {};
1622     }
1623     return inputDevice_->GetCameraDeviceInfo()->GetZoomRatioRange();
1624 }
1625 
GetZoomRatioRange(std::vector<float> & zoomRatioRange)1626 int32_t CaptureSession::GetZoomRatioRange(std::vector<float> &zoomRatioRange)
1627 {
1628     zoomRatioRange.clear();
1629     if (!IsSessionCommited()) {
1630         MEDIA_ERR_LOG("CaptureSession::GetZoomRatioRange Session is not Commited");
1631         return CameraErrorCode::SESSION_NOT_CONFIG;
1632     }
1633     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1634         MEDIA_ERR_LOG("CaptureSession::GetZoomRatioRange camera device is null");
1635         return CameraErrorCode::SUCCESS;
1636     }
1637     zoomRatioRange = inputDevice_->GetCameraDeviceInfo()->GetZoomRatioRange();
1638     return CameraErrorCode::SUCCESS;
1639 }
1640 
GetZoomRatio()1641 float CaptureSession::GetZoomRatio()
1642 {
1643     if (!IsSessionCommited()) {
1644         MEDIA_ERR_LOG("CaptureSession::GetZoomRatio Session is not Commited");
1645         return 0;
1646     }
1647     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1648         MEDIA_ERR_LOG("CaptureSession::GetZoomRatio camera device is null");
1649         return 0;
1650     }
1651     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1652     camera_metadata_item_t item;
1653     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_ZOOM_RATIO, &item);
1654     if (ret != CAM_META_SUCCESS) {
1655         MEDIA_ERR_LOG("CaptureSession::GetZoomRatio Failed with return code %{public}d", ret);
1656         return 0;
1657     }
1658     return static_cast<float>(item.data.f[0]);
1659 }
1660 
GetZoomRatio(float & zoomRatio)1661 int32_t CaptureSession::GetZoomRatio(float &zoomRatio)
1662 {
1663     zoomRatio = 0;
1664     if (!IsSessionCommited()) {
1665         MEDIA_ERR_LOG("CaptureSession::GetZoomRatio Session is not Commited");
1666         return CameraErrorCode::SESSION_NOT_CONFIG;
1667     }
1668     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1669         MEDIA_ERR_LOG("CaptureSession::GetZoomRatio camera device is null");
1670         return CameraErrorCode::SUCCESS;
1671     }
1672     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1673     camera_metadata_item_t item;
1674     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_ZOOM_RATIO, &item);
1675     if (ret != CAM_META_SUCCESS) {
1676         MEDIA_ERR_LOG("CaptureSession::GetZoomRatio Failed with return code %{public}d", ret);
1677         return CameraErrorCode::SUCCESS;
1678     }
1679     zoomRatio = static_cast<float>(item.data.f[0]);
1680     return CameraErrorCode::SUCCESS;
1681 }
1682 
SetZoomRatio(float zoomRatio)1683 int32_t CaptureSession::SetZoomRatio(float zoomRatio)
1684 {
1685     CAMERA_SYNC_TRACE;
1686     if (!IsSessionCommited()) {
1687         MEDIA_ERR_LOG("CaptureSession::SetZoomRatio Session is not Commited");
1688         return CameraErrorCode::SESSION_NOT_CONFIG;
1689     }
1690     if (changedMetadata_ == nullptr) {
1691         MEDIA_ERR_LOG("CaptureSession::SetZoomRatio Need to call LockForControl() before setting camera properties");
1692         return CameraErrorCode::SUCCESS;
1693     }
1694 
1695     bool status = false;
1696     int32_t ret;
1697     int32_t minIndex = 0;
1698     int32_t maxIndex = 1;
1699     int32_t count = 1;
1700     camera_metadata_item_t item;
1701     MEDIA_DEBUG_LOG("CaptureSession::SetZoomRatio Zoom ratio: %{public}f", zoomRatio);
1702     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1703         MEDIA_ERR_LOG("CaptureSession::SetZoomRatio camera device is null");
1704         return CameraErrorCode::SUCCESS;
1705     }
1706     std::vector<float> zoomRange = inputDevice_->GetCameraDeviceInfo()->GetZoomRatioRange();
1707     if (zoomRange.empty()) {
1708         MEDIA_ERR_LOG("CaptureSession::SetZoomRatio Zoom range is empty");
1709         return CameraErrorCode::SUCCESS;
1710     }
1711     if (zoomRatio < zoomRange[minIndex]) {
1712         MEDIA_DEBUG_LOG("CaptureSession::SetZoomRatio Zoom ratio: %{public}f is lesser than minimum zoom: %{public}f",
1713                         zoomRatio, zoomRange[minIndex]);
1714         zoomRatio = zoomRange[minIndex];
1715     } else if (zoomRatio > zoomRange[maxIndex]) {
1716         MEDIA_DEBUG_LOG("CaptureSession::SetZoomRatio Zoom ratio: %{public}f is greater than maximum zoom: %{public}f",
1717                         zoomRatio, zoomRange[maxIndex]);
1718         zoomRatio = zoomRange[maxIndex];
1719     }
1720 
1721     if (zoomRatio == 0) {
1722         MEDIA_ERR_LOG("CaptureSession::SetZoomRatio Invalid zoom ratio");
1723         return CameraErrorCode::SUCCESS;
1724     }
1725 
1726     ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_ZOOM_RATIO, &item);
1727     if (ret == CAM_META_ITEM_NOT_FOUND) {
1728         status = changedMetadata_->addEntry(OHOS_CONTROL_ZOOM_RATIO, &zoomRatio, count);
1729     } else if (ret == CAM_META_SUCCESS) {
1730         status = changedMetadata_->updateEntry(OHOS_CONTROL_ZOOM_RATIO, &zoomRatio, count);
1731     }
1732 
1733     if (!status) {
1734         MEDIA_ERR_LOG("CaptureSession::SetZoomRatio Failed to set zoom mode");
1735     }
1736     return CameraErrorCode::SUCCESS;
1737 }
1738 
SetCaptureMetadataObjectTypes(std::set<camera_face_detect_mode_t> metadataObjectTypes)1739 void CaptureSession::SetCaptureMetadataObjectTypes(std::set<camera_face_detect_mode_t> metadataObjectTypes)
1740 {
1741     if (inputDevice_ == nullptr) {
1742         MEDIA_ERR_LOG("SetCaptureMetadataObjectTypes: inputDevice is null");
1743         return;
1744     }
1745     uint32_t count = 0;
1746     uint8_t objectTypes[metadataObjectTypes.size()];
1747     for (const auto &type : metadataObjectTypes) {
1748         objectTypes[count++] = type;
1749     }
1750     this->LockForControl();
1751     if (!this->changedMetadata_->addEntry(OHOS_STATISTICS_FACE_DETECT_SWITCH, objectTypes, count)) {
1752         MEDIA_ERR_LOG("SetCaptureMetadataObjectTypes: Failed to add detect object types to changed metadata");
1753     }
1754     this->UnlockForControl();
1755 }
1756 
getSupportedFilters()1757 std::vector<FilterType> CaptureSession::getSupportedFilters()
1758 {
1759     if (!IsSessionCommited()) {
1760         MEDIA_ERR_LOG("CaptureSession::getSupportedFilters Session is not Commited");
1761         return {};
1762     }
1763     if (!inputDevice_ || !inputDevice_->GetCameraDeviceInfo()) {
1764         MEDIA_ERR_LOG("CaptureSession::getSupportedFilters camera device is null");
1765         return {};
1766     }
1767     std::vector<FilterType> supportedFilters;
1768     return supportedFilters;
1769 }
1770 
getFilter()1771 FilterType CaptureSession::getFilter()
1772 {
1773     return FilterType::NONE;
1774 }
1775 
setFilter(FilterType filter)1776 void CaptureSession::setFilter(FilterType filter)
1777 {
1778     return;
1779 }
1780 
getSupportedBeautyTypes()1781 std::vector<BeautyType> CaptureSession::getSupportedBeautyTypes()
1782 {
1783     std::vector<BeautyType> supportedBeautyTypes;
1784     return supportedBeautyTypes;
1785 }
1786 
getSupportedBeautyRange(BeautyType type)1787 std::vector<int32_t> CaptureSession::getSupportedBeautyRange(BeautyType type)
1788 {
1789     std::vector<int32_t> supportedBeautyRange;
1790     return supportedBeautyRange;
1791 }
1792 
setBeauty(BeautyType type,int value)1793 void CaptureSession::setBeauty(BeautyType type, int value)
1794 {
1795     return;
1796 }
1797 
getBeauty(BeautyType type)1798 int32_t CaptureSession::getBeauty(BeautyType type)
1799 {
1800     return 0;
1801 }
1802 
SetFrameRateRange(const std::vector<int32_t> & frameRateRange)1803 void CaptureSession::SetFrameRateRange(const std::vector<int32_t>& frameRateRange)
1804 {
1805     std::vector<int32_t> videoFrameRateRange = frameRateRange;
1806     this->LockForControl();
1807     if (!this->changedMetadata_->addEntry(OHOS_CONTROL_FPS_RANGES,
1808         videoFrameRateRange.data(), videoFrameRateRange.size())) {
1809         MEDIA_ERR_LOG("Failed to SetFrameRateRange");
1810     }
1811     this->UnlockForControl();
1812 }
1813 
IsSessionConfiged()1814 bool CaptureSession::IsSessionConfiged()
1815 {
1816     bool isSessionConfiged = false;
1817     if (captureSession_) {
1818         CaptureSessionState currentState;
1819         captureSession_->GetSessionState(currentState);
1820         isSessionConfiged = (currentState == CaptureSessionState::SESSION_CONFIG_INPROGRESS);
1821     }
1822     return isSessionConfiged;
1823 }
1824 
IsSessionCommited()1825 bool CaptureSession::IsSessionCommited()
1826 {
1827     bool isCommitConfig = false;
1828     if (captureSession_) {
1829         CaptureSessionState currentState;
1830         captureSession_->GetSessionState(currentState);
1831         isCommitConfig = (currentState == CaptureSessionState::SESSION_CONFIG_COMMITTED);
1832     }
1833     return isCommitConfig;
1834 }
1835 
CalculateExposureValue(float exposureValue)1836 int32_t CaptureSession::CalculateExposureValue(float exposureValue)
1837 {
1838     camera_metadata_item_t item;
1839     std::shared_ptr<Camera::CameraMetadata> metadata = inputDevice_->GetCameraDeviceInfo()->GetMetadata();
1840     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AE_COMPENSATION_STEP, &item);
1841     if (ret != CAM_META_SUCCESS) {
1842         MEDIA_ERR_LOG("CaptureSession::Get Ae Compensation step Failed with return code %{public}d", ret);
1843         return CameraErrorCode::OPERATION_NOT_ALLOWED;
1844     }
1845 
1846     int32_t stepNumerator = item.data.r->numerator;
1847     int32_t stepDenominator = item.data.r->denominator;
1848     float stepsPerEv = static_cast<float>(stepDenominator) / static_cast<float>(stepNumerator);
1849     MEDIA_DEBUG_LOG("Exposure step numerator: %{public}d, denominatormax: %{public}d, stepsPerEv: %{public}f",
1850         stepNumerator, stepDenominator, stepsPerEv);
1851 
1852     int32_t exposureCompensation = static_cast<int32_t>(stepsPerEv * exposureValue);
1853     MEDIA_DEBUG_LOG("exposureCompensation: %{public}d", exposureCompensation);
1854     return exposureCompensation;
1855 }
1856 } // CameraStandard
1857 } // OHOS
1858