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