1 /*
2  * Copyright (c) 2021-2022 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/night_session.h"
17 #include "camera_util.h"
18 #include "input/camera_input.h"
19 #include "camera_log.h"
20 #include "output/photo_output.h"
21 #include "output/preview_output.h"
22 #include "output/video_output.h"
23 
24 namespace OHOS {
25 namespace CameraStandard {
~NightSession()26 NightSession::~NightSession()
27 {
28     MEDIA_DEBUG_LOG("Enter Into NightSession::~NightSession()");
29 }
30 
GetExposureRange(std::vector<uint32_t> & exposureRange)31 int32_t NightSession::GetExposureRange(std::vector<uint32_t> &exposureRange)
32 {
33     exposureRange.clear();
34     CHECK_ERROR_RETURN_RET_LOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
35         "NightSession::GetExposureRange Session is not Commited");
36     auto inputDevice = GetInputDevice();
37     CHECK_ERROR_RETURN_RET_LOG(!inputDevice, CameraErrorCode::INVALID_ARGUMENT,
38         "NightSession::GetExposureRange camera device is null");
39     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
40     CHECK_ERROR_RETURN_RET_LOG(!inputDeviceInfo, CameraErrorCode::INVALID_ARGUMENT,
41         "NightSession::GetExposureRange camera device is null");
42     std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = inputDeviceInfo->GetMetadata();
43     CHECK_AND_RETURN_RET(metadata != nullptr, CameraErrorCode::INVALID_ARGUMENT);
44     camera_metadata_item_t item;
45     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_NIGHT_MODE_SUPPORTED_EXPOSURE_TIME, &item);
46     CHECK_ERROR_RETURN_RET_LOG(ret != CAM_META_SUCCESS || item.count == 0, CameraErrorCode::INVALID_ARGUMENT,
47         "NightSession::GetExposureRange Failed with return code %{public}d", ret);
48     for (uint32_t i = 0; i < item.count; i++) {
49         exposureRange.emplace_back(item.data.ui32[i]);
50     }
51     return CameraErrorCode::SUCCESS;
52 }
53 
SetExposure(uint32_t exposureValue)54 int32_t NightSession::SetExposure(uint32_t exposureValue)
55 {
56     CHECK_ERROR_RETURN_RET_LOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
57         "NightSession::SetExposure Session is not Commited");
58     CHECK_ERROR_RETURN_RET_LOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
59         "NightSession::SetExposure Need to call LockForControl() before setting camera properties");
60     bool status = false;
61     int32_t count = 1;
62     camera_metadata_item_t item;
63     MEDIA_DEBUG_LOG("NightSession::SetExposure exposure compensation: %{public}d", exposureValue);
64     auto inputDevice = GetInputDevice();
65     CHECK_ERROR_RETURN_RET_LOG(!inputDevice || !inputDevice->GetCameraDeviceInfo(),
66         CameraErrorCode::OPERATION_NOT_ALLOWED, "NightSession::SetExposure camera device is null");
67 
68     std::vector<uint32_t> exposureRange;
69     CHECK_ERROR_RETURN_RET_LOG((GetExposureRange(exposureRange) != CameraErrorCode::SUCCESS) && exposureRange.empty(),
70         CameraErrorCode::OPERATION_NOT_ALLOWED, "NightSession::SetExposure range is empty");
71     const uint32_t autoLongExposure = 0;
72     bool result = std::find(exposureRange.begin(), exposureRange.end(), exposureValue) == exposureRange.end();
73     CHECK_ERROR_RETURN_RET_LOG(result && exposureValue != autoLongExposure, CameraErrorCode::OPERATION_NOT_ALLOWED,
74         "NightSession::SetExposure value(%{public}d)is not supported!", exposureValue);
75     uint32_t exposureCompensation = exposureValue;
76     int ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_MANUAL_EXPOSURE_TIME, &item);
77     if (ret == CAM_META_ITEM_NOT_FOUND) {
78         status = changedMetadata_->addEntry(OHOS_CONTROL_MANUAL_EXPOSURE_TIME, &exposureCompensation, count);
79     } else if (ret == CAM_META_SUCCESS) {
80         status = changedMetadata_->updateEntry(OHOS_CONTROL_MANUAL_EXPOSURE_TIME, &exposureCompensation, count);
81     }
82     CHECK_ERROR_PRINT_LOG(!status, "NightSession::SetExposure Failed to set exposure compensation");
83     return CameraErrorCode::SUCCESS;
84 }
85 
GetExposure(uint32_t & exposureValue)86 int32_t NightSession::GetExposure(uint32_t &exposureValue)
87 {
88     CHECK_ERROR_RETURN_RET_LOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
89         "NightSession::GetExposure Session is not Commited");
90     auto inputDevice = GetInputDevice();
91     CHECK_ERROR_RETURN_RET_LOG(!inputDevice, CameraErrorCode::INVALID_ARGUMENT,
92         "NightSession::GetExposure camera device is null");
93     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
94     CHECK_ERROR_RETURN_RET_LOG(!inputDeviceInfo, CameraErrorCode::INVALID_ARGUMENT,
95         "NightSession::GetExposure camera deviceInfo is null");
96     std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = inputDeviceInfo->GetMetadata();
97     CHECK_AND_RETURN_RET(metadata != nullptr, CameraErrorCode::INVALID_ARGUMENT);
98     camera_metadata_item_t item;
99     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_MANUAL_EXPOSURE_TIME, &item);
100     if (ret != CAM_META_SUCCESS) {
101         MEDIA_ERR_LOG("NightSession::GetExposure Failed with return code %{public}d", ret);
102         return CameraErrorCode::INVALID_ARGUMENT;
103     }
104     CHECK_ERROR_RETURN_RET_LOG(ret != CAM_META_SUCCESS, CameraErrorCode::INVALID_ARGUMENT,
105         "NightSession::GetExposure Failed with return code %{public}d", ret);
106     exposureValue = item.data.ui32[0];
107     MEDIA_DEBUG_LOG("exposureValue: %{public}d", exposureValue);
108     return CameraErrorCode::SUCCESS;
109 }
110 
ProcessCallbacks(const uint64_t timestamp,const std::shared_ptr<OHOS::Camera::CameraMetadata> & result)111 void NightSession::NightSessionMetadataResultProcessor::ProcessCallbacks(
112     const uint64_t timestamp, const std::shared_ptr<OHOS::Camera::CameraMetadata>& result)
113 {
114     MEDIA_INFO_LOG("CaptureSession::NightSessionMetadataResultProcessor ProcessCallbacks");
115     auto session = session_.promote();
116     CHECK_ERROR_RETURN_LOG(session == nullptr,
117         "CaptureSession::NightSessionMetadataResultProcessor ProcessCallbacks but session is null");
118 
119     session->ProcessAutoFocusUpdates(result);
120     session->ProcessLcdFlashStatusUpdates(result);
121 }
122 
CanAddOutput(sptr<CaptureOutput> & output)123 bool NightSession::CanAddOutput(sptr<CaptureOutput>& output)
124 {
125     CAMERA_SYNC_TRACE;
126     MEDIA_DEBUG_LOG("Enter Into NightSession::CanAddOutput");
127     CHECK_ERROR_RETURN_RET_LOG(!IsSessionConfiged() || output == nullptr, false,
128         "NightSession::CanAddOutput operation is Not allowed!");
129     return output->GetOutputType() != CAPTURE_OUTPUT_TYPE_VIDEO && CaptureSession::CanAddOutput(output);
130 }
131 } // CameraStandard
132 } // OHOS
133