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     if (wrapper_ != nullptr) {
31         napi_delete_reference(env_, wrapper_);
32     }
33 }
34 
ApertureVideoSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)35 void ApertureVideoSessionNapi::ApertureVideoSessionNapiDestructor(
36     napi_env env, void* nativeObject, void* finalize_hint)
37 {
38     MEDIA_DEBUG_LOG("ApertureVideoSessionNapi is called");
39     ApertureVideoSessionNapi* cameraObj = reinterpret_cast<ApertureVideoSessionNapi*>(nativeObject);
40     if (cameraObj != nullptr) {
41         delete cameraObj;
42     }
43 }
44 
Init(napi_env env,napi_value exports)45 napi_value ApertureVideoSessionNapi::Init(napi_env env, napi_value exports)
46 {
47     MEDIA_DEBUG_LOG("Init is called");
48     napi_status status;
49     napi_value ctorObj;
50     std::vector<std::vector<napi_property_descriptor>> descriptors = { camera_process_props, auto_exposure_props,
51         color_effect_props, flash_props, focus_props, zoom_props, aperture_props };
52     std::vector<napi_property_descriptor> session_props = CameraNapiUtils::GetPropertyDescriptor(descriptors);
53     status = napi_define_class(env, APERTURE_VIDEO_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
54         ApertureVideoSessionNapiConstructor, nullptr, session_props.size(), session_props.data(), &ctorObj);
55     if (status == napi_ok) {
56         int32_t refCount = 1;
57         status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
58         if (status == napi_ok) {
59             status = napi_set_named_property(env, exports, APERTURE_VIDEO_SESSION_NAPI_CLASS_NAME, ctorObj);
60             if (status == napi_ok) {
61                 return exports;
62             }
63         }
64     }
65     MEDIA_ERR_LOG("Init call Failed!");
66     return nullptr;
67 }
68 
CreateCameraSession(napi_env env)69 napi_value ApertureVideoSessionNapi::CreateCameraSession(napi_env env)
70 {
71     MEDIA_DEBUG_LOG("CreateCameraSession is called");
72     CAMERA_SYNC_TRACE;
73     napi_status status;
74     napi_value result = nullptr;
75     napi_value constructor;
76     status = napi_get_reference_value(env, sConstructor_, &constructor);
77     if (status == napi_ok) {
78         sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(SceneMode::APERTURE_VIDEO);
79         if (sCameraSession_ == nullptr) {
80             MEDIA_ERR_LOG("Failed to create Photo session instance");
81             napi_get_undefined(env, &result);
82             return result;
83         }
84         status = napi_new_instance(env, constructor, 0, nullptr, &result);
85         sCameraSession_ = nullptr;
86         if (status == napi_ok && result != nullptr) {
87             MEDIA_DEBUG_LOG("success to create Photo session napi instance");
88             return result;
89         } else {
90             MEDIA_ERR_LOG("Failed to create Photo session napi instance");
91         }
92     }
93     MEDIA_ERR_LOG("Failed to create Photo session napi instance last");
94     napi_get_undefined(env, &result);
95     return result;
96 }
97 
ApertureVideoSessionNapiConstructor(napi_env env,napi_callback_info info)98 napi_value ApertureVideoSessionNapi::ApertureVideoSessionNapiConstructor(napi_env env, napi_callback_info info)
99 {
100     MEDIA_DEBUG_LOG("ApertureVideoSessionNapiConstructor is called");
101     napi_status status;
102     napi_value result = nullptr;
103     napi_value thisVar = nullptr;
104 
105     napi_get_undefined(env, &result);
106     CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
107 
108     if (status == napi_ok && thisVar != nullptr) {
109         std::unique_ptr<ApertureVideoSessionNapi> obj = std::make_unique<ApertureVideoSessionNapi>();
110         obj->env_ = env;
111         if (sCameraSession_ == nullptr) {
112             MEDIA_ERR_LOG("sCameraSession_ is null");
113             return result;
114         }
115         obj->apertureVideoSession_ = static_cast<ApertureVideoSession*>(sCameraSession_.GetRefPtr());
116         obj->cameraSession_ = obj->apertureVideoSession_;
117         if (obj->apertureVideoSession_ == nullptr) {
118             MEDIA_ERR_LOG("apertureVideoSession_ is null");
119             return result;
120         }
121         status = napi_wrap(
122             env, thisVar, reinterpret_cast<void*>(obj.get()), ApertureVideoSessionNapiDestructor, nullptr, nullptr);
123         if (status == napi_ok) {
124             obj.release();
125             return thisVar;
126         } else {
127             MEDIA_ERR_LOG("ApertureVideoSessionNapiConstructor Failure wrapping js to native napi");
128         }
129     }
130     MEDIA_ERR_LOG("ApertureVideoSessionNapiConstructor call Failed!");
131     return result;
132 }
133 } // namespace CameraStandard
134 } // namespace OHOS
135