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