• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_for_sys.h"
17 
18 #include "camera_log.h"
19 #include "metadata_common_utils.h"
20 
21 namespace OHOS {
22 namespace CameraStandard {
23 
24 namespace {
25 constexpr float DEFAULT_EQUIVALENT_ZOOM = 100.0f;
26 } // namespace
27 
28 const std::unordered_map<EffectSuggestionType, CameraEffectSuggestionType>
29     CaptureSessionForSys::fwkEffectSuggestionTypeMap_ = {
30     {EFFECT_SUGGESTION_NONE, OHOS_CAMERA_EFFECT_SUGGESTION_NONE},
31     {EFFECT_SUGGESTION_PORTRAIT, OHOS_CAMERA_EFFECT_SUGGESTION_PORTRAIT},
32     {EFFECT_SUGGESTION_FOOD, OHOS_CAMERA_EFFECT_SUGGESTION_FOOD},
33     {EFFECT_SUGGESTION_SKY, OHOS_CAMERA_EFFECT_SUGGESTION_SKY},
34     {EFFECT_SUGGESTION_SUNRISE_SUNSET, OHOS_CAMERA_EFFECT_SUGGESTION_SUNRISE_SUNSET},
35     {EFFECT_SUGGESTION_STAGE, OHOS_CAMERA_EFFECT_SUGGESTION_STAGE}
36 };
37 
~CaptureSessionForSys()38 CaptureSessionForSys::~CaptureSessionForSys()
39 {
40     MEDIA_DEBUG_LOG("Enter Into CaptureSessionForSys::~CaptureSessionForSys()");
41 }
42 
IsFocusRangeTypeSupported(FocusRangeType focusRangeType,bool & isSupported)43 int32_t CaptureSessionForSys::IsFocusRangeTypeSupported(FocusRangeType focusRangeType, bool& isSupported)
44 {
45     // LCOV_EXCL_START
46     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
47         "CaptureSessionForSys::IsFocusRangeTypeSupported Session is not Commited");
48     CHECK_RETURN_RET_ELOG(g_fwkocusRangeTypeMap_.find(focusRangeType) == g_fwkocusRangeTypeMap_.end(),
49         CameraErrorCode::PARAMETER_ERROR, "CaptureSessionForSys::IsFocusRangeTypeSupported Unknown focus range type");
50     std::vector<FocusRangeType> vecSupportedFocusRangeTypeList = {};
51     this->GetSupportedFocusRangeTypes(vecSupportedFocusRangeTypeList);
52     if (find(vecSupportedFocusRangeTypeList.begin(), vecSupportedFocusRangeTypeList.end(), focusRangeType) !=
53         vecSupportedFocusRangeTypeList.end()) {
54         isSupported = true;
55         return CameraErrorCode::SUCCESS;
56     }
57     isSupported = false;
58     return CameraErrorCode::SUCCESS;
59     // LCOV_EXCL_STOP
60 }
61 
GetFocusRange(FocusRangeType & focusRangeType)62 int32_t CaptureSessionForSys::GetFocusRange(FocusRangeType& focusRangeType)
63 {
64     // LCOV_EXCL_START
65     focusRangeType = FocusRangeType::FOCUS_RANGE_TYPE_AUTO;
66     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
67         "CaptureSessionForSys::GetFocusRange Session is not Commited");
68     auto inputDevice = GetInputDevice();
69     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(), CameraErrorCode::SUCCESS,
70         "CaptureSessionForSys::GetFocusRange camera device is null");
71     std::shared_ptr<Camera::CameraMetadata> metadata = GetMetadata();
72     CHECK_RETURN_RET_ELOG(metadata == nullptr, CameraErrorCode::SUCCESS,
73         "GetFocusRange camera metadata is null");
74     camera_metadata_item_t item;
75     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_FOCUS_RANGE_TYPE, &item);
76     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, CameraErrorCode::SUCCESS,
77         "CaptureSessionForSys::GetFocusRange Failed with return code %{public}d", ret);
78     auto itr = g_metaFocusRangeTypeMap_.find(static_cast<camera_focus_range_type_t>(item.data.u8[0]));
79     if (itr != g_metaFocusRangeTypeMap_.end()) {
80         focusRangeType = itr->second;
81         return CameraErrorCode::SUCCESS;
82     }
83     return CameraErrorCode::SUCCESS;
84     // LCOV_EXCL_STOP
85 }
86 
SetFocusRange(FocusRangeType focusRangeType)87 int32_t CaptureSessionForSys::SetFocusRange(FocusRangeType focusRangeType)
88 {
89     // LCOV_EXCL_START
90     CAMERA_SYNC_TRACE;
91     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
92         "CaptureSessionForSys::SetFocusRange Session is not Commited");
93     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
94         "CaptureSessionForSys::SetFocusRange Need to call LockForControl() before setting camera properties");
95     CHECK_RETURN_RET_ELOG(g_fwkocusRangeTypeMap_.find(focusRangeType) == g_fwkocusRangeTypeMap_.end(),
96         CameraErrorCode::PARAMETER_ERROR, "CaptureSessionForSys::SetFocusRange Unknown focus range type");
97     bool isSupported = false;
98     IsFocusRangeTypeSupported(focusRangeType, isSupported);
99     CHECK_RETURN_RET(!isSupported, CameraErrorCode::OPERATION_NOT_ALLOWED);
100     uint8_t metaFocusRangeType = OHOS_CAMERA_FOCUS_RANGE_AUTO;
101     auto itr = g_fwkocusRangeTypeMap_.find(focusRangeType);
102     if (itr != g_fwkocusRangeTypeMap_.end()) {
103         metaFocusRangeType = itr->second;
104     }
105 
106     MEDIA_DEBUG_LOG("CaptureSessionForSys::SetFocusRange Focus range type: %{public}d", focusRangeType);
107     bool status = AddOrUpdateMetadata(changedMetadata_, OHOS_CONTROL_FOCUS_RANGE_TYPE, &metaFocusRangeType, 1);
108     CHECK_PRINT_ELOG(!status, "CaptureSessionForSys::SetFocusRange Failed to set focus range type");
109     return CameraErrorCode::SUCCESS;
110     // LCOV_EXCL_STOP
111 }
112 
IsFocusDrivenTypeSupported(FocusDrivenType focusDrivenType,bool & isSupported)113 int32_t CaptureSessionForSys::IsFocusDrivenTypeSupported(FocusDrivenType focusDrivenType, bool& isSupported)
114 {
115     // LCOV_EXCL_START
116     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
117         "CaptureSessionForSys::IsFocusDrivenTypeSupported Session is not Commited");
118     CHECK_RETURN_RET_ELOG(g_fwkFocusDrivenTypeMap_.find(focusDrivenType) == g_fwkFocusDrivenTypeMap_.end(),
119         CameraErrorCode::PARAMETER_ERROR, "CaptureSessionForSys::IsFocusDrivenTypeSupported Unknown focus driven type");
120     std::vector<FocusDrivenType> vecSupportedFocusDrivenTypeList = {};
121     this->GetSupportedFocusDrivenTypes(vecSupportedFocusDrivenTypeList);
122     if (find(vecSupportedFocusDrivenTypeList.begin(), vecSupportedFocusDrivenTypeList.end(), focusDrivenType) !=
123         vecSupportedFocusDrivenTypeList.end()) {
124         isSupported = true;
125         return CameraErrorCode::SUCCESS;
126     }
127     isSupported = false;
128     return CameraErrorCode::SUCCESS;
129     // LCOV_EXCL_STOP
130 }
131 
GetFocusDriven(FocusDrivenType & focusDrivenType)132 int32_t CaptureSessionForSys::GetFocusDriven(FocusDrivenType& focusDrivenType)
133 {
134     // LCOV_EXCL_START
135     focusDrivenType = FocusDrivenType::FOCUS_DRIVEN_TYPE_AUTO;
136     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
137         "CaptureSessionForSys::GetFocusDriven Session is not Commited");
138     auto inputDevice = GetInputDevice();
139     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(), CameraErrorCode::SUCCESS,
140         "CaptureSessionForSys::GetFocusDriven camera device is null");
141     std::shared_ptr<Camera::CameraMetadata> metadata = GetMetadata();
142     CHECK_RETURN_RET_ELOG(metadata == nullptr, CameraErrorCode::SUCCESS,
143         "GetFocusDriven camera metadata is null");
144     camera_metadata_item_t item;
145     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_FOCUS_DRIVEN_TYPE, &item);
146     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, CameraErrorCode::SUCCESS,
147         "CaptureSessionForSys::GetFocusDriven Failed with return code %{public}d", ret);
148     auto itr = g_metaFocusDrivenTypeMap_.find(static_cast<camera_focus_driven_type_t>(item.data.u8[0]));
149     if (itr != g_metaFocusDrivenTypeMap_.end()) {
150         focusDrivenType = itr->second;
151         return CameraErrorCode::SUCCESS;
152     }
153     return CameraErrorCode::SUCCESS;
154     // LCOV_EXCL_STOP
155 }
156 
SetFocusDriven(FocusDrivenType focusDrivenType)157 int32_t CaptureSessionForSys::SetFocusDriven(FocusDrivenType focusDrivenType)
158 {
159     // LCOV_EXCL_START
160     CAMERA_SYNC_TRACE;
161     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
162         "CaptureSessionForSys::SetFocusDriven Session is not Commited");
163     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
164         "CaptureSessionForSys::SetFocusDriven Need to call LockForControl() before setting camera properties");
165     CHECK_RETURN_RET_ELOG(g_fwkFocusDrivenTypeMap_.find(focusDrivenType) == g_fwkFocusDrivenTypeMap_.end(),
166         CameraErrorCode::PARAMETER_ERROR, "CaptureSessionForSys::SetFocusDriven Unknown focus driven type");
167     bool isSupported = false;
168     IsFocusDrivenTypeSupported(focusDrivenType, isSupported);
169     CHECK_RETURN_RET(!isSupported, CameraErrorCode::OPERATION_NOT_ALLOWED);
170     uint8_t metaFocusDrivenType = OHOS_CAMERA_FOCUS_DRIVEN_AUTO;
171     auto itr = g_fwkFocusDrivenTypeMap_.find(focusDrivenType);
172     if (itr != g_fwkFocusDrivenTypeMap_.end()) {
173         metaFocusDrivenType = itr->second;
174     }
175 
176     MEDIA_DEBUG_LOG("CaptureSessionForSys::SetFocusDriven focus driven type: %{public}d", focusDrivenType);
177     bool status = AddOrUpdateMetadata(changedMetadata_, OHOS_CONTROL_FOCUS_DRIVEN_TYPE, &metaFocusDrivenType, 1);
178     CHECK_PRINT_ELOG(!status, "CaptureSessionForSys::SetFocusDriven Failed to set focus driven type");
179     return CameraErrorCode::SUCCESS;
180     // LCOV_EXCL_STOP
181 }
182 
GetSupportedColorReservationTypes(std::vector<ColorReservationType> & types)183 int32_t CaptureSessionForSys::GetSupportedColorReservationTypes(std::vector<ColorReservationType>& types)
184 {
185     // LCOV_EXCL_START
186     types.clear();
187     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
188         "CaptureSessionForSys::GetSupportedColorReservationTypes Session is not Commited");
189     auto inputDevice = GetInputDevice();
190     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(), CameraErrorCode::SUCCESS,
191         "CaptureSessionForSys::GetSupportedColorReservationTypes camera device is null");
192     std::shared_ptr<Camera::CameraMetadata> metadata = GetMetadata();
193     CHECK_RETURN_RET_ELOG(metadata == nullptr, CameraErrorCode::SUCCESS,
194         "GetSupportedColorReservationTypes camera metadata is null");
195     camera_metadata_item_t item;
196     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_COLOR_RESERVATION_TYPES, &item);
197     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, CameraErrorCode::SUCCESS,
198         "CaptureSessionForSys::GetSupportedColorReservationTypes Failed with return code %{public}d", ret);
199     for (uint32_t i = 0; i < item.count; i++) {
200         auto itr = g_metaColorReservationTypeMap_.find(static_cast<camera_color_reservation_type_t>(item.data.u8[i]));
201         if (itr != g_metaColorReservationTypeMap_.end()) {
202             types.emplace_back(itr->second);
203         }
204     }
205     return CameraErrorCode::SUCCESS;
206     // LCOV_EXCL_STOP
207 }
208 
IsColorReservationTypeSupported(ColorReservationType colorReservationType,bool & isSupported)209 int32_t CaptureSessionForSys::IsColorReservationTypeSupported(ColorReservationType colorReservationType,
210     bool& isSupported)
211 {
212     // LCOV_EXCL_START
213     CHECK_RETURN_RET_ELOG(g_fwkColorReservationTypeMap_.find(colorReservationType) ==
214         g_fwkColorReservationTypeMap_.end(), CameraErrorCode::PARAMETER_ERROR,
215         "CaptureSessionForSys::IsColorReservationTypeSupported Unknown color reservation type");
216     std::vector<ColorReservationType> vecSupportedColorReservationTypeList = {};
217     this->GetSupportedColorReservationTypes(vecSupportedColorReservationTypeList);
218     if (find(vecSupportedColorReservationTypeList.begin(), vecSupportedColorReservationTypeList.end(),
219         colorReservationType) != vecSupportedColorReservationTypeList.end()) {
220         isSupported = true;
221         return CameraErrorCode::SUCCESS;
222     }
223     isSupported = false;
224     return CameraErrorCode::SUCCESS;
225     // LCOV_EXCL_STOP
226 }
227 
GetColorReservation(ColorReservationType & colorReservationType)228 int32_t CaptureSessionForSys::GetColorReservation(ColorReservationType& colorReservationType)
229 {
230     // LCOV_EXCL_START
231     colorReservationType = ColorReservationType::COLOR_RESERVATION_TYPE_NONE;
232     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
233         "CaptureSessionForSys::GetColorReservation Session is not Commited");
234     auto inputDevice = GetInputDevice();
235     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(), CameraErrorCode::SUCCESS,
236         "CaptureSessionForSys::GetColorReservation camera device is null");
237     std::shared_ptr<Camera::CameraMetadata> metadata = GetMetadata();
238     CHECK_RETURN_RET_ELOG(metadata == nullptr, CameraErrorCode::SUCCESS,
239         "GetColorReservation camera metadata is null");
240     camera_metadata_item_t item;
241     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_COLOR_RESERVATION_TYPE, &item);
242     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, CameraErrorCode::SUCCESS,
243         "CaptureSessionForSys::GetColorReservation Failed with return code %{public}d", ret);
244     auto itr = g_metaColorReservationTypeMap_.find(static_cast<camera_color_reservation_type_t>(item.data.u8[0]));
245     if (itr != g_metaColorReservationTypeMap_.end()) {
246         colorReservationType = itr->second;
247         return CameraErrorCode::SUCCESS;
248     }
249     return CameraErrorCode::SUCCESS;
250     // LCOV_EXCL_STOP
251 }
252 
SetColorReservation(ColorReservationType colorReservationType)253 int32_t CaptureSessionForSys::SetColorReservation(ColorReservationType colorReservationType)
254 {
255     // LCOV_EXCL_START
256     CAMERA_SYNC_TRACE;
257     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
258         "CaptureSessionForSys::SetColorReservation Session is not Commited");
259     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
260         "CaptureSessionForSys::SetColorReservation Need to call LockForControl() before setting camera properties");
261     CHECK_RETURN_RET_ELOG(g_fwkColorReservationTypeMap_.find(colorReservationType) ==
262         g_fwkColorReservationTypeMap_.end(),
263         CameraErrorCode::PARAMETER_ERROR, "CaptureSessionForSys::SetColorReservation Unknown color reservation type");
264     bool isSupported = false;
265     IsColorReservationTypeSupported(colorReservationType, isSupported);
266     CHECK_RETURN_RET(!isSupported, CameraErrorCode::OPERATION_NOT_ALLOWED);
267     uint8_t metaColorReservationType = OHOS_CAMERA_COLOR_RESERVATION_NONE;
268     auto itr = g_fwkColorReservationTypeMap_.find(colorReservationType);
269     if (itr != g_fwkColorReservationTypeMap_.end()) {
270         metaColorReservationType = itr->second;
271     }
272 
273     MEDIA_DEBUG_LOG("CaptureSessionForSys::SetColorReservation color reservation type: %{public}d",
274         colorReservationType);
275     bool status = AddOrUpdateMetadata(
276         changedMetadata_, OHOS_CONTROL_COLOR_RESERVATION_TYPE, &metaColorReservationType, 1);
277     CHECK_PRINT_ELOG(!status, "CaptureSessionForSys::SetColorReservation Failed to set color reservation type");
278     return CameraErrorCode::SUCCESS;
279     // LCOV_EXCL_STOP
280 }
281 
GetZoomPointInfos(std::vector<ZoomPointInfo> & zoomPointInfoList)282 int32_t CaptureSessionForSys::GetZoomPointInfos(std::vector<ZoomPointInfo>& zoomPointInfoList)
283 {
284     // LCOV_EXCL_START
285     MEDIA_INFO_LOG("CaptureSessionForSys::GetZoomPointInfos is Called");
286     zoomPointInfoList.clear();
287     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
288         "CaptureSessionForSys::GetZoomPointInfos Session is not Commited");
289     auto inputDevice = GetInputDevice();
290     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::SUCCESS,
291         "CaptureSessionForSys::GetZoomPointInfos camera device is null");
292     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
293     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::SUCCESS,
294         "CaptureSessionForSys::GetZoomPointInfos camera deviceInfo is null");
295     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
296     CHECK_RETURN_RET_ELOG(metadata == nullptr, CameraErrorCode::SUCCESS,
297         "GetZoomPointInfos camera metadata is null");
298     camera_metadata_item_t item;
299     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_EQUIVALENT_FOCUS, &item);
300     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS || item.count == 0, CameraErrorCode::SUCCESS,
301         "GetZoomPointInfos Failed with return code:%{public}d, item.count:%{public}d", ret, item.count);
302     SceneMode mode = GetMode();
303     int32_t defaultLen = 0;
304     int32_t modeLen = 0;
305     MEDIA_INFO_LOG("CaptureSessionForSys::GetZoomPointInfos mode:%{public}d", mode);
306     for (uint32_t i = 0; i < item.count; i++) {
307         if ((i & 1) == 0) {
308             MEDIA_DEBUG_LOG("CaptureSessionForSys::GetZoomPointInfos mode:%{public}d, equivalentFocus:%{public}d",
309                 item.data.i32[i], item.data.i32[i + 1]);
310             if (SceneMode::NORMAL == item.data.i32[i]) {
311                 defaultLen = item.data.i32[i + 1];
312             }
313             if (mode == item.data.i32[i]) {
314                 modeLen = item.data.i32[i + 1];
315             }
316         }
317     }
318     // only return 1x zoomPointInfo
319     ZoomPointInfo zoomPointInfo;
320     zoomPointInfo.zoomRatio = DEFAULT_EQUIVALENT_ZOOM;
321     zoomPointInfo.equivalentFocalLength = (modeLen != 0) ? modeLen : defaultLen;
322     zoomPointInfoList.emplace_back(zoomPointInfo);
323     return CameraErrorCode::SUCCESS;
324     // LCOV_EXCL_STOP
325 }
326 
GetBeauty(BeautyType beautyType)327 int32_t CaptureSessionForSys::GetBeauty(BeautyType beautyType)
328 {
329     // LCOV_EXCL_START
330     CHECK_RETURN_RET_ELOG(!(IsSessionCommited() || IsSessionConfiged()), CameraErrorCode::SESSION_NOT_CONFIG,
331         "CaptureSessionForSys::GetBeauty Session is not Commited");
332     auto inputDevice = GetInputDevice();
333     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(), -1,
334         "CaptureSessionForSys::GetBeauty camera device is null");
335     std::vector<BeautyType> supportedBeautyTypes = GetSupportedBeautyTypes();
336     auto itr = std::find(supportedBeautyTypes.begin(), supportedBeautyTypes.end(), beautyType);
337     CHECK_RETURN_RET_ELOG(itr == supportedBeautyTypes.end(), -1,
338         "CaptureSessionForSys::GetBeauty beautyType is NULL");
339     int32_t beautyLevel = 0;
340     auto itrLevel = beautyTypeAndLevels_.find(beautyType);
341     if (itrLevel != beautyTypeAndLevels_.end()) {
342         beautyLevel = itrLevel->second;
343     }
344 
345     return beautyLevel;
346     // LCOV_EXCL_STOP
347 }
348 
SetPortraitThemeType(PortraitThemeType type)349 int32_t CaptureSessionForSys::SetPortraitThemeType(PortraitThemeType type)
350 {
351     // LCOV_EXCL_START
352     MEDIA_DEBUG_LOG("Enter CaptureSessionForSys::SetPortraitThemeType");
353     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
354         "CaptureSessionForSys::SetPortraitThemeType Session is not Commited");
355     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
356         "CaptureSessionForSys::SetPortraitThemeType Need to call LockForControl() before setting camera properties");
357     CHECK_RETURN_RET(!IsPortraitThemeSupported(), CameraErrorCode::OPERATION_NOT_ALLOWED);
358     PortraitThemeType portraitThemeTypeTemp = PortraitThemeType::NATURAL;
359     uint8_t themeType = g_fwkPortraitThemeTypeMap_.at(portraitThemeTypeTemp);
360     auto itr = g_fwkPortraitThemeTypeMap_.find(type);
361     if (itr == g_fwkPortraitThemeTypeMap_.end()) {
362         MEDIA_ERR_LOG("CaptureSessionForSys::SetPortraitThemeType Unknown portrait theme type");
363     } else {
364         themeType = itr->second;
365     }
366     bool status = AddOrUpdateMetadata(changedMetadata_, OHOS_CONTROL_CAMERA_PORTRAIT_THEME_TYPE, &themeType, 1);
367     CHECK_PRINT_ELOG(!status, "CaptureSessionForSys::SetPortraitThemeType Failed to set flash mode");
368     return CameraErrorCode::SUCCESS;
369     // LCOV_EXCL_STOP
370 }
371 
GetColorEffect()372 ColorEffect CaptureSessionForSys::GetColorEffect()
373 {
374     // LCOV_EXCL_START
375     ColorEffect colorEffect = ColorEffect::COLOR_EFFECT_NORMAL;
376     CHECK_RETURN_RET_ELOG(!(IsSessionCommited() || IsSessionConfiged()), colorEffect,
377         "CaptureSessionForSys::GetColorEffect Session is not Commited");
378     auto inputDevice = GetInputDevice();
379     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(), colorEffect,
380         "CaptureSessionForSys::GetColorEffect camera device is null");
381     std::shared_ptr<Camera::CameraMetadata> metadata = GetMetadata();
382     CHECK_RETURN_RET_ELOG(metadata == nullptr, colorEffect,
383         "GetColorEffect camera metadata is null");
384     camera_metadata_item_t item;
385     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_SUPPORTED_COLOR_MODES, &item);
386     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS || item.count == 0, colorEffect,
387         "CaptureSessionForSys::GetColorEffect Failed with return code %{public}d", ret);
388     auto itr = g_metaColorEffectMap_.find(static_cast<camera_xmage_color_type_t>(item.data.u8[0]));
389     if (itr != g_metaColorEffectMap_.end()) {
390         colorEffect = itr->second;
391     }
392     return colorEffect;
393     // LCOV_EXCL_STOP
394 }
395 
SetColorEffect(ColorEffect colorEffect)396 void CaptureSessionForSys::SetColorEffect(ColorEffect colorEffect)
397 {
398     // LCOV_EXCL_START
399     CAMERA_SYNC_TRACE;
400     CHECK_RETURN_ELOG(!(IsSessionCommited() || IsSessionConfiged()),
401         "CaptureSessionForSys::SetColorEffect Session is not Commited");
402     CHECK_RETURN_ELOG(changedMetadata_ == nullptr,
403         "CaptureSessionForSys::SetColorEffect Need to call LockForControl() before setting camera properties");
404     uint8_t colorEffectTemp = ColorEffect::COLOR_EFFECT_NORMAL;
405     auto itr = g_fwkColorEffectMap_.find(colorEffect);
406     CHECK_RETURN_ELOG(itr == g_fwkColorEffectMap_.end(),
407         "CaptureSessionForSys::SetColorEffect unknown is color effect");
408     colorEffectTemp = itr->second;
409     MEDIA_DEBUG_LOG("CaptureSessionForSys::SetColorEffect: %{public}d", colorEffect);
410     bool status = AddOrUpdateMetadata(changedMetadata_, OHOS_CONTROL_SUPPORTED_COLOR_MODES, &colorEffectTemp, 1);
411     wptr<CaptureSessionForSys> weakThis(this);
412     CHECK_EXECUTE(status, AddFunctionToMap(std::to_string(OHOS_CONTROL_SUPPORTED_COLOR_MODES),
413         [weakThis, colorEffect]() {
414             auto sharedThis = weakThis.promote();
415             CHECK_RETURN_ELOG(!sharedThis, "SetColorEffect session is nullptr");
416             sharedThis->LockForControl();
417             sharedThis->SetColorEffect(colorEffect);
418             sharedThis->UnlockForControl();
419         }));
420     CHECK_PRINT_ELOG(!status, "CaptureSessionForSys::SetColorEffect Failed to set color effect");
421     return;
422     // LCOV_EXCL_STOP
423 }
424 
GetFocusDistance(float & focusDistance)425 int32_t CaptureSessionForSys::GetFocusDistance(float& focusDistance)
426 {
427     // LCOV_EXCL_START
428     focusDistance = 0.0;
429     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
430         "CaptureSessionForSys::GetFocusDistance Session is not Commited");
431     auto inputDevice = GetInputDevice();
432     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(), CameraErrorCode::SUCCESS,
433         "CaptureSessionForSys::GetFocusDistance camera device is null");
434     focusDistance = focusDistance_;
435     return CameraErrorCode::SUCCESS;
436     // LCOV_EXCL_STOP
437 }
438 
IsDepthFusionSupported()439 bool CaptureSessionForSys::IsDepthFusionSupported()
440 {
441     // LCOV_EXCL_START
442     CAMERA_SYNC_TRACE;
443     MEDIA_DEBUG_LOG("Enter IsDepthFusionSupported");
444     CHECK_RETURN_RET_ELOG(!(IsSessionCommited() || IsSessionConfiged()), false,
445         "CaptureSessionForSys::IsDepthFusionSupported Session is not Commited");
446     auto inputDevice = GetInputDevice();
447     CHECK_RETURN_RET_ELOG(inputDevice == nullptr, false,
448         "CaptureSessionForSys::IsDepthFusionSupported camera device is null");
449     auto deviceInfo = inputDevice->GetCameraDeviceInfo();
450     CHECK_RETURN_RET_ELOG(deviceInfo == nullptr, false,
451         "CaptureSessionForSys::IsDepthFusionSupported camera deviceInfo is null");
452     std::shared_ptr<Camera::CameraMetadata> metadata = deviceInfo->GetCachedMetadata();
453     CHECK_RETURN_RET_ELOG(metadata == nullptr, false,
454         "IsDepthFusionSupported camera metadata is null");
455     camera_metadata_item_t item;
456     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_CAPTURE_MACRO_DEPTH_FUSION_SUPPORTED, &item);
457     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS || item.count <= 0, false,
458         "CaptureSessionForSys::IsDepthFusionSupported Failed with return code %{public}d", ret);
459     auto supportResult = static_cast<bool>(item.data.u8[0]);
460     return supportResult;
461     // LCOV_EXCL_STOP
462 }
463 
EnableDepthFusion(bool isEnable)464 int32_t CaptureSessionForSys::EnableDepthFusion(bool isEnable)
465 {
466     // LCOV_EXCL_START
467     CAMERA_SYNC_TRACE;
468     MEDIA_DEBUG_LOG("Enter EnableDepthFusion, isEnable:%{public}d", isEnable);
469     CHECK_RETURN_RET_ELOG(!IsDepthFusionSupported(), CameraErrorCode::OPERATION_NOT_ALLOWED,
470         "EnableDepthFusion IsDepthFusionSupported is false");
471     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
472         "CaptureSessionForSys Failed EnableDepthFusion!, session not commited");
473     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
474         "CaptureSessionForSys::EnableDepthFusion Need to call LockForControl() before setting camera properties");
475     uint8_t enableValue = static_cast<uint8_t>(isEnable ? 1 : 0);
476     bool status = AddOrUpdateMetadata(changedMetadata_, OHOS_CONTROL_CAPTURE_MACRO_DEPTH_FUSION, &enableValue, 1);
477     CHECK_PRINT_ELOG(!status, "CaptureSessionForSys::EnableDepthFusion Failed to enable depth fusion");
478     isDepthFusionEnable_ = isEnable;
479     return CameraErrorCode::SUCCESS;
480     // LCOV_EXCL_STOP
481 }
482 
483 // LCOV_EXCL_START
GetDepthFusionThreshold()484 std::vector<float> CaptureSessionForSys::GetDepthFusionThreshold()
485 {
486     std::vector<float> depthFusionThreshold;
487     GetDepthFusionThreshold(depthFusionThreshold);
488     return depthFusionThreshold;
489 }
490 // LCOV_EXCL_STOP
491 
GetDepthFusionThreshold(std::vector<float> & depthFusionThreshold)492 int32_t CaptureSessionForSys::GetDepthFusionThreshold(std::vector<float>& depthFusionThreshold)
493 {
494     // LCOV_EXCL_START
495     MEDIA_DEBUG_LOG("Enter GetDepthFusionThreshold");
496     depthFusionThreshold.clear();
497     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
498         "CaptureSessionForSys::GetDepthFusionThreshold Session is not Commited");
499     auto inputDevice = GetInputDevice();
500     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(), CameraErrorCode::SUCCESS,
501         "CaptureSessionForSys::GetDepthFusionThreshold camera device is null");
502 
503     std::shared_ptr<Camera::CameraMetadata> metadata = GetMetadata();
504     CHECK_RETURN_RET_ELOG(metadata == nullptr, CameraErrorCode::SUCCESS,
505         "GetDepthFusionThreshold camera metadata is null");
506     camera_metadata_item_t item;
507     int ret = Camera::FindCameraMetadataItem(metadata->get(),
508         OHOS_ABILITY_CAPTURE_MACRO_DEPTH_FUSION_ZOOM_RANGE, &item);
509     const int32_t zoomRangeLength = 2;
510     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS || item.count < zoomRangeLength, 0,
511         "CaptureSessionForSys::GetDepthFusionThreshold Failed with return code %{public}d, item.count = %{public}d",
512         ret, item.count);
513     float minDepthFusionZoom = 0.0;
514     float maxDepthFusionZoom = 0.0;
515     MEDIA_INFO_LOG("Capture marco depth fusion zoom range, min: %{public}f, max: %{public}f",
516         item.data.f[0], item.data.f[1]);
517     minDepthFusionZoom = item.data.f[0];
518     maxDepthFusionZoom = item.data.f[1];
519     depthFusionThreshold = {minDepthFusionZoom, maxDepthFusionZoom};
520     return CameraErrorCode::SUCCESS;
521     // LCOV_EXCL_STOP
522 }
523 
IsDepthFusionEnabled()524 bool CaptureSessionForSys::IsDepthFusionEnabled()
525 {
526     return isDepthFusionEnable_;
527 }
528 
SetFeatureDetectionStatusCallback(std::shared_ptr<FeatureDetectionStatusCallback> callback)529 void CaptureSessionForSys::SetFeatureDetectionStatusCallback(std::shared_ptr<FeatureDetectionStatusCallback> callback)
530 {
531     // LCOV_EXCL_START
532     std::lock_guard<std::mutex> lock(sessionCallbackMutex_);
533     featureDetectionStatusCallback_ = callback;
534     return;
535     // LCOV_EXCL_STOP
536 }
537 
SetEffectSuggestionCallback(std::shared_ptr<EffectSuggestionCallback> effectSuggestionCallback)538 void CaptureSessionForSys::SetEffectSuggestionCallback(
539     std::shared_ptr<EffectSuggestionCallback> effectSuggestionCallback)
540 {
541     // LCOV_EXCL_START
542     std::lock_guard<std::mutex> lock(sessionCallbackMutex_);
543     effectSuggestionCallback_ = effectSuggestionCallback;
544     // LCOV_EXCL_STOP
545 }
546 
IsEffectSuggestionSupported()547 bool CaptureSessionForSys::IsEffectSuggestionSupported()
548 {
549     // LCOV_EXCL_START
550     MEDIA_DEBUG_LOG("Enter IsEffectSuggestionSupported");
551     bool isEffectSuggestionSupported = !this->GetSupportedEffectSuggestionType().empty();
552     MEDIA_DEBUG_LOG("IsEffectSuggestionSupported: %{public}s, ScenMode: %{public}d",
553         isEffectSuggestionSupported ? "true" : "false", GetMode());
554     return isEffectSuggestionSupported;
555     // LCOV_EXCL_STOP
556 }
557 
EnableEffectSuggestion(bool isEnable)558 int32_t CaptureSessionForSys::EnableEffectSuggestion(bool isEnable)
559 {
560     // LCOV_EXCL_START
561     MEDIA_DEBUG_LOG("Enter EnableEffectSuggestion, isEnable:%{public}d", isEnable);
562     CHECK_RETURN_RET_ELOG(!IsEffectSuggestionSupported(), CameraErrorCode::OPERATION_NOT_ALLOWED,
563         "EnableEffectSuggestion IsEffectSuggestionSupported is false");
564     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
565         "CaptureSessionForSys Failed EnableEffectSuggestion!, session not commited");
566     uint8_t enableValue = static_cast<uint8_t>(isEnable);
567     MEDIA_DEBUG_LOG("EnableEffectSuggestion enableValue:%{public}d", enableValue);
568     bool status = AddOrUpdateMetadata(changedMetadata_, OHOS_CONTROL_EFFECT_SUGGESTION, &enableValue, 1);
569     CHECK_PRINT_ELOG(!status, "CaptureSessionForSys::EnableEffectSuggestion Failed to enable effectSuggestion");
570     return CameraErrorCode::SUCCESS;
571     // LCOV_EXCL_STOP
572 }
573 
SetEffectSuggestionStatus(std::vector<EffectSuggestionStatus> effectSuggestionStatusList)574 int32_t CaptureSessionForSys::SetEffectSuggestionStatus(std::vector<EffectSuggestionStatus> effectSuggestionStatusList)
575 {
576     MEDIA_DEBUG_LOG("Enter SetEffectSuggestionStatus");
577     CHECK_RETURN_RET_ELOG(!IsEffectSuggestionSupported(), CameraErrorCode::OPERATION_NOT_ALLOWED,
578         "SetEffectSuggestionStatus IsEffectSuggestionSupported is false");
579     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
580         "CaptureSessionForSys Failed SetEffectSuggestionStatus!, session not commited");
581     std::vector<uint8_t> vec = {};
582     for (auto effectSuggestionStatus : effectSuggestionStatusList) {
583         // LCOV_EXCL_START
584         uint8_t type = fwkEffectSuggestionTypeMap_.at(EffectSuggestionType::EFFECT_SUGGESTION_NONE);
585         auto itr = fwkEffectSuggestionTypeMap_.find(effectSuggestionStatus.type);
586         if (itr == fwkEffectSuggestionTypeMap_.end()) {
587             MEDIA_ERR_LOG("CaptureSessionForSys::SetEffectSuggestionStatus Unknown effectSuggestionType");
588         } else {
589             type = itr->second;
590         }
591         vec.emplace_back(type);
592         vec.emplace_back(static_cast<uint8_t>(effectSuggestionStatus.status));
593         MEDIA_DEBUG_LOG("CaptureSessionForSys::SetEffectSuggestionStatus type:%{public}u,status:%{public}u",
594             type, static_cast<uint8_t>(effectSuggestionStatus.status));
595         // LCOV_EXCL_STOP
596     }
597     bool status = AddOrUpdateMetadata(
598         changedMetadata_, OHOS_CONTROL_EFFECT_SUGGESTION_DETECTION, vec.data(), vec.size());
599     CHECK_PRINT_ELOG(!status,
600         "CaptureSessionForSys::SetEffectSuggestionStatus Failed to Set effectSuggestionStatus");
601     return CameraErrorCode::SUCCESS;
602 }
603 
GetSupportedEffectSuggestionInfo()604 EffectSuggestionInfo CaptureSessionForSys::GetSupportedEffectSuggestionInfo()
605 {
606     // LCOV_EXCL_START
607     EffectSuggestionInfo effectSuggestionInfo = {};
608     CHECK_RETURN_RET_ELOG(!(IsSessionCommited() || IsSessionConfiged()), effectSuggestionInfo,
609         "CaptureSessionForSys::GetSupportedEffectSuggestionInfo Session is not Commited");
610     auto inputDevice = GetInputDevice();
611     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(), effectSuggestionInfo,
612         "CaptureSessionForSys::GetSupportedEffectSuggestionInfo camera device is null");
613     std::shared_ptr<Camera::CameraMetadata> metadata = GetMetadata();
614     CHECK_RETURN_RET_ELOG(metadata == nullptr, effectSuggestionInfo,
615         "GetSupportedEffectSuggestionInfo camera metadata is null");
616     camera_metadata_item_t item;
617     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_EFFECT_SUGGESTION_SUPPORTED, &item);
618     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, effectSuggestionInfo,
619         "CaptureSessionForSys::GetSupportedEffectSuggestionInfo Failed with return code %{public}d", ret);
620 
621     std::shared_ptr<EffectSuggestionInfoParse> infoParse = std::make_shared<EffectSuggestionInfoParse>();
622     MEDIA_INFO_LOG("CaptureSessionForSys::GetSupportedEffectSuggestionInfo item.count %{public}d", item.count);
623     infoParse->GetEffectSuggestionInfo(item.data.i32, item.count, effectSuggestionInfo);
624     MEDIA_DEBUG_LOG("SupportedEffectSuggestionInfo: %{public}s", effectSuggestionInfo.to_string().c_str());
625     return effectSuggestionInfo;
626     // LCOV_EXCL_STOP
627 }
628 
GetSupportedEffectSuggestionType()629 std::vector<EffectSuggestionType> CaptureSessionForSys::GetSupportedEffectSuggestionType()
630 {
631     // LCOV_EXCL_START
632     std::vector<EffectSuggestionType> supportedEffectSuggestionList = {};
633     EffectSuggestionInfo effectSuggestionInfo = this->GetSupportedEffectSuggestionInfo();
634     CHECK_RETURN_RET_ELOG(effectSuggestionInfo.modeCount == 0, supportedEffectSuggestionList,
635         "CaptureSessionForSys::GetSupportedEffectSuggestionType Failed, effectSuggestionInfo is null");
636 
637     for (uint32_t i = 0; i < effectSuggestionInfo.modeCount; i++) {
638         if (GetMode() != effectSuggestionInfo.modeInfo[i].modeType) {
639             continue;
640         }
641         std::vector<int32_t> effectSuggestionList = effectSuggestionInfo.modeInfo[i].effectSuggestionList;
642         supportedEffectSuggestionList.reserve(effectSuggestionList.size());
643         for (uint32_t j = 0; j < effectSuggestionList.size(); j++) {
644             auto itr = metaEffectSuggestionTypeMap_.find(
645                 static_cast<CameraEffectSuggestionType>(effectSuggestionList[j]));
646             CHECK_EXECUTE(itr != metaEffectSuggestionTypeMap_.end(),
647                 supportedEffectSuggestionList.emplace_back(itr->second));
648         }
649         std::string supportedEffectSuggestionStr = std::accumulate(
650             supportedEffectSuggestionList.cbegin(), supportedEffectSuggestionList.cend(), std::string(),
651             [](const auto& prefix, const auto& item) {
652                 return prefix + (prefix.empty() ? "" : ",") + std::to_string(item);
653             });
654         MEDIA_DEBUG_LOG("The SupportedEffectSuggestionType List of ScenMode: %{public}d is [%{public}s].", GetMode(),
655             supportedEffectSuggestionStr.c_str());
656         return supportedEffectSuggestionList;
657     }
658     MEDIA_ERR_LOG("no effectSuggestionInfo for mode %{public}d", GetMode());
659     return supportedEffectSuggestionList;
660     // LCOV_EXCL_STOP
661 }
662 
UpdateEffectSuggestion(EffectSuggestionType effectSuggestionType,bool isEnable)663 int32_t CaptureSessionForSys::UpdateEffectSuggestion(EffectSuggestionType effectSuggestionType, bool isEnable)
664 {
665     // LCOV_EXCL_START
666     CAMERA_SYNC_TRACE;
667     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
668         "CaptureSessionForSys::UpdateEffectSuggestion Session is not Commited");
669     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
670         "CaptureSessionForSys::UpdateEffectSuggestion Need to call LockForControl() before setting camera properties");
671     uint8_t type = fwkEffectSuggestionTypeMap_.at(EffectSuggestionType::EFFECT_SUGGESTION_NONE);
672     auto itr = fwkEffectSuggestionTypeMap_.find(effectSuggestionType);
673     CHECK_RETURN_RET_ELOG(itr == fwkEffectSuggestionTypeMap_.end(), CameraErrorCode::INVALID_ARGUMENT,
674         "CaptureSessionForSys::UpdateEffectSuggestion Unknown effectSuggestionType");
675     type = itr->second;
676     std::vector<uint8_t> vec = {type, isEnable};
677     MEDIA_DEBUG_LOG("CaptureSessionForSys::UpdateEffectSuggestion type:%{public}u,isEnable:%{public}u", type, isEnable);
678     bool status = AddOrUpdateMetadata(changedMetadata_, OHOS_CONTROL_EFFECT_SUGGESTION_TYPE, vec.data(), vec.size());
679     CHECK_RETURN_RET_ELOG(!status, CameraErrorCode::SUCCESS,
680         "CaptureSessionForSys::UpdateEffectSuggestion Failed to set effectSuggestionType");
681     return CameraErrorCode::SUCCESS;
682     // LCOV_EXCL_STOP
683 }
684 
GetVirtualAperture(float & aperture)685 int32_t CaptureSessionForSys::GetVirtualAperture(float& aperture)
686 {
687     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
688         "GetVirtualAperture Session is not Commited");
689     // LCOV_EXCL_START
690     auto inputDevice = GetInputDevice();
691     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::SUCCESS, "GetVirtualAperture camera device is null");
692     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
693     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::SUCCESS,
694         "GetVirtualAperture camera deviceInfo is null");
695     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
696     CHECK_RETURN_RET_ELOG(metadata == nullptr, CameraErrorCode::SUCCESS,
697         "GetVirtualAperture camera metadata is null");
698     CHECK_RETURN_RET(metadata == nullptr, CameraErrorCode::SUCCESS);
699     camera_metadata_item_t item;
700     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_CAMERA_VIRTUAL_APERTURE_VALUE, &item);
701     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS || item.count == 0, CameraErrorCode::SUCCESS,
702         "GetVirtualAperture Failed with return code %{public}d", ret);
703     aperture = item.data.f[0];
704     return CameraErrorCode::SUCCESS;
705     // LCOV_EXCL_STOP
706 }
707 
SetVirtualAperture(const float virtualAperture)708 int32_t CaptureSessionForSys::SetVirtualAperture(const float virtualAperture)
709 {
710     CAMERA_SYNC_TRACE;
711     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
712         "SetVirtualAperture Session is not Commited");
713     // LCOV_EXCL_START
714     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
715         "SetVirtualAperture changedMetadata_ is NULL");
716     std::vector<float> supportedVirtualApertures {};
717     GetSupportedVirtualApertures(supportedVirtualApertures);
718     auto res = std::find_if(supportedVirtualApertures.begin(), supportedVirtualApertures.end(),
719         [&virtualAperture](const float item) { return FloatIsEqual(virtualAperture, item); });
720     CHECK_RETURN_RET_ELOG(
721         res == supportedVirtualApertures.end(), CameraErrorCode::SUCCESS, "current virtualAperture is not supported");
722     MEDIA_DEBUG_LOG("SetVirtualAperture virtualAperture: %{public}f", virtualAperture);
723     bool status = AddOrUpdateMetadata(
724         changedMetadata_, OHOS_CONTROL_CAMERA_VIRTUAL_APERTURE_VALUE, &virtualAperture, 1);
725     CHECK_PRINT_ELOG(!status, "SetVirtualAperture Failed to set virtualAperture");
726     return CameraErrorCode::SUCCESS;
727     // LCOV_EXCL_STOP
728 }
729 
GetPhysicalAperture(float & physicalAperture)730 int32_t CaptureSessionForSys::GetPhysicalAperture(float& physicalAperture)
731 {
732     physicalAperture = 0.0;
733     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
734         "GetPhysicalAperture Session is not Commited");
735     // LCOV_EXCL_START
736     auto inputDevice = GetInputDevice();
737     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::SUCCESS,
738         "GetPhysicalAperture camera device is null");
739     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
740     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::SUCCESS,
741         "GetPhysicalAperture camera deviceInfo is null");
742     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
743     CHECK_RETURN_RET_ELOG(metadata == nullptr, CameraErrorCode::SUCCESS,
744         "GetPhysicalAperture camera metadata is null");
745     camera_metadata_item_t item;
746     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_CAMERA_PHYSICAL_APERTURE_VALUE, &item);
747     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS || item.count == 0, CameraErrorCode::SUCCESS,
748         "GetPhysicalAperture Failed with return code %{public}d", ret);
749     physicalAperture = item.data.f[0];
750     return CameraErrorCode::SUCCESS;
751     // LCOV_EXCL_STOP
752 }
753 
SetPhysicalAperture(float physicalAperture)754 int32_t CaptureSessionForSys::SetPhysicalAperture(float physicalAperture)
755 {
756     CAMERA_SYNC_TRACE;
757     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
758         "SetPhysicalAperture Session is not Commited");
759     // LCOV_EXCL_START
760     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
761         "SetPhysicalAperture changedMetadata_ is NULL");
762     MEDIA_DEBUG_LOG(
763         "CaptureSessionForSys::SetPhysicalAperture physicalAperture = %{public}f", ConfusingNumber(physicalAperture));
764     std::vector<std::vector<float>> physicalApertures;
765     GetSupportedPhysicalApertures(physicalApertures);
766     // physicalApertures size is one, means not support change
767     CHECK_RETURN_RET_ELOG(physicalApertures.size() == 1, CameraErrorCode::SUCCESS,
768         "SetPhysicalAperture not support");
769     // accurately currentZoomRatio need smoothing zoom done
770     float currentZoomRatio = targetZoomRatio_;
771     CHECK_EXECUTE(!isSmoothZooming_ || FloatIsEqual(targetZoomRatio_, -1.0), currentZoomRatio = GetZoomRatio());
772     int zoomMinIndex = 0;
773     auto it = std::find_if(physicalApertures.rbegin(), physicalApertures.rend(),
774         [&currentZoomRatio, &zoomMinIndex](const std::vector<float> physicalApertureRange) {
775             return (currentZoomRatio - physicalApertureRange[zoomMinIndex]) >= -std::numeric_limits<float>::epsilon();
776         });
777     float autoAperture = 0.0;
778     CHECK_RETURN_RET_ELOG(it == physicalApertures.rend(), CameraErrorCode::SUCCESS,
779         "current zoomRatio not supported in physical apertures zoom ratio");
780     int physicalAperturesIndex = 2;
781     auto res = std::find_if(std::next((*it).begin(), physicalAperturesIndex), (*it).end(),
782         [&physicalAperture](
783             const float physicalApertureTemp) { return FloatIsEqual(physicalAperture, physicalApertureTemp); });
784     CHECK_RETURN_RET_ELOG((physicalAperture != autoAperture) && res == (*it).end(), CameraErrorCode::SUCCESS,
785         "current physicalAperture is not supported");
786     CHECK_RETURN_RET_ELOG(!AddOrUpdateMetadata(
787         changedMetadata_->get(), OHOS_CONTROL_CAMERA_PHYSICAL_APERTURE_VALUE, &physicalAperture, 1),
788         CameraErrorCode::SUCCESS, "SetPhysicalAperture Failed to set physical aperture");
789     wptr<CaptureSessionForSys> weakThis(this);
790     AddFunctionToMap(std::to_string(OHOS_CONTROL_CAMERA_PHYSICAL_APERTURE_VALUE), [weakThis, physicalAperture]() {
791         auto sharedThis = weakThis.promote();
792         CHECK_RETURN_ELOG(!sharedThis, "SetPhysicalAperture session is nullptr");
793         sharedThis->LockForControl();
794         int32_t retCode = sharedThis->SetPhysicalAperture(physicalAperture);
795         sharedThis->UnlockForControl();
796         CHECK_EXECUTE(retCode != CameraErrorCode::SUCCESS,
797                       sharedThis->SetDeviceCapabilityChangeStatus(true));
798     });
799     apertureValue_ = physicalAperture;
800     return CameraErrorCode::SUCCESS;
801     // LCOV_EXCL_STOP
802 }
803 
EnableLcdFlashDetection(bool isEnable)804 int32_t CaptureSessionForSys::EnableLcdFlashDetection(bool isEnable)
805 {
806     CAMERA_SYNC_TRACE;
807     MEDIA_DEBUG_LOG("Enter EnableLcdFlashDetection, isEnable:%{public}d", isEnable);
808     CHECK_RETURN_RET_ELOG(!IsLcdFlashSupported(), CameraErrorCode::OPERATION_NOT_ALLOWED,
809         "EnableLcdFlashDetection IsLcdFlashSupported is false");
810     // LCOV_EXCL_START
811     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
812         "EnableLcdFlashDetection session not commited");
813     uint8_t enableValue = static_cast<uint8_t>(isEnable);
814     CHECK_PRINT_ELOG(!AddOrUpdateMetadata(changedMetadata_, OHOS_CONTROL_LCD_FLASH_DETECTION, &enableValue, 1),
815         "EnableLcdFlashDetection Failed to enable lcd flash detection");
816     return CameraErrorCode::SUCCESS;
817     // LCOV_EXCL_STOP
818 }
819 
SetLcdFlashStatusCallback(std::shared_ptr<LcdFlashStatusCallback> lcdFlashStatusCallback)820 void CaptureSessionForSys::SetLcdFlashStatusCallback(std::shared_ptr<LcdFlashStatusCallback> lcdFlashStatusCallback)
821 {
822     std::lock_guard<std::mutex> lock(sessionCallbackMutex_);
823     lcdFlashStatusCallback_ = lcdFlashStatusCallback;
824 }
825 
EnableTripodDetection(bool isEnable)826 int32_t CaptureSessionForSys::EnableTripodDetection(bool isEnable)
827 {
828     CAMERA_SYNC_TRACE;
829     MEDIA_DEBUG_LOG("Enter EnableTripodDetection, isEnable:%{public}d", isEnable);
830     CHECK_RETURN_RET_ELOG(!IsTripodDetectionSupported(), CameraErrorCode::OPERATION_NOT_ALLOWED,
831         "EnableTripodDetection IsTripodDetectionSupported is false");
832     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
833         "CaptureSessionForSys::EnableTripodDetection Session is not Commited");
834     // LCOV_EXCL_START
835     uint8_t enableValue = static_cast<uint8_t>(isEnable ? 1 : 0);
836     CHECK_PRINT_ELOG(!AddOrUpdateMetadata(changedMetadata_, OHOS_CONTROL_TRIPOD_DETECTION, &enableValue, 1),
837         "CaptureSessionForSys::EnableTripodDetection failed to enable tripod detection");
838     return CameraErrorCode::SUCCESS;
839     // LCOV_EXCL_STOP
840 }
841 
SetUsage(UsageType usageType,bool enabled)842 void CaptureSessionForSys::SetUsage(UsageType usageType, bool enabled)
843 {
844     CHECK_RETURN_ELOG(changedMetadata_ == nullptr,
845         "CaptureSessionForSys::SetUsage Need to call LockForControl() before setting camera properties");
846     // LCOV_EXCL_START
847     std::vector<int32_t> mode;
848 
849     mode.push_back(static_cast<int32_t>(usageType));
850     mode.push_back(
851         static_cast<int32_t>(enabled ? OHOS_CAMERA_SESSION_USAGE_ENABLE : OHOS_CAMERA_SESSION_USAGE_DISABLE));
852 
853     bool status = changedMetadata_->addEntry(OHOS_CONTROL_CAMERA_SESSION_USAGE, mode.data(), mode.size());
854 
855     CHECK_PRINT_ELOG(!status, "CaptureSessionForSys::SetUsage Failed to set mode");
856     // LCOV_EXCL_STOP
857 }
858 
EnableLcdFlash(bool isEnable)859 int32_t CaptureSessionForSys::EnableLcdFlash(bool isEnable)
860 {
861     CAMERA_SYNC_TRACE;
862     MEDIA_DEBUG_LOG("Enter EnableLcdFlash, isEnable:%{public}d", isEnable);
863     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
864         "EnableLcdFlash session not commited");
865     // LCOV_EXCL_START
866     uint8_t enableValue = static_cast<uint8_t>(isEnable);
867     CHECK_PRINT_ELOG(!AddOrUpdateMetadata(changedMetadata_, OHOS_CONTROL_LCD_FLASH, &enableValue, 1),
868         "EnableLcdFlash Failed to enable lcd flash");
869     return CameraErrorCode::SUCCESS;
870     // LCOV_EXCL_STOP
871 }
872 
873 } // namespace CameraStandard
874 } // namespace OHOS
875