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 #ifndef SLOW_MOTION_SESSION_NAPI_H 17 #define SLOW_MOTION_SESSION_NAPI_H 18 19 #include "napi/native_api.h" 20 #include "napi/native_node_api.h" 21 #include "session/camera_session_napi.h" 22 #include "session/slow_motion_session.h" 23 24 namespace OHOS { 25 namespace CameraStandard { 26 static const char SLOW_MOTION_SESSION_NAPI_CLASS_NAME[] = "SlowMotionSession"; 27 28 class SlowMotionStateListener : public SlowMotionStateCallback, public ListenerBase { 29 public: SlowMotionStateListener(napi_env env)30 explicit SlowMotionStateListener(napi_env env) : ListenerBase(env) 31 { 32 SetSlowMotionState(SlowMotionState::DEFAULT); 33 } 34 ~SlowMotionStateListener() = default; 35 void OnSlowMotionState(const SlowMotionState state) override; 36 private: 37 void OnSlowMotionStateCb(const SlowMotionState state) const; 38 void OnSlowMotionStateCbAsync(const SlowMotionState state) const; 39 }; 40 41 struct SlowMotionStateListenerInfo { 42 SlowMotionState state_; 43 const SlowMotionStateListener* listener_; SlowMotionStateListenerInfoSlowMotionStateListenerInfo44 SlowMotionStateListenerInfo(SlowMotionState state, const SlowMotionStateListener* listener) 45 : state_(state), listener_(listener) {} 46 }; 47 48 class SlowMotionSessionNapi : public CameraSessionNapi { 49 public: 50 static napi_value Init(napi_env env, napi_value exports); 51 static napi_value CreateCameraSession(napi_env env); 52 SlowMotionSessionNapi(); 53 ~SlowMotionSessionNapi() override; 54 55 static void SlowMotionSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint); 56 static napi_value SlowMotionSessionNapiConstructor(napi_env env, napi_callback_info info); 57 static napi_value IsSlowMotionDetectionSupported(napi_env env, napi_callback_info info); 58 static napi_value SetSlowMotionDetectionArea(napi_env env, napi_callback_info info); 59 napi_env env_; 60 sptr<SlowMotionSession> slowMotionSession_; 61 static thread_local napi_ref sConstructor_; 62 std::shared_ptr<SlowMotionStateListener> slowMotionStateListener_; 63 private: 64 static napi_value GetDoubleProperty(napi_env env, napi_value param, const std::string& propertyName, 65 double& propertyValue); 66 void RegisterSlowMotionStateCb(const std::string& eventName, napi_env env, napi_value callback, 67 const std::vector<napi_value>& args, bool isOnce) override; 68 void UnregisterSlowMotionStateCb( 69 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args) override; 70 }; 71 } 72 } 73 #endif /* SLOW_MOTION_SESSION_NAPI_H */ 74