1 /*
2 * Copyright (c) 2022 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 "audio_interrupt_manager_napi.h"
17
18 #include "audio_common_napi.h"
19 #include "audio_errors.h"
20 #include "audio_log.h"
21 #include "hilog/log.h"
22 #include "napi_base_context.h"
23
24 using namespace std;
25 using OHOS::HiviewDFX::HiLog;
26 using OHOS::HiviewDFX::HiLogLabel;
27
28 namespace OHOS {
29 namespace AudioStandard {
30 static __thread napi_ref g_interruptManagerConstructor = nullptr;
31
32 namespace {
33 const int ARGS_TWO = 2;
34 const int PARAM0 = 0;
35 constexpr HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AudioInterruptManagerNapi"};
36 }
37
38 struct AudioInterruptManagerAsyncContext {
39 napi_env env;
40 napi_async_work work;
41 napi_deferred deferred;
42 napi_ref callbackRef = nullptr;
43 int32_t deviceFlag;
44 bool bArgTransFlag = true;
45 int32_t status = SUCCESS;
46 int32_t groupId;
47 std::string networkId;
48 AudioInterruptManagerNapi *objectInfo;
49 };
50
AudioInterruptManagerNapi()51 AudioInterruptManagerNapi::AudioInterruptManagerNapi()
52 : audioSystemMngr_(nullptr), env_(nullptr) {}
53
54 AudioInterruptManagerNapi::~AudioInterruptManagerNapi() = default;
55
Destructor(napi_env env,void * nativeObject,void * finalize_hint)56 void AudioInterruptManagerNapi::Destructor(napi_env env, void *nativeObject, void *finalize_hint)
57 {
58 if (nativeObject != nullptr) {
59 auto obj = static_cast<AudioInterruptManagerNapi *>(nativeObject);
60 delete obj;
61 obj = nullptr;
62 }
63 }
64
Construct(napi_env env,napi_callback_info info)65 napi_value AudioInterruptManagerNapi::Construct(napi_env env, napi_callback_info info)
66 {
67 AUDIO_INFO_LOG("Construct");
68 napi_status status;
69 napi_value result = nullptr;
70 napi_get_undefined(env, &result);
71
72 size_t argc = ARGS_TWO;
73 napi_value argv[ARGS_TWO] = {0};
74 napi_value thisVar = nullptr;
75 void *data = nullptr;
76 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
77 unique_ptr<AudioInterruptManagerNapi> audioInterruptManagerNapi = make_unique<AudioInterruptManagerNapi>();
78 CHECK_AND_RETURN_RET_LOG(audioInterruptManagerNapi != nullptr, result, "No memory");
79
80 audioInterruptManagerNapi->audioSystemMngr_ = AudioSystemManager::GetInstance();
81
82 audioInterruptManagerNapi->env_ = env;
83
84 status = napi_wrap(env, thisVar, static_cast<void*>(audioInterruptManagerNapi.get()),
85 AudioInterruptManagerNapi::Destructor, nullptr, nullptr);
86 if (status == napi_ok) {
87 audioInterruptManagerNapi.release();
88 return thisVar;
89 }
90
91 HiLog::Error(LABEL, "Failed in AudioInterruptManager::Construct()!");
92 return result;
93 }
94
CreateInterruptManagerWrapper(napi_env env)95 napi_value AudioInterruptManagerNapi::CreateInterruptManagerWrapper(napi_env env)
96 {
97 napi_status status;
98 napi_value result = nullptr;
99 napi_value constructor;
100
101 status = napi_get_reference_value(env, g_interruptManagerConstructor, &constructor);
102 if (status == napi_ok) {
103 status = napi_new_instance(env, constructor, 0, nullptr, &result);
104 if (status == napi_ok) {
105 return result;
106 }
107 }
108 HiLog::Error(LABEL, "Failed in AudioInterruptManagerNapi::CreateInterruptManagerWrapper!");
109 napi_get_undefined(env, &result);
110
111 return result;
112 }
113
Init(napi_env env,napi_value exports)114 napi_value AudioInterruptManagerNapi::Init(napi_env env, napi_value exports)
115 {
116 AUDIO_INFO_LOG("Init");
117 napi_status status;
118 napi_value constructor;
119 napi_value result = nullptr;
120 const int32_t refCount = 1;
121 napi_get_undefined(env, &result);
122
123 napi_property_descriptor audio_interrupt_manager_properties[] = {
124
125 };
126
127 status = napi_define_class(env, AUDIO_INTERRUPT_MANAGER_NAPI_CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Construct,
128 nullptr, sizeof(audio_interrupt_manager_properties) / sizeof(audio_interrupt_manager_properties[PARAM0]),
129 audio_interrupt_manager_properties, &constructor);
130 if (status != napi_ok) {
131 return result;
132 }
133 status = napi_create_reference(env, constructor, refCount, &g_interruptManagerConstructor);
134 if (status == napi_ok) {
135 status = napi_set_named_property(env, exports, AUDIO_INTERRUPT_MANAGER_NAPI_CLASS_NAME.c_str(), constructor);
136 if (status == napi_ok) {
137 return exports;
138 }
139 }
140
141 HiLog::Error(LABEL, "Failure in AudioInterruptManagerNapi::Init()");
142 return result;
143 }
144
145 } // namespace AudioStandard
146 } // namespace OHOS
147