• 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/video_session_for_sys.h"
17 
18 #include "camera_error_code.h"
19 #include "camera_log.h"
20 #include "input/camera_manager.h"
21 #include "output/camera_output_capability.h"
22 
23 namespace OHOS {
24 namespace CameraStandard {
25 namespace {
26 constexpr int32_t FOCUS_TRACKING_REGION_DATA_CNT = 4;
27 
GeneratePreconfigProfiles1_1(PreconfigType preconfigType)28 std::shared_ptr<PreconfigProfiles> GeneratePreconfigProfiles1_1(PreconfigType preconfigType)
29 {
30     std::shared_ptr<PreconfigProfiles> configs = std::make_shared<PreconfigProfiles>(ColorSpace::BT709);
31     configs->photoProfile = { CameraFormat::CAMERA_FORMAT_JPEG, { .width = 0, .height = 0 } };
32     configs->photoProfile.sizeRatio_ = RATIO_1_1;
33     configs->photoProfile.sizeFollowSensorMax_ = true;
34     switch (preconfigType) {
35         case PRECONFIG_720P:
36             configs->previewProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, { .width = 720, .height = 720 } };
37             configs->previewProfile.fps_ = { .fixedFps = 30, .minFps = 24, .maxFps = 30 };
38 
39             configs->videoProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, configs->previewProfile.size_,
40                 { configs->previewProfile.fps_.minFps, configs->previewProfile.fps_.maxFps } };
41             break;
42         case PRECONFIG_1080P:
43             configs->previewProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, { .width = 1080, .height = 1080 } };
44             configs->previewProfile.fps_ = { .fixedFps = 30, .minFps = 24, .maxFps = 30 };
45 
46             configs->videoProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, configs->previewProfile.size_,
47                 { configs->previewProfile.fps_.minFps, configs->previewProfile.fps_.maxFps } };
48             break;
49         case PRECONFIG_4K:
50             configs->previewProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, { .width = 1080, .height = 1080 } };
51             configs->previewProfile.fps_ = { .fixedFps = 30, .minFps = 24, .maxFps = 30 };
52 
53             configs->videoProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, { .width = 2160, .height = 2160 },
54                 { configs->previewProfile.fps_.minFps, configs->previewProfile.fps_.maxFps } };
55             break;
56         case PRECONFIG_HIGH_QUALITY:
57             configs->colorSpace = ColorSpace::BT2020_HLG_LIMIT;
58             configs->previewProfile = { CameraFormat::CAMERA_FORMAT_YCRCB_P010, { .width = 1080, .height = 1080 } };
59             configs->previewProfile.fps_ = { .fixedFps = 30, .minFps = 24, .maxFps = 30 };
60 
61             configs->videoProfile = { CameraFormat::CAMERA_FORMAT_YCRCB_P010, { .width = 2160, .height = 2160 },
62                 { configs->previewProfile.fps_.minFps, configs->previewProfile.fps_.maxFps } };
63             break;
64         default:
65             MEDIA_ERR_LOG(
66                 "VideoSessionForSys::GeneratePreconfigProfiles1_1 not support this config:%{public}d", preconfigType);
67             return nullptr;
68     }
69     return configs;
70 }
71 
GeneratePreconfigProfiles4_3(PreconfigType preconfigType)72 std::shared_ptr<PreconfigProfiles> GeneratePreconfigProfiles4_3(PreconfigType preconfigType)
73 {
74     std::shared_ptr<PreconfigProfiles> configs = std::make_shared<PreconfigProfiles>(ColorSpace::BT709);
75     configs->photoProfile = { CameraFormat::CAMERA_FORMAT_JPEG, { .width = 0, .height = 0 } };
76     configs->photoProfile.sizeRatio_ = RATIO_4_3;
77     configs->photoProfile.sizeFollowSensorMax_ = true;
78     switch (preconfigType) {
79         case PRECONFIG_720P:
80             configs->previewProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, { .width = 960, .height = 720 } };
81             configs->previewProfile.fps_ = { .fixedFps = 30, .minFps = 24, .maxFps = 30 };
82 
83             configs->videoProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, configs->previewProfile.size_,
84                 { configs->previewProfile.fps_.minFps, configs->previewProfile.fps_.maxFps } };
85             break;
86         case PRECONFIG_1080P:
87             configs->previewProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, { .width = 1440, .height = 1080 } };
88             configs->previewProfile.fps_ = { .fixedFps = 30, .minFps = 24, .maxFps = 30 };
89 
90             configs->videoProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, configs->previewProfile.size_,
91                 { configs->previewProfile.fps_.minFps, configs->previewProfile.fps_.maxFps } };
92             break;
93         case PRECONFIG_4K:
94             configs->previewProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, { .width = 1440, .height = 1080 } };
95             configs->previewProfile.fps_ = { .fixedFps = 30, .minFps = 24, .maxFps = 30 };
96 
97             configs->videoProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, { .width = 2880, .height = 2160 },
98                 { configs->previewProfile.fps_.minFps, configs->previewProfile.fps_.maxFps } };
99             break;
100         case PRECONFIG_HIGH_QUALITY:
101             configs->colorSpace = ColorSpace::BT2020_HLG_LIMIT;
102             configs->previewProfile = { CameraFormat::CAMERA_FORMAT_YCRCB_P010, { .width = 1440, .height = 1080 } };
103             configs->previewProfile.fps_ = { .fixedFps = 30, .minFps = 24, .maxFps = 30 };
104 
105             configs->videoProfile = { CameraFormat::CAMERA_FORMAT_YCRCB_P010, { .width = 2880, .height = 2160 },
106                 { configs->previewProfile.fps_.minFps, configs->previewProfile.fps_.maxFps } };
107             break;
108         default:
109             MEDIA_ERR_LOG(
110                 "VideoSessionForSys::GeneratePreconfigProfiles4_3 not support this config:%{public}d", preconfigType);
111             return nullptr;
112     }
113     return configs;
114 }
115 
GeneratePreconfigProfiles16_9(PreconfigType preconfigType)116 std::shared_ptr<PreconfigProfiles> GeneratePreconfigProfiles16_9(PreconfigType preconfigType)
117 {
118     std::shared_ptr<PreconfigProfiles> configs = std::make_shared<PreconfigProfiles>(ColorSpace::BT709);
119     configs->photoProfile = { CameraFormat::CAMERA_FORMAT_JPEG, { .width = 0, .height = 0 } };
120     configs->photoProfile.sizeRatio_ = RATIO_16_9;
121     configs->photoProfile.sizeFollowSensorMax_ = true;
122     switch (preconfigType) {
123         case PRECONFIG_720P:
124             configs->previewProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, { .width = 1280, .height = 720 } };
125             configs->previewProfile.fps_ = { .fixedFps = 30, .minFps = 24, .maxFps = 30 };
126 
127             configs->videoProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, configs->previewProfile.size_,
128                 { configs->previewProfile.fps_.minFps, configs->previewProfile.fps_.maxFps } };
129             break;
130         case PRECONFIG_1080P:
131             configs->previewProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, { .width = 1920, .height = 1080 } };
132             configs->previewProfile.fps_ = { .fixedFps = 30, .minFps = 24, .maxFps = 30 };
133 
134             configs->videoProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, configs->previewProfile.size_,
135                 { configs->previewProfile.fps_.minFps, configs->previewProfile.fps_.maxFps } };
136             break;
137         case PRECONFIG_4K:
138             configs->previewProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, { .width = 1920, .height = 1080 } };
139             configs->previewProfile.fps_ = { .fixedFps = 30, .minFps = 24, .maxFps = 30 };
140 
141             configs->videoProfile = { CameraFormat::CAMERA_FORMAT_YUV_420_SP, { .width = 3840, .height = 2160 },
142                 { configs->previewProfile.fps_.minFps, configs->previewProfile.fps_.maxFps } };
143             break;
144         case PRECONFIG_HIGH_QUALITY:
145             configs->colorSpace = ColorSpace::BT2020_HLG_LIMIT;
146             configs->previewProfile = { CameraFormat::CAMERA_FORMAT_YCRCB_P010, { .width = 1920, .height = 1080 } };
147             configs->previewProfile.fps_ = { .fixedFps = 30, .minFps = 24, .maxFps = 30 };
148 
149             configs->videoProfile = { CameraFormat::CAMERA_FORMAT_YCRCB_P010, { .width = 3840, .height = 2160 },
150                 { configs->previewProfile.fps_.minFps, configs->previewProfile.fps_.maxFps } };
151             break;
152         default:
153             MEDIA_ERR_LOG(
154                 "VideoSessionForSys::GeneratePreconfigProfiles16_9 not support this config:%{public}d", preconfigType);
155             return nullptr;
156     }
157     return configs;
158 }
159 } // namespace
160 
161 const std::unordered_map<camera_focus_tracking_mode_t, FocusTrackingMode>
162     VideoSessionForSys::metaToFwFocusTrackingMode_ = {
163     { OHOS_CAMERA_FOCUS_TRACKING_AUTO, FocusTrackingMode::FOCUS_TRACKING_MODE_AUTO }
164 };
165 
FocusTrackingInfo(const FocusTrackingMode mode,const Rect rect)166 FocusTrackingInfo::FocusTrackingInfo(const FocusTrackingMode mode, const Rect rect) : mode_(mode), region_(rect)
167 {}
168 
FocusTrackingInfo(const FocusTrackingInfoParms & parms)169 FocusTrackingInfo::FocusTrackingInfo(const FocusTrackingInfoParms& parms)
170 {
171     mode_ = parms.trackingMode;
172     region_ = parms.trackingRegion;
173 }
174 
~VideoSessionForSys()175 VideoSessionForSys::~VideoSessionForSys()
176 {
177     focusTrackingInfoCallback_ = nullptr;
178     lightStatusCallback_ = nullptr;
179 }
180 
CanAddOutput(sptr<CaptureOutput> & output)181 bool VideoSessionForSys::CanAddOutput(sptr<CaptureOutput>& output)
182 {
183     MEDIA_DEBUG_LOG("Enter Into VideoSessionForSys::CanAddOutput");
184     CHECK_RETURN_RET_ELOG(
185         !IsSessionConfiged() || output == nullptr, false, "VideoSessionForSys::CanAddOutput operation is Not allowed!");
186     return CaptureSession::CanAddOutput(output);
187 }
188 
GeneratePreconfigProfiles(PreconfigType preconfigType,ProfileSizeRatio preconfigRatio)189 std::shared_ptr<PreconfigProfiles> VideoSessionForSys::GeneratePreconfigProfiles(
190     PreconfigType preconfigType, ProfileSizeRatio preconfigRatio)
191 {
192     switch (preconfigRatio) {
193         case RATIO_1_1:
194             return GeneratePreconfigProfiles1_1(preconfigType);
195         case RATIO_4_3:
196             return GeneratePreconfigProfiles4_3(preconfigType);
197         case UNSPECIFIED:
198         // Fall through
199         case RATIO_16_9:
200             return GeneratePreconfigProfiles16_9(preconfigType);
201         default:
202             MEDIA_ERR_LOG("VideoSessionForSys::GeneratePreconfigProfiles unknow profile size ratio.");
203             break;
204     }
205     return nullptr;
206 }
207 
IsPreconfigProfilesLegal(std::shared_ptr<PreconfigProfiles> configs)208 bool VideoSessionForSys::IsPreconfigProfilesLegal(std::shared_ptr<PreconfigProfiles> configs)
209 {
210     auto cameraList = CameraManager::GetInstance()->GetSupportedCameras();
211     int32_t supportedCameraNum = 0;
212     for (auto& device : cameraList) {
213         MEDIA_INFO_LOG("VideoSessionForSys::IsPreconfigProfilesLegal check camera:%{public}s type:%{public}d",
214             device->GetID().c_str(), device->GetCameraType());
215         if (device->GetCameraType() != CAMERA_TYPE_DEFAULT) {
216             continue;
217         }
218         // Check photo
219         bool isPhotoCanPreconfig = IsPhotoProfileLegal(device, configs->photoProfile);
220         CHECK_RETURN_RET_ELOG(!isPhotoCanPreconfig, false,
221             "VideoSessionForSys::IsPreconfigProfilesLegal check photo profile fail, "
222             "no matched photo profiles:%{public}d %{public}dx%{public}d",
223             configs->photoProfile.format_, configs->photoProfile.size_.width, configs->photoProfile.size_.height);
224 
225         // Check preview
226         bool isPreviewCanPreconfig = IsPreviewProfileLegal(device, configs->previewProfile);
227         CHECK_RETURN_RET_ELOG(!isPreviewCanPreconfig, false,
228             "VideoSessionForSys::IsPreconfigProfilesLegal check preview profile fail, "
229             "no matched preview profiles:%{public}d %{public}dx%{public}d",
230             configs->previewProfile.format_, configs->previewProfile.size_.width, configs->previewProfile.size_.height);
231 
232         // Check video
233         auto isVideoCanPreconfig = IsVideoProfileLegal(device, configs->videoProfile);
234         CHECK_RETURN_RET_ELOG(!isVideoCanPreconfig, false,
235             "VideoSessionForSys::IsPreconfigProfilesLegal check video profile fail, "
236             "no matched video profiles:%{public}d %{public}dx%{public}d",
237             configs->videoProfile.format_, configs->videoProfile.size_.width, configs->videoProfile.size_.height);
238         supportedCameraNum++;
239     }
240     MEDIA_INFO_LOG(
241         "VideoSessionForSys::IsPreconfigProfilesLegal check pass, supportedCameraNum is%{public}d", supportedCameraNum);
242     return supportedCameraNum > 0;
243 }
244 
IsPhotoProfileLegal(sptr<CameraDevice> & device,Profile & photoProfile)245 bool VideoSessionForSys::IsPhotoProfileLegal(sptr<CameraDevice>& device, Profile& photoProfile)
246 {
247     auto photoProfilesIt = device->modePhotoProfiles_.find(SceneMode::VIDEO);
248     CHECK_RETURN_RET_ELOG(photoProfilesIt == device->modePhotoProfiles_.end(), false,
249         "VideoSessionForSys::CanPreconfig check photo profile fail, empty photo profiles");
250     auto photoProfiles = photoProfilesIt->second;
251     return std::any_of(photoProfiles.begin(), photoProfiles.end(), [&photoProfile](auto& profile) {
252         CHECK_RETURN_RET(!photoProfile.sizeFollowSensorMax_, profile == photoProfile);
253         return IsProfileSameRatio(profile, photoProfile.sizeRatio_, RATIO_VALUE_16_9);
254     });
255 }
256 
IsPreviewProfileLegal(sptr<CameraDevice> & device,Profile & previewProfile)257 bool VideoSessionForSys::IsPreviewProfileLegal(sptr<CameraDevice>& device, Profile& previewProfile)
258 {
259     auto previewProfilesIt = device->modePreviewProfiles_.find(SceneMode::VIDEO);
260     CHECK_RETURN_RET_ELOG(previewProfilesIt == device->modePreviewProfiles_.end(), false,
261         "VideoSessionForSys::CanPreconfig check preview profile fail, empty preview profiles");
262     auto previewProfiles = previewProfilesIt->second;
263     return std::any_of(previewProfiles.begin(), previewProfiles.end(),
264         [&previewProfile](auto& profile) { return profile == previewProfile; });
265 }
266 
IsVideoProfileLegal(sptr<CameraDevice> & device,VideoProfile & videoProfile)267 bool VideoSessionForSys::IsVideoProfileLegal(sptr<CameraDevice>& device, VideoProfile& videoProfile)
268 {
269     auto videoProfilesIt = device->modeVideoProfiles_.find(SceneMode::VIDEO);
270     CHECK_RETURN_RET_ELOG(videoProfilesIt == device->modeVideoProfiles_.end(), false,
271         "VideoSessionForSys::CanPreconfig check video profile fail, empty video profiles");
272     auto videoProfiles = videoProfilesIt->second;
273     return std::any_of(videoProfiles.begin(), videoProfiles.end(),
274         [&videoProfile](auto& profile) { return profile.IsContains(videoProfile); });
275 }
276 
CanPreconfig(PreconfigType preconfigType,ProfileSizeRatio preconfigRatio)277 bool VideoSessionForSys::CanPreconfig(PreconfigType preconfigType, ProfileSizeRatio preconfigRatio)
278 {
279     MEDIA_INFO_LOG("VideoSessionForSys::CanPreconfig check type:%{public}d, check ratio:%{public}d",
280         preconfigType, preconfigRatio);
281     std::shared_ptr<PreconfigProfiles> configs = GeneratePreconfigProfiles(preconfigType, preconfigRatio);
282     CHECK_RETURN_RET_ELOG(configs == nullptr, false, "VideoSessionForSys::CanPreconfig get configs fail.");
283     return IsPreconfigProfilesLegal(configs);
284 }
285 
Preconfig(PreconfigType preconfigType,ProfileSizeRatio preconfigRatio)286 int32_t VideoSessionForSys::Preconfig(PreconfigType preconfigType, ProfileSizeRatio preconfigRatio)
287 {
288     MEDIA_INFO_LOG("VideoSessionForSys::Preconfig type:%{public}d ratio:%{public}d", preconfigType, preconfigRatio);
289     std::shared_ptr<PreconfigProfiles> configs = GeneratePreconfigProfiles(preconfigType, preconfigRatio);
290     CHECK_RETURN_RET_ELOG(configs == nullptr, SERVICE_FATL_ERROR,
291         "VideoSessionForSys::Preconfig not support this type:%{public}d ratio:%{public}d",
292         preconfigType, preconfigRatio);
293     CHECK_RETURN_RET_ELOG(!IsPreconfigProfilesLegal(configs), SERVICE_FATL_ERROR,
294         "VideoSessionForSys::Preconfig preconfigProfile is illegal.");
295     SetPreconfigProfiles(configs);
296     MEDIA_INFO_LOG("VideoSessionForSys::Preconfig %s", configs->ToString().c_str());
297     return SUCCESS;
298 }
299 
CanSetFrameRateRange(int32_t minFps,int32_t maxFps,CaptureOutput * curOutput)300 bool VideoSessionForSys::CanSetFrameRateRange(int32_t minFps, int32_t maxFps, CaptureOutput* curOutput)
301 {
302     return CanSetFrameRateRangeForOutput(minFps, maxFps, curOutput) ? true : false;
303 }
304 
SetFocusTrackingInfoCallback(std::shared_ptr<FocusTrackingCallback> focusTrackingInfoCallback)305 void VideoSessionForSys::SetFocusTrackingInfoCallback(std::shared_ptr<FocusTrackingCallback> focusTrackingInfoCallback)
306 {
307     std::lock_guard<std::mutex> lock(videoSessionCallbackMutex_);
308     focusTrackingInfoCallback_ = focusTrackingInfoCallback;
309 }
310 
GetFocusTrackingCallback()311 std::shared_ptr<FocusTrackingCallback> VideoSessionForSys::GetFocusTrackingCallback()
312 {
313     std::lock_guard<std::mutex> lock(videoSessionCallbackMutex_);
314     return focusTrackingInfoCallback_;
315 }
316 
ProcessFocusTrackingInfo(const std::shared_ptr<OHOS::Camera::CameraMetadata> & result)317 void VideoSessionForSys::ProcessFocusTrackingInfo(const std::shared_ptr<OHOS::Camera::CameraMetadata>& result)
318 {
319     FocusTrackingMode mode = FOCUS_TRACKING_MODE_AUTO;
320     Rect region = {0.0, 0.0, 0.0, 0.0};
321 
322     auto focusTrackingCallback = GetFocusTrackingCallback();
323     if (focusTrackingCallback == nullptr) {
324         MEDIA_DEBUG_LOG("%{public}s focusTrackingCallback is null", __FUNCTION__);
325         return;
326     }
327 
328     bool ret = ProcessFocusTrackingModeInfo(result, mode);
329     if (!ret) {
330         MEDIA_DEBUG_LOG("ProcessFocusTrackingModeInfo failed");
331         return;
332     }
333     ret = ProcessRectInfo(result, region);
334     if (!ret) {
335         MEDIA_DEBUG_LOG("ProcessRectInfo failed");
336         return;
337     }
338     FocusTrackingInfo focusTrackingInfo(mode, region);
339     focusTrackingCallback->OnFocusTrackingInfoAvailable(focusTrackingInfo);
340 }
341 
ProcessFocusTrackingModeInfo(const std::shared_ptr<OHOS::Camera::CameraMetadata> & metadata,FocusTrackingMode & mode)342 bool VideoSessionForSys::ProcessFocusTrackingModeInfo(const std::shared_ptr<OHOS::Camera::CameraMetadata>& metadata,
343     FocusTrackingMode& mode)
344 {
345     CHECK_RETURN_RET_ELOG(metadata == nullptr, false, "metadata is nullptr");
346     camera_metadata_item_t item;
347     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_FOCUS_TRACKING_MODE, &item);
348     if (ret != CAM_META_SUCCESS || item.count == 0) {
349         MEDIA_DEBUG_LOG("%{public}s FindCameraMetadataItem failed", __FUNCTION__);
350         return false;
351     }
352     auto itr = metaToFwFocusTrackingMode_.find(static_cast<camera_focus_tracking_mode_t>(item.data.u8[0]));
353     CHECK_RETURN_RET_ELOG(itr == metaToFwFocusTrackingMode_.end(), false,
354         "%{public}s trackingMode data error", __FUNCTION__);
355     mode = itr->second;
356     return true;
357 }
358 
ProcessRectInfo(const std::shared_ptr<OHOS::Camera::CameraMetadata> & metadata,Rect & rect)359 bool VideoSessionForSys::ProcessRectInfo(const std::shared_ptr<OHOS::Camera::CameraMetadata>& metadata,
360     Rect& rect)
361 {
362     constexpr int32_t scale = 1000000;
363     constexpr int32_t offsetOne = 1;
364     constexpr int32_t offsetTwo = 2;
365     constexpr int32_t offsetThree = 3;
366 
367     CHECK_RETURN_RET_ELOG(metadata == nullptr, false, "metadata is nullptr");
368     camera_metadata_item_t item;
369     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_FOCUS_TRACKING_REGION, &item);
370     if (ret != CAM_META_SUCCESS || item.count < FOCUS_TRACKING_REGION_DATA_CNT) {
371         MEDIA_DEBUG_LOG("%{public}s FindCameraMetadataItem failed", __FUNCTION__);
372         return false;
373     }
374     int32_t offsetTopLeftX = item.data.i32[0];
375     int32_t offsetTopLeftY = item.data.i32[offsetOne];
376     int32_t offsetBottomRightX = item.data.i32[offsetTwo];
377     int32_t offsetBottomRightY = item.data.i32[offsetThree];
378 
379     double topLeftX = scale - offsetBottomRightY;
380     double topLeftY = offsetTopLeftX;
381     double width = offsetBottomRightY - offsetTopLeftY;
382     double height = offsetBottomRightX - offsetTopLeftX;
383 
384     topLeftX = topLeftX < 0 ? 0 : topLeftX;
385     topLeftX = topLeftX > scale ? scale : topLeftX;
386     topLeftY = topLeftY < 0 ? 0 : topLeftY;
387     topLeftY = topLeftY > scale ? scale : topLeftY;
388 
389     rect = { topLeftX / scale, topLeftY / scale, width / scale, height / scale};
390     return true;
391 }
392 
SetLightStatusCallback(std::shared_ptr<LightStatusCallback> callback)393 void VideoSessionForSys::SetLightStatusCallback(std::shared_ptr<LightStatusCallback> callback)
394 {
395     std::lock_guard<std::mutex> lock(videoSessionCallbackMutex_);
396     lightStatusCallback_ = callback;
397 }
398 
GetLightStatusCallback()399 std::shared_ptr<LightStatusCallback> VideoSessionForSys::GetLightStatusCallback()
400 {
401     std::lock_guard<std::mutex> lock(videoSessionCallbackMutex_);
402     return lightStatusCallback_;
403 }
404 
ProcessLightStatusChange(const std::shared_ptr<OHOS::Camera::CameraMetadata> & result)405 void VideoSessionForSys::ProcessLightStatusChange(const std::shared_ptr<OHOS::Camera::CameraMetadata> &result)
406 {
407     MEDIA_DEBUG_LOG("VideoSessionForSys::ProcessLightStatusChange is called");
408     auto lightStatusCallback = GetLightStatusCallback();
409     if (lightStatusCallback == nullptr) {
410         MEDIA_DEBUG_LOG("%{public}s lightStatusCallback is null", __FUNCTION__);
411         return;
412     }
413     camera_metadata_item_t item;
414     common_metadata_header_t *metadata = result->get();
415     int ret = Camera::FindCameraMetadataItem(metadata, OHOS_STATUS_LIGHT_STATUS, &item);
416     if (ret != CAM_META_SUCCESS || item.count == 0) {
417         MEDIA_DEBUG_LOG("can't get OHOS_STATUS_LIGHT_STATUS value");
418         return;
419     } else {
420         uint8_t value = item.data.u8[0];
421         MEDIA_DEBUG_LOG("the isInsufficientLightDetected is %{public}d", value);
422         MEDIA_DEBUG_LOG("the lightStatus_ is %{public}d", lightStatus_);
423         if (value != lightStatus_) {
424             SetLightStatus(value);
425             LightStatus light;
426             light.status = value;
427             MEDIA_DEBUG_LOG("the current light status is %{public}d", light.status);
428             lightStatusCallback->OnLightStatusChanged(light);
429         }
430     }
431     MEDIA_DEBUG_LOG("VideoSessionForSys::ProcessLightStatusChange is end");
432 }
433 
ProcessCallbacks(const uint64_t timestamp,const std::shared_ptr<OHOS::Camera::CameraMetadata> & result)434 void VideoSessionForSys::VideoSessionMetadataResultProcessor::ProcessCallbacks(const uint64_t timestamp,
435     const std::shared_ptr<OHOS::Camera::CameraMetadata>& result)
436 {
437     MEDIA_DEBUG_LOG("%{public}s is called", __FUNCTION__);
438     auto session = session_.promote();
439     CHECK_RETURN_ELOG(session == nullptr, "%{public}s session is null", __FUNCTION__);
440 
441     (void)timestamp;
442     session->ProcessAutoFocusUpdates(result);
443     session->ProcessMacroStatusChange(result);
444     session->ProcessLcdFlashStatusUpdates(result);
445     session->ProcessFocusTrackingInfo(result);
446     session->ProcessLightStatusChange(result);
447     session->ProcessEffectSuggestionTypeUpdates(result);
448 }
449 } // namespace CameraStandard
450 } // namespace OHOS