1 /*
2 * Copyright (c) 2024-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
17 #include "session/light_painting_session.h"
18 #include "camera_error_code.h"
19 #include "camera_log.h"
20 #include "camera_metadata_operator.h"
21 #include "metadata_common_utils.h"
22
23 namespace OHOS {
24 namespace CameraStandard {
~LightPaintingSession()25 LightPaintingSession::~LightPaintingSession()
26 {
27 MEDIA_DEBUG_LOG("Enter Into LightPaintingSession::~LightPaintingSession()");
28 }
29
GetSupportedLightPaintings(std::vector<LightPaintingType> & lightPaintings)30 int32_t LightPaintingSession::GetSupportedLightPaintings(std::vector<LightPaintingType>& lightPaintings)
31 {
32 // LCOV_EXCL_START
33 CHECK_RETURN_RET_ELOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
34 "LightPaintingSession::GetSupportedLightPaintings Session is not Commited");
35 auto inputDevice = GetInputDevice();
36 CHECK_RETURN_RET_ELOG(!inputDevice,
37 CameraErrorCode::SESSION_NOT_CONFIG,
38 "LightPaintingSession::GetSupportedLightPaintings camera device is null");
39 auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
40 CHECK_RETURN_RET_ELOG(!inputDeviceInfo, CameraErrorCode::SESSION_NOT_CONFIG,
41 "LightPaintingSession::GetSupportedLightPaintings camera deviceInfo is null");
42 std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = inputDeviceInfo->GetCachedMetadata();
43 CHECK_RETURN_RET(metadata == nullptr, CameraErrorCode::OPERATION_NOT_ALLOWED);
44 camera_metadata_item_t item;
45 int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_LIGHT_PAINTING_TYPE, &item);
46 CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS || item.count == 0, CameraErrorCode::OPERATION_NOT_ALLOWED,
47 "LightPaintingSession::GetSupportedLightPaintings Failed with return code %{public}d.", ret);
48 lightPaintings.clear();
49 for (uint32_t i = 0; i < item.count; i++) {
50 auto itr = metaLightPaintingTypeMap_.find(static_cast<CameraLightPaintingType>(item.data.u8[i]));
51 CHECK_EXECUTE(itr != metaLightPaintingTypeMap_.end(), lightPaintings.emplace_back(itr->second));
52 }
53 return CameraErrorCode::SUCCESS;
54 // LCOV_EXCL_STOP
55 }
56
GetLightPainting(LightPaintingType & lightPaintingType)57 int32_t LightPaintingSession::GetLightPainting(LightPaintingType& lightPaintingType)
58 {
59 // LCOV_EXCL_START
60 CHECK_RETURN_RET_ELOG(!(IsSessionCommited() || IsSessionConfiged()), CameraErrorCode::SESSION_NOT_CONFIG,
61 "LightPaintingSession::GetLightPainting Session is not Commited.");
62 auto itr = fwkLightPaintingTypeMap_.find(currentLightPaintingType_);
63 if (itr != fwkLightPaintingTypeMap_.end()) {
64 lightPaintingType = itr->first;
65 }
66 return CameraErrorCode::SUCCESS;
67 // LCOV_EXCL_STOP
68 }
69
SetLightPainting(const LightPaintingType type)70 int32_t LightPaintingSession::SetLightPainting(const LightPaintingType type)
71 {
72 // LCOV_EXCL_START
73 MEDIA_INFO_LOG("SetLightPainting native is called");
74 CHECK_RETURN_RET_ELOG(!(IsSessionCommited() || IsSessionConfiged()), CameraErrorCode::SESSION_NOT_CONFIG,
75 "LightPaintingSession::SetLightPainting Session is not Commited.");
76 CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
77 "LightPaintingSession::SetLightPainting Need to call LockForControl() before setting camera properties.");
78 uint8_t lightPainting = LightPaintingType::CAR;
79 auto itr = fwkLightPaintingTypeMap_.find(type);
80 CHECK_RETURN_RET_ELOG(itr == fwkLightPaintingTypeMap_.end(), CameraErrorCode::INVALID_ARGUMENT,
81 "LightPaintingSession::SetLightPainting unknown type of LightPainting.");
82 lightPainting = itr->second;
83 MEDIA_DEBUG_LOG("LightPaintingSession::SetLightPainting: %{public}d", type);
84 bool status = AddOrUpdateMetadata(changedMetadata_, OHOS_CONTROL_LIGHT_PAINTING_TYPE, &lightPainting, 1);
85 CHECK_RETURN_RET_ELOG(!status, CameraErrorCode::SERVICE_FATL_ERROR,
86 "LightPaintingSession::SetLightPainting Failed to set LightPainting type");
87 currentLightPaintingType_ = type;
88 return CameraErrorCode::SUCCESS;
89 // LCOV_EXCL_STOP
90 }
91
TriggerLighting()92 int32_t LightPaintingSession::TriggerLighting()
93 {
94 // LCOV_EXCL_START
95 MEDIA_INFO_LOG("TriggerLighting native is called");
96 CHECK_RETURN_RET_ELOG(!(IsSessionCommited() || IsSessionConfiged()), CameraErrorCode::SESSION_NOT_CONFIG,
97 "LightPaintingSession::TriggerLighting Session is not Commited.");
98 CHECK_RETURN_RET_ELOG(changedMetadata_ == nullptr, CameraErrorCode::SUCCESS,
99 "LightPaintingSession::TriggerLighting Need to call LockForControl() before setting camera properties.");
100 CHECK_RETURN_RET(currentLightPaintingType_ != LightPaintingType::LIGHT, CameraErrorCode::INVALID_ARGUMENT);
101 uint8_t enableTrigger = 1;
102 MEDIA_DEBUG_LOG("LightPaintingSession::TriggerLighting once.");
103 bool status = AddOrUpdateMetadata(changedMetadata_, OHOS_CONTROL_LIGHT_PAINTING_FLASH, &enableTrigger, 1);
104 CHECK_RETURN_RET_ELOG(!status, CameraErrorCode::SERVICE_FATL_ERROR,
105 "LightPaintingSession::TriggerLighting Failed to trigger lighting");
106 return CameraErrorCode::SUCCESS;
107 // LCOV_EXCL_STOP
108 }
109 } // CameraStandard
110 } // OHOS