1 /*
2  * Copyright (c) 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 #include "session/slow_motion_session.h"
17 #include "input/camera_input.h"
18 #include "input/camera_manager.h"
19 #include "output/camera_output_capability.h"
20 #include "camera_log.h"
21 #include "camera_error_code.h"
22 #include "camera_util.h"
23 
24 namespace OHOS {
25 namespace CameraStandard {
26 
27 const std::unordered_map<camera_slow_motion_status_type_t, SlowMotionState> SlowMotionSession::metaMotionStateMap_ = {
28     {OHOS_CONTROL_SLOW_MOTION_STATUS_DISABLE, SlowMotionState::DISABLE},
29     {OHOS_CONTROL_SLOW_MOTION_STATUS_READY, SlowMotionState::READY},
30     {OHOS_CONTROL_SLOW_MOTION_STATUS_START, SlowMotionState::START},
31     {OHOS_CONTROL_SLOW_MOTION_STATUS_RECORDING, SlowMotionState::RECORDING},
32     {OHOS_CONTROL_SLOW_MOTION_STATUS_FINISH, SlowMotionState::FINISH}
33 };
34 
ProcessCallbacks(const uint64_t timestamp,const std::shared_ptr<OHOS::Camera::CameraMetadata> & result)35 void SlowMotionSession::SlowMotionSessionMetadataResultProcessor::ProcessCallbacks(
36     const uint64_t timestamp, const std::shared_ptr<OHOS::Camera::CameraMetadata>& result)
37 {
38     MEDIA_INFO_LOG("SlowMotionSessionMetadataResultProcessor::ProcessCallbacks is called");
39     auto session = session_.promote();
40     CHECK_ERROR_RETURN_LOG(session == nullptr,
41         "SlowMotionSessionMetadataResultProcessor ProcessCallbacks but session is null");
42 
43     session->OnSlowMotionStateChange(result);
44 }
45 
~SlowMotionSession()46 SlowMotionSession::~SlowMotionSession()
47 {
48 }
49 
CanAddOutput(sptr<CaptureOutput> & output)50 bool SlowMotionSession::CanAddOutput(sptr<CaptureOutput> &output)
51 {
52     MEDIA_DEBUG_LOG("Enter Into CanAddOutput");
53     return CaptureSession::CanAddOutput(output);
54 }
55 
IsSlowMotionDetectionSupported()56 bool SlowMotionSession::IsSlowMotionDetectionSupported()
57 {
58     CAMERA_SYNC_TRACE;
59     MEDIA_DEBUG_LOG("IsSlowMotionDetectionSupported is called");
60     CHECK_ERROR_RETURN_RET_LOG(!IsSessionCommited(), false,
61         "IsSlowMotionDetectionSupported Session is not Commited");
62     auto inputDevice = GetInputDevice();
63     CHECK_ERROR_RETURN_RET_LOG(!inputDevice, false,
64         "IsSlowMotionDetectionSupported camera device is null");
65     auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
66     CHECK_ERROR_RETURN_RET_LOG(!inputDeviceInfo, false,
67         "IsSlowMotionDetectionSupported camera deviceInfo is null");
68     std::shared_ptr<Camera::CameraMetadata> metadata = inputDeviceInfo->GetMetadata();
69     camera_metadata_item_t item;
70     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_MOTION_DETECTION_SUPPORT, &item);
71     CHECK_ERROR_RETURN_RET_LOG(ret != CAM_META_SUCCESS, false,
72         "IsSlowMotionDetectionSupported Failed with return code %{public}d", ret);
73     MEDIA_INFO_LOG("IsSlowMotionDetectionSupported value: %{public}u", item.data.u8[0]);
74     CHECK_AND_RETURN_RET(item.data.u8[0] != 1, true);
75     return false;
76 }
77 
NormalizeRect(Rect & rect)78 void SlowMotionSession::NormalizeRect(Rect& rect)
79 {
80     // Validate and adjust topLeftX and topLeftY
81     rect.topLeftX = std::max(0.0, std::min(1.0, rect.topLeftX));
82     rect.topLeftY = std::max(0.0, std::min(1.0, rect.topLeftY));
83 
84     // Validate and adjust width and height
85     rect.width = std::max(0.0, std::min(1.0, rect.width));
86     rect.height = std::max(0.0, std::min(1.0, rect.height));
87 }
88 
SetSlowMotionDetectionArea(Rect rect)89 void SlowMotionSession::SetSlowMotionDetectionArea(Rect rect)
90 {
91     CAMERA_SYNC_TRACE;
92     MEDIA_DEBUG_LOG("SetSlowMotionDetectionArea is called");
93     CHECK_ERROR_RETURN_LOG(!IsSessionCommited(),
94         "SetSlowMotionDetectionArea Session is not Commited");
95     this->LockForControl();
96     CHECK_ERROR_RETURN_LOG(changedMetadata_ == nullptr,
97         "SetSlowMotionDetectionArea changedMetadata is null");
98     int32_t retCode = EnableMotionDetection(true);
99     CHECK_ERROR_RETURN_LOG(retCode != CameraErrorCode::SUCCESS, "EnableMotionDetection call failed");
100     MEDIA_INFO_LOG("topLeftX: %{public}f, topLeftY: %{public}f, width: %{public}f, height: %{public}f",
101         rect.topLeftX, rect.topLeftY, rect.width, rect.height);
102     NormalizeRect(rect);
103     bool status = false;
104     int32_t ret;
105     camera_metadata_item_t item;
106     std::vector<float> rectVec = {static_cast<float>(rect.topLeftX), static_cast<float>(rect.topLeftY),
107         static_cast<float>(rect.width), static_cast<float>(rect.height)};
108     ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_MOTION_DETECTION_CHECK_AREA, &item);
109     if (ret == CAM_META_SUCCESS) {
110         status = changedMetadata_->updateEntry(OHOS_CONTROL_MOTION_DETECTION_CHECK_AREA,
111             rectVec.data(), rectVec.size());
112     } else if (ret == CAM_META_ITEM_NOT_FOUND) {
113         status = changedMetadata_->addEntry(OHOS_CONTROL_MOTION_DETECTION_CHECK_AREA, rectVec.data(), rectVec.size());
114     }
115     this->UnlockForControl();
116     CHECK_ERROR_PRINT_LOG(!status, "SetSlowMotionDetectionArea failed to set motion rect");
117     return;
118 }
119 
OnSlowMotionStateChange(std::shared_ptr<OHOS::Camera::CameraMetadata> cameraResult)120 void SlowMotionSession::OnSlowMotionStateChange(std::shared_ptr<OHOS::Camera::CameraMetadata> cameraResult)
121 {
122     std::shared_ptr<SlowMotionStateCallback> appCallback = GetApplicationCallback();
123     CHECK_ERROR_RETURN_LOG(appCallback == nullptr, "OnSlowMotionStateChange appCallback is null");
124     SlowMotionState state = SlowMotionState::DISABLE;
125     if (cameraResult != nullptr) {
126         camera_metadata_item_t item;
127         common_metadata_header_t* metadata = cameraResult->get();
128         int ret = OHOS::Camera::FindCameraMetadataItem(metadata, OHOS_STATUS_SLOW_MOTION_DETECTION, &item);
129         if (ret != CAM_META_SUCCESS) {
130             MEDIA_ERR_LOG("OnSlowMotionStateChange Failed with return code %{public}d", ret);
131         } else {
132             MEDIA_DEBUG_LOG("slowMotionState: %{public}d", item.data.u8[0]);
133             auto itr = metaMotionStateMap_.find(static_cast<camera_slow_motion_status_type_t>(item.data.u8[0]));
134             if (itr != metaMotionStateMap_.end()) {
135                 state = itr->second;
136             }
137         }
138     } else {
139         MEDIA_ERR_LOG("cameraResult is null");
140     }
141     if (state != appCallback->GetSlowMotionState()) {
142         MEDIA_INFO_LOG("OnSlowMotionStateChange call success, preState: %{public}d, curState: %{public}d",
143             appCallback->GetSlowMotionState(), state);
144         appCallback->SetSlowMotionState(state);
145         appCallback->OnSlowMotionState(state);
146     }
147 }
148 
SetCallback(std::shared_ptr<SlowMotionStateCallback> callback)149 void SlowMotionSession::SetCallback(std::shared_ptr<SlowMotionStateCallback> callback)
150 {
151     CHECK_ERROR_RETURN_LOG(callback == nullptr, "SlowMotionSession::SetCallback callback is null");
152     std::lock_guard<std::mutex> lock(stateCallbackMutex_);
153     slowMotionStateCallback_ = callback;
154 }
155 
GetApplicationCallback()156 std::shared_ptr<SlowMotionStateCallback> SlowMotionSession::GetApplicationCallback()
157 {
158     std::lock_guard<std::mutex> lock(stateCallbackMutex_);
159     return slowMotionStateCallback_;
160 }
161 
EnableMotionDetection(bool isEnable)162 int32_t SlowMotionSession::EnableMotionDetection(bool isEnable)
163 {
164     CAMERA_SYNC_TRACE;
165     MEDIA_DEBUG_LOG("Enter EnableMotionDetection, isEnable:%{public}d", isEnable);
166     CHECK_ERROR_RETURN_RET_LOG(!IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
167         "EnableMotionDetection session not commited");
168     bool status = false;
169     int32_t ret;
170     camera_metadata_item_t item;
171     ret = Camera::FindCameraMetadataItem(changedMetadata_->get(), OHOS_CONTROL_MOTION_DETECTION, &item);
172     uint8_t enableValue = static_cast<uint8_t>(isEnable ?
173         OHOS_CAMERA_MOTION_DETECTION_ENABLE : OHOS_CAMERA_MOTION_DETECTION_DISABLE);
174     if (ret == CAM_META_ITEM_NOT_FOUND) {
175         status = changedMetadata_->addEntry(OHOS_CONTROL_MOTION_DETECTION, &enableValue, 1);
176     } else if (ret == CAM_META_SUCCESS) {
177         status = changedMetadata_->updateEntry(OHOS_CONTROL_MOTION_DETECTION, &enableValue, 1);
178     }
179     CHECK_ERROR_PRINT_LOG(!status, "EnableMotionDetection Failed to enable motion detection");
180     return CameraErrorCode::SUCCESS;
181 }
182 } // namespace CameraStandard
183 } // namespace OHOS