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 CHECK_ERROR_RETURN_RET(status == napi_ok, exports);
58 }
59 }
60 MEDIA_ERR_LOG("Init call Failed!");
61 return nullptr;
62 }
63
CreateCameraSession(napi_env env)64 napi_value PhotoSessionNapi::CreateCameraSession(napi_env env)
65 {
66 MEDIA_DEBUG_LOG("CreateCameraSession is called");
67 CAMERA_SYNC_TRACE;
68 napi_status status;
69 napi_value result = nullptr;
70 napi_value constructor;
71 status = napi_get_reference_value(env, sConstructor_, &constructor);
72 if (status == napi_ok) {
73 sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(SceneMode::CAPTURE);
74 if (sCameraSession_ == nullptr) {
75 MEDIA_ERR_LOG("Failed to create Photo session instance");
76 napi_get_undefined(env, &result);
77 return result;
78 }
79 status = napi_new_instance(env, constructor, 0, nullptr, &result);
80 sCameraSession_ = nullptr;
81 if (status == napi_ok && result != nullptr) {
82 MEDIA_DEBUG_LOG("success to create Photo session napi instance");
83 return result;
84 } else {
85 MEDIA_ERR_LOG("Failed to create Photo session napi instance");
86 }
87 }
88 MEDIA_ERR_LOG("Failed to create Photo session napi instance last");
89 napi_get_undefined(env, &result);
90 return result;
91 }
92
PhotoSessionNapiConstructor(napi_env env,napi_callback_info info)93 napi_value PhotoSessionNapi::PhotoSessionNapiConstructor(napi_env env, napi_callback_info info)
94 {
95 MEDIA_DEBUG_LOG("PhotoSessionNapiConstructor is called");
96 napi_status status;
97 napi_value result = nullptr;
98 napi_value thisVar = nullptr;
99
100 napi_get_undefined(env, &result);
101 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
102
103 if (status == napi_ok && thisVar != nullptr) {
104 std::unique_ptr<PhotoSessionNapi> obj = std::make_unique<PhotoSessionNapi>();
105 obj->env_ = env;
106 CHECK_ERROR_RETURN_RET_LOG(sCameraSession_ == nullptr, result, "sCameraSession_ is null");
107 obj->photoSession_ = static_cast<PhotoSession*>(sCameraSession_.GetRefPtr());
108 obj->cameraSession_ = obj->photoSession_;
109 CHECK_ERROR_RETURN_RET_LOG(obj->photoSession_ == nullptr, result, "photoSession_ is null");
110 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
111 PhotoSessionNapi::PhotoSessionNapiDestructor, nullptr, nullptr);
112 if (status == napi_ok) {
113 obj.release();
114 return thisVar;
115 } else {
116 MEDIA_ERR_LOG("PhotoSessionNapi Failure wrapping js to native napi");
117 }
118 }
119 MEDIA_ERR_LOG("PhotoSessionNapi call Failed!");
120 return result;
121 }
122 } // namespace CameraStandard
123 } // namespace OHOS