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