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