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/video_session_for_sys_napi.h"
17 #include "ability/camera_ability_napi.h"
18 #include "napi/native_common.h"
19
20 namespace OHOS {
21 namespace CameraStandard {
22 using namespace std;
23
24 thread_local napi_ref VideoSessionForSysNapi::sConstructor_ = nullptr;
25
VideoSessionForSysNapi()26 VideoSessionForSysNapi::VideoSessionForSysNapi() : env_(nullptr)
27 {
28 }
~VideoSessionForSysNapi()29 VideoSessionForSysNapi::~VideoSessionForSysNapi()
30 {
31 MEDIA_DEBUG_LOG("~VideoSessionForSysNapi is called");
32 }
VideoSessionForSysNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)33 void VideoSessionForSysNapi::VideoSessionForSysNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
34 {
35 MEDIA_DEBUG_LOG("VideoSessionForSysNapiDestructor is called");
36 VideoSessionForSysNapi* cameraObj = reinterpret_cast<VideoSessionForSysNapi*>(nativeObject);
37 if (cameraObj != nullptr) {
38 delete cameraObj;
39 }
40 }
Init(napi_env env,napi_value exports)41 napi_value VideoSessionForSysNapi::Init(napi_env env, napi_value exports)
42 {
43 MEDIA_DEBUG_LOG("Init is called");
44 napi_status status;
45 napi_value ctorObj;
46
47 std::vector<std::vector<napi_property_descriptor>> descriptors = { camera_process_props, flash_props,
48 auto_exposure_props, focus_props, zoom_props, filter_props, beauty_props, color_effect_props, macro_props,
49 color_management_props, stabilization_props, preconfig_props, camera_output_capability_props,
50 camera_ability_props };
51 std::vector<napi_property_descriptor> video_session_props = CameraNapiUtils::GetPropertyDescriptor(descriptors);
52 status = napi_define_class(env, VIDEO_SESSION_FOR_SYS_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
53 VideoSessionForSysNapiConstructor, nullptr,
54 video_session_props.size(),
55 video_session_props.data(), &ctorObj);
56 if (status == napi_ok) {
57 int32_t refCount = 1;
58 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
59 if (status == napi_ok) {
60 status = napi_set_named_property(env, exports, VIDEO_SESSION_FOR_SYS_NAPI_CLASS_NAME, ctorObj);
61 if (status == napi_ok) {
62 return exports;
63 }
64 }
65 }
66 MEDIA_ERR_LOG("Init call Failed!");
67 return nullptr;
68 }
69
CreateCameraSession(napi_env env)70 napi_value VideoSessionForSysNapi::CreateCameraSession(napi_env env)
71 {
72 MEDIA_DEBUG_LOG("CreateCameraSession is called");
73 CAMERA_SYNC_TRACE;
74 napi_status status;
75 napi_value result = nullptr;
76 napi_value constructor;
77 status = napi_get_reference_value(env, sConstructor_, &constructor);
78 if (status == napi_ok) {
79 sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(SceneMode::VIDEO);
80 if (sCameraSession_ == nullptr) {
81 MEDIA_ERR_LOG("Failed to create Video session instance");
82 napi_get_undefined(env, &result);
83 return result;
84 }
85 status = napi_new_instance(env, constructor, 0, nullptr, &result);
86 sCameraSession_ = nullptr;
87 if (status == napi_ok && result != nullptr) {
88 MEDIA_DEBUG_LOG("success to create Video session napi instance");
89 return result;
90 } else {
91 MEDIA_ERR_LOG("Failed to create Video session napi instance");
92 }
93 }
94 MEDIA_ERR_LOG("Failed to create Video session napi instance last");
95 napi_get_undefined(env, &result);
96 return result;
97 }
98
VideoSessionForSysNapiConstructor(napi_env env,napi_callback_info info)99 napi_value VideoSessionForSysNapi::VideoSessionForSysNapiConstructor(napi_env env, napi_callback_info info)
100 {
101 MEDIA_DEBUG_LOG("VideoSessionForSysNapiConstructor is called");
102 napi_status status;
103 napi_value result = nullptr;
104 napi_value thisVar = nullptr;
105
106 napi_get_undefined(env, &result);
107 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
108
109 if (status == napi_ok && thisVar != nullptr) {
110 std::unique_ptr<VideoSessionForSysNapi> obj = std::make_unique<VideoSessionForSysNapi>();
111 obj->env_ = env;
112 if (sCameraSession_ == nullptr) {
113 MEDIA_ERR_LOG("sCameraSession_ is null");
114 return result;
115 }
116 obj->videoSession_ = static_cast<VideoSession*>(sCameraSession_.GetRefPtr());
117 obj->cameraSession_ = obj->videoSession_;
118 if (obj->videoSession_ == nullptr) {
119 MEDIA_ERR_LOG("videoSession_ is null");
120 return result;
121 }
122 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
123 VideoSessionForSysNapi::VideoSessionForSysNapiDestructor, nullptr, nullptr);
124 if (status == napi_ok) {
125 obj.release();
126 return thisVar;
127 } else {
128 MEDIA_ERR_LOG("VideoSessionForSysNapi Failure wrapping js to native napi");
129 }
130 }
131 MEDIA_ERR_LOG("VideoSessionForSysNapi call Failed!");
132 return result;
133 }
134 } // namespace CameraStandard
135 } // namespace OHOS
136