• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 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/photo_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 PhotoSessionNapi::sConstructor_ = nullptr;
25 
PhotoSessionNapi()26 PhotoSessionNapi::PhotoSessionNapi() : env_(nullptr)
27 {
28 }
~PhotoSessionNapi()29 PhotoSessionNapi::~PhotoSessionNapi()
30 {
31     MEDIA_DEBUG_LOG("~PhotoSessionNapi is called");
32 }
PhotoSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)33 void PhotoSessionNapi::PhotoSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
34 {
35     MEDIA_DEBUG_LOG("PhotoSessionNapiDestructor is called");
36     PhotoSessionNapi* cameraObj = reinterpret_cast<PhotoSessionNapi*>(nativeObject);
37     if (cameraObj != nullptr) {
38         delete cameraObj;
39     }
40 }
Init(napi_env env,napi_value exports)41 napi_value PhotoSessionNapi::Init(napi_env env, napi_value exports)
42 {
43     MEDIA_DEBUG_LOG("Init is called");
44     napi_status status;
45     napi_value ctorObj;
46     std::vector<std::vector<napi_property_descriptor>> descriptors = { camera_process_props, camera_process_sys_props,
47         flash_props, flash_sys_props, auto_exposure_props, focus_props, focus_sys_props, zoom_props, zoom_sys_props,
48         filter_props, preconfig_props, color_management_props, auto_switch_props, macro_props, white_balance_props };
49     std::vector<napi_property_descriptor> photo_session_props = CameraNapiUtils::GetPropertyDescriptor(descriptors);
50     status = napi_define_class(env, PHOTO_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
51                                PhotoSessionNapiConstructor, nullptr,
52                                photo_session_props.size(),
53                                photo_session_props.data(), &ctorObj);
54     if (status == napi_ok) {
55         status = NapiRefManager::CreateMemSafetyRef(env, ctorObj, &sConstructor_);
56         if (status == napi_ok) {
57             status = napi_set_named_property(env, exports, PHOTO_SESSION_NAPI_CLASS_NAME, ctorObj);
58             CHECK_RETURN_RET(status == napi_ok, exports);
59         }
60     }
61     MEDIA_ERR_LOG("Init call Failed!");
62     return nullptr;
63 }
64 
CreateCameraSession(napi_env env)65 napi_value PhotoSessionNapi::CreateCameraSession(napi_env env)
66 {
67     MEDIA_DEBUG_LOG("CreateCameraSession is called");
68     CAMERA_SYNC_TRACE;
69     napi_status status;
70     napi_value result = nullptr;
71     napi_value constructor;
72     status = napi_get_reference_value(env, sConstructor_, &constructor);
73     if (status == napi_ok) {
74         sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(SceneMode::CAPTURE);
75         if (sCameraSession_ == nullptr) {
76             MEDIA_ERR_LOG("PhotoSessionNapi::CreateCameraSession Failed to create instance");
77             napi_get_undefined(env, &result);
78             return result;
79         }
80         status = napi_new_instance(env, constructor, 0, nullptr, &result);
81         sCameraSession_ = nullptr;
82         if (status == napi_ok && result != nullptr) {
83             MEDIA_DEBUG_LOG("PhotoSessionNapi::CreateCameraSession success to create napi instance");
84             return result;
85         } else {
86             MEDIA_ERR_LOG("PhotoSessionNapi::CreateCameraSession Failed to create napi instance");
87         }
88     }
89     MEDIA_ERR_LOG("PhotoSessionNapi::CreateCameraSession Failed to create napi instance last");
90     napi_get_undefined(env, &result);
91     return result;
92 }
93 
PhotoSessionNapiConstructor(napi_env env,napi_callback_info info)94 napi_value PhotoSessionNapi::PhotoSessionNapiConstructor(napi_env env, napi_callback_info info)
95 {
96     MEDIA_DEBUG_LOG("PhotoSessionNapiConstructor is called");
97     napi_status status;
98     napi_value result = nullptr;
99     napi_value thisVar = nullptr;
100 
101     napi_get_undefined(env, &result);
102     CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
103 
104     if (status == napi_ok && thisVar != nullptr) {
105         std::unique_ptr<PhotoSessionNapi> photoSessionObj = std::make_unique<PhotoSessionNapi>();
106         photoSessionObj->env_ = env;
107         CHECK_RETURN_RET_ELOG(sCameraSession_ == nullptr, result, "sCameraSession_ is null");
108         photoSessionObj->photoSession_ = static_cast<PhotoSession*>(sCameraSession_.GetRefPtr());
109         photoSessionObj->cameraSession_ = photoSessionObj->photoSession_;
110         CHECK_RETURN_RET_ELOG(photoSessionObj->photoSession_ == nullptr, result, "photoSession_ is null");
111         status = napi_wrap(env, thisVar, reinterpret_cast<void*>(photoSessionObj.get()),
112             PhotoSessionNapi::PhotoSessionNapiDestructor, nullptr, nullptr);
113         if (status == napi_ok) {
114             photoSessionObj.release();
115             return thisVar;
116         } else {
117             MEDIA_ERR_LOG("PhotoSessionNapi Failure wrapping js to native napi");
118         }
119     }
120     MEDIA_ERR_LOG("PhotoSessionNapi call Failed!");
121     return result;
122 }
123 
RegisterPressureStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)124 void PhotoSessionNapi::RegisterPressureStatusCallbackListener(
125     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
126 {
127     MEDIA_INFO_LOG("PhotoSessionNapi::RegisterPressureStatusCallbackListener");
128     if (pressureCallback_ == nullptr) {
129         pressureCallback_ = std::make_shared<PressureCallbackListener>(env);
130         cameraSession_->SetPressureCallback(pressureCallback_);
131     }
132     pressureCallback_->SaveCallbackReference(eventName, callback, isOnce);
133 }
134 
UnregisterPressureStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)135 void PhotoSessionNapi::UnregisterPressureStatusCallbackListener(
136     const std::string &eventName, napi_env env, napi_value callback, const std::vector<napi_value> &args)
137 {
138     MEDIA_INFO_LOG("PhotoSessionNapi::UnregisterPressureStatusCallbackListener");
139     if (pressureCallback_ == nullptr) {
140         MEDIA_INFO_LOG("pressureCallback is null");
141         return;
142     }
143     pressureCallback_->RemoveCallbackRef(eventName, callback);
144 }
145 
146 } // namespace CameraStandard
147 } // namespace OHOS