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