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 #ifndef OHOS_CAMERA_SLOW_MOTION_SESSION_H
17 #define OHOS_CAMERA_SLOW_MOTION_SESSION_H
18 
19 #include "capture_session.h"
20 #include "icapture_session.h"
21 #include "input/capture_input.h"
22 #include "output/metadata_output.h"
23 
24 namespace OHOS {
25 namespace CameraStandard {
26 enum SlowMotionState {
27     DEFAULT = -1,
28     DISABLE = 0,
29     READY,
30     START,
31     RECORDING,
32     FINISH
33 };
34 
35 class SlowMotionStateCallback {
36 public:
37     SlowMotionStateCallback() = default;
38     virtual ~SlowMotionStateCallback() = default;
39     virtual void OnSlowMotionState(const SlowMotionState state) = 0;
40 
SetSlowMotionState(const SlowMotionState state)41     void SetSlowMotionState(const SlowMotionState state)
42     {
43         std::lock_guard<std::mutex> lock(mutex_);
44         currentState_ = state;
45     }
GetSlowMotionState()46     SlowMotionState GetSlowMotionState()
47     {
48         std::lock_guard<std::mutex> lock(mutex_);
49         return currentState_;
50     }
51 
52 private:
53     SlowMotionState currentState_;
54     std::mutex mutex_;
55 };
56 
57 class SlowMotionSession : public CaptureSession {
58 public:
59     class SlowMotionSessionMetadataResultProcessor : public MetadataResultProcessor {
60     public:
SlowMotionSessionMetadataResultProcessor(wptr<SlowMotionSession> session)61         explicit SlowMotionSessionMetadataResultProcessor(wptr<SlowMotionSession> session) : session_(session) {}
62         void ProcessCallbacks(
63             const uint64_t timestamp, const std::shared_ptr<OHOS::Camera::CameraMetadata>& result) override;
64 
65     private:
66         wptr<SlowMotionSession> session_;
67     };
SlowMotionSession(sptr<ICaptureSession> & slowMotionSession)68     explicit SlowMotionSession(sptr<ICaptureSession> &slowMotionSession): CaptureSession(slowMotionSession)
69     {
70         metadataResultProcessor_ = std::make_shared<SlowMotionSessionMetadataResultProcessor>(this);
71     }
72     ~SlowMotionSession();
73 
74     /**
75      * @brief Determine if the given Ouput can be added to session.
76      *
77      * @param CaptureOutput to be added to session.
78      */
79     bool CanAddOutput(sptr<CaptureOutput>& output) override;
80 
81     /**
82      * @brief Checks if motion detection is supported.
83      *
84      * @return true if motion detection is supported, false otherwise.
85      */
86     bool IsSlowMotionDetectionSupported();
87 
88     /**
89      * @brief Starts motion monitoring within the specified rectangle.
90      *
91      * @param rect The rectangle defining the area where motion monitoring will be started.
92      *             It consists of the coordinates of the top-left corner (topLeftX, topLeftY),
93      *             width, and height.
94      */
95     void SetSlowMotionDetectionArea(Rect rect);
96 
97     /**
98      * @brief Callback function invoked when there is a change in slow motion state.
99      *
100      * @param state The new state of slow motion.
101      */
102     void OnSlowMotionStateChange(std::shared_ptr<OHOS::Camera::CameraMetadata> cameraResult);
103 
104     /**
105      * @brief Sets the callback function for handling changes in slow motion state.
106      *
107      * @param callback A shared pointer to the callback function object.
108      */
109     void SetCallback(std::shared_ptr<SlowMotionStateCallback> callback);
110 
111     /**
112      * @brief Normalizes the values of a Rect object, setting values greater than 1 to 1
113      * and values less than 0 to 0.
114      *
115      * @param rect The Rect object to be normalized.
116      */
117     void NormalizeRect(Rect& rect);
118 
119     /**
120      * @brief Enables or disables motion detection.
121      *
122      * @param isEnable true to enable motion detection, false to disable.
123      *
124      * @return If the operation is successful, returns 0; otherwise, returns the corresponding error code.
125      */
126     int32_t EnableMotionDetection(bool isEnable);
127 
128     /**
129      * @brief Retrieves the application callback for slow motion state changes.
130      *
131      * @return A shared pointer to the application callback for slow motion state changes.
132      */
133     std::shared_ptr<SlowMotionStateCallback> GetApplicationCallback();
134 
135 private:
136     std::mutex stateCallbackMutex_;
137     std::shared_ptr<SlowMotionStateCallback> slowMotionStateCallback_;
138     static const std::unordered_map<camera_slow_motion_status_type_t, SlowMotionState> metaMotionStateMap_;
139 };
140 } // namespace CameraStandard
141 } // namespace OHOS
142 #endif // OHOS_CAMERA_SLOW_MOTION_SESSION_H