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