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