• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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/time_lapse_photo_session.h"
17 #include "camera_log.h"
18 #include "camera_util.h"
19 #include "metadata_common_utils.h"
20 
21 using namespace std;
22 
23 namespace OHOS {
24 namespace CameraStandard {
25 constexpr int32_t DEFAULT_ITEMS = 10;
26 constexpr int32_t DEFAULT_DATA_LENGTH = 100;
27 
GetMetadata()28 std::shared_ptr<OHOS::Camera::CameraMetadata> TimeLapsePhotoSession::GetMetadata()
29 {
30     std::string phyCameraId = std::to_string(physicalCameraId_.load());
31     auto physicalCameraDevice =
32         std::find_if(supportedDevices_.begin(), supportedDevices_.end(), [phyCameraId](const auto& device) -> bool {
33             std::string cameraId = device->GetID();
34             size_t delimPos = cameraId.find("/");
35             CHECK_RETURN_RET(delimPos == std::string::npos, false);
36             string id = cameraId.substr(delimPos + 1);
37             return id.compare(phyCameraId) == 0;
38         });
39     if (physicalCameraDevice != supportedDevices_.end()) {
40         MEDIA_DEBUG_LOG("%{public}s: physicalCameraId: device/%{public}s", __FUNCTION__, phyCameraId.c_str());
41         if ((*physicalCameraDevice)->GetCameraType() == CAMERA_TYPE_WIDE_ANGLE && isRawImageDelivery_) {
42             auto inputDevice = GetInputDevice();
43             CHECK_RETURN_RET(inputDevice == nullptr,
44                 std::make_shared<OHOS::Camera::CameraMetadata>(DEFAULT_ITEMS, DEFAULT_DATA_LENGTH));
45             auto info = inputDevice->GetCameraDeviceInfo();
46             CHECK_RETURN_RET(
47                 info == nullptr, std::make_shared<OHOS::Camera::CameraMetadata>(DEFAULT_ITEMS, DEFAULT_DATA_LENGTH));
48             MEDIA_DEBUG_LOG("%{public}s: using main sensor: %{public}s", __FUNCTION__, info->GetID().c_str());
49             return info->GetCachedMetadata();
50         }
51         if ((*physicalCameraDevice)->GetCachedMetadata() == nullptr) {
52             GetMetadataFromService(*physicalCameraDevice);
53         }
54         return (*physicalCameraDevice)->GetCachedMetadata();
55     }
56     auto inputDevice = GetInputDevice();
57     CHECK_RETURN_RET(
58         inputDevice == nullptr, std::make_shared<OHOS::Camera::CameraMetadata>(DEFAULT_ITEMS, DEFAULT_DATA_LENGTH));
59     auto cameraObj = inputDevice->GetCameraDeviceInfo();
60     CHECK_RETURN_RET(!cameraObj, std::make_shared<OHOS::Camera::CameraMetadata>(DEFAULT_ITEMS, DEFAULT_DATA_LENGTH));
61     MEDIA_DEBUG_LOG("%{public}s: no physicalCamera, using current camera device:%{public}s", __FUNCTION__,
62         cameraObj->GetID().c_str());
63     return cameraObj->GetCachedMetadata();
64 }
65 
ProcessCallbacks(const uint64_t timestamp,const std::shared_ptr<OHOS::Camera::CameraMetadata> & result)66 void TimeLapsePhotoSessionMetadataResultProcessor::ProcessCallbacks(
67     const uint64_t timestamp, const std::shared_ptr<OHOS::Camera::CameraMetadata>& result)
68 {
69     auto session = session_.promote();
70     CHECK_RETURN_ELOG(session == nullptr, "ProcessCallbacks session is nullptr");
71     session->ProcessAutoFocusUpdates(result);
72     session->ProcessIsoInfoChange(result);
73     session->ProcessExposureChange(result);
74     session->ProcessLuminationChange(result);
75     session->ProcessSetTryAEChange(result);
76     session->ProcessPhysicalCameraSwitch(result);
77 }
78 
ProcessIsoInfoChange(const shared_ptr<OHOS::Camera::CameraMetadata> & meta)79 void TimeLapsePhotoSession::ProcessIsoInfoChange(const shared_ptr<OHOS::Camera::CameraMetadata>& meta)
80 {
81     camera_metadata_item_t item;
82     common_metadata_header_t* metadata = meta->get();
83     int ret = Camera::FindCameraMetadataItem(metadata, OHOS_STATUS_ISO_VALUE, &item);
84     if (ret == CAM_META_SUCCESS) {
85         MEDIA_DEBUG_LOG("%{public}s: Iso = %{public}d", __FUNCTION__, item.data.ui32[0]);
86         IsoInfo info = {
87             .isoValue = item.data.ui32[0],
88         };
89         std::lock_guard<std::mutex> lock(cbMtx_);
90         if (isoInfoCallback_ != nullptr && item.data.ui32[0] != iso_) {
91             CHECK_EXECUTE(iso_ != 0, isoInfoCallback_->OnIsoInfoChanged(info));
92             iso_ = item.data.ui32[0];
93         }
94     }
95 }
96 
ProcessExposureChange(const shared_ptr<OHOS::Camera::CameraMetadata> & meta)97 void TimeLapsePhotoSession::ProcessExposureChange(const shared_ptr<OHOS::Camera::CameraMetadata>& meta)
98 {
99     camera_metadata_item_t item;
100     CHECK_RETURN_ELOG(meta == nullptr, "ProcessExposureChange Error! meta is null.");
101     common_metadata_header_t* metadata = meta->get();
102     int ret = Camera::FindCameraMetadataItem(metadata, OHOS_STATUS_SENSOR_EXPOSURE_TIME, &item);
103     if (ret == CAM_META_SUCCESS) {
104         int32_t numerator = item.data.r->numerator;
105         int32_t denominator = item.data.r->denominator;
106         CHECK_RETURN_ELOG(denominator == 0, "ProcessExposureChange Error! divide by 0");
107         constexpr int32_t timeUnit = 1000000;
108         uint32_t value = static_cast<uint32_t>(numerator / (denominator / timeUnit));
109         MEDIA_DEBUG_LOG("%{public}s: exposure = %{public}d", __FUNCTION__, value);
110         ExposureInfo info = {
111             .exposureDurationValue = value,
112         };
113         std::lock_guard<std::mutex> lock(cbMtx_);
114         if (exposureInfoCallback_ != nullptr && (value != exposureDurationValue_)) {
115             CHECK_EXECUTE(exposureDurationValue_ != 0, exposureInfoCallback_->OnExposureInfoChanged(info));
116             exposureDurationValue_ = value;
117         }
118     }
119 }
120 
ProcessLuminationChange(const shared_ptr<OHOS::Camera::CameraMetadata> & meta)121 void TimeLapsePhotoSession::ProcessLuminationChange(const shared_ptr<OHOS::Camera::CameraMetadata>& meta)
122 {
123     constexpr float normalizedMeanValue = 255.0;
124     camera_metadata_item_t item;
125     CHECK_RETURN_ELOG(meta == nullptr, "ProcessLuminationChange Error! meta is nullptr");
126     common_metadata_header_t* metadata = meta->get();
127     int ret = Camera::FindCameraMetadataItem(metadata, OHOS_STATUS_ALGO_MEAN_Y, &item);
128     float value = item.data.ui32[0] / normalizedMeanValue;
129     if (ret == CAM_META_SUCCESS) {
130         MEDIA_DEBUG_LOG("%{public}s: Lumination = %{public}f", __FUNCTION__, value);
131         LuminationInfo info = {
132             .luminationValue = value,
133         };
134         std::lock_guard<std::mutex> lock(cbMtx_);
135         if (luminationInfoCallback_ != nullptr && value != luminationValue_) {
136             luminationInfoCallback_->OnLuminationInfoChanged(info);
137             luminationValue_ = value;
138         }
139     }
140 }
141 
ProcessSetTryAEChange(const shared_ptr<OHOS::Camera::CameraMetadata> & meta)142 void TimeLapsePhotoSession::ProcessSetTryAEChange(const shared_ptr<OHOS::Camera::CameraMetadata>& meta)
143 {
144     TryAEInfo info = info_;
145     camera_metadata_item_t item;
146     int32_t ret;
147     bool changed = false;
148     ret = Camera::FindCameraMetadataItem(meta->get(), OHOS_STATUS_TIME_LAPSE_TRYAE_DONE, &item);
149     if (ret == CAM_META_SUCCESS) {
150         info.isTryAEDone = item.data.u8[0];
151         changed = changed || info.isTryAEDone != info_.isTryAEDone;
152     }
153     ret = Camera::FindCameraMetadataItem(meta->get(), OHOS_STATUS_TIME_LAPSE_TRYAE_HINT, &item);
154     if (ret == CAM_META_SUCCESS) {
155         info.isTryAEHintNeeded = item.data.u8[0];
156         changed = changed || info.isTryAEHintNeeded != info_.isTryAEHintNeeded;
157     }
158     ret = Camera::FindCameraMetadataItem(meta->get(), OHOS_STATUS_TIME_LAPSE_PREVIEW_TYPE, &item);
159     if (ret == CAM_META_SUCCESS) {
160         info.previewType = static_cast<TimeLapsePreviewType>(item.data.u8[0]);
161         changed = changed || info.previewType != info_.previewType;
162     }
163     ret = Camera::FindCameraMetadataItem(meta->get(), OHOS_STATUS_TIME_LAPSE_CAPTURE_INTERVAL, &item);
164     if (ret == CAM_META_SUCCESS) {
165         info.captureInterval = item.data.i32[0];
166         changed = changed || info.captureInterval != info_.captureInterval;
167     }
168     if (changed) {
169         lock_guard<mutex> lg(cbMtx_);
170         info_ = info;
171         CHECK_EXECUTE(tryAEInfoCallback_ != nullptr, tryAEInfoCallback_->OnTryAEInfoChanged(info));
172     }
173 }
174 
ProcessPhysicalCameraSwitch(const shared_ptr<OHOS::Camera::CameraMetadata> & meta)175 void TimeLapsePhotoSession::ProcessPhysicalCameraSwitch(const shared_ptr<OHOS::Camera::CameraMetadata>& meta)
176 {
177     camera_metadata_item_t item;
178     CHECK_RETURN_ELOG(meta == nullptr, "ProcessPhysicalCameraSwitch Error! meta is nullptr");
179     common_metadata_header_t* metadata = meta->get();
180     int ret = Camera::FindCameraMetadataItem(metadata, OHOS_STATUS_PREVIEW_PHYSICAL_CAMERA_ID, &item);
181     CHECK_RETURN(ret != CAM_META_SUCCESS);
182     if (physicalCameraId_ != item.data.u8[0]) {
183         MEDIA_DEBUG_LOG("%{public}s: physicalCameraId = %{public}d", __FUNCTION__, item.data.u8[0]);
184         physicalCameraId_ = item.data.u8[0];
185         ExecuteAbilityChangeCallback();
186     }
187 }
188 
IsTryAENeeded(bool & result)189 int32_t TimeLapsePhotoSession::IsTryAENeeded(bool& result)
190 {
191     CAMERA_SYNC_TRACE;
192     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
193         "TimeLapsePhotoSession::IsTryAENeeded Session is not Commited");
194     auto inputDevice = GetInputDevice();
195     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::OPERATION_NOT_ALLOWED,
196         "TimeLapsePhotoSession::IsTryAENeeded camera device is null");
197     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
198     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::OPERATION_NOT_ALLOWED,
199         "TimeLapsePhotoSession::IsTryAENeeded camera deviceInfo is null");
200     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
201     camera_metadata_item_t item;
202     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_TIME_LAPSE_TRYAE_STATE, &item);
203     result = ret == CAM_META_SUCCESS;
204     return CameraErrorCode::SUCCESS;
205 }
206 
StartTryAE()207 int32_t TimeLapsePhotoSession::StartTryAE()
208 {
209     CAMERA_SYNC_TRACE;
210     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
211         "TimeLapsePhotoSession::StartTryAE Session is not Commited");
212     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
213         "TimeLapsePhotoSession::StartTryAE Need to call LockForControl() before setting camera properties");
214     auto inputDevice = GetInputDevice();
215     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(),
216         CameraErrorCode::OPERATION_NOT_ALLOWED, "TimeLapsePhotoSession::StartTryAE camera device is null");
217     uint8_t data = 1;
218     MEDIA_INFO_LOG("Set tag OHOS_CONTROL_TIME_LAPSE_TRYAE_STATE value = %{public}d", data);
219     bool ret = AddOrUpdateMetadata(changedMetadata_->get(), OHOS_CONTROL_TIME_LAPSE_TRYAE_STATE, &data, 1);
220     if (ret) {
221         info_ = TryAEInfo();
222     }
223     CHECK_PRINT_ELOG(!ret, "Set tag OHOS_CONTROL_TIME_LAPSE_TRYAE_STATE Failed");
224     return CameraErrorCode::SUCCESS;
225 }
226 
StopTryAE()227 int32_t TimeLapsePhotoSession::StopTryAE()
228 {
229     CAMERA_SYNC_TRACE;
230     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
231         "TimeLapsePhotoSession::StopTryAE Session is not Commited");
232     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
233         "TimeLapsePhotoSession::StopTryAE Need to call LockForControl() before setting camera properties");
234     auto inputDevice = GetInputDevice();
235     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(),
236         CameraErrorCode::OPERATION_NOT_ALLOWED, "TimeLapsePhotoSession::StopTryAE camera device is null");
237     uint8_t data = 0;
238     MEDIA_INFO_LOG("Set tag OHOS_CONTROL_TIME_LAPSE_TRYAE_STATE value = %{public}d", data);
239     bool ret = AddOrUpdateMetadata(changedMetadata_->get(), OHOS_CONTROL_TIME_LAPSE_TRYAE_STATE, &data, 1);
240     if (ret) {
241         info_ = TryAEInfo();
242     }
243     CHECK_PRINT_ELOG(!ret, "Set tag OHOS_CONTROL_TIME_LAPSE_TRYAE_STATE Failed");
244     return CameraErrorCode::SUCCESS;
245 }
246 
GetSupportedTimeLapseIntervalRange(vector<int32_t> & result)247 int32_t TimeLapsePhotoSession::GetSupportedTimeLapseIntervalRange(vector<int32_t>& result)
248 {
249     CAMERA_SYNC_TRACE;
250     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
251         "TimeLapsePhotoSession::GetSupportedTimeLapseIntervalRange Session is not Commited");
252     auto inputDevice = GetInputDevice();
253     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::OPERATION_NOT_ALLOWED,
254         "GetSupportedTimeLapseIntervalRange camera device is null");
255     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
256     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::OPERATION_NOT_ALLOWED,
257         "GetSupportedTimeLapseIntervalRange camera device is null");
258     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
259     camera_metadata_item_t item;
260     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_TIME_LAPSE_INTERVAL_RANGE, &item);
261     if (ret == CAM_META_SUCCESS) {
262         for (uint32_t i = 0; i < item.count; i++) {
263             result.push_back(item.data.i32[i]);
264         }
265     }
266     return CameraErrorCode::SUCCESS;
267 }
268 
GetTimeLapseInterval(int32_t & result)269 int32_t TimeLapsePhotoSession::GetTimeLapseInterval(int32_t& result)
270 {
271     CAMERA_SYNC_TRACE;
272     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
273         "TimeLapsePhotoSession::GetTimeLapseInterval Session is not Commited");
274     auto inputDevice = GetInputDevice();
275     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::OPERATION_NOT_ALLOWED,
276         "TimeLapsePhotoSession::GetTimeLapseInterval camera device is null");
277     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
278     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::OPERATION_NOT_ALLOWED,
279         "TimeLapsePhotoSession::GetTimeLapseInterval camera deviceInfo is null");
280     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
281     camera_metadata_item_t item;
282     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_TIME_LAPSE_INTERVAL, &item);
283     if (ret == CAM_META_SUCCESS) {
284         result = item.data.i32[0];
285     }
286     return CameraErrorCode::SUCCESS;
287 }
288 
SetTimeLapseInterval(int32_t interval)289 int32_t TimeLapsePhotoSession::SetTimeLapseInterval(int32_t interval)
290 {
291     CAMERA_SYNC_TRACE;
292     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
293         "TimeLapsePhotoSession::SetTimeLapseInterval Session is not Commited");
294     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
295         "TimeLapsePhotoSession::SetTimeLapseInterval Need to call LockForControl() before setting camera properties");
296     auto inputDevice = GetInputDevice();
297     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(),
298         CameraErrorCode::OPERATION_NOT_ALLOWED, "TimeLapsePhotoSession::SetTimeLapseInterval camera device is null");
299     MEDIA_INFO_LOG("Set tag OHOS_CONTROL_TIME_LAPSE_INTERVAL value = %{public}d", interval);
300     bool ret = AddOrUpdateMetadata(changedMetadata_->get(), OHOS_CONTROL_TIME_LAPSE_INTERVAL, &interval, 1);
301     CHECK_PRINT_ELOG(!ret, "Set tag OHOS_CONTROL_TIME_LAPSE_INTERVAL failed");
302     return CameraErrorCode::SUCCESS;
303 }
304 
SetTimeLapseRecordState(TimeLapseRecordState state)305 int32_t TimeLapsePhotoSession::SetTimeLapseRecordState(TimeLapseRecordState state)
306 {
307     CAMERA_SYNC_TRACE;
308     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
309         "TimeLapsePhotoSession::SetTimeLapseRecordState Session is not Commited");
310     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
311         "SetTimeLapseRecordState Need to call LockForControl() before setting camera properties");
312     auto inputDevice = GetInputDevice();
313     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(),
314         CameraErrorCode::OPERATION_NOT_ALLOWED, "SetTimeLapseRecordState camera device is null");
315     MEDIA_INFO_LOG("Set tag OHOS_CONTROL_TIME_LAPSE_RECORD_STATE value = %{public}d", state);
316     bool ret = AddOrUpdateMetadata(changedMetadata_->get(), OHOS_CONTROL_TIME_LAPSE_RECORD_STATE, &state, 1);
317     CHECK_PRINT_ELOG(!ret, "Set tag OHOS_CONTROL_TIME_LAPSE_RECORD_STATE failed");
318     return CameraErrorCode::SUCCESS;
319 }
320 
SetTimeLapsePreviewType(TimeLapsePreviewType type)321 int32_t TimeLapsePhotoSession::SetTimeLapsePreviewType(TimeLapsePreviewType type)
322 {
323     CAMERA_SYNC_TRACE;
324     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
325         "TimeLapsePhotoSession::SetTimeLapsePreviewType Session is not Commited");
326     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
327         "SetTimeLapsePreviewType Need to call LockForControl() before setting camera properties");
328     auto inputDevice = GetInputDevice();
329     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(),
330         CameraErrorCode::OPERATION_NOT_ALLOWED, "SetTimeLapsePreviewType camera device is null");
331     MEDIA_INFO_LOG("Set tag OHOS_CONTROL_TIME_LAPSE_PREVIEW_TYPE value = %{public}d", type);
332     bool ret = AddOrUpdateMetadata(changedMetadata_->get(), OHOS_CONTROL_TIME_LAPSE_PREVIEW_TYPE, &type, 1);
333     CHECK_PRINT_ELOG(!ret, "Set tag OHOS_CONTROL_TIME_LAPSE_PREVIEW_TYPE failed");
334     return CameraErrorCode::SUCCESS;
335 }
336 
337 const unordered_map<ExposureHintMode, camera_exposure_hint_mode_enum_t>
338     TimeLapsePhotoSession::fwkExposureHintModeMap_ = {
339     { EXPOSURE_HINT_UNSUPPORTED, OHOS_CAMERA_EXPOSURE_HINT_UNSUPPORTED },
340     { EXPOSURE_HINT_MODE_ON, OHOS_CAMERA_EXPOSURE_HINT_MODE_ON },
341     { EXPOSURE_HINT_MODE_OFF, OHOS_CAMERA_EXPOSURE_HINT_MODE_OFF },
342 };
343 
SetExposureHintMode(ExposureHintMode mode)344 int32_t TimeLapsePhotoSession::SetExposureHintMode(ExposureHintMode mode)
345 {
346     CAMERA_SYNC_TRACE;
347     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
348         "TimeLapsePhotoSession::SetExposureHintMode Session is not Commited");
349     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
350         "TimeLapsePhotoSession::SetExposureHintMode Need to call LockForControl() before setting camera properties");
351     uint8_t exposureHintMode = OHOS_CAMERA_EXPOSURE_HINT_UNSUPPORTED;
352     auto itr = fwkExposureHintModeMap_.find(mode);
353     if (itr == fwkExposureHintModeMap_.end()) {
354         MEDIA_ERR_LOG("%{public}s: Unknown mode", __FUNCTION__);
355     } else {
356         exposureHintMode = itr->second;
357     }
358     MEDIA_INFO_LOG("Set tag OHOS_CONTROL_EXPOSURE_HINT_MODE value = %{public}d", exposureHintMode);
359     bool ret = AddOrUpdateMetadata(changedMetadata_->get(), OHOS_CONTROL_EXPOSURE_HINT_MODE, &exposureHintMode, 1);
360     CHECK_PRINT_ELOG(!ret, "Set tag OHOS_CONTROL_EXPOSURE_HINT_MODE Failed");
361     return CameraErrorCode::SUCCESS;
362 }
363 
364 //----- set callbacks -----
SetIsoInfoCallback(shared_ptr<IsoInfoCallback> callback)365 void TimeLapsePhotoSession::SetIsoInfoCallback(shared_ptr<IsoInfoCallback> callback)
366 {
367     lock_guard<mutex> lg(cbMtx_);
368     isoInfoCallback_ = callback;
369 }
370 
SetExposureInfoCallback(shared_ptr<ExposureInfoCallback> callback)371 void TimeLapsePhotoSession::SetExposureInfoCallback(shared_ptr<ExposureInfoCallback> callback)
372 {
373     lock_guard<mutex> lg(cbMtx_);
374     exposureInfoCallback_ = callback;
375 }
376 
SetLuminationInfoCallback(shared_ptr<LuminationInfoCallback> callback)377 void TimeLapsePhotoSession::SetLuminationInfoCallback(shared_ptr<LuminationInfoCallback> callback)
378 {
379     lock_guard<mutex> lg(cbMtx_);
380     luminationInfoCallback_ = callback;
381 }
382 
SetTryAEInfoCallback(shared_ptr<TryAEInfoCallback> callback)383 void TimeLapsePhotoSession::SetTryAEInfoCallback(shared_ptr<TryAEInfoCallback> callback)
384 {
385     lock_guard<mutex> lg(cbMtx_);
386     tryAEInfoCallback_ = callback;
387 }
388 
389 //----- ManualExposure -----
GetExposure(uint32_t & result)390 int32_t TimeLapsePhotoSession::GetExposure(uint32_t& result)
391 {
392     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
393         "TimeLapsePhotoSession::GetExposure Session is not Commited");
394     auto inputDevice = GetInputDevice();
395     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::OPERATION_NOT_ALLOWED,
396         "TimeLapsePhotoSession::GetExposure camera device is null");
397     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
398     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::OPERATION_NOT_ALLOWED,
399         "TimeLapsePhotoSession::GetExposure camera deviceInfo is null");
400     std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
401     camera_metadata_item_t item;
402     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_SENSOR_EXPOSURE_TIME, &item);
403     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, CameraErrorCode::INVALID_ARGUMENT,
404         "TimeLapsePhotoSession::GetExposure Failed with return code %{public}d", ret);
405     result = item.data.ui32[0];
406     MEDIA_DEBUG_LOG("exposureTime: %{public}d", result);
407     return CameraErrorCode::SUCCESS;
408 }
409 
SetExposure(uint32_t exposure)410 int32_t TimeLapsePhotoSession::SetExposure(uint32_t exposure)
411 {
412     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
413         "TimeLapsePhotoSession::SetExposure Session is not Commited");
414     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
415         "TimeLapsePhotoSession::SetExposure Need to call LockForControl() before setting camera properties");
416     MEDIA_DEBUG_LOG("exposure: %{public}d", exposure);
417     auto inputDevice = GetInputDevice();
418     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(),
419         CameraErrorCode::OPERATION_NOT_ALLOWED, "TimeLapsePhotoSession::SetExposure camera device is null");
420     std::vector<uint32_t> sensorExposureTimeRange;
421     CHECK_RETURN_RET_ELOG((GetSensorExposureTimeRange(sensorExposureTimeRange) != CameraErrorCode::SUCCESS) &&
422         sensorExposureTimeRange.empty(), CameraErrorCode::OPERATION_NOT_ALLOWED, "range is empty");
423     const uint32_t autoLongExposure = 0;
424     int32_t minIndex = 0;
425     int32_t maxIndex = 1;
426     if (exposure != autoLongExposure && exposure < sensorExposureTimeRange[minIndex]) {
427         MEDIA_DEBUG_LOG("exposureTime:"
428                         "%{public}d is lesser than minimum exposureTime: %{public}d",
429                         exposure, sensorExposureTimeRange[minIndex]);
430         exposure = sensorExposureTimeRange[minIndex];
431     } else if (exposure > sensorExposureTimeRange[maxIndex]) {
432         MEDIA_DEBUG_LOG("exposureTime: "
433                         "%{public}d is greater than maximum exposureTime: %{public}d",
434                         exposure, sensorExposureTimeRange[maxIndex]);
435         exposure = sensorExposureTimeRange[maxIndex];
436     }
437     constexpr int32_t timeUnit = 1000000;
438     camera_rational_t value = {.numerator = exposure, .denominator = timeUnit};
439     MEDIA_INFO_LOG("Set tag OHOS_CONTROL_SENSOR_EXPOSURE_TIME value = %{public}d, %{public}d", exposure, timeUnit);
440     bool ret = AddOrUpdateMetadata(changedMetadata_->get(), OHOS_CONTROL_SENSOR_EXPOSURE_TIME, &value, 1);
441     CHECK_PRINT_ELOG(!ret, "Set tag OHOS_CONTROL_SENSOR_EXPOSURE_TIME Failed");
442     exposureDurationValue_ = exposure;
443     return CameraErrorCode::SUCCESS;
444 }
445 
GetSupportedExposureRange(vector<uint32_t> & result)446 int32_t TimeLapsePhotoSession::GetSupportedExposureRange(vector<uint32_t>& result)
447 {
448     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
449         "TimeLapsePhotoSession::GetSupportedExposureRange Session is not Commited");
450     auto inputDevice = GetInputDevice();
451     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(),
452         CameraErrorCode::OPERATION_NOT_ALLOWED, "GetSupportedExposureRange camera device is null");
453     std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = GetMetadata();
454     camera_metadata_item_t item;
455     CHECK_RETURN_RET(metadata == nullptr, CameraErrorCode::INVALID_ARGUMENT);
456     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_SENSOR_EXPOSURE_TIME_RANGE, &item);
457     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS || item.count == 0, CameraErrorCode::INVALID_ARGUMENT,
458         "TimeLapsePhotoSession::GetSupportedExposureRange Failed with return code %{public}d", ret);
459     int32_t numerator = 0;
460     int32_t denominator = 0;
461     uint32_t value = 0;
462     constexpr int32_t timeUnit = 1000000;
463     for (uint32_t i = 0; i < item.count; i++) {
464         numerator = item.data.r[i].numerator;
465         denominator = item.data.r[i].denominator;
466         CHECK_RETURN_RET_ELOG(denominator == 0, CameraErrorCode::INVALID_ARGUMENT,
467             "TimeLapsePhotoSession::GetSupportedExposureRange divide by 0! numerator=%{public}d", numerator);
468         value = static_cast<uint32_t>(numerator / (denominator / timeUnit));
469         MEDIA_DEBUG_LOG("numerator=%{public}d, denominator=%{public}d,"
470                         " value=%{public}d", numerator, denominator, value);
471         result.emplace_back(value);
472     }
473     MEDIA_INFO_LOG("range=%{public}s, len = %{public}zu",
474                    Container2String(result.begin(), result.end()).c_str(),
475                    result.size());
476     return CameraErrorCode::SUCCESS;
477 }
478 
479 const std::unordered_map<camera_meter_mode_t, MeteringMode> TimeLapsePhotoSession::metaMeteringModeMap_ = {
480     {OHOS_CAMERA_SPOT_METERING,             METERING_MODE_SPOT},
481     {OHOS_CAMERA_REGION_METERING,           METERING_MODE_REGION},
482     {OHOS_CAMERA_OVERALL_METERING,          METERING_MODE_OVERALL},
483     {OHOS_CAMERA_CENTER_WEIGHTED_METERING,  METERING_MODE_CENTER_WEIGHTED}
484 };
485 
GetSupportedMeteringModes(vector<MeteringMode> & result)486 int32_t TimeLapsePhotoSession::GetSupportedMeteringModes(vector<MeteringMode>& result)
487 {
488     result.clear();
489     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
490         "TimeLapsePhotoSession::GetSupportedMeteringModes Session is not Commited");
491     auto inputDevice = GetInputDevice();
492     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::OPERATION_NOT_ALLOWED,
493         "GetSupportedMeteringModes camera device is null");
494     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
495     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::OPERATION_NOT_ALLOWED,
496         "GetSupportedMeteringModes camera deviceInfo is null");
497     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
498     camera_metadata_item_t item;
499     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_METER_MODES, &item);
500     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, CameraErrorCode::SUCCESS,
501         "TimeLapsePhotoSession::GetSupportedMeteringModes Failed with return code %{public}d", ret);
502     for (uint32_t i = 0; i < item.count; i++) {
503         auto itr = metaMeteringModeMap_.find(static_cast<camera_meter_mode_t>(item.data.u8[i]));
504         CHECK_EXECUTE(itr != metaMeteringModeMap_.end(), result.emplace_back(itr->second));
505     }
506     return CameraErrorCode::SUCCESS;
507 }
508 
IsExposureMeteringModeSupported(MeteringMode mode,bool & result)509 int32_t TimeLapsePhotoSession::IsExposureMeteringModeSupported(MeteringMode mode, bool& result)
510 {
511     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
512         "TimeLapsePhotoSession::IsExposureMeteringModeSupported Session is not Commited");
513     std::vector<MeteringMode> vecSupportedMeteringModeList;
514     (void)this->GetSupportedMeteringModes(vecSupportedMeteringModeList);
515     result = find(vecSupportedMeteringModeList.begin(), vecSupportedMeteringModeList.end(),
516         mode) != vecSupportedMeteringModeList.end();
517     return CameraErrorCode::SUCCESS;
518 }
519 
GetExposureMeteringMode(MeteringMode & result)520 int32_t TimeLapsePhotoSession::GetExposureMeteringMode(MeteringMode& result)
521 {
522     result = METERING_MODE_SPOT;
523     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
524         "TimeLapsePhotoSession::GetExposureMeteringMode Session is not Commited");
525     auto inputDevice = GetInputDevice();
526     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::OPERATION_NOT_ALLOWED,
527         "GetExposureMeteringMode camera device is null");
528     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
529     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::OPERATION_NOT_ALLOWED,
530         "GetExposureMeteringMode camera deviceInfo is null");
531     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
532     camera_metadata_item_t item;
533     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_METER_MODE, &item);
534     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, CameraErrorCode::SUCCESS,
535         "TimeLapsePhotoSession::GetExposureMeteringMode Failed with return code %{public}d", ret);
536     auto itr = metaMeteringModeMap_.find(static_cast<camera_meter_mode_t>(item.data.u8[0]));
537     if (itr != metaMeteringModeMap_.end()) {
538         result = itr->second;
539     }
540     return CameraErrorCode::SUCCESS;
541 }
542 
543 const std::unordered_map<MeteringMode, camera_meter_mode_t> TimeLapsePhotoSession::fwkMeteringModeMap_ = {
544     {METERING_MODE_SPOT,                    OHOS_CAMERA_SPOT_METERING},
545     {METERING_MODE_REGION,                  OHOS_CAMERA_REGION_METERING},
546     {METERING_MODE_OVERALL,                 OHOS_CAMERA_OVERALL_METERING},
547     {METERING_MODE_CENTER_WEIGHTED,         OHOS_CAMERA_CENTER_WEIGHTED_METERING}
548 };
549 
SetExposureMeteringMode(MeteringMode mode)550 int32_t TimeLapsePhotoSession::SetExposureMeteringMode(MeteringMode mode)
551 {
552     CAMERA_SYNC_TRACE;
553     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
554         "TimeLapsePhotoSession::SetExposureMeteringMode Session is not Commited");
555     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
556         "SetExposureMeteringMode Need to call LockForControl() before setting camera properties");
557     camera_meter_mode_t meteringMode = OHOS_CAMERA_SPOT_METERING;
558     auto itr = fwkMeteringModeMap_.find(mode);
559     if (itr == fwkMeteringModeMap_.end()) {
560         MEDIA_ERR_LOG("Unknown exposure mode");
561     } else {
562         meteringMode = itr->second;
563     }
564     MEDIA_INFO_LOG("Set tag OHOS_CONTROL_METER_MODE value = %{public}d", meteringMode);
565     bool ret = AddOrUpdateMetadata(changedMetadata_->get(), OHOS_CONTROL_METER_MODE, &meteringMode, 1);
566     CHECK_PRINT_ELOG(!ret, "Set tag OHOS_CONTROL_METER_MODE Failed");
567     return CameraErrorCode::SUCCESS;
568 }
569 
570 //----- ManualIso -----
GetIso(int32_t & result)571 int32_t TimeLapsePhotoSession::GetIso(int32_t& result)
572 {
573     CAMERA_SYNC_TRACE;
574     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
575         "TimeLapsePhotoSession::GetIso Session is not Commited");
576     auto inputDevice = GetInputDevice();
577     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::OPERATION_NOT_ALLOWED,
578         "TimeLapsePhotoSession::GetIso camera device is null");
579     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
580     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::OPERATION_NOT_ALLOWED,
581         "TimeLapsePhotoSession::GetIso camera deviceInfo is null");
582     std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
583     camera_metadata_item_t item;
584     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_ISO_VALUE, &item);
585     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, CameraErrorCode::SUCCESS,
586         "TimeLapsePhotoSession::GetIso Failed with return code %{public}d", ret);
587     result = item.data.i32[0];
588     MEDIA_DEBUG_LOG("%{public}s: iso = %{public}d", __FUNCTION__, result);
589     return CameraErrorCode::SUCCESS;
590 }
591 
SetIso(int32_t iso)592 int32_t TimeLapsePhotoSession::SetIso(int32_t iso)
593 {
594     CAMERA_SYNC_TRACE;
595     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
596         "TimeLapsePhotoSession::SetIso Session is not Commited");
597     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
598         "TimeLapsePhotoSession::SetIso Need to call LockForControl() before setting camera properties");
599     auto inputDevice = GetInputDevice();
600     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(),
601         CameraErrorCode::OPERATION_NOT_ALLOWED, "TimeLapsePhotoSession::SetIso camera device is null");
602     MEDIA_DEBUG_LOG("TimeLapsePhotoSession::SetIso: iso = %{public}d", iso);
603     std::vector<int32_t> isoRange;
604     CHECK_RETURN_RET_ELOG((GetIsoRange(isoRange) != CameraErrorCode::SUCCESS) && isoRange.empty(),
605         CameraErrorCode::OPERATION_NOT_ALLOWED, "TimeLapsePhotoSession::SetIso range is empty");
606     const int32_t autoIsoValue = 0;
607     CHECK_RETURN_RET(iso != autoIsoValue && std::find(isoRange.begin(), isoRange.end(), iso) == isoRange.end(),
608         CameraErrorCode::INVALID_ARGUMENT);
609     MEDIA_INFO_LOG("Set tag OHOS_CONTROL_ISO_VALUE value = %{public}d", iso);
610     bool ret = AddOrUpdateMetadata(changedMetadata_->get(), OHOS_CONTROL_ISO_VALUE, &iso, 1);
611     CHECK_PRINT_ELOG(!ret, "Set tag OHOS_CONTROL_ISO_VALUE Failed");
612     iso_ = static_cast<uint32_t>(iso);
613     return CameraErrorCode::SUCCESS;
614 }
615 
IsManualIsoSupported(bool & result)616 int32_t TimeLapsePhotoSession::IsManualIsoSupported(bool& result)
617 {
618     CAMERA_SYNC_TRACE;
619     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
620         "TimeLapsePhotoSession::IsManualIsoSupported Session is not Commited");
621     auto inputDevice = GetInputDevice();
622     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::OPERATION_NOT_ALLOWED,
623         "TimeLapsePhotoSession::IsManualIsoSupported camera device is null");
624     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
625     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::OPERATION_NOT_ALLOWED,
626         "TimeLapsePhotoSession::IsManualIsoSupported camera deviceInfo is null");
627     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
628     camera_metadata_item_t item;
629     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_ISO_VALUES, &item);
630     result = ret == CAM_META_SUCCESS && item.count != 0;
631     CHECK_PRINT_ELOG(!result, "Failed find metadata with return code %{public}d", ret);
632     return CameraErrorCode::SUCCESS;
633 }
634 
GetIsoRange(vector<int32_t> & result)635 int32_t TimeLapsePhotoSession::GetIsoRange(vector<int32_t>& result)
636 {
637     CAMERA_SYNC_TRACE;
638     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
639         "TimeLapsePhotoSession::GetIsoRange Session is not Commited");
640     auto inputDevice = GetInputDevice();
641     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(),
642         CameraErrorCode::OPERATION_NOT_ALLOWED, "TimeLapsePhotoSession::GetIsoRange camera device is null");
643     std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = GetMetadata();
644     camera_metadata_item_t item;
645     CHECK_RETURN_RET(metadata == nullptr, CameraErrorCode::INVALID_ARGUMENT);
646     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_ISO_VALUES, &item);
647     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS || item.count == 0, CameraErrorCode::INVALID_ARGUMENT,
648         "TimeLapsePhotoSession::GetIsoRange Failed with return code %{public}d", ret);
649     std::vector<std::vector<int32_t> > modeIsoRanges = {};
650     std::vector<int32_t> modeRange = {};
651     for (uint32_t i = 0; i < item.count; i++) {
652         if (item.data.i32[i] != -1) {
653             modeRange.emplace_back(item.data.i32[i]);
654             continue;
655         }
656         MEDIA_DEBUG_LOG("%{public}s: mode %{public}d, range=%{public}s", __FUNCTION__,
657                         GetMode(), Container2String(modeRange.begin(), modeRange.end()).c_str());
658         modeIsoRanges.emplace_back(std::move(modeRange));
659         modeRange.clear();
660     }
661 
662     for (auto it : modeIsoRanges) {
663         MEDIA_DEBUG_LOG("%{public}s: ranges=%{public}s", __FUNCTION__,
664                         Container2String(it.begin(), it.end()).c_str());
665         if (GetMode() == it.at(0) && it.size() > 0) {
666             result.resize(it.size() - 1);
667             std::copy(it.begin() + 1, it.end(), result.begin());
668         }
669     }
670     MEDIA_INFO_LOG("%{public}s: isoRange=%{public}s, len = %{public}zu", __FUNCTION__,
671                    Container2String(result.begin(), result.end()).c_str(), result.size());
672     return CameraErrorCode::SUCCESS;
673 }
674 
675 //----- WhiteBalance -----
IsWhiteBalanceModeSupported(WhiteBalanceMode mode,bool & result)676 int32_t TimeLapsePhotoSession::IsWhiteBalanceModeSupported(WhiteBalanceMode mode, bool& result)
677 {
678     CAMERA_SYNC_TRACE;
679     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
680         "TimeLapsePhotoSession::IsWhiteBalanceModeSupported Session is not Commited");
681     std::vector<WhiteBalanceMode> modes;
682     CHECK_RETURN_RET_ELOG(GetSupportedWhiteBalanceModes(modes) != CameraErrorCode::SUCCESS,
683         CameraErrorCode::OPERATION_NOT_ALLOWED, "Get supported white balance modes failed");
684     result = find(modes.begin(), modes.end(), mode) != modes.end();
685     return CameraErrorCode::SUCCESS;
686 }
687 
688 const std::unordered_map<camera_awb_mode_t, WhiteBalanceMode> TimeLapsePhotoSession::metaWhiteBalanceModeMap_ = {
689     { OHOS_CAMERA_AWB_MODE_OFF, AWB_MODE_OFF },
690     { OHOS_CAMERA_AWB_MODE_AUTO, AWB_MODE_AUTO },
691     { OHOS_CAMERA_AWB_MODE_INCANDESCENT, AWB_MODE_INCANDESCENT },
692     { OHOS_CAMERA_AWB_MODE_FLUORESCENT, AWB_MODE_FLUORESCENT },
693     { OHOS_CAMERA_AWB_MODE_WARM_FLUORESCENT, AWB_MODE_WARM_FLUORESCENT },
694     { OHOS_CAMERA_AWB_MODE_DAYLIGHT, AWB_MODE_DAYLIGHT },
695     { OHOS_CAMERA_AWB_MODE_CLOUDY_DAYLIGHT, AWB_MODE_CLOUDY_DAYLIGHT },
696     { OHOS_CAMERA_AWB_MODE_TWILIGHT, AWB_MODE_TWILIGHT },
697     { OHOS_CAMERA_AWB_MODE_SHADE, AWB_MODE_SHADE },
698 };
699 
GetSupportedWhiteBalanceModes(std::vector<WhiteBalanceMode> & result)700 int32_t TimeLapsePhotoSession::GetSupportedWhiteBalanceModes(std::vector<WhiteBalanceMode> &result)
701 {
702     CAMERA_SYNC_TRACE;
703     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
704         "TimeLapsePhotoSession::GetSupportedWhiteBalanceModes Session is not Commited");
705     auto inputDevice = GetInputDevice();
706     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::OPERATION_NOT_ALLOWED,
707         "GetSupportedWhiteBalanceModes camera device is null");
708     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
709     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::OPERATION_NOT_ALLOWED,
710         "GetSupportedWhiteBalanceModes camera device is null");
711     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
712     camera_metadata_item_t item;
713     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_AWB_MODES, &item);
714     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, CameraErrorCode::SUCCESS,
715         "TimeLapsePhotoSession::GetSupportedWhiteBalanceModes Failed with return code %{public}d", ret);
716     for (uint32_t i = 0; i < item.count; i++) {
717         auto itr = metaWhiteBalanceModeMap_.find(static_cast<camera_awb_mode_t>(item.data.u8[i]));
718         CHECK_EXECUTE(itr != metaWhiteBalanceModeMap_.end(), result.emplace_back(itr->second));
719     }
720     return CameraErrorCode::SUCCESS;
721 }
722 
GetWhiteBalanceRange(vector<int32_t> & result)723 int32_t TimeLapsePhotoSession::GetWhiteBalanceRange(vector<int32_t>& result)
724 {
725     CAMERA_SYNC_TRACE;
726     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
727         "TimeLapsePhotoSession::GetWhiteBalanceRange Session is not Commited");
728     auto inputDevice = GetInputDevice();
729     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::OPERATION_NOT_ALLOWED,
730         "GetWhiteBalanceRange camera device is null");
731     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
732     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::OPERATION_NOT_ALLOWED,
733         "GetWhiteBalanceRange camera deviceInfo is null");
734     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
735     camera_metadata_item_t item;
736     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_SENSOR_WB_VALUES, &item);
737     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, CameraErrorCode::SUCCESS,
738         "TimeLapsePhotoSession::GetWhiteBalanceRange Failed with return code %{public}d", ret);
739 
740     for (uint32_t i = 0; i < item.count; i++) {
741         result.emplace_back(item.data.i32[i]);
742     }
743     return CameraErrorCode::SUCCESS;
744 }
745 
GetWhiteBalanceMode(WhiteBalanceMode & result)746 int32_t TimeLapsePhotoSession::GetWhiteBalanceMode(WhiteBalanceMode& result)
747 {
748     CAMERA_SYNC_TRACE;
749     result = AWB_MODE_OFF;
750     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
751         "TimeLapsePhotoSession::GetWhiteBalanceMode Session is not Commited");
752     auto inputDevice = GetInputDevice();
753     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::OPERATION_NOT_ALLOWED,
754         "TimeLapsePhotoSession::GetWhiteBalanceMode camera device is null");
755     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
756     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::OPERATION_NOT_ALLOWED,
757         "TimeLapsePhotoSession::GetWhiteBalanceMode camera deviceInfo is null");
758     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
759     camera_metadata_item_t item;
760     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_AWB_MODE, &item);
761     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, CameraErrorCode::SUCCESS,
762         "TimeLapsePhotoSession::GetWhiteBalanceMode Failed with return code %{public}d", ret);
763     auto itr = metaWhiteBalanceModeMap_.find(static_cast<camera_awb_mode_t>(item.data.u8[0]));
764     if (itr != metaWhiteBalanceModeMap_.end()) {
765         result = itr->second;
766     }
767     return CameraErrorCode::SUCCESS;
768 }
769 
770 const std::unordered_map<WhiteBalanceMode, camera_awb_mode_t> TimeLapsePhotoSession::fwkWhiteBalanceModeMap_ = {
771     { AWB_MODE_AUTO, OHOS_CAMERA_AWB_MODE_AUTO },
772     { AWB_MODE_OFF, OHOS_CAMERA_AWB_MODE_OFF },
773     { AWB_MODE_INCANDESCENT, OHOS_CAMERA_AWB_MODE_INCANDESCENT },
774     { AWB_MODE_FLUORESCENT, OHOS_CAMERA_AWB_MODE_FLUORESCENT },
775     { AWB_MODE_WARM_FLUORESCENT, OHOS_CAMERA_AWB_MODE_WARM_FLUORESCENT },
776     { AWB_MODE_DAYLIGHT, OHOS_CAMERA_AWB_MODE_DAYLIGHT },
777     { AWB_MODE_CLOUDY_DAYLIGHT, OHOS_CAMERA_AWB_MODE_CLOUDY_DAYLIGHT },
778     { AWB_MODE_TWILIGHT, OHOS_CAMERA_AWB_MODE_TWILIGHT },
779     { AWB_MODE_SHADE, OHOS_CAMERA_AWB_MODE_SHADE },
780 };
781 
SetWhiteBalanceMode(WhiteBalanceMode mode)782 int32_t TimeLapsePhotoSession::SetWhiteBalanceMode(WhiteBalanceMode mode)
783 {
784     CAMERA_SYNC_TRACE;
785     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
786         "TimeLapsePhotoSession::SetWhiteBalanceMode Session is not Commited");
787     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
788         "TimeLapsePhotoSession::SetWhiteBalanceMode Need to call LockForControl() before setting camera properties");
789     camera_awb_mode_t whiteBalanceMode = OHOS_CAMERA_AWB_MODE_OFF;
790     auto itr = fwkWhiteBalanceModeMap_.find(mode);
791     if (itr == fwkWhiteBalanceModeMap_.end()) {
792         MEDIA_WARNING_LOG("%{public}s: Unknown exposure mode", __FUNCTION__);
793     } else {
794         whiteBalanceMode = itr->second;
795     }
796     MEDIA_DEBUG_LOG("%{public}s: WhiteBalance mode: %{public}d", __FUNCTION__, whiteBalanceMode);
797     // no manual wb mode need set maunual value to 0
798     CHECK_EXECUTE(mode != AWB_MODE_OFF, SetWhiteBalance(0));
799     bool ret = AddOrUpdateMetadata(changedMetadata_->get(), OHOS_CONTROL_AWB_MODE, &whiteBalanceMode, 1);
800     CHECK_PRINT_ELOG(!ret, "%{public}s: Failed to set WhiteBalance mode", __FUNCTION__);
801     return CameraErrorCode::SUCCESS;
802 }
803 
GetWhiteBalance(int32_t & result)804 int32_t TimeLapsePhotoSession::GetWhiteBalance(int32_t& result)
805 {
806     CAMERA_SYNC_TRACE;
807     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
808         "TimeLapsePhotoSession::GetWhiteBalance Session is not Commited");
809     auto inputDevice = GetInputDevice();
810     CHECK_RETURN_RET_ELOG(!inputDevice, CameraErrorCode::OPERATION_NOT_ALLOWED,
811         "TimeLapsePhotoSession::GetWhiteBalance camera device is null");
812     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
813     CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::OPERATION_NOT_ALLOWED,
814         "TimeLapsePhotoSession::GetWhiteBalance camera deviceInfo is null");
815     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
816     camera_metadata_item_t item;
817     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_SENSOR_WB_VALUE, &item);
818     CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, CameraErrorCode::SUCCESS,
819         "TimeLapsePhotoSession::GetWhiteBalance Failed with return code %{public}d", ret);
820     if (item.count != 0) {
821         result = item.data.i32[0];
822     }
823     return CameraErrorCode::SUCCESS;
824 }
825 
SetWhiteBalance(int32_t wb)826 int32_t TimeLapsePhotoSession::SetWhiteBalance(int32_t wb)
827 {
828     CAMERA_SYNC_TRACE;
829     CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
830         "TimeLapsePhotoSession::SetWhiteBalance Session is not Commited");
831     CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
832         "TimeLapsePhotoSession::SetWhiteBalance Need to call LockForControl() before setting camera properties");
833     auto inputDevice = GetInputDevice();
834     CHECK_RETURN_RET_ELOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(),
835         CameraErrorCode::OPERATION_NOT_ALLOWED, "TimeLapsePhotoSession::SetWhiteBalance camera device is null");
836     MEDIA_INFO_LOG("Set tag OHOS_CONTROL_SENSOR_WB_VALUE %{public}d", wb);
837     bool res = AddOrUpdateMetadata(changedMetadata_->get(), OHOS_CONTROL_SENSOR_WB_VALUE, &wb, 1);
838     CHECK_PRINT_ELOG(!res, "TimeLapsePhotoSession::SetWhiteBalance Failed");
839     return CameraErrorCode::SUCCESS;
840 }
841 }
842 }
843 
844