1 /*
2 * Copyright (c) 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 #ifndef LOG_TAG
16 #define LOG_TAG "NapiAudioInterruptManager"
17 #endif
18
19 #include "napi_audio_interrupt_manager.h"
20 #include "napi_audio_enum.h"
21 #include "napi_audio_error.h"
22 #include "napi_param_utils.h"
23 #include "audio_errors.h"
24 #include "audio_manager_log.h"
25
26 namespace OHOS {
27 namespace AudioStandard {
28 using namespace std;
29 static __thread napi_ref g_interruptManagerConstructor = nullptr;
30
NapiAudioInterruptManager()31 NapiAudioInterruptManager::NapiAudioInterruptManager()
32 : audioSystemMngr_(nullptr), env_(nullptr) {}
33
34 NapiAudioInterruptManager::~NapiAudioInterruptManager() = default;
35
Destructor(napi_env env,void * nativeObject,void * finalizeHint)36 void NapiAudioInterruptManager::Destructor(napi_env env, void *nativeObject, void *finalizeHint)
37 {
38 if (nativeObject == nullptr) {
39 AUDIO_WARNING_LOG("Native object is null");
40 return;
41 }
42 auto obj = static_cast<NapiAudioInterruptManager *>(nativeObject);
43 ObjectRefMap<NapiAudioInterruptManager>::DecreaseRef(obj);
44 AUDIO_INFO_LOG("Decrease obj count");
45 }
46
Construct(napi_env env,napi_callback_info info)47 napi_value NapiAudioInterruptManager::Construct(napi_env env, napi_callback_info info)
48 {
49 AUDIO_INFO_LOG("Construct");
50 napi_status status;
51 napi_value result = nullptr;
52 napi_get_undefined(env, &result);
53
54 size_t argc = ARGS_TWO;
55 napi_value argv[ARGS_TWO] = {0};
56 napi_value thisVar = nullptr;
57 void *data = nullptr;
58 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
59 unique_ptr<NapiAudioInterruptManager> napiAudioInterruptManager = make_unique<NapiAudioInterruptManager>();
60 CHECK_AND_RETURN_RET_LOG(napiAudioInterruptManager != nullptr, result, "No memory");
61
62 napiAudioInterruptManager->audioSystemMngr_ = AudioSystemManager::GetInstance();
63
64 napiAudioInterruptManager->env_ = env;
65 ObjectRefMap<NapiAudioInterruptManager>::Insert(napiAudioInterruptManager.get());
66
67 status = napi_wrap(env, thisVar, static_cast<void*>(napiAudioInterruptManager.get()),
68 NapiAudioInterruptManager::Destructor, nullptr, nullptr);
69 if (status != napi_ok) {
70 ObjectRefMap<NapiAudioInterruptManager>::Erase(napiAudioInterruptManager.get());
71 return result;
72 }
73 napiAudioInterruptManager.release();
74 return thisVar;
75 }
76
CreateInterruptManagerWrapper(napi_env env)77 napi_value NapiAudioInterruptManager::CreateInterruptManagerWrapper(napi_env env)
78 {
79 napi_status status;
80 napi_value result = nullptr;
81 napi_value constructor;
82
83 status = napi_get_reference_value(env, g_interruptManagerConstructor, &constructor);
84 if (status != napi_ok) {
85 AUDIO_ERR_LOG("Failed in CreateInterruptManagerWrapper, %{public}d", status);
86 goto fail;
87 }
88 status = napi_new_instance(env, constructor, PARAM0, nullptr, &result);
89 if (status != napi_ok) {
90 AUDIO_ERR_LOG("napi_new_instance failed, status:%{public}d", status);
91 goto fail;
92 }
93 return result;
94
95 fail:
96 napi_get_undefined(env, &result);
97 return result;
98 }
99
Init(napi_env env,napi_value exports)100 napi_value NapiAudioInterruptManager::Init(napi_env env, napi_value exports)
101 {
102 AUDIO_DEBUG_LOG("Init");
103 napi_status status;
104 napi_value constructor;
105 napi_value result = nullptr;
106 const int32_t refCount = ARGS_ONE;
107 napi_get_undefined(env, &result);
108
109 napi_property_descriptor audio_interrupt_manager_properties[] = {};
110
111 status = napi_define_class(env, AUDIO_INTERRUPT_MANAGER_NAPI_CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Construct,
112 nullptr, sizeof(audio_interrupt_manager_properties) / sizeof(audio_interrupt_manager_properties[PARAM0]),
113 audio_interrupt_manager_properties, &constructor);
114 CHECK_AND_RETURN_RET_LOG(status == napi_ok, result, "napi_define_class fail");
115 status = napi_create_reference(env, constructor, refCount, &g_interruptManagerConstructor);
116 CHECK_AND_RETURN_RET_LOG(status == napi_ok, result, "napi_create_reference fail");
117 status = napi_set_named_property(env, exports, AUDIO_INTERRUPT_MANAGER_NAPI_CLASS_NAME.c_str(), constructor);
118 CHECK_AND_RETURN_RET_LOG(status == napi_ok, result, "napi_set_named_property fail");
119 return exports;
120 }
121 } // namespace AudioStandard
122 } // namespace OHOS