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