1 /*
2 * Copyright (C) 2021 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 "media_capability_napi.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 #include "common_napi.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "MediaCapsNapi"};
23 }
24
25 namespace OHOS {
26 namespace Media {
27 thread_local napi_ref MediaCapsNapi::constructor_ = nullptr;
28 const std::string CLASS_NAME = "MediaCapability";
29
MediaCapsNapi()30 MediaCapsNapi::MediaCapsNapi()
31 {
32 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
33 }
34
~MediaCapsNapi()35 MediaCapsNapi::~MediaCapsNapi()
36 {
37 if (wrap_ != nullptr) {
38 napi_delete_reference(env_, wrap_);
39 }
40 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
41 }
42
Init(napi_env env,napi_value exports)43 napi_value MediaCapsNapi::Init(napi_env env, napi_value exports)
44 {
45 napi_property_descriptor properties[] = {};
46 napi_property_descriptor staticProperty[] = {
47 DECLARE_NAPI_STATIC_FUNCTION("getMediaCapability", GetMediaCapability),
48 };
49
50 napi_value constructor = nullptr;
51 napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr,
52 sizeof(properties) / sizeof(properties[0]), properties, &constructor);
53 CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define AudioDecodeProcessor class");
54
55 status = napi_create_reference(env, constructor, 1, &constructor_);
56 CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to create reference of constructor");
57
58 status = napi_set_named_property(env, exports, CLASS_NAME.c_str(), constructor);
59 CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to set constructor");
60
61 status = napi_define_properties(env, exports, sizeof(staticProperty) / sizeof(staticProperty[0]), staticProperty);
62 CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define static function");
63
64 MEDIA_LOGD("Init success");
65 return exports;
66 }
67
Constructor(napi_env env,napi_callback_info info)68 napi_value MediaCapsNapi::Constructor(napi_env env, napi_callback_info info)
69 {
70 napi_value result = nullptr;
71 napi_get_undefined(env, &result);
72
73 napi_value jsThis = nullptr;
74 size_t argCount = 0;
75 napi_status status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
76 CHECK_AND_RETURN_RET(status == napi_ok, result);
77
78 MediaCapsNapi *napi = new(std::nothrow) MediaCapsNapi();
79 CHECK_AND_RETURN_RET(napi != nullptr, result);
80
81 napi->env_ = env;
82
83 status = napi_wrap(env, jsThis, reinterpret_cast<void *>(napi),
84 MediaCapsNapi::Destructor, nullptr, &(napi->wrap_));
85 if (status != napi_ok) {
86 delete napi;
87 MEDIA_LOGE("Failed to wrap native instance");
88 return result;
89 }
90
91 MEDIA_LOGD("Constructor success");
92 return jsThis;
93 }
94
Destructor(napi_env env,void * nativeObject,void * finalize)95 void MediaCapsNapi::Destructor(napi_env env, void *nativeObject, void *finalize)
96 {
97 (void)env;
98 (void)finalize;
99 if (nativeObject != nullptr) {
100 delete reinterpret_cast<MediaCapsNapi *>(nativeObject);
101 }
102 MEDIA_LOGD("Destructor success");
103 }
104
GetMediaCapability(napi_env env,napi_callback_info info)105 napi_value MediaCapsNapi::GetMediaCapability(napi_env env, napi_callback_info info)
106 {
107 MEDIA_LOGD("GetMediaCapability");
108 napi_value result = nullptr;
109 napi_get_undefined(env, &result);
110
111 auto asyncCtx = std::make_unique<MediaAsyncContext>(env);
112
113 napi_value jsThis = nullptr;
114 napi_value args[1] = { nullptr };
115 size_t argCount = 1;
116 napi_status status = napi_get_cb_info(env, info, &argCount, args, &jsThis, nullptr);
117 if (status != napi_ok) {
118 asyncCtx->SignError(MSERR_EXT_INVALID_VAL, "Failed to napi_get_cb_info");
119 }
120
121 asyncCtx->callbackRef = CommonNapi::CreateReference(env, args[0]);
122 asyncCtx->deferred = CommonNapi::CreatePromise(env, asyncCtx->callbackRef, result);
123 asyncCtx->JsResult = std::make_unique<MediaJsResultInstance>(constructor_);
124 asyncCtx->ctorFlag = true;
125
126 napi_value resource = nullptr;
127 napi_create_string_utf8(env, "GetMediaCapability", NAPI_AUTO_LENGTH, &resource);
128 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, [](napi_env env, void* data) {},
129 MediaAsyncContext::CompleteCallback, static_cast<void *>(asyncCtx.get()), &asyncCtx->work));
130 NAPI_CALL(env, napi_queue_async_work(env, asyncCtx->work));
131 asyncCtx.release();
132
133 return result;
134 }
135 } // namespace Media
136 } // namespace OHOS
137