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 namespace OHOS {
19 namespace CameraStandard {
20 using namespace std;
21
22 thread_local napi_ref PhotoSessionNapi::sConstructor_ = nullptr;
23
PhotoSessionNapi()24 PhotoSessionNapi::PhotoSessionNapi() : env_(nullptr)
25 {
26 }
~PhotoSessionNapi()27 PhotoSessionNapi::~PhotoSessionNapi()
28 {
29 MEDIA_DEBUG_LOG("~PhotoSessionNapi is called");
30 }
PhotoSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)31 void PhotoSessionNapi::PhotoSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
32 {
33 MEDIA_DEBUG_LOG("PhotoSessionNapiDestructor is called");
34 PhotoSessionNapi* cameraObj = reinterpret_cast<PhotoSessionNapi*>(nativeObject);
35 if (cameraObj != nullptr) {
36 delete cameraObj;
37 }
38 }
Init(napi_env env,napi_value exports)39 napi_value PhotoSessionNapi::Init(napi_env env, napi_value exports)
40 {
41 MEDIA_DEBUG_LOG("Init is called");
42 napi_status status;
43 napi_value ctorObj;
44 std::vector<std::vector<napi_property_descriptor>> descriptors = {camera_process_props, flash_props,
45 auto_exposure_props, focus_props, zoom_props, filter_props, preconfig_props, color_management_props,
46 auto_switch_props};
47 std::vector<napi_property_descriptor> photo_session_props = CameraNapiUtils::GetPropertyDescriptor(descriptors);
48 status = napi_define_class(env, PHOTO_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
49 PhotoSessionNapiConstructor, nullptr,
50 photo_session_props.size(),
51 photo_session_props.data(), &ctorObj);
52 if (status == napi_ok) {
53 int32_t refCount = 1;
54 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
55 if (status == napi_ok) {
56 status = napi_set_named_property(env, exports, PHOTO_SESSION_NAPI_CLASS_NAME, ctorObj);
57 if (status == napi_ok) {
58 return exports;
59 }
60 }
61 }
62 MEDIA_ERR_LOG("Init call Failed!");
63 return nullptr;
64 }
65
CreateCameraSession(napi_env env)66 napi_value PhotoSessionNapi::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::CAPTURE);
76 if (sCameraSession_ == nullptr) {
77 MEDIA_ERR_LOG("Failed to create Photo 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 Photo session napi instance");
85 return result;
86 } else {
87 MEDIA_ERR_LOG("Failed to create Photo session napi instance");
88 }
89 }
90 MEDIA_ERR_LOG("Failed to create Photo session napi instance last");
91 napi_get_undefined(env, &result);
92 return result;
93 }
94
PhotoSessionNapiConstructor(napi_env env,napi_callback_info info)95 napi_value PhotoSessionNapi::PhotoSessionNapiConstructor(napi_env env, napi_callback_info info)
96 {
97 MEDIA_DEBUG_LOG("PhotoSessionNapiConstructor is called");
98 napi_status status;
99 napi_value result = nullptr;
100 napi_value thisVar = nullptr;
101
102 napi_get_undefined(env, &result);
103 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
104
105 if (status == napi_ok && thisVar != nullptr) {
106 std::unique_ptr<PhotoSessionNapi> obj = std::make_unique<PhotoSessionNapi>();
107 obj->env_ = env;
108 if (sCameraSession_ == nullptr) {
109 MEDIA_ERR_LOG("sCameraSession_ is null");
110 return result;
111 }
112 obj->photoSession_ = static_cast<PhotoSession*>(sCameraSession_.GetRefPtr());
113 obj->cameraSession_ = obj->photoSession_;
114 if (obj->photoSession_ == nullptr) {
115 MEDIA_ERR_LOG("photoSession_ is null");
116 return result;
117 }
118 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
119 PhotoSessionNapi::PhotoSessionNapiDestructor, nullptr, nullptr);
120 if (status == napi_ok) {
121 obj.release();
122 return thisVar;
123 } else {
124 MEDIA_ERR_LOG("PhotoSessionNapi Failure wrapping js to native napi");
125 }
126 }
127 MEDIA_ERR_LOG("PhotoSessionNapi call Failed!");
128 return result;
129 }
130 } // namespace CameraStandard
131 } // namespace OHOS