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