1 /*
2 * Copyright (c) 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 "input/camera_pre_launch_config_napi.h"
17 #include "input/camera_info_napi.h"
18
19 namespace OHOS {
20 namespace CameraStandard {
21 thread_local napi_ref CameraPrelaunchConfigNapi::sConstructor_ = nullptr;
22 thread_local PrelaunchConfig* CameraPrelaunchConfigNapi::sPrelaunchConfig_ = nullptr;
23
CameraPrelaunchConfigNapi()24 CameraPrelaunchConfigNapi::CameraPrelaunchConfigNapi() : env_(nullptr), wrapper_(nullptr)
25 {
26 }
27
~CameraPrelaunchConfigNapi()28 CameraPrelaunchConfigNapi::~CameraPrelaunchConfigNapi()
29 {
30 MEDIA_DEBUG_LOG("~CameraPrelaunchConfigNapi is called");
31 if (wrapper_ != nullptr) {
32 napi_delete_reference(env_, wrapper_);
33 }
34 if (sPrelaunchConfig_) {
35 sPrelaunchConfig_ = nullptr;
36 }
37 }
38
CameraPrelaunchConfigNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)39 void CameraPrelaunchConfigNapi::CameraPrelaunchConfigNapiDestructor(
40 napi_env env, void* nativeObject, void* finalize_hint)
41 {
42 MEDIA_DEBUG_LOG("CameraPrelaunchConfigNapiDestructor is called");
43 CameraPrelaunchConfigNapi* cameraPrelaunchConfigNapi = reinterpret_cast<CameraPrelaunchConfigNapi*>(nativeObject);
44 if (cameraPrelaunchConfigNapi != nullptr) {
45 MEDIA_INFO_LOG("CameraPrelaunchConfigNapiDestructor ~");
46 delete cameraPrelaunchConfigNapi;
47 }
48 }
49
Init(napi_env env,napi_value exports)50 napi_value CameraPrelaunchConfigNapi::Init(napi_env env, napi_value exports)
51 {
52 MEDIA_INFO_LOG("Init is called");
53 napi_status status;
54 napi_value ctorObj;
55 napi_property_descriptor prelaunch_config_props[] = {
56 DECLARE_NAPI_GETTER("cameraDevice", GetPrelaunchCameraDevice)
57 };
58 status = napi_define_class(env, CAMERA_PRELAUNCH_CONFIG_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
59 CameraPrelaunchConfigNapiConstructor, nullptr,
60 sizeof(prelaunch_config_props) / sizeof(prelaunch_config_props[PARAM0]),
61 prelaunch_config_props, &ctorObj);
62 if (status == napi_ok) {
63 int32_t refCount = 1;
64 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
65 if (status == napi_ok) {
66 status = napi_set_named_property(env, exports, CAMERA_PRELAUNCH_CONFIG_NAPI_CLASS_NAME, ctorObj);
67 if (status == napi_ok) {
68 return exports;
69 }
70 }
71 }
72 MEDIA_ERR_LOG("Init call Failed!");
73 return nullptr;
74 }
75
76 // Constructor callback
CameraPrelaunchConfigNapiConstructor(napi_env env,napi_callback_info info)77 napi_value CameraPrelaunchConfigNapi::CameraPrelaunchConfigNapiConstructor(napi_env env, napi_callback_info info)
78 {
79 MEDIA_DEBUG_LOG("CameraProfileNapiConstructor is called");
80 napi_status status;
81 napi_value result = nullptr;
82 napi_value thisVar = nullptr;
83
84 napi_get_undefined(env, &result);
85 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
86
87 if (status == napi_ok && thisVar != nullptr) {
88 std::unique_ptr<CameraPrelaunchConfigNapi> obj = std::make_unique<CameraPrelaunchConfigNapi>();
89 obj->env_ = env;
90 obj->prelaunchConfig_ = sPrelaunchConfig_;
91 MEDIA_INFO_LOG("CameraPrelaunchConfigNapiConstructor cameraId = %{public}s",
92 obj->prelaunchConfig_->GetCameraDevice()->GetID().c_str());
93 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
94 CameraPrelaunchConfigNapi::CameraPrelaunchConfigNapiDestructor, nullptr, nullptr);
95 if (status == napi_ok) {
96 obj.release();
97 return thisVar;
98 } else {
99 MEDIA_ERR_LOG("Failure wrapping js to native napi");
100 }
101 }
102 MEDIA_ERR_LOG("CameraPrelaunchConfigNapiConstructor call Failed!");
103 return result;
104 }
105
GetPrelaunchCameraDevice(napi_env env,napi_callback_info info)106 napi_value CameraPrelaunchConfigNapi::GetPrelaunchCameraDevice(napi_env env, napi_callback_info info)
107 {
108 MEDIA_DEBUG_LOG("GetCameraProfileSize is called");
109 napi_status status;
110 napi_value jsResult = nullptr;
111 napi_value undefinedResult = nullptr;
112 CameraPrelaunchConfigNapi* obj = nullptr;
113 sptr<CameraDevice> cameraDevice;
114 napi_value thisVar = nullptr;
115
116 napi_get_undefined(env, &undefinedResult);
117 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
118
119 if (status != napi_ok || thisVar == nullptr) {
120 MEDIA_ERR_LOG("Invalid arguments!");
121 return undefinedResult;
122 }
123
124 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
125 if ((status == napi_ok) && (obj != nullptr)) {
126 cameraDevice = obj->prelaunchConfig_->GetCameraDevice();
127 MEDIA_INFO_LOG("GetPrelaunchCameraDevice cameraId = %{public}s",
128 obj->prelaunchConfig_->GetCameraDevice()->GetID().c_str());
129 jsResult = CameraDeviceNapi::CreateCameraObj(env, cameraDevice);
130 return jsResult;
131 }
132 MEDIA_ERR_LOG("GetCameraProfileSize call Failed!");
133 return undefinedResult;
134 }
135 } // namespace CameraStandard
136 } // namespace OHOS