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