1 /*
2 * Copyright (c) 2023-2025 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 "NapiAudioStreamMgr"
17 #endif
18
19 #include "napi_audio_stream_manager.h"
20 #include "napi_audio_error.h"
21 #include "napi_param_utils.h"
22 #include "napi_audio_enum.h"
23 #include "audio_errors.h"
24 #include "audio_manager_log.h"
25 #include "napi_audio_renderer_state_callback.h"
26 #include "napi_audio_capturer_state_callback.h"
27
28 namespace OHOS {
29 namespace AudioStandard {
30 using namespace std;
31 using namespace HiviewDFX;
32 static __thread napi_ref g_streamMgrConstructor = nullptr;
33
NapiAudioStreamMgr()34 NapiAudioStreamMgr::NapiAudioStreamMgr()
35 : env_(nullptr), audioStreamMngr_(nullptr) {}
36
37 NapiAudioStreamMgr::~NapiAudioStreamMgr() = default;
38
Destructor(napi_env env,void * nativeObject,void * finalizeHint)39 void NapiAudioStreamMgr::Destructor(napi_env env, void *nativeObject, void *finalizeHint)
40 {
41 if (nativeObject == nullptr) {
42 AUDIO_WARNING_LOG("Native object is null");
43 return;
44 }
45 auto obj = static_cast<NapiAudioStreamMgr *>(nativeObject);
46 ObjectRefMap<NapiAudioStreamMgr>::DecreaseRef(obj);
47 AUDIO_INFO_LOG("Decrease obj count");
48 }
49
Construct(napi_env env,napi_callback_info info)50 napi_value NapiAudioStreamMgr::Construct(napi_env env, napi_callback_info info)
51 {
52 AUDIO_DEBUG_LOG("Construct");
53 napi_status status;
54 napi_value result = nullptr;
55 NapiParamUtils::GetUndefinedValue(env);
56
57 size_t argc = ARGS_TWO;
58 napi_value argv[ARGS_TWO] = {0};
59 napi_value thisVar = nullptr;
60 void *data = nullptr;
61 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
62 unique_ptr<NapiAudioStreamMgr> napiStreamMgr = make_unique<NapiAudioStreamMgr>();
63 CHECK_AND_RETURN_RET_LOG(napiStreamMgr != nullptr, result, "No memory");
64
65 napiStreamMgr->env_ = env;
66 napiStreamMgr->audioStreamMngr_ = AudioStreamManager::GetInstance();
67 napiStreamMgr->cachedClientId_ = getpid();
68 ObjectRefMap<NapiAudioStreamMgr>::Insert(napiStreamMgr.get());
69
70 status = napi_wrap(env, thisVar, static_cast<void*>(napiStreamMgr.get()),
71 NapiAudioStreamMgr::Destructor, nullptr, nullptr);
72 if (status != napi_ok) {
73 ObjectRefMap<NapiAudioStreamMgr>::Erase(napiStreamMgr.get());
74 return result;
75 }
76 napiStreamMgr.release();
77 return thisVar;
78 }
79
Init(napi_env env,napi_value exports)80 napi_value NapiAudioStreamMgr::Init(napi_env env, napi_value exports)
81 {
82 napi_status status;
83 napi_value constructor;
84 napi_value result = nullptr;
85 const int32_t refCount = ARGS_ONE;
86 napi_get_undefined(env, &result);
87
88 napi_property_descriptor audio_stream_mgr_properties[] = {
89 DECLARE_NAPI_FUNCTION("on", On),
90 DECLARE_NAPI_FUNCTION("off", Off),
91 DECLARE_NAPI_FUNCTION("getCurrentAudioRendererInfoArray", GetCurrentAudioRendererInfos),
92 DECLARE_NAPI_FUNCTION("getCurrentAudioRendererInfoArraySync", GetCurrentAudioRendererInfosSync),
93 DECLARE_NAPI_FUNCTION("getCurrentAudioCapturerInfoArray", GetCurrentAudioCapturerInfos),
94 DECLARE_NAPI_FUNCTION("getCurrentAudioCapturerInfoArraySync", GetCurrentAudioCapturerInfosSync),
95 DECLARE_NAPI_FUNCTION("isActive", IsStreamActive),
96 DECLARE_NAPI_FUNCTION("isActiveSync", IsStreamActiveSync),
97 DECLARE_NAPI_FUNCTION("isStreamActive", IsStreamActiveByStreamUsage),
98 DECLARE_NAPI_FUNCTION("getAudioEffectInfoArray", GetEffectInfoArray),
99 DECLARE_NAPI_FUNCTION("getAudioEffectInfoArraySync", GetEffectInfoArraySync),
100 DECLARE_NAPI_FUNCTION("getHardwareOutputSamplingRate", GetHardwareOutputSamplingRate),
101 DECLARE_NAPI_FUNCTION("getSupportedAudioEffectProperty", GetSupportedAudioEffectProperty),
102 DECLARE_NAPI_FUNCTION("getAudioEffectProperty", GetAudioEffectProperty),
103 DECLARE_NAPI_FUNCTION("setAudioEffectProperty", SetAudioEffectProperty),
104 DECLARE_NAPI_FUNCTION("getSupportedAudioEnhanceProperty", GetSupportedAudioEnhanceProperty),
105 DECLARE_NAPI_FUNCTION("getAudioEnhanceProperty", GetAudioEnhanceProperty),
106 DECLARE_NAPI_FUNCTION("setAudioEnhanceProperty", SetAudioEnhanceProperty),
107 DECLARE_NAPI_FUNCTION("isAcousticEchoCancelerSupported", IsAcousticEchoCancelerSupported),
108 DECLARE_NAPI_FUNCTION("isRecordingAvailable", IsRecordingAvailable),
109 DECLARE_NAPI_FUNCTION("isAudioLoopbackSupported", IsAudioLoopbackSupported),
110 };
111
112 status = napi_define_class(env, AUDIO_STREAM_MGR_NAPI_CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Construct, nullptr,
113 sizeof(audio_stream_mgr_properties) / sizeof(audio_stream_mgr_properties[PARAM0]),
114 audio_stream_mgr_properties, &constructor);
115 CHECK_AND_RETURN_RET_LOG(status == napi_ok, result, "napi_define_class fail");
116
117 status = napi_create_reference(env, constructor, refCount, &g_streamMgrConstructor);
118 CHECK_AND_RETURN_RET_LOG(status == napi_ok, result, "napi_create_reference fail");
119 status = napi_set_named_property(env, exports, AUDIO_STREAM_MGR_NAPI_CLASS_NAME.c_str(), constructor);
120 CHECK_AND_RETURN_RET_LOG(status == napi_ok, result, "napi_set_named_property fail");
121 return exports;
122 }
123
CreateStreamManagerWrapper(napi_env env)124 napi_value NapiAudioStreamMgr::CreateStreamManagerWrapper(napi_env env)
125 {
126 napi_status status;
127 napi_value result = nullptr;
128 napi_value constructor;
129
130 status = napi_get_reference_value(env, g_streamMgrConstructor, &constructor);
131 if (status != napi_ok) {
132 AUDIO_ERR_LOG("Failed in CreateStreamManagerWrapper, %{public}d", status);
133 goto fail;
134 }
135 status = napi_new_instance(env, constructor, PARAM0, nullptr, &result);
136 if (status != napi_ok) {
137 AUDIO_ERR_LOG("napi_new_instance failed, status:%{public}d", status);
138 goto fail;
139 }
140 return result;
141
142 fail:
143 napi_get_undefined(env, &result);
144 return result;
145 }
146
CheckContextStatus(std::shared_ptr<AudioStreamMgrAsyncContext> context)147 bool NapiAudioStreamMgr::CheckContextStatus(std::shared_ptr<AudioStreamMgrAsyncContext> context)
148 {
149 CHECK_AND_RETURN_RET_LOG(context != nullptr, false, "context object is nullptr.");
150 if (context->native == nullptr) {
151 context->SignError(NAPI_ERR_SYSTEM);
152 return false;
153 }
154 return true;
155 }
156
CheckAudioStreamManagerStatus(NapiAudioStreamMgr * napi,std::shared_ptr<AudioStreamMgrAsyncContext> context)157 bool NapiAudioStreamMgr::CheckAudioStreamManagerStatus(NapiAudioStreamMgr *napi,
158 std::shared_ptr<AudioStreamMgrAsyncContext> context)
159 {
160 CHECK_AND_RETURN_RET_LOG(napi != nullptr, false, "napi object is nullptr.");
161 if (napi->audioStreamMngr_ == nullptr) {
162 context->SignError(NAPI_ERR_SYSTEM);
163 return false;
164 }
165 return true;
166 }
167
GetParamWithSync(const napi_env & env,napi_callback_info info,size_t & argc,napi_value * args)168 NapiAudioStreamMgr* NapiAudioStreamMgr::GetParamWithSync(const napi_env &env, napi_callback_info info,
169 size_t &argc, napi_value *args)
170 {
171 napi_status status;
172 NapiAudioStreamMgr *napiStreamMgr = nullptr;
173 napi_value jsThis = nullptr;
174 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
175 CHECK_AND_RETURN_RET_LOG(status == napi_ok && jsThis != nullptr, nullptr,
176 "GetParamWithSync fail to napi_get_cb_info");
177
178 status = napi_unwrap(env, jsThis, (void **)&napiStreamMgr);
179 CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "napi_unwrap failed");
180 CHECK_AND_RETURN_RET_LOG(napiStreamMgr != nullptr && napiStreamMgr->audioStreamMngr_ !=
181 nullptr, napiStreamMgr, "GetParamWithSync fail to napi_unwrap");
182 return napiStreamMgr;
183 }
184
GetCurrentAudioRendererInfos(napi_env env,napi_callback_info info)185 napi_value NapiAudioStreamMgr::GetCurrentAudioRendererInfos(napi_env env, napi_callback_info info)
186 {
187 auto context = std::make_shared<AudioStreamMgrAsyncContext>();
188 if (context == nullptr) {
189 AUDIO_ERR_LOG("GetCurrentAudioRendererInfos failed : no memory");
190 NapiAudioError::ThrowError(env, "GetCurrentAudioRendererInfos failed : no memory",
191 NAPI_ERR_NO_MEMORY);
192 return NapiParamUtils::GetUndefinedValue(env);
193 }
194
195 context->GetCbInfo(env, info);
196
197 auto executor = [context]() {
198 CHECK_AND_RETURN_LOG(CheckContextStatus(context), "context object state is error.");
199 auto obj = reinterpret_cast<NapiAudioStreamMgr*>(context->native);
200 ObjectRefMap objectGuard(obj);
201 auto *napiStreamMgr = objectGuard.GetPtr();
202 CHECK_AND_RETURN_LOG(CheckAudioStreamManagerStatus(napiStreamMgr, context),
203 "context object state is error.");
204 context->intValue = napiStreamMgr->audioStreamMngr_->GetCurrentRendererChangeInfos(
205 context->audioRendererChangeInfos);
206 NAPI_CHECK_ARGS_RETURN_VOID(context, context->intValue == SUCCESS,
207 "GetCurrentAudioRendererInfos failed", NAPI_ERR_SYSTEM);
208 };
209
210 auto complete = [env, context](napi_value &output) {
211 NapiParamUtils::SetRendererChangeInfos(env, context->audioRendererChangeInfos, output);
212 };
213 return NapiAsyncWork::Enqueue(env, context, "GetCurrentAudioRendererInfos", executor, complete);
214 }
215
GetCurrentAudioRendererInfosSync(napi_env env,napi_callback_info info)216 napi_value NapiAudioStreamMgr::GetCurrentAudioRendererInfosSync(napi_env env, napi_callback_info info)
217 {
218 AUDIO_INFO_LOG("GetCurrentAudioRendererInfosSync");
219 napi_value result = nullptr;
220 size_t argc = PARAM0;
221 auto *napiStreamMgr = GetParamWithSync(env, info, argc, nullptr);
222 if (argc > 0) {
223 NapiAudioError::ThrowError(env, NAPI_ERROR_INVALID_PARAM);
224 }
225 CHECK_AND_RETURN_RET_LOG(napiStreamMgr!= nullptr, result, "napiStreamMgr is nullptr");
226
227 vector<std::shared_ptr<AudioRendererChangeInfo>> audioRendererChangeInfos;
228 int32_t ret = napiStreamMgr->audioStreamMngr_->GetCurrentRendererChangeInfos(audioRendererChangeInfos);
229 CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, result, "GetCurrentRendererChangeInfos failure!");
230
231 NapiParamUtils::SetRendererChangeInfos(env, audioRendererChangeInfos, result);
232
233 return result;
234 }
235
GetCurrentAudioCapturerInfos(napi_env env,napi_callback_info info)236 napi_value NapiAudioStreamMgr::GetCurrentAudioCapturerInfos(napi_env env, napi_callback_info info)
237 {
238 auto context = std::make_shared<AudioStreamMgrAsyncContext>();
239 if (context == nullptr) {
240 AUDIO_ERR_LOG("GetCurrentAudioCapturerInfos failed : no memory");
241 NapiAudioError::ThrowError(env, "GetCurrentAudioCapturerInfos failed : no memory",
242 NAPI_ERR_NO_MEMORY);
243 return NapiParamUtils::GetUndefinedValue(env);
244 }
245
246 context->GetCbInfo(env, info);
247
248 auto executor = [context]() {
249 CHECK_AND_RETURN_LOG(CheckContextStatus(context), "context object state is error.");
250 auto obj = reinterpret_cast<NapiAudioStreamMgr*>(context->native);
251 ObjectRefMap objectGuard(obj);
252 auto *napiStreamMgr = objectGuard.GetPtr();
253 CHECK_AND_RETURN_LOG(CheckAudioStreamManagerStatus(napiStreamMgr, context),
254 "context object state is error.");
255 napiStreamMgr->audioStreamMngr_->GetCurrentCapturerChangeInfos(
256 context->audioCapturerChangeInfos);
257 };
258
259 auto complete = [env, context](napi_value &output) {
260 NapiParamUtils::SetCapturerChangeInfos(env, context->audioCapturerChangeInfos, output);
261 };
262 return NapiAsyncWork::Enqueue(env, context, "GetCurrentAudioCapturerInfos", executor, complete);
263 }
264
GetCurrentAudioCapturerInfosSync(napi_env env,napi_callback_info info)265 napi_value NapiAudioStreamMgr::GetCurrentAudioCapturerInfosSync(napi_env env, napi_callback_info info)
266 {
267 AUDIO_INFO_LOG("GetCurrentAudioCapturerInfosSync");
268 napi_value result = nullptr;
269 size_t argc = PARAM0;
270 auto *napiStreamMgr = GetParamWithSync(env, info, argc, nullptr);
271 if (argc > 0) {
272 NapiAudioError::ThrowError(env, NAPI_ERROR_INVALID_PARAM);
273 }
274 CHECK_AND_RETURN_RET_LOG(napiStreamMgr!= nullptr, result, "napiStreamMgr is nullptr");
275
276 vector<std::shared_ptr<AudioCapturerChangeInfo>> audioCapturerChangeInfos;
277 int32_t ret = napiStreamMgr->audioStreamMngr_->GetCurrentCapturerChangeInfos(audioCapturerChangeInfos);
278 if (ret != AUDIO_OK) {
279 AUDIO_ERR_LOG("GetCurrentCapturerChangeInfos failure!");
280 return result;
281 }
282 NapiParamUtils::SetCapturerChangeInfos(env, audioCapturerChangeInfos, result);
283
284 return result;
285 }
286
IsStreamActive(napi_env env,napi_callback_info info)287 napi_value NapiAudioStreamMgr::IsStreamActive(napi_env env, napi_callback_info info)
288 {
289 auto context = std::make_shared<AudioStreamMgrAsyncContext>();
290 if (context == nullptr) {
291 AUDIO_ERR_LOG("IsStreamActive failed : no memory");
292 NapiAudioError::ThrowError(env, "IsStreamActive failed : no memory", NAPI_ERR_NO_MEMORY);
293 return NapiParamUtils::GetUndefinedValue(env);
294 }
295
296 auto inputParser = [env, context](size_t argc, napi_value *argv) {
297 NAPI_CHECK_ARGS_RETURN_VOID(context, argc >= ARGS_ONE, "invalid arguments",
298 NAPI_ERR_INVALID_PARAM);
299 context->status = NapiParamUtils::GetValueInt32(env, context->volType, argv[PARAM0]);
300 NAPI_CHECK_ARGS_RETURN_VOID(context, context->status == napi_ok, "getvoltype failed",
301 NAPI_ERR_INVALID_PARAM);
302 if (!NapiAudioEnum::IsLegalInputArgumentVolType(context->volType)) {
303 context->SignError(context->errCode == NAPI_ERR_INVALID_PARAM?
304 NAPI_ERR_INVALID_PARAM : NAPI_ERR_UNSUPPORTED);
305 }
306 };
307 context->GetCbInfo(env, info, inputParser);
308
309 auto executor = [context]() {
310 CHECK_AND_RETURN_LOG(CheckContextStatus(context), "context object state is error.");
311 auto obj = reinterpret_cast<NapiAudioStreamMgr*>(context->native);
312 ObjectRefMap objectGuard(obj);
313 auto *napiStreamMgr = objectGuard.GetPtr();
314 CHECK_AND_RETURN_LOG(CheckAudioStreamManagerStatus(napiStreamMgr, context),
315 "context object state is error.");
316 context->isActive = napiStreamMgr->audioStreamMngr_->IsStreamActive(
317 NapiAudioEnum::GetNativeAudioVolumeType(context->volType));
318 context->isTrue = context->isActive;
319 };
320
321 auto complete = [env, context](napi_value &output) {
322 NapiParamUtils::SetValueBoolean(env, context->isTrue, output);
323 };
324 return NapiAsyncWork::Enqueue(env, context, "IsStreamActive", executor, complete);
325 }
326
IsStreamActiveSync(napi_env env,napi_callback_info info)327 napi_value NapiAudioStreamMgr::IsStreamActiveSync(napi_env env, napi_callback_info info)
328 {
329 napi_value result = nullptr;
330 size_t argc = ARGS_ONE;
331 napi_value args[ARGS_ONE] = {};
332 auto *napiStreamMgr = GetParamWithSync(env, info, argc, args);
333 CHECK_AND_RETURN_RET_LOG(argc >= ARGS_ONE, NapiAudioError::ThrowErrorAndReturn(env,
334 NAPI_ERR_INPUT_INVALID, "mandatory parameters are left unspecified"), "invalid arguments");
335
336 napi_valuetype valueType = napi_undefined;
337 napi_typeof(env, args[PARAM0], &valueType);
338 CHECK_AND_RETURN_RET_LOG(valueType == napi_number, NapiAudioError::ThrowErrorAndReturn(env,
339 NAPI_ERR_INPUT_INVALID, "incorrect parameter types: The type of volumeType must be number"),
340 "invalid valueType");
341
342 int32_t volType;
343 NapiParamUtils::GetValueInt32(env, volType, args[PARAM0]);
344 CHECK_AND_RETURN_RET_LOG(NapiAudioEnum::IsLegalInputArgumentVolType(volType),
345 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
346 "parameter verification failed: The param of volumeType must be enum AudioVolumeType"), "get volType failed");
347
348 CHECK_AND_RETURN_RET_LOG(napiStreamMgr != nullptr, result, "napiStreamMgr is nullptr");
349 CHECK_AND_RETURN_RET_LOG(napiStreamMgr->audioStreamMngr_ != nullptr, result,
350 "audioStreamMngr_ is nullptr");
351 bool isActive = napiStreamMgr->audioStreamMngr_->
352 IsStreamActive(NapiAudioEnum::GetNativeAudioVolumeType(volType));
353 NapiParamUtils::SetValueBoolean(env, isActive, result);
354 return result;
355 }
356
IsStreamActiveByStreamUsage(napi_env env,napi_callback_info info)357 napi_value NapiAudioStreamMgr::IsStreamActiveByStreamUsage(napi_env env, napi_callback_info info)
358 {
359 napi_value result = nullptr;
360 size_t argc = ARGS_ONE;
361 napi_value args[ARGS_ONE] = {};
362 auto *napiStreamMgr = GetParamWithSync(env, info, argc, args);
363 CHECK_AND_RETURN_RET_LOG(argc >= ARGS_ONE, NapiAudioError::ThrowErrorAndReturn(env,
364 NAPI_ERR_INVALID_PARAM, "mandatory parameters are left unspecified"), "invalid arguments");
365
366 napi_valuetype valueType = napi_undefined;
367 napi_typeof(env, args[PARAM0], &valueType);
368 CHECK_AND_RETURN_RET_LOG(valueType == napi_number, NapiAudioError::ThrowErrorAndReturn(env,
369 NAPI_ERR_INVALID_PARAM, "incorrect parameter types: The type of volumeType must be number"),
370 "invalid valueType");
371
372 int32_t streamUsage;
373 NapiParamUtils::GetValueInt32(env, streamUsage, args[PARAM0]);
374 CHECK_AND_RETURN_RET_LOG(NapiAudioEnum::IsLegalInputArgumentStreamUsage(streamUsage),
375 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
376 "parameter verification failed: The param of streamUsage must be enum StreamUsage"), "get streamUsage failed");
377
378 CHECK_AND_RETURN_RET_LOG(napiStreamMgr != nullptr, result, "napiStreamMgr is nullptr");
379 CHECK_AND_RETURN_RET_LOG(napiStreamMgr->audioStreamMngr_ != nullptr, result,
380 "audioStreamMngr_ is nullptr");
381 bool isActive = napiStreamMgr->audioStreamMngr_->
382 IsStreamActiveByStreamUsage(NapiAudioEnum::GetNativeStreamUsage(streamUsage));
383 NapiParamUtils::SetValueBoolean(env, isActive, result);
384 return result;
385 }
386
GetEffectInfoArray(napi_env env,napi_callback_info info)387 napi_value NapiAudioStreamMgr::GetEffectInfoArray(napi_env env, napi_callback_info info)
388 {
389 auto context = std::make_shared<AudioStreamMgrAsyncContext>();
390 if (context == nullptr) {
391 AUDIO_ERR_LOG("GetEffectInfoArray failed : no memory");
392 NapiAudioError::ThrowError(env, "GetEffectInfoArray failed : no memory", NAPI_ERR_NO_MEMORY);
393 return NapiParamUtils::GetUndefinedValue(env);
394 }
395
396 auto inputParser = [env, context](size_t argc, napi_value *argv) {
397 NAPI_CHECK_ARGS_RETURN_VOID(context, argc >= ARGS_ONE, "mandatory parameters are left unspecified",
398 NAPI_ERR_INPUT_INVALID);
399 context->status = NapiParamUtils::GetValueInt32(env, context->streamUsage, argv[PARAM0]);
400 NAPI_CHECK_ARGS_RETURN_VOID(context, context->status == napi_ok,
401 "incorrect parameter types: The type of usage must be number", NAPI_ERR_INPUT_INVALID);
402 if (!NapiAudioEnum::IsLegalInputArgumentStreamUsage(context->streamUsage)) {
403 context->SignError(NAPI_ERR_INVALID_PARAM,
404 "parameter verification failed: The param of usage must be enum StreamUsage");
405 }
406 };
407 context->GetCbInfo(env, info, inputParser);
408
409 if ((context->status != napi_ok) && (context->errCode == NAPI_ERR_INPUT_INVALID)) {
410 NapiAudioError::ThrowError(env, context->errCode, context->errMessage);
411 return NapiParamUtils::GetUndefinedValue(env);
412 }
413
414 auto executor = [context]() {
415 CHECK_AND_RETURN_LOG(CheckContextStatus(context), "context object state is error.");
416 auto obj = reinterpret_cast<NapiAudioStreamMgr*>(context->native);
417 ObjectRefMap objectGuard(obj);
418 auto *napiStreamMgr = objectGuard.GetPtr();
419 CHECK_AND_RETURN_LOG(CheckAudioStreamManagerStatus(napiStreamMgr, context),
420 "context object state is error.");
421 StreamUsage streamUsage = static_cast<StreamUsage>(context->streamUsage);
422 context->intValue = napiStreamMgr->audioStreamMngr_->GetEffectInfoArray(
423 context->audioSceneEffectInfo, streamUsage);
424 NAPI_CHECK_ARGS_RETURN_VOID(context, context->intValue == SUCCESS, "GetEffectInfoArray failed",
425 NAPI_ERR_SYSTEM);
426 };
427
428 auto complete = [env, context](napi_value &output) {
429 NapiParamUtils::SetEffectInfo(env, context->audioSceneEffectInfo, output);
430 };
431 return NapiAsyncWork::Enqueue(env, context, "GetEffectInfoArray", executor, complete);
432 }
433
GetEffectInfoArraySync(napi_env env,napi_callback_info info)434 napi_value NapiAudioStreamMgr::GetEffectInfoArraySync(napi_env env, napi_callback_info info)
435 {
436 napi_value result = nullptr;
437 size_t argc = ARGS_ONE;
438 napi_value args[ARGS_ONE] = {};
439 auto *napiStreamMgr = GetParamWithSync(env, info, argc, args);
440 CHECK_AND_RETURN_RET_LOG(argc >= ARGS_ONE, NapiAudioError::ThrowErrorAndReturn(env,
441 NAPI_ERR_INPUT_INVALID, "mandatory parameters are left unspecified"), "invalid arguments");
442
443 napi_valuetype valueType = napi_undefined;
444 napi_typeof(env, args[PARAM0], &valueType);
445 CHECK_AND_RETURN_RET_LOG(valueType == napi_number, NapiAudioError::ThrowErrorAndReturn(env,
446 NAPI_ERR_INPUT_INVALID, "incorrect parameter types: The type of usage must be number"),
447 "invalid valueType");
448
449 int32_t streamUsage;
450 NapiParamUtils::GetValueInt32(env, streamUsage, args[PARAM0]);
451 CHECK_AND_RETURN_RET_LOG(NapiAudioEnum::IsLegalInputArgumentStreamUsage(streamUsage),
452 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
453 "parameter verification failed: The param of usage must be enum StreamUsage"), "get streamUsage failed");
454
455 CHECK_AND_RETURN_RET_LOG(napiStreamMgr != nullptr, result, "napiStreamMgr is nullptr");
456 CHECK_AND_RETURN_RET_LOG(napiStreamMgr->audioStreamMngr_ != nullptr, result,
457 "audioStreamMngr_ is nullptr");
458 AudioSceneEffectInfo audioSceneEffectInfo;
459 int32_t ret = napiStreamMgr->audioStreamMngr_->GetEffectInfoArray(audioSceneEffectInfo,
460 static_cast<StreamUsage>(streamUsage));
461 CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, result, "GetEffectInfoArray failure!");
462 NapiParamUtils::SetEffectInfo(env, audioSceneEffectInfo, result);
463 return result;
464 }
465
GetHardwareOutputSamplingRate(napi_env env,napi_callback_info info)466 napi_value NapiAudioStreamMgr::GetHardwareOutputSamplingRate(napi_env env, napi_callback_info info)
467 {
468 napi_value result = nullptr;
469 std::shared_ptr<AudioDeviceDescriptor> deviceDescriptor = nullptr;
470 size_t argc = ARGS_ONE;
471 napi_value args[ARGS_ONE] = {};
472 auto *napiStreamMgr = GetParamWithSync(env, info, argc, args);
473 CHECK_AND_RETURN_RET_LOG(napiStreamMgr != nullptr, result, "napiStreamMgr is nullptr");
474 CHECK_AND_RETURN_RET_LOG(napiStreamMgr->audioStreamMngr_ != nullptr, result,
475 "audioStreamMngr_ is nullptr");
476
477 if (argc < ARGS_ONE) {
478 int32_t rate = napiStreamMgr->audioStreamMngr_->GetHardwareOutputSamplingRate(deviceDescriptor);
479 NapiParamUtils::SetValueInt32(env, rate, result);
480 return result;
481 }
482
483 deviceDescriptor = std::make_shared<AudioDeviceDescriptor>();
484 CHECK_AND_RETURN_RET_LOG(deviceDescriptor != nullptr, result, "AudioDeviceDescriptor alloc failed!");
485
486 bool argTransFlag = false;
487 NapiParamUtils::GetAudioDeviceDescriptor(env, deviceDescriptor, argTransFlag, args[PARAM0]);
488 CHECK_AND_RETURN_RET_LOG(argTransFlag && NapiAudioEnum::IsLegalOutputDeviceType(deviceDescriptor->deviceType_) &&
489 (deviceDescriptor->deviceRole_ == DeviceRole::OUTPUT_DEVICE),
490 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM), "invalid deviceDescriptor");
491
492 int32_t rate = napiStreamMgr->audioStreamMngr_->GetHardwareOutputSamplingRate(deviceDescriptor);
493 NapiParamUtils::SetValueInt32(env, rate, result);
494 return result;
495 }
496
RegisterCallback(napi_env env,napi_value jsThis,napi_value * args,const std::string & cbName)497 void NapiAudioStreamMgr::RegisterCallback(napi_env env, napi_value jsThis,
498 napi_value *args, const std::string &cbName)
499 {
500 NapiAudioStreamMgr *napiStreamMgr = nullptr;
501 napi_status status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&napiStreamMgr));
502 CHECK_AND_RETURN_LOG((status == napi_ok) && (napiStreamMgr != nullptr) &&
503 (napiStreamMgr->audioStreamMngr_ != nullptr), "Failed to retrieve stream mgr napi instance.");
504
505 if (!cbName.compare(RENDERERCHANGE_CALLBACK_NAME)) {
506 RegisterRendererStateChangeCallback(env, args, cbName, napiStreamMgr);
507 } else if (!cbName.compare(CAPTURERCHANGE_CALLBACK_NAME)) {
508 RegisterCapturerStateChangeCallback(env, args, cbName, napiStreamMgr);
509 } else {
510 AUDIO_ERR_LOG("NapiAudioStreamMgr::No such callback supported");
511 NapiAudioError::ThrowError(env, NAPI_ERR_INVALID_PARAM,
512 "parameter verification failed: The param of type is not supported");
513 }
514 }
515
RegisterRendererStateChangeCallback(napi_env env,napi_value * args,const std::string & cbName,NapiAudioStreamMgr * napiStreamMgr)516 void NapiAudioStreamMgr::RegisterRendererStateChangeCallback(napi_env env, napi_value *args,
517 const std::string &cbName, NapiAudioStreamMgr *napiStreamMgr)
518 {
519 if (!napiStreamMgr->rendererStateChangeCallbackNapi_) {
520 napiStreamMgr->rendererStateChangeCallbackNapi_ = std::make_shared<NapiAudioRendererStateCallback>(env);
521 CHECK_AND_RETURN_LOG(napiStreamMgr->rendererStateChangeCallbackNapi_ != nullptr,
522 "NapiAudioStreamMgr: Memory Allocation Failed !!");
523
524 int32_t ret =
525 napiStreamMgr->audioStreamMngr_->RegisterAudioRendererEventListener(napiStreamMgr->cachedClientId_,
526 napiStreamMgr->rendererStateChangeCallbackNapi_);
527 CHECK_AND_RETURN_LOG(ret == SUCCESS,
528 "NapiAudioStreamMgr: Registering of Renderer State Change Callback Failed");
529 }
530
531 std::shared_ptr<NapiAudioRendererStateCallback> cb =
532 std::static_pointer_cast<NapiAudioRendererStateCallback>(napiStreamMgr->rendererStateChangeCallbackNapi_);
533 cb->SaveCallbackReference(args[PARAM1]);
534 if (!cb->GetRendererStateTsfnFlag()) {
535 cb->CreateRendererStateTsfn(env);
536 }
537
538 AUDIO_INFO_LOG("OnRendererStateChangeCallback is successful");
539 }
540
RegisterCapturerStateChangeCallback(napi_env env,napi_value * args,const std::string & cbName,NapiAudioStreamMgr * napiStreamMgr)541 void NapiAudioStreamMgr::RegisterCapturerStateChangeCallback(napi_env env, napi_value *args,
542 const std::string &cbName, NapiAudioStreamMgr *napiStreamMgr)
543 {
544 if (!napiStreamMgr->capturerStateChangeCallbackNapi_) {
545 napiStreamMgr->capturerStateChangeCallbackNapi_ = std::make_shared<NapiAudioCapturerStateCallback>(env);
546 CHECK_AND_RETURN_LOG(napiStreamMgr->capturerStateChangeCallbackNapi_ != nullptr,
547 "Memory Allocation Failed !!");
548
549 int32_t ret =
550 napiStreamMgr->audioStreamMngr_->RegisterAudioCapturerEventListener(napiStreamMgr->cachedClientId_,
551 napiStreamMgr->capturerStateChangeCallbackNapi_);
552 CHECK_AND_RETURN_LOG(ret == SUCCESS,
553 "Registering of Capturer State Change Callback Failed");
554 }
555 std::lock_guard<std::mutex> lock(napiStreamMgr->capturerStateChangeCallbackNapi_->cbMutex_);
556
557 std::shared_ptr<NapiAudioCapturerStateCallback> cb =
558 std::static_pointer_cast<NapiAudioCapturerStateCallback>(napiStreamMgr->capturerStateChangeCallbackNapi_);
559 cb->SaveCallbackReference(args[PARAM1]);
560 if (!cb->GetCaptureStateTsfnFlag()) {
561 cb->CreateCaptureStateTsfn(env);
562 }
563
564 AUDIO_INFO_LOG("OnCapturerStateChangeCallback is successful");
565 }
566
On(napi_env env,napi_callback_info info)567 napi_value NapiAudioStreamMgr::On(napi_env env, napi_callback_info info)
568 {
569 const size_t requireArgc = ARGS_TWO;
570 size_t argc = ARGS_THREE;
571
572 napi_value undefinedResult = nullptr;
573 napi_get_undefined(env, &undefinedResult);
574
575 napi_value args[requireArgc + PARAM1] = {nullptr, nullptr, nullptr};
576 napi_value jsThis = nullptr;
577 napi_status status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
578 CHECK_AND_RETURN_RET_LOG(status == napi_ok && argc == requireArgc, NapiAudioError::ThrowErrorAndReturn(env,
579 NAPI_ERR_INPUT_INVALID, "mandatory parameters are left unspecified"),
580 "status or arguments error");
581
582 napi_valuetype eventType = napi_undefined;
583 napi_typeof(env, args[PARAM0], &eventType);
584 CHECK_AND_RETURN_RET_LOG(eventType == napi_string, NapiAudioError::ThrowErrorAndReturn(env,
585 NAPI_ERR_INPUT_INVALID, "incorrect parameter types: The type of eventType must be string"),
586 "eventType error");
587
588 std::string callbackName = NapiParamUtils::GetStringArgument(env, args[PARAM0]);
589 AUDIO_DEBUG_LOG("AudioStreamMgrNapi: On callbackName: %{public}s", callbackName.c_str());
590
591 napi_valuetype handler = napi_undefined;
592
593 napi_typeof(env, args[PARAM1], &handler);
594 CHECK_AND_RETURN_RET_LOG(handler == napi_function, NapiAudioError::ThrowErrorAndReturn(env,
595 NAPI_ERR_INPUT_INVALID, "incorrect parameter types: The type of callback must be function"),
596 "handler is invalid");
597
598 RegisterCallback(env, jsThis, args, callbackName);
599 return undefinedResult;
600 }
601
UnregisterCallback(napi_env env,napi_value jsThis,size_t argc,napi_value * args,const std::string & cbName)602 void NapiAudioStreamMgr::UnregisterCallback(napi_env env, napi_value jsThis,
603 size_t argc, napi_value *args, const std::string &cbName)
604 {
605 AUDIO_INFO_LOG("UnregisterCallback");
606 NapiAudioStreamMgr *napiStreamMgr = nullptr;
607 napi_status status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&napiStreamMgr));
608 CHECK_AND_RETURN_LOG((status == napi_ok) && (napiStreamMgr != nullptr) &&
609 (napiStreamMgr->audioStreamMngr_ != nullptr), "Failed to retrieve stream mgr napi instance.");
610
611 if (!cbName.compare(RENDERERCHANGE_CALLBACK_NAME)) {
612 UnregisterRendererChangeCallback(napiStreamMgr, argc, args);
613 AUDIO_INFO_LOG("UnRegistering of renderer State Change Callback successful");
614 } else if (!cbName.compare(CAPTURERCHANGE_CALLBACK_NAME)) {
615 UnregisterCapturerChangeCallback(napiStreamMgr, argc, args);
616 AUDIO_INFO_LOG("UnRegistering of capturer State Change Callback successful");
617 } else {
618 AUDIO_ERR_LOG("No such callback supported");
619 NapiAudioError::ThrowError(env, NAPI_ERR_INVALID_PARAM,
620 "parameter verification failed: The param of type is not supported");
621 }
622 }
623
UnregisterRendererChangeCallback(NapiAudioStreamMgr * napiStreamMgr,size_t argc,napi_value * args)624 void NapiAudioStreamMgr::UnregisterRendererChangeCallback(NapiAudioStreamMgr *napiStreamMgr,
625 size_t argc, napi_value *args)
626 {
627 CHECK_AND_RETURN_LOG(napiStreamMgr->rendererStateChangeCallbackNapi_ != nullptr,
628 "rendererStateChangeCallbackNapi is nullptr");
629 std::shared_ptr<NapiAudioRendererStateCallback> cb =
630 std::static_pointer_cast<NapiAudioRendererStateCallback>(napiStreamMgr->rendererStateChangeCallbackNapi_);
631 napi_value callback = nullptr;
632 if (argc == ARGS_TWO) {
633 callback = args[PARAM1];
634 CHECK_AND_RETURN_LOG(cb->IsSameCallback(callback),
635 "The callback need to be unregistered is not the same as the registered callback");
636 }
637 int32_t ret = napiStreamMgr->audioStreamMngr_->
638 UnregisterAudioRendererEventListener(napiStreamMgr->cachedClientId_);
639 CHECK_AND_RETURN_LOG(ret == SUCCESS, "Unregister renderer state change callback failed");
640 cb->RemoveCallbackReference(callback);
641 napiStreamMgr->rendererStateChangeCallbackNapi_.reset();
642 }
643
UnregisterCapturerChangeCallback(NapiAudioStreamMgr * napiStreamMgr,size_t argc,napi_value * args)644 void NapiAudioStreamMgr::UnregisterCapturerChangeCallback(NapiAudioStreamMgr *napiStreamMgr,
645 size_t argc, napi_value *args)
646 {
647 CHECK_AND_RETURN_LOG(napiStreamMgr->capturerStateChangeCallbackNapi_ != nullptr,
648 "capturerStateChangeCallbackNapi is nullptr");
649 std::shared_ptr<NapiAudioCapturerStateCallback> cb =
650 std::static_pointer_cast<NapiAudioCapturerStateCallback>(napiStreamMgr->capturerStateChangeCallbackNapi_);
651 napi_value callback = nullptr;
652 if (argc == ARGS_TWO) {
653 callback = args[PARAM1];
654 CHECK_AND_RETURN_LOG(cb->IsSameCallback(callback),
655 "The callback need to be unregistered is not the same as the registered callback");
656 }
657 int32_t ret = napiStreamMgr->audioStreamMngr_->
658 UnregisterAudioCapturerEventListener(napiStreamMgr->cachedClientId_);
659 CHECK_AND_RETURN_LOG(ret == SUCCESS, "Unregister capturer state change callback failed");
660 cb->RemoveCallbackReference(callback);
661 napiStreamMgr->capturerStateChangeCallbackNapi_.reset();
662 }
663
Off(napi_env env,napi_callback_info info)664 napi_value NapiAudioStreamMgr::Off(napi_env env, napi_callback_info info)
665 {
666 const size_t requireArgc = ARGS_ONE;
667 size_t argc = ARGS_TWO;
668
669 napi_value undefinedResult = nullptr;
670 napi_get_undefined(env, &undefinedResult);
671
672 napi_value args[requireArgc + PARAM2] = {nullptr, nullptr, nullptr};
673 napi_value jsThis = nullptr;
674 napi_status status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
675 CHECK_AND_RETURN_RET_LOG(status == napi_ok && argc >= requireArgc, NapiAudioError::ThrowErrorAndReturn(env,
676 NAPI_ERR_INPUT_INVALID,
677 "mandatory parameters are left unspecified"), "status or arguments error");
678
679 napi_valuetype eventType = napi_undefined;
680 CHECK_AND_RETURN_RET_LOG(napi_typeof(env, args[PARAM0], &eventType) == napi_ok && eventType == napi_string,
681 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID,
682 "incorrect parameter types: The type of eventType must be string"), "eventType error");
683
684 std::string callbackName = NapiParamUtils::GetStringArgument(env, args[0]);
685 AUDIO_DEBUG_LOG("NapiAudioStreamMgr: Off callbackName: %{public}s", callbackName.c_str());
686
687 UnregisterCallback(env, jsThis, argc, args, callbackName);
688 return undefinedResult;
689 }
690
GetSupportedAudioEffectProperty(napi_env env,napi_callback_info info)691 napi_value NapiAudioStreamMgr::GetSupportedAudioEffectProperty(napi_env env, napi_callback_info info)
692 {
693 napi_value result = nullptr;
694 size_t argc = PARAM0;
695 auto *napiStreamMgr = GetParamWithSync(env, info, argc, nullptr);
696 CHECK_AND_RETURN_RET_LOG(argc == PARAM0 && napiStreamMgr != nullptr && napiStreamMgr->audioStreamMngr_ != nullptr,
697 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_SYSTEM,
698 "incorrect parameter types: The type of options must be empty"), "argcCount invalid");
699
700 AudioEffectPropertyArray propertyArray = {};
701 int32_t ret = napiStreamMgr->audioStreamMngr_->GetSupportedAudioEffectProperty(propertyArray);
702 CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, NapiAudioError::ThrowErrorAndReturn(env, ret,
703 "interface operation failed"), "get support audio effect property failure!");
704
705 napi_status status = NapiParamUtils::SetEffectProperty(env, propertyArray, result);
706 CHECK_AND_RETURN_RET_LOG(status == napi_ok, NapiAudioError::ThrowErrorAndReturn(env,
707 NAPI_ERR_SYSTEM, "Combining property data fail"), "fill support effect property failed");
708
709 return result;
710 }
711
GetSupportedAudioEnhanceProperty(napi_env env,napi_callback_info info)712 napi_value NapiAudioStreamMgr::GetSupportedAudioEnhanceProperty(napi_env env, napi_callback_info info)
713 {
714 napi_value result = nullptr;
715 size_t argc = PARAM0;
716 auto *napiStreamMgr = GetParamWithSync(env, info, argc, nullptr);
717 CHECK_AND_RETURN_RET_LOG(argc == PARAM0 && napiStreamMgr != nullptr && napiStreamMgr->audioStreamMngr_ != nullptr,
718 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_SYSTEM,
719 "incorrect parameter types: The type of options must be empty"), "argcCount invalid");
720
721 AudioEnhancePropertyArray propertyArray = {};
722 int32_t ret = napiStreamMgr->audioStreamMngr_->GetSupportedAudioEnhanceProperty(propertyArray);
723 CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, NapiAudioError::ThrowErrorAndReturn(env, ret,
724 "interface operation failed"), "get support audio enhance property failure!");
725
726 napi_status status = NapiParamUtils::SetEnhanceProperty(env, propertyArray, result);
727 CHECK_AND_RETURN_RET_LOG(status == napi_ok, NapiAudioError::ThrowErrorAndReturn(env,
728 NAPI_ERR_SYSTEM, "Combining property data fail"), "fill enhance property failed");
729 return result;
730 }
731
GetAudioEffectProperty(napi_env env,napi_callback_info info)732 napi_value NapiAudioStreamMgr::GetAudioEffectProperty(napi_env env, napi_callback_info info)
733 {
734 napi_value result = nullptr;
735 size_t argc = PARAM0;
736 auto *napiStreamMgr = GetParamWithSync(env, info, argc, nullptr);
737 CHECK_AND_RETURN_RET_LOG(argc == PARAM0 && napiStreamMgr != nullptr && napiStreamMgr->audioStreamMngr_ != nullptr,
738 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_SYSTEM,
739 "incorrect parameter types: The type of options must be empty"), "argcCount invalid");
740
741 AudioEffectPropertyArray propertyArray = {};
742 int32_t ret = napiStreamMgr->audioStreamMngr_->GetAudioEffectProperty(propertyArray);
743 CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, NapiAudioError::ThrowErrorAndReturn(env, ret,
744 "interface operation failed"), "get audio enhance property failure!");
745
746 napi_status status = NapiParamUtils::SetEffectProperty(env, propertyArray, result);
747 CHECK_AND_RETURN_RET_LOG(status == napi_ok, NapiAudioError::ThrowErrorAndReturn(env,
748 NAPI_ERR_SYSTEM, "combining property data fail"), "fill effect property failed");
749
750 return result;
751 }
752
SetAudioEffectProperty(napi_env env,napi_callback_info info)753 napi_value NapiAudioStreamMgr::SetAudioEffectProperty(napi_env env, napi_callback_info info)
754 {
755 napi_value result = nullptr;
756 size_t argc = ARGS_ONE;
757 napi_value args[ARGS_ONE] = {};
758 auto *napiStreamMgr = GetParamWithSync(env, info, argc, args);
759 CHECK_AND_RETURN_RET_LOG(argc == ARGS_ONE && napiStreamMgr != nullptr &&
760 napiStreamMgr->audioStreamMngr_ != nullptr, NapiAudioError::ThrowErrorAndReturn(env,
761 NAPI_ERR_INPUT_INVALID,
762 "parameter verification failed: mandatory parameters are left unspecified"), "argcCount invalid");
763
764 napi_valuetype valueType = napi_undefined;
765 napi_typeof(env, args[PARAM0], &valueType);
766 CHECK_AND_RETURN_RET_LOG(valueType == napi_object, NapiAudioError::ThrowErrorAndReturn(env,
767 NAPI_ERR_INPUT_INVALID, "incorrect parameter types: The type of options must be array"),
768 "invaild valueType");
769
770 AudioEffectPropertyArray propertyArray = {};
771 napi_status status = NapiParamUtils::GetEffectPropertyArray(env, propertyArray, args[PARAM0]);
772 CHECK_AND_RETURN_RET_LOG(status == napi_ok && propertyArray.property.size() > 0,
773 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
774 "parameter verification failed: mandatory parameters are left unspecified"), "status or arguments error");
775
776 int32_t ret = napiStreamMgr->audioStreamMngr_->SetAudioEffectProperty(propertyArray);
777 CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, NapiAudioError::ThrowErrorAndReturn(env, ret,
778 "interface operation failed"), "set audio effect property failure!");
779
780 return result;
781 }
782
GetAudioEnhanceProperty(napi_env env,napi_callback_info info)783 napi_value NapiAudioStreamMgr::GetAudioEnhanceProperty(napi_env env, napi_callback_info info)
784 {
785 napi_value result = nullptr;
786 size_t argc = PARAM0;
787 auto *napiStreamMgr = GetParamWithSync(env, info, argc, nullptr);
788 CHECK_AND_RETURN_RET_LOG(argc == PARAM0 && napiStreamMgr != nullptr && napiStreamMgr->audioStreamMngr_ != nullptr,
789 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_SYSTEM,
790 "incorrect parameter types: The type of options must be empty"), "argcCount invalid");
791
792 AudioEnhancePropertyArray propertyArray = {};
793 int32_t ret = napiStreamMgr->audioStreamMngr_->GetAudioEnhanceProperty(propertyArray);
794 CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, NapiAudioError::ThrowErrorAndReturn(env, ret,
795 "interface operation failed"), "get audio enhance property failure!");
796
797 napi_status status = NapiParamUtils::SetEnhanceProperty(env, propertyArray, result);
798 CHECK_AND_RETURN_RET_LOG(status == napi_ok, NapiAudioError::ThrowErrorAndReturn(env,
799 NAPI_ERR_SYSTEM, "combining property data fail"), "fill effect property failed");
800
801 return result;
802 }
803
SetAudioEnhanceProperty(napi_env env,napi_callback_info info)804 napi_value NapiAudioStreamMgr::SetAudioEnhanceProperty(napi_env env, napi_callback_info info)
805 {
806 napi_value result = nullptr;
807 size_t argc = ARGS_ONE;
808 napi_value args[ARGS_ONE] = {};
809 auto *napiStreamMgr = GetParamWithSync(env, info, argc, args);
810 CHECK_AND_RETURN_RET_LOG(argc == ARGS_ONE && napiStreamMgr != nullptr &&
811 napiStreamMgr->audioStreamMngr_ != nullptr, NapiAudioError::ThrowErrorAndReturn(env,
812 NAPI_ERR_INPUT_INVALID,
813 "parameter verification failed: mandatory parameters are left unspecified"), "argcCount invalid");
814
815 napi_valuetype valueType = napi_undefined;
816 napi_typeof(env, args[PARAM0], &valueType);
817 CHECK_AND_RETURN_RET_LOG(valueType == napi_object, NapiAudioError::ThrowErrorAndReturn(env,
818 NAPI_ERR_INPUT_INVALID, "incorrect parameter types: The type of options must be array"),
819 "invaild valueType");
820
821 AudioEnhancePropertyArray propertyArray = {};
822 napi_status status = NapiParamUtils::GetEnhancePropertyArray(env, propertyArray, args[PARAM0]);
823 CHECK_AND_RETURN_RET_LOG(status == napi_ok && propertyArray.property.size() > 0,
824 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
825 "parameter verification failed: mandatory parameters are left unspecified"), "status or arguments error");
826
827 int32_t ret = napiStreamMgr->audioStreamMngr_->SetAudioEnhanceProperty(propertyArray);
828 CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, NapiAudioError::ThrowErrorAndReturn(env, ret,
829 "interface operation failed"), "set audio enhance property failure!");
830
831 return result;
832 }
833
IsAcousticEchoCancelerSupported(napi_env env,napi_callback_info info)834 napi_value NapiAudioStreamMgr::IsAcousticEchoCancelerSupported(napi_env env, napi_callback_info info)
835 {
836 napi_value result = nullptr;
837 size_t argc = ARGS_ONE;
838 napi_value args[ARGS_ONE] = {};
839 auto *napiStreamMgr = GetParamWithSync(env, info, argc, args);
840 CHECK_AND_RETURN_RET_LOG(argc == ARGS_ONE && napiStreamMgr != nullptr &&
841 napiStreamMgr->audioStreamMngr_ != nullptr, NapiAudioError::ThrowErrorAndReturn(env,
842 NAPI_ERR_INPUT_INVALID,
843 "parameter verification failed: mandatory parameters are left unspecified"), "argcCount invalid");
844 napi_valuetype valueType = napi_undefined;
845 napi_typeof(env, args[PARAM0], &valueType);
846 CHECK_AND_RETURN_RET_LOG(valueType == napi_number, NapiAudioError::ThrowErrorAndReturn(env,
847 NAPI_ERR_INPUT_INVALID, "incorrect parameter types: The type of options must be number"),
848 "invaild valueType");
849 int32_t sourceType;
850 NapiParamUtils::GetValueInt32(env, sourceType, args[PARAM0]);
851 CHECK_AND_RETURN_RET_LOG(NapiAudioEnum::IsValidSourceType(sourceType),
852 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
853 "parameter verification failed: The param of sourceType must be enum SourceType"), "get sourceType failed");
854
855 bool isSupported = napiStreamMgr->audioStreamMngr_->IsAcousticEchoCancelerSupported(
856 static_cast<SourceType>(sourceType));
857 NapiParamUtils::SetValueBoolean(env, isSupported, result);
858 return result;
859 }
860
IsRecordingAvailable(napi_env env,napi_callback_info info)861 napi_value NapiAudioStreamMgr::IsRecordingAvailable(napi_env env, napi_callback_info info)
862 {
863 napi_value result = nullptr;
864 size_t argc = ARGS_ONE;
865 napi_value args[ARGS_ONE] = {};
866 auto *napiStreamMgr = GetParamWithSync(env, info, argc, args);
867 CHECK_AND_RETURN_RET_LOG(argc == ARGS_ONE && napiStreamMgr != nullptr &&
868 napiStreamMgr->audioStreamMngr_ != nullptr, NapiAudioError::ThrowErrorAndReturn(env,
869 NAPI_ERR_INVALID_PARAM,
870 "parameter verification failed: mandatory parameters are left unspecified"), "argcCount invalid");
871 AudioCapturerInfo capturerInfo = {};
872 napi_status status = NapiParamUtils::GetAudioCapturerInfo(env, &capturerInfo, args[PARAM0]);
873 CHECK_AND_RETURN_RET_LOG(status == napi_ok && capturerInfo.sourceType != SourceType::SOURCE_TYPE_INVALID,
874 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM, "parameter verification failed"),
875 "get audioCapturerChangeInfo failed");
876
877 bool isAvailable = napiStreamMgr->audioStreamMngr_->IsCapturerFocusAvailable(capturerInfo);
878 NapiParamUtils::SetValueBoolean(env, isAvailable, result);
879 return result;
880 }
881
IsAudioLoopbackSupported(napi_env env,napi_callback_info info)882 napi_value NapiAudioStreamMgr::IsAudioLoopbackSupported(napi_env env, napi_callback_info info)
883 {
884 napi_value result = nullptr;
885 size_t argc = ARGS_ONE;
886 napi_value args[ARGS_ONE] = {};
887 auto *napiStreamMgr = GetParamWithSync(env, info, argc, args);
888 CHECK_AND_RETURN_RET_LOG(argc >= ARGS_ONE, NapiAudioError::ThrowErrorAndReturn(env,
889 NAPI_ERR_INPUT_INVALID, "mandatory parameters are left unspecified"), "invalid arguments");
890
891 napi_valuetype valueType = napi_undefined;
892 napi_typeof(env, args[PARAM0], &valueType);
893 CHECK_AND_RETURN_RET_LOG(valueType == napi_number, NapiAudioError::ThrowErrorAndReturn(env,
894 NAPI_ERR_INPUT_INVALID, "incorrect parameter types: The type of loopback mode must be number"),
895 "invalid valueType");
896
897 int32_t loopbackMode;
898 NapiParamUtils::GetValueInt32(env, loopbackMode, args[PARAM0]);
899 CHECK_AND_RETURN_RET_LOG(NapiAudioEnum::IsLegalInputArgumentAudioLoopbackMode(loopbackMode),
900 NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM,
901 "parameter verification failed: The param of loopbackMode must be enum AudioLoopbackMode"),
902 "get loopbackMode failed");
903
904 CHECK_AND_RETURN_RET_LOG(napiStreamMgr != nullptr, result, "napiStreamMgr is nullptr");
905 CHECK_AND_RETURN_RET_LOG(napiStreamMgr->audioStreamMngr_ != nullptr, result,
906 "audioStreamMngr_ is nullptr");
907 bool isSupported = napiStreamMgr->audioStreamMngr_->
908 IsAudioLoopbackSupported(static_cast<AudioLoopbackMode>(loopbackMode));
909 NapiParamUtils::SetValueBoolean(env, isSupported, result);
910 return result;
911 }
912 } // namespace AudioStandard
913 } // namespace OHOS
914