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