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/aperture_video_session_napi.h"
17 #include "aperture_video_session.h"
18
19 namespace OHOS {
20 namespace CameraStandard {
21 using namespace std;
22
23 thread_local napi_ref ApertureVideoSessionNapi::sConstructor_ = nullptr;
24
ApertureVideoSessionNapi()25 ApertureVideoSessionNapi::ApertureVideoSessionNapi() : env_(nullptr), wrapper_(nullptr) {}
26
~ApertureVideoSessionNapi()27 ApertureVideoSessionNapi::~ApertureVideoSessionNapi()
28 {
29 MEDIA_DEBUG_LOG("~ApertureVideoSessionNapi is called");
30 CHECK_EXECUTE(wrapper_ != nullptr, napi_delete_reference(env_, wrapper_));
31 }
32
ApertureVideoSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)33 void ApertureVideoSessionNapi::ApertureVideoSessionNapiDestructor(
34 napi_env env, void* nativeObject, void* finalize_hint)
35 {
36 MEDIA_DEBUG_LOG("ApertureVideoSessionNapi is called");
37 ApertureVideoSessionNapi* cameraObj = reinterpret_cast<ApertureVideoSessionNapi*>(nativeObject);
38 if (cameraObj != nullptr) {
39 delete cameraObj;
40 }
41 }
42
Init(napi_env env,napi_value exports)43 napi_value ApertureVideoSessionNapi::Init(napi_env env, napi_value exports)
44 {
45 MEDIA_DEBUG_LOG("Init is called");
46 napi_status status;
47 napi_value ctorObj;
48 std::vector<std::vector<napi_property_descriptor>> descriptors = { camera_process_props, auto_exposure_props,
49 color_effect_props, flash_props, focus_props, zoom_props, aperture_props };
50 std::vector<napi_property_descriptor> session_props = CameraNapiUtils::GetPropertyDescriptor(descriptors);
51 status = napi_define_class(env, APERTURE_VIDEO_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
52 ApertureVideoSessionNapiConstructor, nullptr, session_props.size(), session_props.data(), &ctorObj);
53 if (status == napi_ok) {
54 int32_t refCount = 1;
55 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
56 if (status == napi_ok) {
57 status = napi_set_named_property(env, exports, APERTURE_VIDEO_SESSION_NAPI_CLASS_NAME, ctorObj);
58 CHECK_ERROR_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 ApertureVideoSessionNapi::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::APERTURE_VIDEO);
75 if (sCameraSession_ == nullptr) {
76 MEDIA_ERR_LOG("Failed to create Photo session 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("success to create Photo session napi instance");
84 return result;
85 } else {
86 MEDIA_ERR_LOG("Failed to create Photo session napi instance");
87 }
88 }
89 MEDIA_ERR_LOG("Failed to create Photo session napi instance last");
90 napi_get_undefined(env, &result);
91 return result;
92 }
93
ApertureVideoSessionNapiConstructor(napi_env env,napi_callback_info info)94 napi_value ApertureVideoSessionNapi::ApertureVideoSessionNapiConstructor(napi_env env, napi_callback_info info)
95 {
96 MEDIA_DEBUG_LOG("ApertureVideoSessionNapiConstructor 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<ApertureVideoSessionNapi> obj = std::make_unique<ApertureVideoSessionNapi>();
106 obj->env_ = env;
107 CHECK_ERROR_RETURN_RET_LOG(sCameraSession_ == nullptr, result, "sCameraSession_ is null");
108 obj->apertureVideoSession_ = static_cast<ApertureVideoSession*>(sCameraSession_.GetRefPtr());
109 obj->cameraSession_ = obj->apertureVideoSession_;
110 CHECK_ERROR_RETURN_RET_LOG(obj->apertureVideoSession_ == nullptr, result, "apertureVideoSession_ is null");
111 status = napi_wrap(
112 env, thisVar, reinterpret_cast<void*>(obj.get()), ApertureVideoSessionNapiDestructor, nullptr, nullptr);
113 if (status == napi_ok) {
114 obj.release();
115 return thisVar;
116 } else {
117 MEDIA_ERR_LOG("ApertureVideoSessionNapiConstructor Failure wrapping js to native napi");
118 }
119 }
120 MEDIA_ERR_LOG("ApertureVideoSessionNapiConstructor call Failed!");
121 return result;
122 }
123 } // namespace CameraStandard
124 } // namespace OHOS
125