• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "mode/secure_camera_session_napi.h"
17 
18 namespace OHOS {
19 namespace CameraStandard {
20 using namespace std;
21 
22 thread_local napi_ref SecureCameraSessionNapi::sConstructor_ = nullptr;
23 
SecureCameraSessionNapi()24 SecureCameraSessionNapi::SecureCameraSessionNapi() : env_(nullptr) {}
~SecureCameraSessionNapi()25 SecureCameraSessionNapi::~SecureCameraSessionNapi()
26 {
27     MEDIA_DEBUG_LOG("~SecureCameraSessionNapi is called");
28 }
SecureCameraSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)29 void SecureCameraSessionNapi::SecureCameraSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
30 {
31     MEDIA_DEBUG_LOG("SecureCameraSessionNapiDestructor is called");
32     SecureCameraSessionNapi* cameraObj = reinterpret_cast<SecureCameraSessionNapi*>(nativeObject);
33     if (cameraObj != nullptr) {
34         delete cameraObj;
35     }
36 }
Init(napi_env env,napi_value exports)37 napi_value SecureCameraSessionNapi::Init(napi_env env, napi_value exports)
38 {
39     MEDIA_DEBUG_LOG("Init is called");
40     napi_status status;
41     napi_value ctorObj;
42     std::vector<napi_property_descriptor> manual_exposure_props = {
43             DECLARE_NAPI_FUNCTION("addSecureOutput", SecureCameraSessionNapi::AddSecureOutput)
44     };
45     std::vector<std::vector<napi_property_descriptor>> descriptors = {camera_process_props, stabilization_props,
46         flash_props, auto_exposure_props, focus_props, zoom_props, filter_props, beauty_props,
47         color_effect_props, macro_props, color_management_props, manual_exposure_props};
48     std::vector<napi_property_descriptor> secure_camera_session_props =
49         CameraNapiUtils::GetPropertyDescriptor(descriptors);
50     status = napi_define_class(env, SECURE_CAMERA_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
51         SecureCameraSessionNapiConstructor, nullptr,
52         secure_camera_session_props.size(),
53         secure_camera_session_props.data(), &ctorObj);
54     if (status == napi_ok) {
55         int32_t refCount = 1;
56         status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
57         if (status == napi_ok) {
58             status = napi_set_named_property(env, exports, SECURE_CAMERA_SESSION_NAPI_CLASS_NAME, ctorObj);
59             CHECK_ERROR_RETURN_RET(status == napi_ok, exports);
60         }
61     }
62     MEDIA_ERR_LOG("Init call Failed!");
63     return nullptr;
64 }
65 
CreateCameraSession(napi_env env)66 napi_value SecureCameraSessionNapi::CreateCameraSession(napi_env env)
67 {
68     MEDIA_DEBUG_LOG("CreateCameraSession is called");
69     CAMERA_SYNC_TRACE;
70     napi_status status;
71     napi_value result = nullptr;
72     napi_value constructor;
73     status = napi_get_reference_value(env, sConstructor_, &constructor);
74     if (status == napi_ok) {
75         sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(SceneMode::SECURE);
76         if (sCameraSession_ == nullptr) {
77             MEDIA_ERR_LOG("Failed to create Camera session instance");
78             napi_get_undefined(env, &result);
79             return result;
80         }
81         status = napi_new_instance(env, constructor, 0, nullptr, &result);
82         sCameraSession_ = nullptr;
83         if (status == napi_ok && result != nullptr) {
84             MEDIA_DEBUG_LOG("success to create Camera session napi instance");
85             return result;
86         } else {
87             MEDIA_ERR_LOG("Failed to create Camera session napi instance");
88         }
89     }
90     MEDIA_ERR_LOG("Failed to create Camera session napi instance last");
91     napi_get_undefined(env, &result);
92     return result;
93 }
94 
AddSecureOutput(napi_env env,napi_callback_info info)95 napi_value SecureCameraSessionNapi::AddSecureOutput(napi_env env, napi_callback_info info)
96 {
97     MEDIA_INFO_LOG("AddSecureOutput is called");
98     napi_status status;
99     napi_value result = nullptr;
100     size_t argc = ARGS_ONE;
101     napi_value argv[ARGS_ONE] = {0};
102     napi_value thisVar = nullptr;
103 
104     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
105     CHECK_ERROR_RETURN_RET(!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, ADD_OUTPUT), result);
106 
107     napi_get_undefined(env, &result);
108     SecureCameraSessionNapi* secureCameraSessionNapi = nullptr;
109     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&secureCameraSessionNapi));
110     if (status == napi_ok && secureCameraSessionNapi != nullptr) {
111         sptr<CaptureOutput> cameraOutput = nullptr;
112         MEDIA_INFO_LOG("AddSecureOutput GetJSArgsForCameraOutput is called");
113         result = GetJSArgsForCameraOutput(env, argc, argv, cameraOutput);
114         int32_t ret = secureCameraSessionNapi->secureCameraSession_->AddSecureOutput(cameraOutput);
115         CHECK_ERROR_RETURN_RET(!CameraNapiUtils::CheckError(env, ret), nullptr);
116     } else {
117         MEDIA_ERR_LOG("AddOutput call Failed!");
118     }
119     return result;
120 }
121 
SecureCameraSessionNapiConstructor(napi_env env,napi_callback_info info)122 napi_value SecureCameraSessionNapi::SecureCameraSessionNapiConstructor(napi_env env, napi_callback_info info)
123 {
124     MEDIA_DEBUG_LOG("SecureCameraSessionNapiConstructor is called");
125     napi_status status;
126     napi_value result = nullptr;
127     napi_value thisVar = nullptr;
128 
129     napi_get_undefined(env, &result);
130     CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
131 
132     if (status == napi_ok && thisVar != nullptr) {
133         std::unique_ptr<SecureCameraSessionNapi> obj = std::make_unique<SecureCameraSessionNapi>();
134         obj->env_ = env;
135         CHECK_ERROR_RETURN_RET_LOG(sCameraSession_ == nullptr, result, "sCameraSession_ is null");
136         obj->secureCameraSession_ = static_cast<SecureCameraSession*>(sCameraSession_.GetRefPtr());
137         obj->cameraSession_ = obj->secureCameraSession_;
138         CHECK_ERROR_RETURN_RET_LOG(obj->secureCameraSession_ == nullptr, result, "secureCameraSession_ is null");
139         status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
140                            SecureCameraSessionNapi::SecureCameraSessionNapiDestructor, nullptr, nullptr);
141         if (status == napi_ok) {
142             obj.release();
143             return thisVar;
144         } else {
145             MEDIA_ERR_LOG("SecureCameraSessionNapi Failure wrapping js to native napi");
146         }
147     }
148     MEDIA_ERR_LOG("SecureCameraSessionNapi call Failed!");
149     return result;
150 }
151 } // namespace CameraStandard
152 } // namespace OHOS