• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Init(napi_env env,napi_value exports)312 napi_value AudioParametersNapi::Init(napi_env env, napi_value exports)
313 {
314     HiLog::Info(LABEL, "AudioParametersNapi::Init()");
315     napi_status status;
316     napi_value constructor;
317     napi_value result = nullptr;
318     napi_get_undefined(env, &result);
319 
320     napi_property_descriptor audio_parameters_properties[] = {
321         DECLARE_NAPI_GETTER_SETTER("format", GetAudioSampleFormat, SetAudioSampleFormat),
322         DECLARE_NAPI_GETTER_SETTER("channels", GetAudioChannel, SetAudioChannel),
323         DECLARE_NAPI_GETTER_SETTER("samplingRate", GetAudioSamplingRate, SetAudioSamplingRate),
324         DECLARE_NAPI_GETTER_SETTER("encoding", GetAudioEncodingType, SetAudioEncodingType),
325         DECLARE_NAPI_GETTER_SETTER("contentType", GetContentType, SetContentType),
326         DECLARE_NAPI_GETTER_SETTER("usage", GetStreamUsage, SetStreamUsage),
327         DECLARE_NAPI_GETTER_SETTER("deviceRole", GetDeviceRole, SetDeviceRole),
328         DECLARE_NAPI_GETTER_SETTER("deviceType", GetDeviceType, SetDeviceType)
329     };
330 
331     napi_property_descriptor static_prop[] = {
332         DECLARE_NAPI_PROPERTY("AudioChannel", CreateAudioChannelObject(env)),
333         DECLARE_NAPI_PROPERTY("AudioSamplingRate", CreateSamplingRateObject(env)),
334         DECLARE_NAPI_PROPERTY("AudioEncodingType", CreateEncodingTypeObject(env)),
335         DECLARE_NAPI_PROPERTY("ContentType", CreateContentTypeObject(env)),
336         DECLARE_NAPI_PROPERTY("StreamUsage", CreateStreamUsageObject(env)),
337         DECLARE_NAPI_PROPERTY("DeviceRole", CreateDeviceRoleObject(env)),
338         DECLARE_NAPI_PROPERTY("DeviceType", CreateDeviceTypeObject(env)),
339         DECLARE_NAPI_PROPERTY("SourceType", CreateSourceTypeObject(env))
340     };
341 
342     status = napi_define_class(env, AUDIO_PARAMETERS_NAPI_CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Construct,
343                                nullptr, sizeof(audio_parameters_properties) / sizeof(audio_parameters_properties[0]),
344                                audio_parameters_properties, &constructor);
345     if (status != napi_ok) {
346         return result;
347     }
348 
349     status = napi_create_reference(env, constructor, REFERENCE_CREATION_COUNT, &sConstructor_);
350     if (status == napi_ok) {
351         status = napi_set_named_property(env, exports, AUDIO_PARAMETERS_NAPI_CLASS_NAME.c_str(),
352                                          constructor);
353         if (status == napi_ok) {
354             status = napi_define_properties(env, exports,
355                                             sizeof(static_prop) / sizeof(static_prop[0]), static_prop);
356             if (status == napi_ok) {
357                 return exports;
358             }
359         }
360     }
361     HiLog::Error(LABEL, "Failure in AudioParametersNapi::Init()");
362 
363     return result;
364 }
365 
Construct(napi_env env,napi_callback_info info)366 napi_value AudioParametersNapi::Construct(napi_env env, napi_callback_info info)
367 {
368     napi_status status;
369     napi_value jsThis = nullptr;
370     size_t argCount = 0;
371 
372     status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
373     if (status == napi_ok) {
374         unique_ptr<AudioParametersNapi> obj = make_unique<AudioParametersNapi>();
375         if (obj != nullptr) {
376             obj->env_ = env;
377             obj->audioParameters_ = move(sAudioParameters_);
378             status = napi_wrap(env, jsThis, static_cast<void*>(obj.get()),
379                                AudioParametersNapi::Destructor, nullptr, nullptr);
380             if (status == napi_ok) {
381                 obj.release();
382                 return jsThis;
383             }
384         }
385     }
386     HiLog::Error(LABEL, "Failed in AudioParametersNapi::Construct()!");
387     napi_get_undefined(env, &jsThis);
388 
389     return jsThis;
390 }
391 
GetAudioSampleFormat(napi_env env,napi_callback_info info)392 napi_value AudioParametersNapi::GetAudioSampleFormat(napi_env env, napi_callback_info info)
393 {
394     napi_status status;
395     AudioParametersNapi *audioParametersNapi = nullptr;
396     size_t argc = 0;
397     napi_value jsThis = nullptr;
398     AudioSampleFormat audioSampleFormat;
399     napi_value jsResult = nullptr;
400     napi_get_undefined(env, &jsResult);
401 
402     status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
403     if (status != napi_ok || jsThis == nullptr) {
404         HiLog::Error(LABEL, "Get audio sample format fail to napi_get_cb_info");
405         return jsResult;
406     }
407 
408     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
409     if (status == napi_ok) {
410         if (!((audioParametersNapi != nullptr) && (audioParametersNapi->audioParameters_ != nullptr))) {
411             HiLog::Error(LABEL, "Get audio sample format fail to napi_unwrap");
412             return jsResult;
413         }
414         audioSampleFormat = audioParametersNapi->audioParameters_->format;
415         status = napi_create_int32(env, audioSampleFormat, &jsResult);
416         if (status == napi_ok) {
417             return jsResult;
418         }
419     }
420 
421     return jsResult;
422 }
423 
SetAudioSampleFormat(napi_env env,napi_callback_info info)424 napi_value AudioParametersNapi::SetAudioSampleFormat(napi_env env, napi_callback_info info)
425 {
426     napi_status status;
427     AudioParametersNapi *audioParametersNapi = nullptr;
428     size_t argc = 1;
429     napi_value args[1] = { nullptr };
430     napi_value jsThis = nullptr;
431     int32_t audioSampleFormat;
432     napi_value jsResult = nullptr;
433     napi_get_undefined(env, &jsResult);
434 
435     status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
436     if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
437         HiLog::Error(LABEL, "set sample format fail to napi_get_cb_info");
438         return jsResult;
439     }
440 
441     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
442     if (status == napi_ok) {
443         napi_valuetype valueType = napi_undefined;
444         if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
445             HiLog::Error(LABEL, "set sample format fail: wrong data type");
446             return jsResult;
447         }
448     }
449 
450     status = napi_get_value_int32(env, args[0], &audioSampleFormat);
451     if (status == napi_ok) {
452         audioParametersNapi->audioParameters_->format = static_cast<AudioSampleFormat>(audioSampleFormat);
453     }
454 
455     return jsResult;
456 }
457 
GetAudioChannel(napi_env env,napi_callback_info info)458 napi_value AudioParametersNapi::GetAudioChannel(napi_env env, napi_callback_info info)
459 {
460     napi_status status;
461     AudioParametersNapi *audioParametersNapi = nullptr;
462     size_t argc = 0;
463     napi_value jsThis = nullptr;
464     AudioChannel audioChannel;
465     napi_value jsResult = nullptr;
466     napi_get_undefined(env, &jsResult);
467 
468     status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
469     if (status != napi_ok || jsThis == nullptr) {
470         HiLog::Error(LABEL, "Get audio channels fail to napi_get_cb_info");
471         return jsResult;
472     }
473 
474     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
475     if (status == napi_ok) {
476         audioChannel = audioParametersNapi->audioParameters_->channels;
477         status = napi_create_int32(env, audioChannel, &jsResult);
478         if (status == napi_ok) {
479             return jsResult;
480         }
481     }
482 
483     return jsResult;
484 }
485 
SetAudioChannel(napi_env env,napi_callback_info info)486 napi_value AudioParametersNapi::SetAudioChannel(napi_env env, napi_callback_info info)
487 {
488     napi_status status;
489     AudioParametersNapi *audioParametersNapi = nullptr;
490     size_t argc = 1;
491     napi_value args[1] = { nullptr };
492     napi_value jsThis = nullptr;
493     int32_t audioChannel;
494     napi_value jsResult = nullptr;
495     napi_get_undefined(env, &jsResult);
496 
497     status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
498     if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
499         HiLog::Error(LABEL, "set audio channel fail to napi_get_cb_info");
500         return jsResult;
501     }
502 
503     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
504     if (status == napi_ok) {
505         napi_valuetype valueType = napi_undefined;
506         if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
507             HiLog::Error(LABEL, "set audio channel fail: wrong data type");
508             return jsResult;
509         }
510     }
511 
512     status = napi_get_value_int32(env, args[0], &audioChannel);
513     if (status == napi_ok) {
514         audioParametersNapi->audioParameters_->channels = static_cast<AudioChannel>(audioChannel);
515     }
516 
517     return jsResult;
518 }
519 
GetAudioSamplingRate(napi_env env,napi_callback_info info)520 napi_value AudioParametersNapi::GetAudioSamplingRate(napi_env env, napi_callback_info info)
521 {
522     napi_status status;
523     AudioParametersNapi *audioParametersNapi = nullptr;
524     size_t argc = 0;
525     napi_value jsThis = nullptr;
526     AudioSamplingRate samplingRate;
527     napi_value jsResult = nullptr;
528     napi_get_undefined(env, &jsResult);
529 
530     status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
531     if (status != napi_ok || jsThis == nullptr) {
532         HiLog::Error(LABEL, "Get sampling rate fail to napi_get_cb_info");
533         return jsResult;
534     }
535 
536     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
537     if (status == napi_ok) {
538         samplingRate = audioParametersNapi->audioParameters_->samplingRate;
539         status = napi_create_int32(env, samplingRate, &jsResult);
540         if (status == napi_ok) {
541             return jsResult;
542         }
543     }
544 
545     return jsResult;
546 }
547 
SetAudioSamplingRate(napi_env env,napi_callback_info info)548 napi_value AudioParametersNapi::SetAudioSamplingRate(napi_env env, napi_callback_info info)
549 {
550     napi_status status;
551     AudioParametersNapi *audioParametersNapi = nullptr;
552     size_t argc = 1;
553     napi_value args[1] = { nullptr };
554     napi_value jsThis = nullptr;
555     int32_t samplingRate;
556     napi_value jsResult = nullptr;
557     napi_get_undefined(env, &jsResult);
558 
559     status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
560     if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
561         HiLog::Error(LABEL, "set sampling rate fail to napi_get_cb_info");
562         return jsResult;
563     }
564 
565     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
566     if (status == napi_ok) {
567         napi_valuetype valueType = napi_undefined;
568         if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
569             HiLog::Error(LABEL, "set sampling rate fail: wrong data type");
570             return jsResult;
571         }
572     }
573 
574     status = napi_get_value_int32(env, args[0], &samplingRate);
575     if (status == napi_ok) {
576         audioParametersNapi->audioParameters_->samplingRate = static_cast<AudioSamplingRate>(samplingRate);
577     }
578 
579     return jsResult;
580 }
581 
GetAudioEncodingType(napi_env env,napi_callback_info info)582 napi_value AudioParametersNapi::GetAudioEncodingType(napi_env env, napi_callback_info info)
583 {
584     napi_status status;
585     AudioParametersNapi *audioParametersNapi = nullptr;
586     size_t argc = 0;
587     napi_value jsThis = nullptr;
588     AudioEncodingType encodingType;
589     napi_value jsResult = nullptr;
590     napi_get_undefined(env, &jsResult);
591 
592     status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
593     if (status != napi_ok || jsThis == nullptr) {
594         HiLog::Error(LABEL, "Get encoding type fail to napi_get_cb_info");
595         return jsResult;
596     }
597 
598     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
599     if (status == napi_ok) {
600         encodingType = audioParametersNapi->audioParameters_->encoding;
601         status = napi_create_int32(env, encodingType, &jsResult);
602         if (status == napi_ok) {
603             return jsResult;
604         }
605     }
606 
607     return jsResult;
608 }
609 
SetAudioEncodingType(napi_env env,napi_callback_info info)610 napi_value AudioParametersNapi::SetAudioEncodingType(napi_env env, napi_callback_info info)
611 {
612     napi_status status;
613     AudioParametersNapi *audioParametersNapi = nullptr;
614     size_t argc = 1;
615     napi_value args[1] = { nullptr };
616     napi_value jsThis = nullptr;
617     int32_t encodingType;
618     napi_value jsResult = nullptr;
619     napi_get_undefined(env, &jsResult);
620 
621     status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
622     if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
623         HiLog::Error(LABEL, "set audio encoding type fail to napi_get_cb_info");
624         return jsResult;
625     }
626 
627     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
628     if (status == napi_ok) {
629         napi_valuetype valueType = napi_undefined;
630         if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
631             HiLog::Error(LABEL, "set audio encoding type fail: wrong data type");
632             return jsResult;
633         }
634     }
635 
636     status = napi_get_value_int32(env, args[0], &encodingType);
637     if (status == napi_ok) {
638         audioParametersNapi->audioParameters_->encoding = static_cast<AudioEncodingType>(encodingType);
639     }
640 
641     return jsResult;
642 }
643 
GetContentType(napi_env env,napi_callback_info info)644 napi_value AudioParametersNapi::GetContentType(napi_env env, napi_callback_info info)
645 {
646     napi_status status;
647     AudioParametersNapi *audioParametersNapi = nullptr;
648     size_t argc = 0;
649     napi_value jsThis = nullptr;
650     ContentType contentType;
651     napi_value jsResult = nullptr;
652     napi_get_undefined(env, &jsResult);
653 
654     status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
655     if (status != napi_ok || jsThis == nullptr) {
656         HiLog::Error(LABEL, "Get content type fail to napi_get_cb_info");
657         return jsResult;
658     }
659 
660     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
661     if (status == napi_ok) {
662         contentType = audioParametersNapi->audioParameters_->contentType;
663         status = napi_create_int32(env, contentType, &jsResult);
664         if (status == napi_ok) {
665             return jsResult;
666         }
667     }
668 
669     return jsResult;
670 }
671 
SetContentType(napi_env env,napi_callback_info info)672 napi_value AudioParametersNapi::SetContentType(napi_env env, napi_callback_info info)
673 {
674     napi_status status;
675     AudioParametersNapi *audioParametersNapi = nullptr;
676     size_t argc = 1;
677     napi_value args[1] = { nullptr };
678     napi_value jsThis = nullptr;
679     int32_t contentType;
680     napi_value jsResult = nullptr;
681     napi_get_undefined(env, &jsResult);
682 
683     status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
684     if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
685         HiLog::Error(LABEL, "set content type fail to napi_get_cb_info");
686         return jsResult;
687     }
688 
689     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
690     if (status == napi_ok) {
691         napi_valuetype valueType = napi_undefined;
692         if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
693             HiLog::Error(LABEL, "set content type fail: wrong data type");
694             return jsResult;
695         }
696     }
697 
698     status = napi_get_value_int32(env, args[0], &contentType);
699     if (status == napi_ok) {
700         audioParametersNapi->audioParameters_->contentType = static_cast<ContentType>(contentType);
701     }
702 
703     return jsResult;
704 }
705 
GetStreamUsage(napi_env env,napi_callback_info info)706 napi_value AudioParametersNapi::GetStreamUsage(napi_env env, napi_callback_info info)
707 {
708     napi_status status;
709     AudioParametersNapi *audioParametersNapi = nullptr;
710     size_t argc = 0;
711     napi_value jsThis = nullptr;
712     StreamUsage usage;
713     napi_value jsResult = nullptr;
714     napi_get_undefined(env, &jsResult);
715 
716     status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
717     if (status != napi_ok || jsThis == nullptr) {
718         HiLog::Error(LABEL, "Get stream usage fail to napi_get_cb_info");
719         return jsResult;
720     }
721 
722     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
723     if (status == napi_ok) {
724         usage = audioParametersNapi->audioParameters_->usage;
725         status = napi_create_int32(env, usage, &jsResult);
726         if (status == napi_ok) {
727             return jsResult;
728         }
729     }
730 
731     return jsResult;
732 }
733 
SetStreamUsage(napi_env env,napi_callback_info info)734 napi_value AudioParametersNapi::SetStreamUsage(napi_env env, napi_callback_info info)
735 {
736     napi_status status;
737     AudioParametersNapi *audioParametersNapi = nullptr;
738     size_t argc = 1;
739     napi_value args[1] = { nullptr };
740     napi_value jsThis = nullptr;
741     int32_t usage;
742     napi_value jsResult = nullptr;
743     napi_get_undefined(env, &jsResult);
744 
745     status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
746     if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
747         HiLog::Error(LABEL, "set stream usage fail to napi_get_cb_info");
748         return jsResult;
749     }
750 
751     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
752     if (status == napi_ok) {
753         napi_valuetype valueType = napi_undefined;
754         if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
755             HiLog::Error(LABEL, "set stream usage fail: wrong data type");
756             return jsResult;
757         }
758     }
759 
760     status = napi_get_value_int32(env, args[0], &usage);
761     if (status == napi_ok) {
762         audioParametersNapi->audioParameters_->usage = static_cast<StreamUsage>(usage);
763     }
764 
765     return jsResult;
766 }
767 
GetDeviceRole(napi_env env,napi_callback_info info)768 napi_value AudioParametersNapi::GetDeviceRole(napi_env env, napi_callback_info info)
769 {
770     napi_status status;
771     AudioParametersNapi *audioParametersNapi = nullptr;
772     size_t argc = 0;
773     napi_value jsThis = nullptr;
774     DeviceRole deviceRole;
775     napi_value jsResult = nullptr;
776     napi_get_undefined(env, &jsResult);
777 
778     status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
779     if (status != napi_ok || jsThis == nullptr) {
780         HiLog::Error(LABEL, "Get device role fail to napi_get_cb_info");
781         return jsResult;
782     }
783 
784     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
785     if (status == napi_ok) {
786         deviceRole = audioParametersNapi->audioParameters_->deviceRole;
787         status = napi_create_int32(env, deviceRole, &jsResult);
788         if (status == napi_ok) {
789             return jsResult;
790         }
791     }
792 
793     return jsResult;
794 }
795 
SetDeviceRole(napi_env env,napi_callback_info info)796 napi_value AudioParametersNapi::SetDeviceRole(napi_env env, napi_callback_info info)
797 {
798     napi_status status;
799     AudioParametersNapi *audioParametersNapi = nullptr;
800     size_t argc = 1;
801     napi_value args[1] = { nullptr };
802     napi_value jsThis = nullptr;
803     int32_t deviceRole;
804     napi_value jsResult = nullptr;
805     napi_get_undefined(env, &jsResult);
806 
807     status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
808     if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
809         HiLog::Error(LABEL, "set device role fail to napi_get_cb_info");
810         return jsResult;
811     }
812 
813     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
814     if (status == napi_ok) {
815         napi_valuetype valueType = napi_undefined;
816         if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
817             HiLog::Error(LABEL, "set device role fail: wrong data type");
818             return jsResult;
819         }
820     }
821 
822     status = napi_get_value_int32(env, args[0], &deviceRole);
823     if (status == napi_ok) {
824         audioParametersNapi->audioParameters_->deviceRole = static_cast<DeviceRole>(deviceRole);
825     }
826 
827     return jsResult;
828 }
829 
GetDeviceType(napi_env env,napi_callback_info info)830 napi_value AudioParametersNapi::GetDeviceType(napi_env env, napi_callback_info info)
831 {
832     napi_status status;
833     AudioParametersNapi *audioParametersNapi = nullptr;
834     size_t argc = 0;
835     napi_value jsThis = nullptr;
836     DeviceType deviceType;
837     napi_value jsResult = nullptr;
838     napi_get_undefined(env, &jsResult);
839 
840     status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
841     if (status != napi_ok || jsThis == nullptr) {
842         HiLog::Error(LABEL, "Get device type fail to napi_get_cb_info");
843         return jsResult;
844     }
845 
846     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
847     if (status == napi_ok) {
848         deviceType = audioParametersNapi->audioParameters_->deviceType;
849         HiLog::Info(LABEL, "get device type: %d", deviceType);
850         status = napi_create_int32(env, deviceType, &jsResult);
851         if (status == napi_ok) {
852             return jsResult;
853         }
854     }
855 
856     return jsResult;
857 }
858 
SetDeviceType(napi_env env,napi_callback_info info)859 napi_value AudioParametersNapi::SetDeviceType(napi_env env, napi_callback_info info)
860 {
861     napi_status status;
862     AudioParametersNapi *audioParametersNapi = nullptr;
863     size_t argc = 1;
864     napi_value args[1] = { nullptr };
865     napi_value jsThis = nullptr;
866     int32_t deviceType;
867     napi_value jsResult = nullptr;
868     napi_get_undefined(env, &jsResult);
869 
870     status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
871     if ((status != napi_ok) || (jsThis == nullptr) || (args[0] == nullptr)) {
872         HiLog::Error(LABEL, "set device type fail to napi_get_cb_info");
873         return jsResult;
874     }
875 
876     status = napi_unwrap(env, jsThis, (void **)&audioParametersNapi);
877     if (status == napi_ok) {
878         napi_valuetype valueType = napi_undefined;
879         if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_number) {
880             HiLog::Error(LABEL, "set device type fail: wrong data type");
881             return jsResult;
882         }
883     }
884 
885     status = napi_get_value_int32(env, args[0], &deviceType);
886     if (status == napi_ok) {
887         audioParametersNapi->audioParameters_->deviceType = static_cast<DeviceType>(deviceType);
888         HiLog::Info(LABEL, "set device type: %d", audioParametersNapi->audioParameters_->deviceType);
889     }
890 
891     return jsResult;
892 }
893 }  // namespace AudioStandard
894 }  // namespace OHOS
895