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