1 /*
2 * Copyright (c) 2021-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_parameters_napi.h"
17 #include "hilog/log.h"
18
19 using namespace std;
20 using OHOS::HiviewDFX::HiLog;
21 using OHOS::HiviewDFX::HiLogLabel;
22
23 namespace OHOS {
24 namespace AudioStandard {
25 napi_ref AudioParametersNapi::sConstructor_ = nullptr;
26 unique_ptr<AudioParameters> AudioParametersNapi::sAudioParameters_ = nullptr;
27
28 napi_ref AudioParametersNapi::audioChannel_ = nullptr;
29 napi_ref AudioParametersNapi::samplingRate_ = nullptr;
30 napi_ref AudioParametersNapi::encodingType_ = nullptr;
31 napi_ref AudioParametersNapi::contentType_ = nullptr;
32 napi_ref AudioParametersNapi::streamUsage_ = nullptr;
33 napi_ref AudioParametersNapi::deviceRole_ = nullptr;
34 napi_ref AudioParametersNapi::deviceType_ = nullptr;
35 napi_ref AudioParametersNapi::sourceType_ = nullptr;
36
37 namespace {
38 constexpr HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AudioParametersNapi"};
39 }
40
AudioParametersNapi()41 AudioParametersNapi::AudioParametersNapi()
42 : env_(nullptr) {
43 }
44
~AudioParametersNapi()45 AudioParametersNapi::~AudioParametersNapi()
46 {
47 audioParameters_ = nullptr;
48 }
49
Destructor(napi_env env,void * nativeObject,void * finalize_hint)50 void AudioParametersNapi::Destructor(napi_env env, void *nativeObject, void *finalize_hint)
51 {
52 if (nativeObject != nullptr) {
53 auto obj = static_cast<AudioParametersNapi *>(nativeObject);
54 delete obj;
55 }
56 }
57
AddNamedProperty(napi_env env,napi_value object,const std::string name,int32_t enumValue)58 napi_status AudioParametersNapi::AddNamedProperty(napi_env env, napi_value object,
59 const std::string name, int32_t enumValue)
60 {
61 napi_status status;
62 napi_value enumNapiValue;
63
64 status = napi_create_int32(env, enumValue, &enumNapiValue);
65 if (status == napi_ok) {
66 status = napi_set_named_property(env, object, name.c_str(), enumNapiValue);
67 }
68
69 return status;
70 }
71
CreateAudioChannelObject(napi_env env)72 napi_value AudioParametersNapi::CreateAudioChannelObject(napi_env env)
73 {
74 napi_value result = nullptr;
75 napi_status status;
76 std::string propName;
77
78 status = napi_create_object(env, &result);
79 if (status == napi_ok) {
80 for (auto &iter: audioChannelMap) {
81 propName = iter.first;
82 status = AddNamedProperty(env, result, propName, iter.second);
83 if (status != napi_ok) {
84 HiLog::Error(LABEL, "Failed to add named prop!");
85 break;
86 }
87 propName.clear();
88 }
89 if (status == napi_ok) {
90 status = napi_create_reference(env, result, REFERENCE_CREATION_COUNT, &audioChannel_);
91 if (status == napi_ok) {
92 return result;
93 }
94 }
95 }
96 HiLog::Error(LABEL, "CreateAudioChannelObject is Failed!");
97 napi_get_undefined(env, &result);
98
99 return result;
100 }
101
CreateSamplingRateObject(napi_env env)102 napi_value AudioParametersNapi::CreateSamplingRateObject(napi_env env)
103 {
104 napi_value result = nullptr;
105 napi_status status;
106 std::string propName;
107
108 status = napi_create_object(env, &result);
109 if (status == napi_ok) {
110 for (auto &iter: samplingRateMap) {
111 propName = iter.first;
112 status = AddNamedProperty(env, result, propName, iter.second);
113 if (status != napi_ok) {
114 HiLog::Error(LABEL, "Failed to add named prop!");
115 break;
116 }
117 propName.clear();
118 }
119 if (status == napi_ok) {
120 status = napi_create_reference(env, result, REFERENCE_CREATION_COUNT, &samplingRate_);
121 if (status == napi_ok) {
122 return result;
123 }
124 }
125 }
126 HiLog::Error(LABEL, "CreateSamplingRateObject is Failed!");
127 napi_get_undefined(env, &result);
128
129 return result;
130 }
131
CreateEncodingTypeObject(napi_env env)132 napi_value AudioParametersNapi::CreateEncodingTypeObject(napi_env env)
133 {
134 napi_value result = nullptr;
135 napi_status status;
136 std::string propName;
137
138 status = napi_create_object(env, &result);
139 if (status == napi_ok) {
140 for (auto &iter: encodingTypeMap) {
141 propName = iter.first;
142 status = AddNamedProperty(env, result, propName, iter.second);
143 if (status != napi_ok) {
144 HiLog::Error(LABEL, "Failed to add named prop!");
145 break;
146 }
147 propName.clear();
148 }
149 if (status == napi_ok) {
150 status = napi_create_reference(env, result, REFERENCE_CREATION_COUNT, &encodingType_);
151 if (status == napi_ok) {
152 return result;
153 }
154 }
155 }
156 HiLog::Error(LABEL, "CreateEncodingTypeObject is failed!");
157 napi_get_undefined(env, &result);
158
159 return result;
160 }
161
CreateContentTypeObject(napi_env env)162 napi_value AudioParametersNapi::CreateContentTypeObject(napi_env env)
163 {
164 napi_value result = nullptr;
165 napi_status status;
166 std::string propName;
167
168 status = napi_create_object(env, &result);
169 if (status == napi_ok) {
170 for (auto &iter: contentTypeMap) {
171 propName = iter.first;
172 status = AddNamedProperty(env, result, propName, iter.second);
173 if (status != napi_ok) {
174 HiLog::Error(LABEL, "Failed to add named prop!");
175 break;
176 }
177 propName.clear();
178 }
179 if (status == napi_ok) {
180 status = napi_create_reference(env, result, REFERENCE_CREATION_COUNT, &contentType_);
181 if (status == napi_ok) {
182 return result;
183 }
184 }
185 }
186 HiLog::Error(LABEL, "CreateContentTypeObject is Failed!");
187 napi_get_undefined(env, &result);
188
189 return result;
190 }
191
CreateStreamUsageObject(napi_env env)192 napi_value AudioParametersNapi::CreateStreamUsageObject(napi_env env)
193 {
194 napi_value result = nullptr;
195 napi_status status;
196 std::string propName;
197
198 status = napi_create_object(env, &result);
199 if (status == napi_ok) {
200 for (auto &iter: streamUsageMap) {
201 propName = iter.first;
202 status = AddNamedProperty(env, result, propName, iter.second);
203 if (status != napi_ok) {
204 HiLog::Error(LABEL, "Failed to add named prop!");
205 break;
206 }
207 propName.clear();
208 }
209 if (status == napi_ok) {
210 status = napi_create_reference(env, result, REFERENCE_CREATION_COUNT, &streamUsage_);
211 if (status == napi_ok) {
212 return result;
213 }
214 }
215 }
216 HiLog::Error(LABEL, "CreateStreamUsageObject is Failed!");
217 napi_get_undefined(env, &result);
218
219 return result;
220 }
221
CreateDeviceRoleObject(napi_env env)222 napi_value AudioParametersNapi::CreateDeviceRoleObject(napi_env env)
223 {
224 napi_value result = nullptr;
225 napi_status status;
226 std::string propName;
227
228 status = napi_create_object(env, &result);
229 if (status == napi_ok) {
230 for (auto &iter: deviceRoleMap) {
231 propName = iter.first;
232 status = AddNamedProperty(env, result, propName, iter.second);
233 if (status != napi_ok) {
234 HiLog::Error(LABEL, "Failed to add named prop!");
235 break;
236 }
237 propName.clear();
238 }
239 if (status == napi_ok) {
240 status = napi_create_reference(env, result, REFERENCE_CREATION_COUNT, &deviceRole_);
241 if (status == napi_ok) {
242 return result;
243 }
244 }
245 }
246 HiLog::Error(LABEL, "CreateDeviceRoleObject is Failed!");
247 napi_get_undefined(env, &result);
248
249 return result;
250 }
251
CreateDeviceTypeObject(napi_env env)252 napi_value AudioParametersNapi::CreateDeviceTypeObject(napi_env env)
253 {
254 napi_value result = nullptr;
255 napi_status status;
256 std::string propName;
257
258 status = napi_create_object(env, &result);
259 if (status == napi_ok) {
260 for (auto &iter: deviceTypeMap) {
261 propName = iter.first;
262 status = AddNamedProperty(env, result, propName, iter.second);
263 if (status != napi_ok) {
264 HiLog::Error(LABEL, "Failed to add named prop!");
265 break;
266 }
267 propName.clear();
268 }
269 if (status == napi_ok) {
270 status = napi_create_reference(env, result, REFERENCE_CREATION_COUNT, &deviceType_);
271 if (status == napi_ok) {
272 return result;
273 }
274 }
275 }
276 HiLog::Error(LABEL, "CreateDeviceRoleObject is Failed!");
277 napi_get_undefined(env, &result);
278
279 return result;
280 }
281
CreateSourceTypeObject(napi_env env)282 napi_value AudioParametersNapi::CreateSourceTypeObject(napi_env env)
283 {
284 napi_value result = nullptr;
285 napi_status status;
286 std::string propName;
287
288 status = napi_create_object(env, &result);
289 if (status == napi_ok) {
290 for (auto &iter: sourceTypeMap) {
291 propName = iter.first;
292 status = AddNamedProperty(env, result, propName, iter.second);
293 if (status != napi_ok) {
294 HiLog::Error(LABEL, "Failed to add named prop! in CreateSourceTypeObject");
295 break;
296 }
297 propName.clear();
298 }
299 if (status == napi_ok) {
300 status = napi_create_reference(env, result, REFERENCE_CREATION_COUNT, &sourceType_);
301 if (status == napi_ok) {
302 return result;
303 }
304 }
305 }
306 HiLog::Error(LABEL, "CreateSourceTypeObject is Failed!");
307 napi_get_undefined(env, &result);
308
309 return result;
310 }
311
CreateVolumeAdjustTypeObject(napi_env env)312 napi_value AudioParametersNapi::CreateVolumeAdjustTypeObject(napi_env env)
313 {
314 napi_value result = nullptr;
315 napi_status status;
316 std::string propName;
317
318 status = napi_create_object(env, &result);
319 if (status == napi_ok) {
320 for (auto &iter: volumeAdjustTypeMap) {
321 propName = iter.first;
322 status = AddNamedProperty(env, result, propName, iter.second);
323 if (status != napi_ok) {
324 HiLog::Error(LABEL, "Failed to add named prop! in CreateVolumeAdjustTypeObject");
325 break;
326 }
327 propName.clear();
328 }
329 if (status == napi_ok) {
330 status = napi_create_reference(env, result, REFERENCE_CREATION_COUNT, &sourceType_);
331 if (status == napi_ok) {
332 return result;
333 }
334 }
335 }
336 HiLog::Error(LABEL, "CreateVolumeAdjustTypeObject is Failed!");
337 napi_get_undefined(env, &result);
338
339 return result;
340 }
341
Init(napi_env env,napi_value exports)342 napi_value AudioParametersNapi::Init(napi_env env, napi_value exports)
343 {
344 HiLog::Info(LABEL, "AudioParametersNapi::Init()");
345 napi_status status;
346 napi_value constructor;
347 napi_value result = nullptr;
348 napi_get_undefined(env, &result);
349
350 napi_property_descriptor audio_parameters_properties[] = {
351 DECLARE_NAPI_GETTER_SETTER("format", GetAudioSampleFormat, SetAudioSampleFormat),
352 DECLARE_NAPI_GETTER_SETTER("channels", GetAudioChannel, SetAudioChannel),
353 DECLARE_NAPI_GETTER_SETTER("samplingRate", GetAudioSamplingRate, SetAudioSamplingRate),
354 DECLARE_NAPI_GETTER_SETTER("encoding", GetAudioEncodingType, SetAudioEncodingType),
355 DECLARE_NAPI_GETTER_SETTER("contentType", GetContentType, SetContentType),
356 DECLARE_NAPI_GETTER_SETTER("usage", GetStreamUsage, SetStreamUsage),
357 DECLARE_NAPI_GETTER_SETTER("deviceRole", GetDeviceRole, SetDeviceRole),
358 DECLARE_NAPI_GETTER_SETTER("deviceType", GetDeviceType, SetDeviceType)
359 };
360
361 napi_property_descriptor static_prop[] = {
362 DECLARE_NAPI_PROPERTY("AudioChannel", CreateAudioChannelObject(env)),
363 DECLARE_NAPI_PROPERTY("AudioSamplingRate", CreateSamplingRateObject(env)),
364 DECLARE_NAPI_PROPERTY("AudioEncodingType", CreateEncodingTypeObject(env)),
365 DECLARE_NAPI_PROPERTY("ContentType", CreateContentTypeObject(env)),
366 DECLARE_NAPI_PROPERTY("StreamUsage", CreateStreamUsageObject(env)),
367 DECLARE_NAPI_PROPERTY("DeviceRole", CreateDeviceRoleObject(env)),
368 DECLARE_NAPI_PROPERTY("DeviceType", CreateDeviceTypeObject(env)),
369 DECLARE_NAPI_PROPERTY("SourceType", CreateSourceTypeObject(env)),
370 DECLARE_NAPI_PROPERTY("VolumeAdjustType", CreateVolumeAdjustTypeObject(env))
371 };
372
373 status = napi_define_class(env, AUDIO_PARAMETERS_NAPI_CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Construct,
374 nullptr, sizeof(audio_parameters_properties) / sizeof(audio_parameters_properties[0]),
375 audio_parameters_properties, &constructor);
376 if (status != napi_ok) {
377 return result;
378 }
379
380 status = napi_create_reference(env, constructor, REFERENCE_CREATION_COUNT, &sConstructor_);
381 if (status == napi_ok) {
382 status = napi_set_named_property(env, exports, AUDIO_PARAMETERS_NAPI_CLASS_NAME.c_str(),
383 constructor);
384 if (status == napi_ok) {
385 status = napi_define_properties(env, exports,
386 sizeof(static_prop) / sizeof(static_prop[0]), static_prop);
387 if (status == napi_ok) {
388 return exports;
389 }
390 }
391 }
392 HiLog::Error(LABEL, "Failure in AudioParametersNapi::Init()");
393
394 return result;
395 }
396
Construct(napi_env env,napi_callback_info info)397 napi_value AudioParametersNapi::Construct(napi_env env, napi_callback_info info)
398 {
399 napi_status status;
400 napi_value jsThis = nullptr;
401 size_t argCount = 0;
402
403 status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
404 if (status == napi_ok) {
405 unique_ptr<AudioParametersNapi> obj = make_unique<AudioParametersNapi>();
406 if (obj != nullptr) {
407 obj->env_ = env;
408 obj->audioParameters_ = move(sAudioParameters_);
409 status = napi_wrap(env, jsThis, static_cast<void*>(obj.get()),
410 AudioParametersNapi::Destructor, nullptr, nullptr);
411 if (status == napi_ok) {
412 obj.release();
413 return jsThis;
414 }
415 }
416 }
417 HiLog::Error(LABEL, "Failed in AudioParametersNapi::Construct()!");
418 napi_get_undefined(env, &jsThis);
419
420 return jsThis;
421 }
422
GetAudioSampleFormat(napi_env env,napi_callback_info info)423 napi_value AudioParametersNapi::GetAudioSampleFormat(napi_env env, napi_callback_info info)
424 {
425 napi_status status;
426 AudioParametersNapi *audioParametersNapi = nullptr;
427 size_t argc = 0;
428 napi_value jsThis = nullptr;
429 AudioSampleFormat audioSampleFormat;
430 napi_value jsResult = nullptr;
431 napi_get_undefined(env, &jsResult);
432
433 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
434 if (status != napi_ok || jsThis == nullptr) {
435 HiLog::Error(LABEL, "Get audio sample format fail to napi_get_cb_info");
436 return jsResult;
437 }
438
439 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
440 if (status == napi_ok) {
441 if (!((audioParametersNapi != nullptr) && (audioParametersNapi->audioParameters_ != nullptr))) {
442 HiLog::Error(LABEL, "Get audio sample format fail to napi_unwrap");
443 return jsResult;
444 }
445 audioSampleFormat = audioParametersNapi->audioParameters_->format;
446 status = napi_create_int32(env, audioSampleFormat, &jsResult);
447 if (status == napi_ok) {
448 return jsResult;
449 }
450 }
451
452 return jsResult;
453 }
454
SetAudioSampleFormat(napi_env env,napi_callback_info info)455 napi_value AudioParametersNapi::SetAudioSampleFormat(napi_env env, napi_callback_info info)
456 {
457 napi_status status;
458 AudioParametersNapi *audioParametersNapi = nullptr;
459 size_t argc = 1;
460 napi_value args[1] = { nullptr };
461 napi_value jsThis = nullptr;
462 int32_t audioSampleFormat;
463 napi_value jsResult = nullptr;
464 napi_get_undefined(env, &jsResult);
465
466 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
467 if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
468 HiLog::Error(LABEL, "set sample format fail to napi_get_cb_info");
469 return jsResult;
470 }
471
472 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
473 if (status == napi_ok) {
474 napi_valuetype valueType = napi_undefined;
475 if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
476 HiLog::Error(LABEL, "set sample format fail: wrong data type");
477 return jsResult;
478 }
479 }
480
481 status = napi_get_value_int32(env, args[0], &audioSampleFormat);
482 if (status == napi_ok) {
483 audioParametersNapi->audioParameters_->format = static_cast<AudioSampleFormat>(audioSampleFormat);
484 }
485
486 return jsResult;
487 }
488
GetAudioChannel(napi_env env,napi_callback_info info)489 napi_value AudioParametersNapi::GetAudioChannel(napi_env env, napi_callback_info info)
490 {
491 napi_status status;
492 AudioParametersNapi *audioParametersNapi = nullptr;
493 size_t argc = 0;
494 napi_value jsThis = nullptr;
495 AudioChannel audioChannel;
496 napi_value jsResult = nullptr;
497 napi_get_undefined(env, &jsResult);
498
499 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
500 if (status != napi_ok || jsThis == nullptr) {
501 HiLog::Error(LABEL, "Get audio channels fail to napi_get_cb_info");
502 return jsResult;
503 }
504
505 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
506 if (status == napi_ok) {
507 audioChannel = audioParametersNapi->audioParameters_->channels;
508 status = napi_create_int32(env, audioChannel, &jsResult);
509 if (status == napi_ok) {
510 return jsResult;
511 }
512 }
513
514 return jsResult;
515 }
516
SetAudioChannel(napi_env env,napi_callback_info info)517 napi_value AudioParametersNapi::SetAudioChannel(napi_env env, napi_callback_info info)
518 {
519 napi_status status;
520 AudioParametersNapi *audioParametersNapi = nullptr;
521 size_t argc = 1;
522 napi_value args[1] = { nullptr };
523 napi_value jsThis = nullptr;
524 int32_t audioChannel;
525 napi_value jsResult = nullptr;
526 napi_get_undefined(env, &jsResult);
527
528 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
529 if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
530 HiLog::Error(LABEL, "set audio channel fail to napi_get_cb_info");
531 return jsResult;
532 }
533
534 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
535 if (status == napi_ok) {
536 napi_valuetype valueType = napi_undefined;
537 if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
538 HiLog::Error(LABEL, "set audio channel fail: wrong data type");
539 return jsResult;
540 }
541 }
542
543 status = napi_get_value_int32(env, args[0], &audioChannel);
544 if (status == napi_ok) {
545 audioParametersNapi->audioParameters_->channels = static_cast<AudioChannel>(audioChannel);
546 }
547
548 return jsResult;
549 }
550
GetAudioSamplingRate(napi_env env,napi_callback_info info)551 napi_value AudioParametersNapi::GetAudioSamplingRate(napi_env env, napi_callback_info info)
552 {
553 napi_status status;
554 AudioParametersNapi *audioParametersNapi = nullptr;
555 size_t argc = 0;
556 napi_value jsThis = nullptr;
557 AudioSamplingRate samplingRate;
558 napi_value jsResult = nullptr;
559 napi_get_undefined(env, &jsResult);
560
561 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
562 if (status != napi_ok || jsThis == nullptr) {
563 HiLog::Error(LABEL, "Get sampling rate fail to napi_get_cb_info");
564 return jsResult;
565 }
566
567 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
568 if (status == napi_ok) {
569 samplingRate = audioParametersNapi->audioParameters_->samplingRate;
570 status = napi_create_int32(env, samplingRate, &jsResult);
571 if (status == napi_ok) {
572 return jsResult;
573 }
574 }
575
576 return jsResult;
577 }
578
SetAudioSamplingRate(napi_env env,napi_callback_info info)579 napi_value AudioParametersNapi::SetAudioSamplingRate(napi_env env, napi_callback_info info)
580 {
581 napi_status status;
582 AudioParametersNapi *audioParametersNapi = nullptr;
583 size_t argc = 1;
584 napi_value args[1] = { nullptr };
585 napi_value jsThis = nullptr;
586 int32_t samplingRate;
587 napi_value jsResult = nullptr;
588 napi_get_undefined(env, &jsResult);
589
590 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
591 if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
592 HiLog::Error(LABEL, "set sampling rate fail to napi_get_cb_info");
593 return jsResult;
594 }
595
596 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
597 if (status == napi_ok) {
598 napi_valuetype valueType = napi_undefined;
599 if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
600 HiLog::Error(LABEL, "set sampling rate fail: wrong data type");
601 return jsResult;
602 }
603 }
604
605 status = napi_get_value_int32(env, args[0], &samplingRate);
606 if (status == napi_ok) {
607 audioParametersNapi->audioParameters_->samplingRate = static_cast<AudioSamplingRate>(samplingRate);
608 }
609
610 return jsResult;
611 }
612
GetAudioEncodingType(napi_env env,napi_callback_info info)613 napi_value AudioParametersNapi::GetAudioEncodingType(napi_env env, napi_callback_info info)
614 {
615 napi_status status;
616 AudioParametersNapi *audioParametersNapi = nullptr;
617 size_t argc = 0;
618 napi_value jsThis = nullptr;
619 AudioEncodingType encodingType;
620 napi_value jsResult = nullptr;
621 napi_get_undefined(env, &jsResult);
622
623 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
624 if (status != napi_ok || jsThis == nullptr) {
625 HiLog::Error(LABEL, "Get encoding type fail to napi_get_cb_info");
626 return jsResult;
627 }
628
629 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
630 if (status == napi_ok) {
631 encodingType = audioParametersNapi->audioParameters_->encoding;
632 status = napi_create_int32(env, encodingType, &jsResult);
633 if (status == napi_ok) {
634 return jsResult;
635 }
636 }
637
638 return jsResult;
639 }
640
SetAudioEncodingType(napi_env env,napi_callback_info info)641 napi_value AudioParametersNapi::SetAudioEncodingType(napi_env env, napi_callback_info info)
642 {
643 napi_status status;
644 AudioParametersNapi *audioParametersNapi = nullptr;
645 size_t argc = 1;
646 napi_value args[1] = { nullptr };
647 napi_value jsThis = nullptr;
648 int32_t encodingType;
649 napi_value jsResult = nullptr;
650 napi_get_undefined(env, &jsResult);
651
652 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
653 if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
654 HiLog::Error(LABEL, "set audio encoding type fail to napi_get_cb_info");
655 return jsResult;
656 }
657
658 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
659 if (status == napi_ok) {
660 napi_valuetype valueType = napi_undefined;
661 if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
662 HiLog::Error(LABEL, "set audio encoding type fail: wrong data type");
663 return jsResult;
664 }
665 }
666
667 status = napi_get_value_int32(env, args[0], &encodingType);
668 if (status == napi_ok) {
669 audioParametersNapi->audioParameters_->encoding = static_cast<AudioEncodingType>(encodingType);
670 }
671
672 return jsResult;
673 }
674
GetContentType(napi_env env,napi_callback_info info)675 napi_value AudioParametersNapi::GetContentType(napi_env env, napi_callback_info info)
676 {
677 napi_status status;
678 AudioParametersNapi *audioParametersNapi = nullptr;
679 size_t argc = 0;
680 napi_value jsThis = nullptr;
681 ContentType contentType;
682 napi_value jsResult = nullptr;
683 napi_get_undefined(env, &jsResult);
684
685 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
686 if (status != napi_ok || jsThis == nullptr) {
687 HiLog::Error(LABEL, "Get content type fail to napi_get_cb_info");
688 return jsResult;
689 }
690
691 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
692 if (status == napi_ok) {
693 contentType = audioParametersNapi->audioParameters_->contentType;
694 status = napi_create_int32(env, contentType, &jsResult);
695 if (status == napi_ok) {
696 return jsResult;
697 }
698 }
699
700 return jsResult;
701 }
702
SetContentType(napi_env env,napi_callback_info info)703 napi_value AudioParametersNapi::SetContentType(napi_env env, napi_callback_info info)
704 {
705 napi_status status;
706 AudioParametersNapi *audioParametersNapi = nullptr;
707 size_t argc = 1;
708 napi_value args[1] = { nullptr };
709 napi_value jsThis = nullptr;
710 int32_t contentType;
711 napi_value jsResult = nullptr;
712 napi_get_undefined(env, &jsResult);
713
714 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
715 if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
716 HiLog::Error(LABEL, "set content type fail to napi_get_cb_info");
717 return jsResult;
718 }
719
720 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
721 if (status == napi_ok) {
722 napi_valuetype valueType = napi_undefined;
723 if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
724 HiLog::Error(LABEL, "set content type fail: wrong data type");
725 return jsResult;
726 }
727 }
728
729 status = napi_get_value_int32(env, args[0], &contentType);
730 if (status == napi_ok) {
731 audioParametersNapi->audioParameters_->contentType = static_cast<ContentType>(contentType);
732 }
733
734 return jsResult;
735 }
736
GetStreamUsage(napi_env env,napi_callback_info info)737 napi_value AudioParametersNapi::GetStreamUsage(napi_env env, napi_callback_info info)
738 {
739 napi_status status;
740 AudioParametersNapi *audioParametersNapi = nullptr;
741 size_t argc = 0;
742 napi_value jsThis = nullptr;
743 StreamUsage usage;
744 napi_value jsResult = nullptr;
745 napi_get_undefined(env, &jsResult);
746
747 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
748 if (status != napi_ok || jsThis == nullptr) {
749 HiLog::Error(LABEL, "Get stream usage fail to napi_get_cb_info");
750 return jsResult;
751 }
752
753 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
754 if (status == napi_ok) {
755 usage = audioParametersNapi->audioParameters_->usage;
756 status = napi_create_int32(env, usage, &jsResult);
757 if (status == napi_ok) {
758 return jsResult;
759 }
760 }
761
762 return jsResult;
763 }
764
SetStreamUsage(napi_env env,napi_callback_info info)765 napi_value AudioParametersNapi::SetStreamUsage(napi_env env, napi_callback_info info)
766 {
767 napi_status status;
768 AudioParametersNapi *audioParametersNapi = nullptr;
769 size_t argc = 1;
770 napi_value args[1] = { nullptr };
771 napi_value jsThis = nullptr;
772 int32_t usage;
773 napi_value jsResult = nullptr;
774 napi_get_undefined(env, &jsResult);
775
776 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
777 if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
778 HiLog::Error(LABEL, "set stream usage fail to napi_get_cb_info");
779 return jsResult;
780 }
781
782 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
783 if (status == napi_ok) {
784 napi_valuetype valueType = napi_undefined;
785 if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
786 HiLog::Error(LABEL, "set stream usage fail: wrong data type");
787 return jsResult;
788 }
789 }
790
791 status = napi_get_value_int32(env, args[0], &usage);
792 if (status == napi_ok) {
793 audioParametersNapi->audioParameters_->usage = static_cast<StreamUsage>(usage);
794 }
795
796 return jsResult;
797 }
798
GetDeviceRole(napi_env env,napi_callback_info info)799 napi_value AudioParametersNapi::GetDeviceRole(napi_env env, napi_callback_info info)
800 {
801 napi_status status;
802 AudioParametersNapi *audioParametersNapi = nullptr;
803 size_t argc = 0;
804 napi_value jsThis = nullptr;
805 DeviceRole deviceRole;
806 napi_value jsResult = nullptr;
807 napi_get_undefined(env, &jsResult);
808
809 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
810 if (status != napi_ok || jsThis == nullptr) {
811 HiLog::Error(LABEL, "Get device role fail to napi_get_cb_info");
812 return jsResult;
813 }
814
815 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
816 if (status == napi_ok) {
817 deviceRole = audioParametersNapi->audioParameters_->deviceRole;
818 status = napi_create_int32(env, deviceRole, &jsResult);
819 if (status == napi_ok) {
820 return jsResult;
821 }
822 }
823
824 return jsResult;
825 }
826
SetDeviceRole(napi_env env,napi_callback_info info)827 napi_value AudioParametersNapi::SetDeviceRole(napi_env env, napi_callback_info info)
828 {
829 napi_status status;
830 AudioParametersNapi *audioParametersNapi = nullptr;
831 size_t argc = 1;
832 napi_value args[1] = { nullptr };
833 napi_value jsThis = nullptr;
834 int32_t deviceRole;
835 napi_value jsResult = nullptr;
836 napi_get_undefined(env, &jsResult);
837
838 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
839 if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
840 HiLog::Error(LABEL, "set device role fail to napi_get_cb_info");
841 return jsResult;
842 }
843
844 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
845 if (status == napi_ok) {
846 napi_valuetype valueType = napi_undefined;
847 if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
848 HiLog::Error(LABEL, "set device role fail: wrong data type");
849 return jsResult;
850 }
851 }
852
853 status = napi_get_value_int32(env, args[0], &deviceRole);
854 if (status == napi_ok) {
855 audioParametersNapi->audioParameters_->deviceRole = static_cast<DeviceRole>(deviceRole);
856 }
857
858 return jsResult;
859 }
860
GetDeviceType(napi_env env,napi_callback_info info)861 napi_value AudioParametersNapi::GetDeviceType(napi_env env, napi_callback_info info)
862 {
863 napi_status status;
864 AudioParametersNapi *audioParametersNapi = nullptr;
865 size_t argc = 0;
866 napi_value jsThis = nullptr;
867 DeviceType deviceType;
868 napi_value jsResult = nullptr;
869 napi_get_undefined(env, &jsResult);
870
871 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
872 if (status != napi_ok || jsThis == nullptr) {
873 HiLog::Error(LABEL, "Get device type fail to napi_get_cb_info");
874 return jsResult;
875 }
876
877 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
878 if (status == napi_ok) {
879 deviceType = audioParametersNapi->audioParameters_->deviceType;
880 HiLog::Info(LABEL, "get device type: %d", deviceType);
881 status = napi_create_int32(env, deviceType, &jsResult);
882 if (status == napi_ok) {
883 return jsResult;
884 }
885 }
886
887 return jsResult;
888 }
889
SetDeviceType(napi_env env,napi_callback_info info)890 napi_value AudioParametersNapi::SetDeviceType(napi_env env, napi_callback_info info)
891 {
892 napi_status status;
893 AudioParametersNapi *audioParametersNapi = nullptr;
894 size_t argc = 1;
895 napi_value args[1] = { nullptr };
896 napi_value jsThis = nullptr;
897 int32_t deviceType;
898 napi_value jsResult = nullptr;
899 napi_get_undefined(env, &jsResult);
900
901 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
902 if ((status != napi_ok) || (jsThis == nullptr) || (args[0] == nullptr)) {
903 HiLog::Error(LABEL, "set device type fail to napi_get_cb_info");
904 return jsResult;
905 }
906
907 status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
908 if (status == napi_ok) {
909 napi_valuetype valueType = napi_undefined;
910 if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
911 HiLog::Error(LABEL, "set device type fail: wrong data type");
912 return jsResult;
913 }
914 }
915
916 status = napi_get_value_int32(env, args[0], &deviceType);
917 if (status == napi_ok) {
918 audioParametersNapi->audioParameters_->deviceType = static_cast<DeviceType>(deviceType);
919 HiLog::Info(LABEL, "set device type: %d", audioParametersNapi->audioParameters_->deviceType);
920 }
921
922 return jsResult;
923 }
924 } // namespace AudioStandard
925 } // namespace OHOS
926