1 /*
2 * Copyright (c) 2023-2025 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 "mode/video_session_napi.h"
17
18 #include "input/camera_manager.h"
19
20 namespace OHOS {
21 namespace CameraStandard {
22 using namespace std;
23
24 thread_local napi_ref VideoSessionNapi::sConstructor_ = nullptr;
25
VideoSessionNapi()26 VideoSessionNapi::VideoSessionNapi() : env_(nullptr) {}
~VideoSessionNapi()27 VideoSessionNapi::~VideoSessionNapi()
28 {
29 MEDIA_DEBUG_LOG("~VideoSessionNapi is called");
30 if (videoSession_) {
31 videoSession_ = nullptr;
32 }
33 }
VideoSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)34 void VideoSessionNapi::VideoSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
35 {
36 MEDIA_DEBUG_LOG("VideoSessionNapiDestructor is called");
37 VideoSessionNapi* cameraObj = reinterpret_cast<VideoSessionNapi*>(nativeObject);
38 if (cameraObj != nullptr) {
39 delete cameraObj;
40 }
41 }
Init(napi_env env,napi_value exports)42 napi_value VideoSessionNapi::Init(napi_env env, napi_value exports)
43 {
44 MEDIA_DEBUG_LOG("Init is called");
45 napi_status status;
46 napi_value ctorObj;
47 std::vector<std::vector<napi_property_descriptor>> descriptors = { camera_process_props, camera_process_sys_props,
48 flash_props, flash_sys_props, auto_exposure_props, focus_props, focus_sys_props, zoom_props, zoom_sys_props,
49 filter_props, stabilization_props, preconfig_props, color_management_props, auto_switch_props,
50 quality_prioritization_props, macro_props, white_balance_props, control_center_props };
51 std::vector<napi_property_descriptor> video_session_props = CameraNapiUtils::GetPropertyDescriptor(descriptors);
52 status = napi_define_class(env, VIDEO_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
53 VideoSessionNapiConstructor, nullptr,
54 video_session_props.size(),
55 video_session_props.data(), &ctorObj);
56 if (status == napi_ok) {
57 status = NapiRefManager::CreateMemSafetyRef(env, ctorObj, &sConstructor_);
58 if (status == napi_ok) {
59 status = napi_set_named_property(env, exports, VIDEO_SESSION_NAPI_CLASS_NAME, ctorObj);
60 CHECK_RETURN_RET(status == napi_ok, exports);
61 }
62 }
63 MEDIA_ERR_LOG("Init call Failed!");
64 return nullptr;
65 }
66
CreateCameraSession(napi_env env)67 napi_value VideoSessionNapi::CreateCameraSession(napi_env env)
68 {
69 MEDIA_DEBUG_LOG("VideoSessionNapi::CreateCameraSession is called");
70 CAMERA_SYNC_TRACE;
71 napi_status status;
72 napi_value result = nullptr;
73 napi_value constructor;
74 status = napi_get_reference_value(env, sConstructor_, &constructor);
75 if (status == napi_ok) {
76 sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(SceneMode::VIDEO);
77 if (sCameraSession_ == nullptr) {
78 MEDIA_ERR_LOG("VideoSessionNapi::CreateCameraSession Failed to create instance");
79 napi_get_undefined(env, &result);
80 return result;
81 }
82 status = napi_new_instance(env, constructor, 0, nullptr, &result);
83 sCameraSession_ = nullptr;
84 if (status == napi_ok && result != nullptr) {
85 MEDIA_DEBUG_LOG("VideoSessionNapi::CreateCameraSession success to create napi instance");
86 return result;
87 } else {
88 MEDIA_ERR_LOG("VideoSessionNapi::CreateCameraSession Failed to create napi instance");
89 }
90 }
91 MEDIA_ERR_LOG("VideoSessionNapi::CreateCameraSession Failed to create napi instance last");
92 napi_get_undefined(env, &result);
93 return result;
94 }
95
VideoSessionNapiConstructor(napi_env env,napi_callback_info info)96 napi_value VideoSessionNapi::VideoSessionNapiConstructor(napi_env env, napi_callback_info info)
97 {
98 MEDIA_DEBUG_LOG("VideoSessionNapiConstructor is called");
99 napi_status status;
100 napi_value result = nullptr;
101 napi_value thisVar = nullptr;
102
103 napi_get_undefined(env, &result);
104 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
105
106 if (status == napi_ok && thisVar != nullptr) {
107 std::unique_ptr<VideoSessionNapi> obj = std::make_unique<VideoSessionNapi>();
108 obj->env_ = env;
109 CHECK_RETURN_RET_ELOG(sCameraSession_ == nullptr, result, "sCameraSession_ is null");
110 obj->videoSession_ = static_cast<VideoSession*>(sCameraSession_.GetRefPtr());
111 obj->cameraSession_ = obj->videoSession_;
112 CHECK_RETURN_RET_ELOG(obj->videoSession_ == nullptr, result, "videoSession_ is null");
113 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
114 VideoSessionNapi::VideoSessionNapiDestructor, nullptr, nullptr);
115 if (status == napi_ok) {
116 obj.release();
117 return thisVar;
118 } else {
119 MEDIA_ERR_LOG("VideoSessionNapi Failure wrapping js to native napi");
120 }
121 }
122 MEDIA_ERR_LOG("VideoSessionNapi call Failed!");
123 return result;
124 }
125
RegisterPressureStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)126 void VideoSessionNapi::RegisterPressureStatusCallbackListener(
127 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
128 {
129 MEDIA_INFO_LOG("VideoSessionNapi::RegisterPressureStatusCallbackListener");
130 if (pressureCallback_ == nullptr) {
131 pressureCallback_ = std::make_shared<PressureCallbackListener>(env);
132 cameraSession_->SetPressureCallback(pressureCallback_);
133 }
134 pressureCallback_->SaveCallbackReference(eventName, callback, isOnce);
135 }
136
UnregisterPressureStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)137 void VideoSessionNapi::UnregisterPressureStatusCallbackListener(
138 const std::string &eventName, napi_env env, napi_value callback, const std::vector<napi_value> &args)
139 {
140 MEDIA_INFO_LOG("VideoSessionNapi::UnregisterPressureStatusCallbackListener");
141 if (pressureCallback_ == nullptr) {
142 MEDIA_INFO_LOG("pressureCallback is null");
143 return;
144 }
145 pressureCallback_->RemoveCallbackRef(eventName, callback);
146 }
147
RegisterControlCenterEffectStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)148 void VideoSessionNapi::RegisterControlCenterEffectStatusCallbackListener(
149 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
150 {
151 MEDIA_INFO_LOG("VideoSessionNapi::RegisterControlCenterEffectStatusCallbackListener");
152 if (controlCenterEffectStatusCallback_ == nullptr) {
153 controlCenterEffectStatusCallback_ = std::make_shared<ControlCenterEffectStatusCallbackListener>(env);
154 cameraSession_->SetControlCenterEffectStatusCallback(controlCenterEffectStatusCallback_);
155 }
156 controlCenterEffectStatusCallback_->SaveCallbackReference(eventName, callback, isOnce);
157 }
158
UnregisterControlCenterEffectStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)159 void VideoSessionNapi::UnregisterControlCenterEffectStatusCallbackListener(
160 const std::string &eventName, napi_env env, napi_value callback, const std::vector<napi_value> &args)
161 {
162 MEDIA_INFO_LOG("VideoSessionNapi::UnregisterControlCenterEffectStatusCallbackListener");
163 if (controlCenterEffectStatusCallback_ == nullptr) {
164 MEDIA_INFO_LOG("controlCenterEffectStatusCallback_ is null");
165 return;
166 }
167 controlCenterEffectStatusCallback_->RemoveCallbackRef(eventName, callback);
168 }
169
170 } // namespace CameraStandard
171 } // namespace OHOS