• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef LOG_TAG
16 #define LOG_TAG "NapiParamUtils"
17 #endif
18 
19 #include "napi_param_utils.h"
20 #include "napi_audio_enum.h"
21 #include "audio_effect.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
25 
26 const std::vector<DeviceRole> DEVICE_ROLE_SET = {
27     DEVICE_ROLE_NONE,
28     INPUT_DEVICE,
29     OUTPUT_DEVICE
30 };
31 
32 const std::vector<DeviceType> DEVICE_TYPE_SET = {
33     DEVICE_TYPE_NONE,
34     DEVICE_TYPE_INVALID,
35     DEVICE_TYPE_EARPIECE,
36     DEVICE_TYPE_SPEAKER,
37     DEVICE_TYPE_WIRED_HEADSET,
38     DEVICE_TYPE_WIRED_HEADPHONES,
39     DEVICE_TYPE_BLUETOOTH_SCO,
40     DEVICE_TYPE_BLUETOOTH_A2DP,
41     DEVICE_TYPE_MIC,
42     DEVICE_TYPE_WAKEUP,
43     DEVICE_TYPE_USB_HEADSET,
44     DEVICE_TYPE_DP,
45     DEVICE_TYPE_REMOTE_CAST,
46     DEVICE_TYPE_USB_DEVICE,
47     DEVICE_TYPE_REMOTE_DAUDIO,
48     DEVICE_TYPE_USB_ARM_HEADSET,
49     DEVICE_TYPE_FILE_SINK,
50     DEVICE_TYPE_FILE_SOURCE,
51     DEVICE_TYPE_EXTERN_CABLE,
52     DEVICE_TYPE_HDMI,
53     DEVICE_TYPE_DEFAULT
54 };
55 
GetUndefinedValue(napi_env env)56 napi_value NapiParamUtils::GetUndefinedValue(napi_env env)
57 {
58     napi_value result {};
59     napi_get_undefined(env, &result);
60     return result;
61 }
62 
GetParam(const napi_env & env,napi_callback_info info,size_t & argc,napi_value * args)63 napi_status NapiParamUtils::GetParam(const napi_env &env, napi_callback_info info, size_t &argc, napi_value *args)
64 {
65     napi_value thisVar = nullptr;
66     void *data;
67     return napi_get_cb_info(env, info, &argc, args, &thisVar, &data);
68 }
69 
GetValueInt32(const napi_env & env,int32_t & value,napi_value in)70 napi_status NapiParamUtils::GetValueInt32(const napi_env &env, int32_t &value, napi_value in)
71 {
72     napi_status status = napi_get_value_int32(env, in, &value);
73     CHECK_AND_RETURN_RET_PRELOG(status == napi_ok, status, "GetValueInt32 napi_get_value_int32 failed");
74     return status;
75 }
76 
SetValueInt32(const napi_env & env,const int32_t & value,napi_value & result)77 napi_status NapiParamUtils::SetValueInt32(const napi_env &env, const int32_t &value, napi_value &result)
78 {
79     napi_status status = napi_create_int32(env, value, &result);
80     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32 napi_create_int32 failed");
81     return status;
82 }
83 
GetValueInt32(const napi_env & env,const std::string & fieldStr,int32_t & value,napi_value in)84 napi_status NapiParamUtils::GetValueInt32(const napi_env &env, const std::string &fieldStr,
85     int32_t &value, napi_value in)
86 {
87     napi_value jsValue = nullptr;
88     napi_status status = napi_get_named_property(env, in, fieldStr.c_str(), &jsValue);
89     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueInt32 napi_get_named_property failed");
90     status = GetValueInt32(env, value, jsValue);
91     return status;
92 }
93 
SetValueInt32(const napi_env & env,const std::string & fieldStr,const int32_t value,napi_value & result)94 napi_status NapiParamUtils::SetValueInt32(const napi_env &env, const std::string &fieldStr,
95     const int32_t value, napi_value &result)
96 {
97     napi_value jsValue = nullptr;
98     napi_status status = SetValueInt32(env, value, jsValue);
99     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32 napi_create_int32 failed");
100     status = napi_set_named_property(env, result, fieldStr.c_str(), jsValue);
101     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32 napi_create_int32 failed");
102     return status;
103 }
104 
GetValueUInt32(const napi_env & env,uint32_t & value,napi_value in)105 napi_status NapiParamUtils::GetValueUInt32(const napi_env &env, uint32_t &value, napi_value in)
106 {
107     napi_status status = napi_get_value_uint32(env, in, &value);
108     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueUInt32 napi_get_value_uint32 failed");
109     return status;
110 }
111 
SetValueUInt32(const napi_env & env,const uint32_t & value,napi_value & result)112 napi_status NapiParamUtils::SetValueUInt32(const napi_env &env, const uint32_t &value, napi_value &result)
113 {
114     napi_status status = napi_create_uint32(env, value, &result);
115     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueUInt32 napi_create_uint32 failed");
116     return status;
117 }
118 
GetValueDouble(const napi_env & env,double & value,napi_value in)119 napi_status NapiParamUtils::GetValueDouble(const napi_env &env, double &value, napi_value in)
120 {
121     napi_status status = napi_get_value_double(env, in, &value);
122     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueDouble napi_get_value_double failed");
123     return status;
124 }
125 
SetValueDouble(const napi_env & env,const double & value,napi_value & result)126 napi_status NapiParamUtils::SetValueDouble(const napi_env &env, const double &value, napi_value &result)
127 {
128     napi_status status = napi_create_double(env, value, &result);
129     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueDouble napi_create_double failed");
130     return status;
131 }
132 
GetValueDouble(const napi_env & env,const std::string & fieldStr,double & value,napi_value in)133 napi_status NapiParamUtils::GetValueDouble(const napi_env &env, const std::string &fieldStr,
134     double &value, napi_value in)
135 {
136     napi_value jsValue = nullptr;
137     napi_status status = napi_get_named_property(env, in, fieldStr.c_str(), &jsValue);
138     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueDouble napi_get_named_property failed");
139     status = GetValueDouble(env, value, jsValue);
140     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueDouble failed");
141     return status;
142 }
143 
SetValueDouble(const napi_env & env,const std::string & fieldStr,const double value,napi_value & result)144 napi_status NapiParamUtils::SetValueDouble(const napi_env &env, const std::string &fieldStr,
145     const double value, napi_value &result)
146 {
147     napi_value jsValue = nullptr;
148     napi_status status = SetValueDouble(env, value, jsValue);
149     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueDouble SetValueDouble failed");
150     status = napi_set_named_property(env, result, fieldStr.c_str(), jsValue);
151     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueDouble napi_set_named_property failed");
152     return status;
153 }
154 
GetPropertyString(napi_env env,napi_value value,const std::string & fieldStr)155 std::string NapiParamUtils::GetPropertyString(napi_env env, napi_value value, const std::string &fieldStr)
156 {
157     std::string invalid = "";
158     bool exist = false;
159     napi_status status = napi_has_named_property(env, value, fieldStr.c_str(), &exist);
160     if (status != napi_ok || !exist) {
161         AUDIO_ERR_LOG("can not find %{public}s property", fieldStr.c_str());
162         return invalid;
163     }
164 
165     napi_value item = nullptr;
166     if (napi_get_named_property(env, value, fieldStr.c_str(), &item) != napi_ok) {
167         AUDIO_ERR_LOG("get %{public}s property fail", fieldStr.c_str());
168         return invalid;
169     }
170 
171     return GetStringArgument(env, item);
172 }
173 
GetStringArgument(napi_env env,napi_value value)174 std::string NapiParamUtils::GetStringArgument(napi_env env, napi_value value)
175 {
176     std::string strValue = "";
177     size_t bufLength = 0;
178     napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &bufLength);
179     if (status == napi_ok && bufLength > 0 && bufLength < PATH_MAX) {
180         strValue.reserve(bufLength + 1);
181         strValue.resize(bufLength);
182         status = napi_get_value_string_utf8(env, value, strValue.data(), bufLength + 1, &bufLength);
183         if (status == napi_ok) {
184             AUDIO_DEBUG_LOG("argument = %{public}s", strValue.c_str());
185         }
186     }
187     return strValue;
188 }
189 
SetValueString(const napi_env & env,const std::string & stringValue,napi_value & result)190 napi_status NapiParamUtils::SetValueString(const napi_env &env, const std::string &stringValue, napi_value &result)
191 {
192     napi_status status = napi_create_string_utf8(env, stringValue.c_str(), NAPI_AUTO_LENGTH, &result);
193     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueString napi_create_string_utf8 failed");
194     return status;
195 }
196 
SetValueString(const napi_env & env,const std::string & fieldStr,const std::string & stringValue,napi_value & result)197 napi_status NapiParamUtils::SetValueString(const napi_env &env, const std::string &fieldStr,
198     const std::string &stringValue, napi_value &result)
199 {
200     napi_value value = nullptr;
201     napi_status status = SetValueString(env, stringValue.c_str(), value);
202     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueString failed");
203     status = napi_set_named_property(env, result, fieldStr.c_str(), value);
204     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueString napi_set_named_property failed");
205     return status;
206 }
207 
GetValueBoolean(const napi_env & env,bool & boolValue,napi_value in)208 napi_status NapiParamUtils::GetValueBoolean(const napi_env &env, bool &boolValue, napi_value in)
209 {
210     napi_status status = napi_get_value_bool(env, in, &boolValue);
211     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueBoolean napi_get_boolean failed");
212     return status;
213 }
214 
SetValueBoolean(const napi_env & env,const bool boolValue,napi_value & result)215 napi_status NapiParamUtils::SetValueBoolean(const napi_env &env, const bool boolValue, napi_value &result)
216 {
217     napi_status status = napi_get_boolean(env, boolValue, &result);
218     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueBoolean napi_get_boolean failed");
219     return status;
220 }
221 
GetValueBoolean(const napi_env & env,const std::string & fieldStr,bool & boolValue,napi_value in)222 napi_status NapiParamUtils::GetValueBoolean(const napi_env &env, const std::string &fieldStr,
223     bool &boolValue, napi_value in)
224 {
225     napi_value jsValue = nullptr;
226     napi_status status = napi_get_named_property(env, in, fieldStr.c_str(), &jsValue);
227     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueBoolean napi_get_named_property failed");
228     status = GetValueBoolean(env, boolValue, jsValue);
229     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueBoolean failed");
230     return status;
231 }
232 
SetValueBoolean(const napi_env & env,const std::string & fieldStr,const bool boolValue,napi_value & result)233 napi_status NapiParamUtils::SetValueBoolean(const napi_env &env, const std::string &fieldStr,
234     const bool boolValue, napi_value &result)
235 {
236     napi_value value = nullptr;
237     napi_status status = SetValueBoolean(env, boolValue, value);
238     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueBoolean SetValueBoolean failed");
239     status = napi_set_named_property(env, result, fieldStr.c_str(), value);
240     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "napi_set_named_property failed");
241     return status;
242 }
243 
GetValueInt64(const napi_env & env,int64_t & value,napi_value in)244 napi_status NapiParamUtils::GetValueInt64(const napi_env &env, int64_t &value, napi_value in)
245 {
246     napi_status status = napi_get_value_int64(env, in, &value);
247     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueInt64 napi_get_value_int64 failed");
248     return status;
249 }
250 
SetValueInt64(const napi_env & env,const int64_t & value,napi_value & result)251 napi_status NapiParamUtils::SetValueInt64(const napi_env &env, const int64_t &value, napi_value &result)
252 {
253     napi_status status = napi_create_int64(env, value, &result);
254     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt64 napi_create_int64 failed");
255     return status;
256 }
257 
GetValueInt64(const napi_env & env,const std::string & fieldStr,int64_t & value,napi_value in)258 napi_status NapiParamUtils::GetValueInt64(const napi_env &env, const std::string &fieldStr,
259     int64_t &value, napi_value in)
260 {
261     napi_value jsValue = nullptr;
262     napi_status status = napi_get_named_property(env, in, fieldStr.c_str(), &jsValue);
263     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueInt64 napi_get_named_property failed");
264     status = GetValueInt64(env, value, jsValue);
265     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueInt64 failed");
266     return status;
267 }
268 
SetValueInt64(const napi_env & env,const std::string & fieldStr,const int64_t value,napi_value & result)269 napi_status NapiParamUtils::SetValueInt64(const napi_env &env, const std::string &fieldStr,
270     const int64_t value, napi_value &result)
271 {
272     napi_value jsValue = nullptr;
273     napi_status status = SetValueInt64(env, value, jsValue);
274     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt64 failed");
275     status = napi_set_named_property(env, result, fieldStr.c_str(), jsValue);
276     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt64 napi_set_named_property failed");
277     return status;
278 }
279 
GetArrayBuffer(const napi_env & env,void * & data,size_t & length,napi_value in)280 napi_status NapiParamUtils::GetArrayBuffer(const napi_env &env, void* &data, size_t &length, napi_value in)
281 {
282     napi_status status = napi_get_arraybuffer_info(env, in, &data, &length);
283     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetArrayBuffer napi_get_arraybuffer_info failed");
284     return status;
285 }
286 
CreateArrayBuffer(const napi_env & env,const std::string & fieldStr,size_t bufferLen,uint8_t * bufferData,napi_value & result)287 napi_status NapiParamUtils::CreateArrayBuffer(const napi_env &env, const std::string &fieldStr, size_t bufferLen,
288     uint8_t *bufferData, napi_value &result)
289 {
290     napi_value value = nullptr;
291 
292     napi_status status = napi_create_arraybuffer(env, bufferLen, (void**)&bufferData, &value);
293     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "napi_create_arraybuffer failed");
294     status = napi_set_named_property(env, result, fieldStr.c_str(), value);
295     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "napi_set_named_property failed");
296 
297     return status;
298 }
299 
CreateArrayBuffer(const napi_env & env,const size_t bufferLen,const uint8_t * bufferData,napi_value & result)300 napi_status NapiParamUtils::CreateArrayBuffer(const napi_env &env, const size_t bufferLen,
301     const uint8_t *bufferData, napi_value &result)
302 {
303     uint8_t *native = nullptr;
304     napi_status status = napi_create_arraybuffer(env, bufferLen, reinterpret_cast<void **>(&native), &result);
305     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "napi_create_arraybuffer failed");
306     if (memcpy_s(native, bufferLen, bufferData, bufferLen)) {
307         result = nullptr;
308     }
309     return status;
310 }
311 
ConvertDeviceInfoToAudioDeviceDescriptor(std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor,const AudioDeviceDescriptor & deviceInfo)312 void NapiParamUtils::ConvertDeviceInfoToAudioDeviceDescriptor(
313     std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor, const AudioDeviceDescriptor &deviceInfo)
314 {
315     CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "audioDeviceDescriptor is nullptr");
316     audioDeviceDescriptor->deviceRole_ = deviceInfo.deviceRole_;
317     audioDeviceDescriptor->deviceType_ = deviceInfo.deviceType_;
318     audioDeviceDescriptor->deviceId_ = deviceInfo.deviceId_;
319     audioDeviceDescriptor->channelMasks_ = deviceInfo.channelMasks_;
320     audioDeviceDescriptor->channelIndexMasks_ = deviceInfo.channelIndexMasks_;
321     audioDeviceDescriptor->deviceName_ = deviceInfo.deviceName_;
322     audioDeviceDescriptor->macAddress_ = deviceInfo.macAddress_;
323     audioDeviceDescriptor->interruptGroupId_ = deviceInfo.interruptGroupId_;
324     audioDeviceDescriptor->volumeGroupId_ = deviceInfo.volumeGroupId_;
325     audioDeviceDescriptor->networkId_ = deviceInfo.networkId_;
326     audioDeviceDescriptor->displayName_ = deviceInfo.displayName_;
327     audioDeviceDescriptor->audioStreamInfo_.samplingRate = deviceInfo.audioStreamInfo_.samplingRate;
328     audioDeviceDescriptor->audioStreamInfo_.encoding = deviceInfo.audioStreamInfo_.encoding;
329     audioDeviceDescriptor->audioStreamInfo_.format = deviceInfo.audioStreamInfo_.format;
330     audioDeviceDescriptor->audioStreamInfo_.channels = deviceInfo.audioStreamInfo_.channels;
331 }
332 
GetRendererOptions(const napi_env & env,AudioRendererOptions * opts,napi_value in)333 napi_status NapiParamUtils::GetRendererOptions(const napi_env &env, AudioRendererOptions *opts, napi_value in)
334 {
335     napi_value res = nullptr;
336 
337     napi_status status = napi_get_named_property(env, in, "rendererInfo", &res);
338     if (status == napi_ok) {
339         GetRendererInfo(env, &(opts->rendererInfo), res);
340     }
341 
342     status = napi_get_named_property(env, in, "streamInfo", &res);
343     if (status == napi_ok) {
344         GetStreamInfo(env, &(opts->streamInfo), res);
345     }
346 
347     int32_t intValue = {};
348     status = GetValueInt32(env, "privacyType", intValue, in);
349     if (status == napi_ok) {
350         opts->privacyType = static_cast<AudioPrivacyType>(intValue);
351     }
352 
353     return napi_ok;
354 }
355 
GetRendererInfo(const napi_env & env,AudioRendererInfo * rendererInfo,napi_value in)356 napi_status NapiParamUtils::GetRendererInfo(const napi_env &env, AudioRendererInfo *rendererInfo, napi_value in)
357 {
358     napi_valuetype valueType = napi_undefined;
359     napi_typeof(env, in, &valueType);
360     CHECK_AND_RETURN_RET_LOG(valueType == napi_object, napi_invalid_arg,
361         "GetRendererInfo failed, vauleType is not object");
362 
363     int32_t intValue = {0};
364     napi_status status = GetValueInt32(env, "content", intValue, in);
365     if (status == napi_ok) {
366         rendererInfo->contentType = static_cast<ContentType>(intValue);
367     }
368 
369     status = GetValueInt32(env, "usage", intValue, in);
370     if (status == napi_ok) {
371         if (NapiAudioEnum::IsLegalInputArgumentStreamUsage(intValue)) {
372             rendererInfo->streamUsage = static_cast<StreamUsage>(intValue);
373         } else {
374             rendererInfo->streamUsage = StreamUsage::STREAM_USAGE_INVALID;
375         }
376     }
377 
378     GetValueInt32(env, "rendererFlags", rendererInfo->rendererFlags, in);
379 
380     status = GetValueInt32(env, "volumeMode", intValue, in);
381     if (status == napi_ok) {
382         AUDIO_INFO_LOG("volume mode = %{public}d", intValue);
383         if (NapiAudioEnum::IsLegalInputArgumentVolumeMode(intValue)) {
384             rendererInfo->volumeMode = static_cast<AudioVolumeMode>(intValue);
385         } else {
386             AUDIO_INFO_LOG("AudioVolumeMode is invalid parameter");
387             return napi_invalid_arg;
388         }
389     }
390 
391     return napi_ok;
392 }
393 
SetRendererInfo(const napi_env & env,const AudioRendererInfo & rendererInfo,napi_value & result)394 napi_status NapiParamUtils::SetRendererInfo(const napi_env &env, const AudioRendererInfo &rendererInfo,
395     napi_value &result)
396 {
397     napi_status status = napi_create_object(env, &result);
398     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetRendererInfo napi_create_object failed");
399     SetValueInt32(env, "content", static_cast<int32_t>(rendererInfo.contentType), result);
400     SetValueInt32(env, "usage", static_cast<int32_t>(rendererInfo.streamUsage), result);
401     SetValueInt32(env, "rendererFlags", rendererInfo.rendererFlags, result);
402     SetValueInt32(env, "volumeMode", static_cast<int32_t>(rendererInfo.volumeMode), result);
403     return napi_ok;
404 }
405 
GetStreamInfo(const napi_env & env,AudioStreamInfo * streamInfo,napi_value in)406 napi_status NapiParamUtils::GetStreamInfo(const napi_env &env, AudioStreamInfo *streamInfo, napi_value in)
407 {
408     int32_t intValue = {0};
409     napi_status status = GetValueInt32(env, "samplingRate", intValue, in);
410     if (status == napi_ok) {
411         if (intValue >= SAMPLE_RATE_8000 && intValue <= SAMPLE_RATE_192000) {
412             streamInfo->samplingRate = static_cast<AudioSamplingRate>(intValue);
413         } else {
414             AUDIO_ERR_LOG("invaild samplingRate");
415             return napi_generic_failure;
416         }
417     }
418 
419     status = GetValueInt32(env, "channels", intValue, in);
420     if (status == napi_ok) {
421         streamInfo->channels = static_cast<AudioChannel>(intValue);
422     }
423 
424     status = GetValueInt32(env, "sampleFormat", intValue, in);
425     if (status == napi_ok) {
426         streamInfo->format = static_cast<OHOS::AudioStandard::AudioSampleFormat>(intValue);
427     }
428 
429     status = GetValueInt32(env, "encodingType", intValue, in);
430     if (status == napi_ok) {
431         streamInfo->encoding = static_cast<AudioEncodingType>(intValue);
432     }
433 
434     int64_t int64Value = 0;
435     status = GetValueInt64(env, "channelLayout", int64Value, in);
436     if (status == napi_ok) {
437         streamInfo->channelLayout = static_cast<AudioChannelLayout>(int64Value);
438     }
439 
440     return napi_ok;
441 }
442 
SetStreamInfo(const napi_env & env,const AudioStreamInfo & streamInfo,napi_value & result)443 napi_status NapiParamUtils::SetStreamInfo(const napi_env &env, const AudioStreamInfo &streamInfo, napi_value &result)
444 {
445     napi_status status = napi_create_object(env, &result);
446     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetStreamInfo napi_create_object failed");
447     SetValueInt32(env, "samplingRate", static_cast<int32_t>(streamInfo.samplingRate), result);
448     SetValueInt32(env, "channels", static_cast<int32_t>(streamInfo.channels), result);
449     SetValueInt32(env, "sampleFormat", static_cast<int32_t>(streamInfo.format), result);
450     SetValueInt32(env, "encodingType", static_cast<int32_t>(streamInfo.encoding), result);
451     SetValueInt64(env, "channelLayout", static_cast<uint64_t>(streamInfo.channelLayout), result);
452 
453     return napi_ok;
454 }
455 
SetTimeStampInfo(const napi_env & env,const Timestamp & timestamp,napi_value & result)456 napi_status NapiParamUtils::SetTimeStampInfo(const napi_env &env, const Timestamp &timestamp, napi_value &result)
457 {
458     napi_status status = napi_create_object(env, &result);
459     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetTimeStampInfo napi_create_object failed");
460     SetValueInt32(env, "framePos", static_cast<int64_t>(timestamp.framePosition), result);
461     static const int64_t secToNano = 1000000000;
462     int64_t time = timestamp.time.tv_sec * secToNano + timestamp.time.tv_nsec;
463     SetValueInt32(env, "timestamp", time, result);
464 
465     return napi_ok;
466 }
467 
SetValueInt32Element(const napi_env & env,const std::string & fieldStr,const std::vector<int32_t> & values,napi_value & result)468 napi_status NapiParamUtils::SetValueInt32Element(const napi_env &env, const std::string &fieldStr,
469     const std::vector<int32_t> &values, napi_value &result)
470 {
471     napi_value jsValues = nullptr;
472     napi_status status = napi_create_array_with_length(env, values.size(), &jsValues);
473     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32Element napi_create_array_with_length failed");
474     size_t count = 0;
475     for (const auto &value : values) {
476         napi_value jsValue = nullptr;
477         status = SetValueInt32(env, value, jsValue);
478         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32Element SetValueInt32 failed");
479         status = napi_set_element(env, jsValues, count, jsValue);
480         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32Element napi_set_element failed");
481         count++;
482     }
483     status = napi_set_named_property(env, result, fieldStr.c_str(), jsValues);
484     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32Element napi_set_named_property failed");
485     return status;
486 }
487 
SetDeviceDescriptor(const napi_env & env,const AudioDeviceDescriptor & deviceInfo,napi_value & result)488 napi_status NapiParamUtils::SetDeviceDescriptor(const napi_env &env, const AudioDeviceDescriptor &deviceInfo,
489     napi_value &result)
490 {
491     (void)napi_create_object(env, &result);
492     SetValueInt32(env, "deviceRole", static_cast<int32_t>(deviceInfo.deviceRole_), result);
493     SetValueInt32(env, "deviceType", static_cast<int32_t>(deviceInfo.deviceType_), result);
494     SetValueInt32(env, "id", static_cast<int32_t>(deviceInfo.deviceId_), result);
495     SetValueString(env, "name", deviceInfo.deviceName_, result);
496     SetValueString(env, "address", deviceInfo.macAddress_, result);
497     SetValueString(env, "networkId", deviceInfo.networkId_, result);
498     SetValueInt32(env, "dmDeviceType", static_cast<int32_t>(deviceInfo.dmDeviceType_), result);
499     SetValueString(env, "displayName", deviceInfo.displayName_, result);
500     SetValueInt32(env, "interruptGroupId", static_cast<int32_t>(deviceInfo.interruptGroupId_), result);
501     SetValueInt32(env, "volumeGroupId", static_cast<int32_t>(deviceInfo.volumeGroupId_), result);
502 
503     napi_value value = nullptr;
504     napi_value sampleRates;
505     size_t size = deviceInfo.audioStreamInfo_.samplingRate.size();
506     napi_create_array_with_length(env, size, &sampleRates);
507     size_t count = 0;
508     for (const auto &samplingRate : deviceInfo.audioStreamInfo_.samplingRate) {
509         napi_create_int32(env, samplingRate, &value);
510         napi_set_element(env, sampleRates, count, value);
511         count++;
512     }
513     napi_set_named_property(env, result, "sampleRates", sampleRates);
514 
515     napi_value channelCounts;
516     size = deviceInfo.audioStreamInfo_.channels.size();
517     napi_create_array_with_length(env, size, &channelCounts);
518     count = 0;
519     for (const auto &channels : deviceInfo.audioStreamInfo_.channels) {
520         napi_create_int32(env, channels, &value);
521         napi_set_element(env, channelCounts, count, value);
522         count++;
523     }
524     napi_set_named_property(env, result, "channelCounts", channelCounts);
525 
526     std::vector<int32_t> channelMasks_;
527     channelMasks_.push_back(deviceInfo.channelMasks_);
528     SetValueInt32Element(env, "channelMasks", channelMasks_, result);
529 
530     std::vector<int32_t> channelIndexMasks_;
531     channelIndexMasks_.push_back(deviceInfo.channelIndexMasks_);
532     SetValueInt32Element(env, "channelIndexMasks", channelIndexMasks_, result);
533 
534     std::vector<int32_t> encoding;
535     encoding.push_back(deviceInfo.audioStreamInfo_.encoding);
536     SetValueInt32Element(env, "encodingTypes", encoding, result);
537     SetValueBoolean(env, "spatializationSupported", deviceInfo.spatializationSupported_, result);
538     return napi_ok;
539 }
540 
SetDeviceDescriptors(const napi_env & env,const std::vector<std::shared_ptr<AudioDeviceDescriptor>> & deviceDescriptors,napi_value & result)541 napi_status NapiParamUtils::SetDeviceDescriptors(const napi_env &env,
542     const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &deviceDescriptors, napi_value &result)
543 {
544     napi_status status = napi_create_array_with_length(env, deviceDescriptors.size(), &result);
545     for (size_t i = 0; i < deviceDescriptors.size(); i++) {
546         if (deviceDescriptors[i] != nullptr) {
547             napi_value valueParam = nullptr;
548             SetDeviceDescriptor(env, deviceDescriptors[i], valueParam);
549             napi_set_element(env, result, i, valueParam);
550         }
551     }
552     return status;
553 }
SetAudioSpatialEnabledStateForDevice(const napi_env & env,const AudioSpatialEnabledStateForDevice audioSpatialEnabledStateForDevice,napi_value & result)554 napi_status NapiParamUtils::SetAudioSpatialEnabledStateForDevice(const napi_env &env,
555     const AudioSpatialEnabledStateForDevice audioSpatialEnabledStateForDevice, napi_value &result)
556 {
557     (void)napi_create_object(env, &result);
558     napi_value jsArray;
559     NapiParamUtils::SetDeviceDescriptor(env, audioSpatialEnabledStateForDevice.deviceDescriptor, jsArray);
560     napi_set_named_property(env, result, "deviceDescriptor", jsArray);
561 
562     NapiParamUtils::SetValueBoolean(env, "enabled", audioSpatialEnabledStateForDevice.enabled, result);
563     return napi_ok;
564 }
565 
SetValueDeviceInfo(const napi_env & env,const AudioDeviceDescriptor & deviceInfo,napi_value & result)566 napi_status NapiParamUtils::SetValueDeviceInfo(const napi_env &env, const AudioDeviceDescriptor &deviceInfo,
567     napi_value &result)
568 {
569     std::vector<std::shared_ptr<AudioDeviceDescriptor>> deviceDescriptors;
570     std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor = std::make_shared<AudioDeviceDescriptor>();
571     CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptor != nullptr, napi_generic_failure,
572         "audioDeviceDescriptor malloc failed");
573     ConvertDeviceInfoToAudioDeviceDescriptor(audioDeviceDescriptor, deviceInfo);
574     deviceDescriptors.push_back(std::move(audioDeviceDescriptor));
575     SetDeviceDescriptors(env, deviceDescriptors, result);
576     return napi_ok;
577 }
578 
SetInterruptEvent(const napi_env & env,const InterruptEvent & interruptEvent,napi_value & result)579 napi_status NapiParamUtils::SetInterruptEvent(const napi_env &env, const InterruptEvent &interruptEvent,
580     napi_value &result)
581 {
582     napi_create_object(env, &result);
583     SetValueInt32(env, "eventType", static_cast<int32_t>(interruptEvent.eventType), result);
584     SetValueInt32(env, "forceType", static_cast<int32_t>(interruptEvent.forceType), result);
585     SetValueInt32(env, "hintType", static_cast<int32_t>(interruptEvent.hintType), result);
586     return napi_ok;
587 }
588 
SetNativeAudioRendererDataInfo(const napi_env & env,const AudioRendererDataInfo & audioRendererDataInfo,napi_value & result)589 napi_status NapiParamUtils::SetNativeAudioRendererDataInfo(const napi_env &env,
590     const AudioRendererDataInfo &audioRendererDataInfo, napi_value &result)
591 {
592     napi_status status = napi_create_object(env, &result);
593 
594     SetValueInt32(env, "flag", static_cast<int32_t>(audioRendererDataInfo.flag), result);
595     CreateArrayBuffer(env, "buffer", audioRendererDataInfo.flag, audioRendererDataInfo.buffer, result);
596 
597     return status;
598 }
599 
GetCapturerInfo(const napi_env & env,AudioCapturerInfo * capturerInfo,napi_value in)600 napi_status NapiParamUtils::GetCapturerInfo(const napi_env &env, AudioCapturerInfo *capturerInfo, napi_value in)
601 {
602     int32_t intValue = {0};
603     napi_status status = NapiParamUtils::GetValueInt32(env, "source", intValue, in);
604     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetCapturerInfo GetValueInt32 source failed");
605     CHECK_AND_RETURN_RET_LOG(NapiAudioEnum::IsLegalCapturerType(intValue),
606         napi_generic_failure, "Invailed captureType");
607     capturerInfo->sourceType = static_cast<SourceType>(intValue);
608 
609     status = NapiParamUtils::GetValueInt32(env, "capturerFlags", capturerInfo->capturerFlags, in);
610     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetCapturerInfo GetValueInt32 capturerFlags failed");
611     return status;
612 }
613 
SetCapturerInfo(const napi_env & env,const AudioCapturerInfo & capturerInfo,napi_value & result)614 napi_status NapiParamUtils::SetCapturerInfo(const napi_env &env,
615     const AudioCapturerInfo &capturerInfo, napi_value &result)
616 {
617     napi_status status = napi_create_object(env, &result);
618     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetCapturerInfo napi_create_object failed");
619     SetValueInt32(env, "source", static_cast<int32_t>(capturerInfo.sourceType), result);
620     SetValueInt32(env, "capturerFlags", static_cast<int32_t>(capturerInfo.capturerFlags), result);
621 
622     return napi_ok;
623 }
624 
GetCaptureFilterOptionsVector(const napi_env & env,CaptureFilterOptions * filterOptions,napi_value in)625 napi_status NapiParamUtils::GetCaptureFilterOptionsVector(const napi_env &env,
626     CaptureFilterOptions *filterOptions, napi_value in)
627 {
628     napi_value usagesValue = nullptr;
629     napi_status status = napi_get_named_property(env, in, "usages", &usagesValue);
630     CHECK_AND_RETURN_RET_LOG(status == napi_ok, napi_ok, "GetUsages failed");
631 
632     uint32_t arrayLen = 0;
633     napi_get_array_length(env, usagesValue, &arrayLen);
634 
635     if (arrayLen == 0) {
636         filterOptions->usages = {};
637         AUDIO_INFO_LOG("ParseCaptureFilterOptions get empty usage");
638         return napi_ok;
639     }
640 
641     for (size_t i = 0; i < static_cast<size_t>(arrayLen); i++) {
642         napi_value element;
643         if (napi_get_element(env, usagesValue, i, &element) == napi_ok) {
644             int32_t val = {0};
645             napi_get_value_int32(env, element, &val);
646             filterOptions->usages.emplace_back(static_cast<StreamUsage>(val));
647         }
648     }
649 
650     return napi_ok;
651 }
652 
GetPlaybackCaptureConfig(const napi_env & env,AudioPlaybackCaptureConfig * captureConfig,napi_value in)653 napi_status NapiParamUtils::GetPlaybackCaptureConfig(const napi_env &env,
654     AudioPlaybackCaptureConfig *captureConfig, napi_value in)
655 {
656     napi_value res = nullptr;
657 
658     if (napi_get_named_property(env, in, "filterOptions", &res) == napi_ok) {
659         return GetCaptureFilterOptionsVector(env, &(captureConfig->filterOptions), res);
660     }
661 
662     return NapiParamUtils::GetCaptureFilterOptionsVector(env, &(captureConfig->filterOptions), res);
663 }
664 
GetCapturerOptions(const napi_env & env,AudioCapturerOptions * opts,napi_value in)665 napi_status NapiParamUtils::GetCapturerOptions(const napi_env &env, AudioCapturerOptions *opts, napi_value in)
666 {
667     napi_value result = nullptr;
668     napi_status status = napi_get_named_property(env, in, "streamInfo", &result);
669     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get streamInfo name failed");
670 
671     status = GetStreamInfo(env, &(opts->streamInfo), result);
672     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "ParseStreamInfo failed");
673 
674     status = napi_get_named_property(env, in, "capturerInfo", &result);
675     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get capturerInfo name failed");
676 
677     status = GetCapturerInfo(env, &(opts->capturerInfo), result);
678     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "ParseCapturerInfo failed");
679 
680     if (napi_get_named_property(env, in, "playbackCaptureConfig", &result) == napi_ok) {
681         return GetPlaybackCaptureConfig(env, &(opts->playbackCaptureConfig), result);
682     }
683 
684     AUDIO_INFO_LOG("ParseCapturerOptions, without playbackCaptureConfig");
685     return napi_ok;
686 }
687 
SetAudioCapturerChangeInfoDescriptors(const napi_env & env,const AudioCapturerChangeInfo & changeInfo,napi_value & result)688 napi_status NapiParamUtils::SetAudioCapturerChangeInfoDescriptors(const napi_env &env,
689     const AudioCapturerChangeInfo &changeInfo, napi_value &result)
690 {
691     napi_status status = napi_create_object(env, &result);
692     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "audioDeviceDescriptor malloc failed");
693     SetValueInt32(env, "streamId", changeInfo.sessionId, result);
694     SetValueInt32(env, "clientUid", changeInfo.clientUID, result);
695     SetValueInt32(env, "capturerState", static_cast<int32_t>(changeInfo.capturerState), result);
696     SetValueBoolean(env, "muted", changeInfo.muted, result);
697 
698     napi_value jsCapInfoObj = nullptr;
699     SetCapturerInfo(env, changeInfo.capturerInfo, jsCapInfoObj);
700     napi_set_named_property(env, result, "capturerInfo", jsCapInfoObj);
701 
702     napi_value deviceInfo = nullptr;
703     SetValueDeviceInfo(env, changeInfo.inputDeviceInfo, deviceInfo);
704     napi_set_named_property(env, result, "deviceDescriptors", deviceInfo);
705     return status;
706 }
707 
SetMicrophoneDescriptor(const napi_env & env,const sptr<MicrophoneDescriptor> & micDesc,napi_value & result)708 napi_status NapiParamUtils::SetMicrophoneDescriptor(const napi_env &env, const sptr<MicrophoneDescriptor> &micDesc,
709     napi_value &result)
710 {
711     napi_create_object(env, &result);
712     napi_value jsPositionObj = nullptr;
713     napi_value jsOrientationObj = nullptr;
714 
715     SetValueInt32(env, "id", micDesc->micId_, result);
716     SetValueInt32(env, "deviceType", static_cast<int32_t>(micDesc->deviceType_), result);
717     SetValueInt32(env, "groupId", micDesc->groupId_, result);
718     SetValueInt32(env, "sensitivity", micDesc->sensitivity_, result);
719     napi_create_object(env, &jsPositionObj);
720     SetValueDouble(env, "x", micDesc->position_.x, jsPositionObj);
721     SetValueDouble(env, "y", micDesc->position_.y, jsPositionObj);
722     SetValueDouble(env, "z", micDesc->position_.z, jsPositionObj);
723     napi_set_named_property(env, result, "position", jsPositionObj);
724 
725     napi_create_object(env, &jsOrientationObj);
726     SetValueDouble(env, "x", micDesc->orientation_.x, jsOrientationObj);
727     SetValueDouble(env, "y", micDesc->orientation_.y, jsOrientationObj);
728     SetValueDouble(env, "z", micDesc->orientation_.z, jsOrientationObj);
729     napi_set_named_property(env, result, "orientation", jsOrientationObj);
730     return napi_ok;
731 }
732 
SetMicrophoneDescriptors(const napi_env & env,const std::vector<sptr<MicrophoneDescriptor>> & micDescs,napi_value & result)733 napi_status NapiParamUtils::SetMicrophoneDescriptors(const napi_env &env,
734     const std::vector<sptr<MicrophoneDescriptor>> &micDescs, napi_value &result)
735 {
736     napi_status status = napi_create_array_with_length(env, micDescs.size(), &result);
737     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "audioDeviceDescriptor malloc failed");
738     int32_t index = 0;
739     for (const auto &micDesc : micDescs) {
740         napi_value valueParam = nullptr;
741         SetMicrophoneDescriptor(env, micDesc, valueParam);
742         napi_set_element(env, result, index, valueParam);
743         index++;
744     }
745     return status;
746 }
747 
SetValueMicStateChange(const napi_env & env,const MicStateChangeEvent & micStateChangeEvent,napi_value & result)748 napi_status NapiParamUtils::SetValueMicStateChange(const napi_env &env, const MicStateChangeEvent &micStateChangeEvent,
749     napi_value &result)
750 {
751     napi_create_object(env, &result);
752     NapiParamUtils::SetValueBoolean(env, "mute", micStateChangeEvent.mute, result);
753     return napi_ok;
754 }
755 
SetVolumeGroupInfos(const napi_env & env,const std::vector<sptr<VolumeGroupInfo>> & volumeGroupInfos,napi_value & result)756 napi_status NapiParamUtils::SetVolumeGroupInfos(const napi_env &env,
757     const std::vector<sptr<VolumeGroupInfo>> &volumeGroupInfos, napi_value &result)
758 {
759     napi_value valueParam = nullptr;
760     napi_status status = napi_create_array_with_length(env, volumeGroupInfos.size(), &result);
761     for (size_t i = 0; i < volumeGroupInfos.size(); i++) {
762         if (volumeGroupInfos[i] != nullptr) {
763             (void)napi_create_object(env, &valueParam);
764             SetValueString(env, "networkId", static_cast<std::string>(
765                 volumeGroupInfos[i]->networkId_), valueParam);
766             SetValueInt32(env, "groupId", static_cast<int32_t>(
767                 volumeGroupInfos[i]->volumeGroupId_), valueParam);
768             SetValueInt32(env, "mappingId", static_cast<int32_t>(
769                 volumeGroupInfos[i]->mappingId_), valueParam);
770             SetValueString(env, "groupName", static_cast<std::string>(
771                 volumeGroupInfos[i]->groupName_), valueParam);
772             SetValueInt32(env, "ConnectType", static_cast<int32_t>(
773                 volumeGroupInfos[i]->connectType_), valueParam);
774             napi_set_element(env, result, i, valueParam);
775         }
776     }
777     return status;
778 }
779 
SetValueVolumeEvent(const napi_env & env,const VolumeEvent & volumeEvent,napi_value & result)780 napi_status NapiParamUtils::SetValueVolumeEvent(const napi_env& env, const VolumeEvent &volumeEvent,
781     napi_value &result)
782 {
783     napi_create_object(env, &result);
784     SetValueInt32(env, "volumeType",
785         NapiAudioEnum::GetJsAudioVolumeType(static_cast<AudioStreamType>(volumeEvent.volumeType)), result);
786     SetValueInt32(env, "volume", static_cast<int32_t>(volumeEvent.volume), result);
787     SetValueBoolean(env, "updateUi", volumeEvent.updateUi, result);
788     SetValueInt32(env, "volumeGroupId", volumeEvent.volumeGroupId, result);
789     SetValueString(env, "networkId", volumeEvent.networkId, result);
790     SetValueInt32(env, "volumeMode",
791         NapiAudioEnum::GetJsAudioVolumeMode(static_cast<AudioVolumeMode>(volumeEvent.volumeMode)), result);
792     return napi_ok;
793 }
794 
GetAudioDeviceDescriptor(const napi_env & env,std::shared_ptr<AudioDeviceDescriptor> & selectedAudioDevice,bool & argTransFlag,napi_value in)795 napi_status NapiParamUtils::GetAudioDeviceDescriptor(const napi_env &env,
796     std::shared_ptr<AudioDeviceDescriptor> &selectedAudioDevice, bool &argTransFlag, napi_value in)
797 {
798     int32_t intValue = {0};
799     argTransFlag = true;
800     bool hasDeviceRole = true;
801     bool hasNetworkId  = true;
802     napi_status status = napi_has_named_property(env, in, "deviceRole", &hasDeviceRole);
803     status = napi_has_named_property(env, in, "networkId", &hasNetworkId);
804     if ((!hasDeviceRole) || (!hasNetworkId)) {
805         argTransFlag = false;
806         return status;
807     }
808 
809     status = GetValueInt32(env, "deviceRole", intValue, in);
810     if (status == napi_ok) {
811         if (std::find(DEVICE_ROLE_SET.begin(), DEVICE_ROLE_SET.end(), intValue) == DEVICE_ROLE_SET.end()) {
812             argTransFlag = false;
813             return status;
814         }
815         selectedAudioDevice->deviceRole_ = static_cast<DeviceRole>(intValue);
816     }
817 
818     status = GetValueInt32(env, "deviceType", intValue, in);
819     if (status == napi_ok) {
820         if (std::find(DEVICE_TYPE_SET.begin(), DEVICE_TYPE_SET.end(), intValue) == DEVICE_TYPE_SET.end()) {
821             argTransFlag = false;
822             return status;
823         }
824         selectedAudioDevice->deviceType_ = static_cast<DeviceType>(intValue);
825     }
826 
827     selectedAudioDevice->networkId_ = GetPropertyString(env, in, "networkId");
828 
829     if (GetValueInt32(env, "dmDeviceType", intValue, in) == napi_ok) {
830         selectedAudioDevice->dmDeviceType_ = static_cast<uint16_t>(intValue);
831     }
832     selectedAudioDevice->displayName_ = GetPropertyString(env, in, "displayName");
833 
834     status = GetValueInt32(env, "interruptGroupId", intValue, in);
835     if (status == napi_ok) {
836         selectedAudioDevice->interruptGroupId_ = intValue;
837     }
838 
839     status = GetValueInt32(env, "volumeGroupId", intValue, in);
840     if (status == napi_ok) {
841         selectedAudioDevice->volumeGroupId_ = intValue;
842     }
843 
844     selectedAudioDevice->macAddress_ = GetPropertyString(env, in, "address");
845 
846     status = GetValueInt32(env, "id", intValue, in);
847     if (status == napi_ok) {
848         selectedAudioDevice->deviceId_ = intValue;
849     }
850 
851     return napi_ok;
852 }
853 
GetAudioDeviceDescriptorVector(const napi_env & env,std::vector<std::shared_ptr<AudioDeviceDescriptor>> & deviceDescriptorsVector,bool & argTransFlag,napi_value in)854 napi_status NapiParamUtils::GetAudioDeviceDescriptorVector(const napi_env &env,
855     std::vector<std::shared_ptr<AudioDeviceDescriptor>> &deviceDescriptorsVector, bool &argTransFlag, napi_value in)
856 {
857     uint32_t arrayLen = 0;
858     napi_get_array_length(env, in, &arrayLen);
859     if (arrayLen == 0) {
860         deviceDescriptorsVector = {};
861         AUDIO_INFO_LOG("Error: AudioDeviceDescriptor vector is NULL!");
862     }
863 
864     for (size_t i = 0; i < arrayLen; i++) {
865         napi_value element;
866         napi_get_element(env, in, i, &element);
867         std::shared_ptr<AudioDeviceDescriptor> selectedAudioDevice = std::make_shared<AudioDeviceDescriptor>();
868         GetAudioDeviceDescriptor(env, selectedAudioDevice, argTransFlag, element);
869         if (!argTransFlag) {
870             return napi_ok;
871         }
872         deviceDescriptorsVector.push_back(selectedAudioDevice);
873     }
874     return napi_ok;
875 }
876 
GetAudioCapturerFilter(const napi_env & env,sptr<AudioCapturerFilter> & audioCapturerFilter,napi_value in)877 napi_status NapiParamUtils::GetAudioCapturerFilter(const napi_env &env, sptr<AudioCapturerFilter> &audioCapturerFilter,
878     napi_value in)
879 {
880     int32_t intValue = {0};
881     audioCapturerFilter = new(std::nothrow) AudioCapturerFilter();
882 
883     napi_status status = GetValueInt32(env, "uid", intValue, in);
884     if (status == napi_ok) {
885         audioCapturerFilter->uid = intValue;
886     }
887 
888     napi_value tempValue = nullptr;
889     if (napi_get_named_property(env, in, "capturerInfo", &tempValue) == napi_ok) {
890         GetCapturerInfo(env, &(audioCapturerFilter->capturerInfo), tempValue);
891     }
892     return napi_ok;
893 }
894 
GetAudioCapturerInfo(const napi_env & env,AudioCapturerInfo * capturerInfo,napi_value in)895 napi_status NapiParamUtils::GetAudioCapturerInfo(const napi_env &env, AudioCapturerInfo *capturerInfo, napi_value in)
896 {
897     napi_valuetype valueType = napi_undefined;
898     napi_typeof(env, in, &valueType);
899     CHECK_AND_RETURN_RET_LOG(valueType == napi_object, napi_invalid_arg,
900         "GetRendererInfo failed, vauleType is not object");
901 
902     int32_t intValue = {0};
903     napi_value tempValue = nullptr;
904     napi_status status = napi_get_named_property(env, in, "source", &tempValue);
905     if (status == napi_ok) {
906         GetValueInt32(env, intValue, tempValue);
907         if (NapiAudioEnum::IsValidSourceType(intValue)) {
908             capturerInfo->sourceType = static_cast<SourceType>(intValue);
909         } else {
910             capturerInfo->sourceType = SourceType::SOURCE_TYPE_INVALID;
911         }
912     }
913 
914     status = GetValueInt32(env, "capturerFlags", intValue, in);
915     if (status == napi_ok) {
916         capturerInfo->capturerFlags = intValue;
917     }
918 
919     return napi_ok;
920 }
921 
GetAudioRendererFilter(const napi_env & env,sptr<AudioRendererFilter> & audioRendererFilter,bool & argTransFlag,napi_value in)922 napi_status NapiParamUtils::GetAudioRendererFilter(const napi_env &env, sptr<AudioRendererFilter> &audioRendererFilter,
923     bool &argTransFlag, napi_value in)
924 {
925     napi_value tempValue = nullptr;
926     int32_t intValue = {0};
927     argTransFlag = true;
928     audioRendererFilter = new(std::nothrow) AudioRendererFilter();
929 
930     napi_status status = GetValueInt32(env, "uid", intValue, in);
931     if (status == napi_ok) {
932         audioRendererFilter->uid = intValue;
933     }
934 
935     if (napi_get_named_property(env, in, "rendererInfo", &tempValue) == napi_ok) {
936         GetRendererInfo(env, &(audioRendererFilter->rendererInfo), tempValue);
937     }
938 
939     status = GetValueInt32(env, "rendererId", intValue, in);
940     if (status == napi_ok) {
941         audioRendererFilter->streamId = intValue;
942     }
943 
944     return napi_ok;
945 }
946 
GetAudioDeviceUsage(const napi_env & env,AudioDeviceUsage & audioDevUsage,napi_value in)947 napi_status NapiParamUtils::GetAudioDeviceUsage(const napi_env &env, AudioDeviceUsage &audioDevUsage, napi_value in)
948 {
949     napi_valuetype valueType = napi_undefined;
950     napi_status status = napi_typeof(env, in, &valueType);
951     CHECK_AND_RETURN_RET_LOG(status == napi_ok && valueType == napi_number, napi_invalid_arg, "valueType invalid");
952 
953     int32_t intValue = 0;
954     status = napi_get_value_int32(env, in, &intValue);
955     CHECK_AND_RETURN_RET_LOG(status == napi_ok && NapiAudioEnum::IsLegalDeviceUsage(intValue),
956         napi_invalid_arg, "invalid deviceusage");
957 
958     audioDevUsage = static_cast<AudioDeviceUsage>(intValue);
959 
960     return napi_ok;
961 }
962 
SetValueDeviceChangeAction(const napi_env & env,const DeviceChangeAction & action,napi_value & result)963 napi_status NapiParamUtils::SetValueDeviceChangeAction(const napi_env& env, const DeviceChangeAction &action,
964     napi_value &result)
965 {
966     napi_create_object(env, &result);
967     NapiParamUtils::SetValueInt32(env, "type", static_cast<int32_t>(action.type), result);
968 
969     napi_value jsArray;
970     NapiParamUtils::SetDeviceDescriptors(env, action.deviceDescriptors, jsArray);
971     napi_set_named_property(env, result, "deviceDescriptors", jsArray);
972     return napi_ok;
973 }
974 
SetValueBlockedDeviceAction(const napi_env & env,const MicrophoneBlockedInfo & action,napi_value & result)975 napi_status NapiParamUtils::SetValueBlockedDeviceAction(const napi_env& env, const MicrophoneBlockedInfo &action,
976     napi_value &result)
977 {
978     napi_create_object(env, &result);
979     NapiParamUtils::SetValueInt32(env, "blockStatus", static_cast<int32_t>(action.blockStatus), result);
980 
981     napi_value jsArray;
982     NapiParamUtils::SetDeviceDescriptors(env, action.devices, jsArray);
983     napi_set_named_property(env, result, "devices", jsArray);
984     return napi_ok;
985 }
986 
SetRendererChangeInfos(const napi_env & env,const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & changeInfos,napi_value & result)987 napi_status NapiParamUtils::SetRendererChangeInfos(const napi_env &env,
988     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &changeInfos, napi_value &result)
989 {
990     int32_t position = 0;
991     napi_value jsChangeInfoObj = nullptr;
992     napi_value jsRenInfoObj = nullptr;
993     napi_create_array_with_length(env, changeInfos.size(), &result);
994     for (const auto &changeInfo : changeInfos) {
995         if (changeInfo) {
996             napi_create_object(env, &jsChangeInfoObj);
997             SetValueInt32(env, "streamId", changeInfo->sessionId, jsChangeInfoObj);
998             SetValueInt32(env, "rendererState", static_cast<int32_t>(changeInfo->rendererState), jsChangeInfoObj);
999             SetValueInt32(env, "clientUid", changeInfo->clientUID, jsChangeInfoObj);
1000             SetRendererInfo(env, changeInfo->rendererInfo, jsRenInfoObj);
1001             napi_set_named_property(env, jsChangeInfoObj, "rendererInfo", jsRenInfoObj);
1002             napi_value deviceInfo = nullptr;
1003             SetValueDeviceInfo(env, changeInfo->outputDeviceInfo, deviceInfo);
1004             napi_set_named_property(env, jsChangeInfoObj, "deviceDescriptors", deviceInfo);
1005             napi_set_element(env, result, position, jsChangeInfoObj);
1006             position++;
1007         }
1008     }
1009     return napi_ok;
1010 }
1011 
SetCapturerChangeInfos(const napi_env & env,const std::vector<std::shared_ptr<AudioCapturerChangeInfo>> & changeInfos,napi_value & result)1012 napi_status NapiParamUtils::SetCapturerChangeInfos(const napi_env &env,
1013     const std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &changeInfos, napi_value &result)
1014 {
1015     int32_t position = 0;
1016     napi_value jsChangeInfoObj = nullptr;
1017     napi_create_array_with_length(env, changeInfos.size(), &result);
1018     for (const auto &changeInfo : changeInfos) {
1019         if (changeInfo) {
1020             SetAudioCapturerChangeInfoDescriptors(env, *changeInfo, jsChangeInfoObj);
1021             napi_set_element(env, result, position, jsChangeInfoObj);
1022             position++;
1023         }
1024     }
1025     return napi_ok;
1026 }
1027 
SetEffectInfo(const napi_env & env,const AudioSceneEffectInfo & audioSceneEffectInfo,napi_value & result)1028 napi_status NapiParamUtils::SetEffectInfo(const napi_env &env,
1029     const AudioSceneEffectInfo &audioSceneEffectInfo, napi_value &result)
1030 {
1031     int32_t position = 0;
1032     napi_value jsEffectInofObj = nullptr;
1033     napi_create_array_with_length(env, audioSceneEffectInfo.mode.size(), &result);
1034     napi_create_object(env, &jsEffectInofObj);
1035     for (const auto &mode : audioSceneEffectInfo.mode) {
1036         SetValueUInt32(env, mode, jsEffectInofObj);
1037         napi_set_element(env, result, position, jsEffectInofObj);
1038         position++;
1039     }
1040     return napi_ok;
1041 }
1042 
GetAudioInterrupt(const napi_env & env,AudioInterrupt & audioInterrupt,napi_value in)1043 napi_status NapiParamUtils::GetAudioInterrupt(const napi_env &env, AudioInterrupt &audioInterrupt,
1044     napi_value in)
1045 {
1046     int32_t propValue = -1;
1047     napi_status status = NapiParamUtils::GetValueInt32(env, "contentType", propValue, in);
1048     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetAudioInterrupt: Failed to retrieve contentType");
1049     audioInterrupt.contentType = static_cast<ContentType>(propValue);
1050 
1051     status = NapiParamUtils::GetValueInt32(env, "streamUsage", propValue, in);
1052     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetAudioInterrupt: Failed to retrieve streamUsage");
1053     audioInterrupt.streamUsage = static_cast<StreamUsage>(propValue);
1054 
1055     status = NapiParamUtils::GetValueBoolean(env, "pauseWhenDucked", audioInterrupt.pauseWhenDucked, in);
1056     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetAudioInterrupt: Failed to retrieve pauseWhenDucked");
1057     audioInterrupt.audioFocusType.streamType = AudioSystemManager::GetStreamType(audioInterrupt.contentType,
1058         audioInterrupt.streamUsage);
1059     return status;
1060 }
1061 
SetValueInterruptAction(const napi_env & env,const InterruptAction & interruptAction,napi_value & result)1062 napi_status NapiParamUtils::SetValueInterruptAction(const napi_env &env, const InterruptAction &interruptAction,
1063     napi_value &result)
1064 {
1065     napi_create_object(env, &result);
1066     SetValueInt32(env, "actionType", static_cast<int32_t>(interruptAction.actionType), result);
1067     SetValueInt32(env, "type", static_cast<int32_t>(interruptAction.interruptType), result);
1068     SetValueInt32(env, "hint", static_cast<int32_t>(interruptAction.interruptHint), result);
1069     SetValueBoolean(env, "activated", interruptAction.activated, result);
1070     return napi_ok;
1071 }
1072 
GetSpatialDeviceState(napi_env env,AudioSpatialDeviceState * spatialDeviceState,napi_value in)1073 napi_status NapiParamUtils::GetSpatialDeviceState(napi_env env, AudioSpatialDeviceState *spatialDeviceState,
1074     napi_value in)
1075 {
1076     napi_value res = nullptr;
1077     int32_t intValue = {0};
1078     napi_valuetype valueType = napi_undefined;
1079     napi_status status = napi_get_named_property(env, in, "address", &res);
1080     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Get address name failed");
1081     napi_typeof(env, res, &valueType);
1082     CHECK_AND_RETURN_RET_LOG(valueType == napi_string, napi_invalid_arg, "Get address type failed");
1083     spatialDeviceState->address = NapiParamUtils::GetStringArgument(env, res);
1084 
1085     status = GetValueBoolean(env, "isSpatializationSupported", spatialDeviceState->isSpatializationSupported, in);
1086     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Get isSpatializationSupported failed");
1087 
1088     status = GetValueBoolean(env, "isHeadTrackingSupported", spatialDeviceState->isHeadTrackingSupported, in);
1089     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Get isHeadTrackingSupported failed");
1090 
1091     status = GetValueInt32(env, "spatialDeviceType", intValue, in);
1092     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Get spatialDeviceType failed");
1093     CHECK_AND_RETURN_RET_LOG((intValue >= EARPHONE_TYPE_NONE) && (intValue <= EARPHONE_TYPE_OTHERS),
1094         napi_invalid_arg, "Get spatialDeviceType failed");
1095     spatialDeviceState->spatialDeviceType = static_cast<AudioSpatialDeviceType>(intValue);
1096 
1097     return napi_ok;
1098 }
1099 
GetExtraParametersSubKV(napi_env env,std::vector<std::pair<std::string,std::string>> & subKV,napi_value in)1100 napi_status NapiParamUtils::GetExtraParametersSubKV(napi_env env,
1101     std::vector<std::pair<std::string, std::string>> &subKV, napi_value in)
1102 {
1103     napi_value jsProNameList = nullptr;
1104     uint32_t jsProCount = 0;
1105     napi_status status = napi_get_property_names(env, in, &jsProNameList);
1106     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get property name failed");
1107     status = napi_get_array_length(env, jsProNameList, &jsProCount);
1108     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get subKeys length failed");
1109 
1110     napi_value jsProName = nullptr;
1111     napi_value jsProValue = nullptr;
1112     for (uint32_t i = 0; i < jsProCount; i++) {
1113         status = napi_get_element(env, jsProNameList, i, &jsProName);
1114         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get sub key failed");
1115 
1116         std::string strProName = NapiParamUtils::GetStringArgument(env, jsProName);
1117         status = napi_get_named_property(env, in, strProName.c_str(), &jsProValue);
1118         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get sub value failed");
1119 
1120         subKV.push_back(std::make_pair(strProName, NapiParamUtils::GetStringArgument(env, jsProValue)));
1121     }
1122 
1123     return napi_ok;
1124 }
1125 
GetExtraParametersVector(const napi_env & env,std::vector<std::string> & subKeys,napi_value in)1126 napi_status NapiParamUtils::GetExtraParametersVector(const napi_env &env,
1127     std::vector<std::string> &subKeys, napi_value in)
1128 {
1129     uint32_t arrayLen = 0;
1130     napi_get_array_length(env, in, &arrayLen);
1131 
1132     for (uint32_t i = 0; i < arrayLen; i++) {
1133         napi_value element;
1134         if (napi_get_element(env, in, i, &element) == napi_ok) {
1135             subKeys.push_back(GetStringArgument(env, element));
1136         }
1137     }
1138 
1139     return napi_ok;
1140 }
1141 
SetExtraAudioParametersInfo(const napi_env & env,const std::vector<std::pair<std::string,std::string>> & extraParameters,napi_value & result)1142 napi_status NapiParamUtils::SetExtraAudioParametersInfo(const napi_env &env,
1143     const std::vector<std::pair<std::string, std::string>> &extraParameters, napi_value &result)
1144 {
1145     napi_status status = napi_create_object(env, &result);
1146     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "malloc array buffer failed");
1147 
1148     for (auto it = extraParameters.begin(); it != extraParameters.end(); it++) {
1149         status = SetValueString(env, it->first, it->second, result);
1150         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueString failed");
1151     }
1152 
1153     return status;
1154 }
1155 
UniqueEffectPropertyData(AudioEffectPropertyArrayV3 & propertyArray)1156 int32_t NapiParamUtils::UniqueEffectPropertyData(AudioEffectPropertyArrayV3 &propertyArray)
1157 {
1158     int32_t propSize = static_cast<int32_t>(propertyArray.property.size());
1159     std::set<std::string> classSet;
1160     for (int32_t i = 0; i < propSize; i++)    {
1161         if (propertyArray.property[i].category != "" && propertyArray.property[i].name != "") {
1162                 classSet.insert(propertyArray.property[i].name);
1163             }
1164     }
1165     return static_cast<int32_t>(classSet.size());
1166 }
1167 
GetEffectPropertyArray(napi_env env,AudioEffectPropertyArrayV3 & propertyArray,napi_value in)1168 napi_status NapiParamUtils::GetEffectPropertyArray(napi_env env,
1169     AudioEffectPropertyArrayV3 &propertyArray, napi_value in)
1170 {
1171     uint32_t arrayLen = 0;
1172     napi_status status = napi_get_array_length(env, in, &arrayLen);
1173     CHECK_AND_RETURN_RET_LOG(status == napi_ok && arrayLen > 0, status, "get array length invalid");
1174 
1175     AudioEffectPropertyArrayV3 effectArray;
1176     AudioEffectPropertyArrayV3 enhanceArray;
1177     for (uint32_t i = 0; i < arrayLen; i++) {
1178         napi_value element = nullptr;
1179         napi_get_element(env, in, i, &element);
1180 
1181         AudioEffectPropertyV3 prop;
1182         napi_value propValue = nullptr;
1183 
1184         status = napi_get_named_property(env, element, "name", &propValue);
1185         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get name failed");
1186         prop.name = GetStringArgument(env, propValue);
1187 
1188         status = napi_get_named_property(env, element, "category", &propValue);
1189         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get category failed");
1190         prop.category = GetStringArgument(env, propValue);
1191 
1192         int32_t effectFlag = {-1};
1193         status = GetValueInt32(env, "flag", effectFlag, element);
1194         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get flag failed");
1195         prop.flag = static_cast<EffectFlag>(effectFlag);
1196 
1197         propertyArray.property.push_back(prop);
1198         if (prop.flag == RENDER_EFFECT_FLAG) {
1199             effectArray.property.push_back(prop);
1200         } else if (prop.flag == CAPTURE_EFFECT_FLAG) {
1201             enhanceArray.property.push_back(prop);
1202         }
1203     }
1204 
1205     int32_t effectSize = UniqueEffectPropertyData(effectArray);
1206     CHECK_AND_RETURN_RET_LOG(effectSize == static_cast<int32_t>(effectArray.property.size()),
1207         napi_invalid_arg, "audio effect property array exist duplicate data");
1208 
1209     int32_t enhanceSize = UniqueEffectPropertyData(enhanceArray);
1210     CHECK_AND_RETURN_RET_LOG(enhanceSize == static_cast<int32_t>(enhanceArray.property.size()),
1211         napi_invalid_arg, "audio enhance property array exist duplicate data");
1212 
1213     int32_t size = static_cast<int32_t>(propertyArray.property.size());
1214     CHECK_AND_RETURN_RET_LOG(size > 0 && size <= AUDIO_EFFECT_COUNT_UPPER_LIMIT,
1215         napi_invalid_arg, "Audio enhance property array size invalid");
1216 
1217     return napi_ok;
1218 }
1219 
SetEffectProperty(const napi_env & env,const AudioEffectPropertyArrayV3 & propertyArray,napi_value & result)1220 napi_status NapiParamUtils::SetEffectProperty(const napi_env &env,
1221     const AudioEffectPropertyArrayV3 &propertyArray, napi_value &result)
1222 {
1223     int32_t position = 0;
1224     napi_value jsEffectInfoObj = nullptr;
1225     napi_status status = napi_create_array_with_length(env, propertyArray.property.size(), &result);
1226     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get create array failed");
1227     for (const auto &property : propertyArray.property) {
1228         napi_create_object(env, &jsEffectInfoObj);
1229         status = SetValueString(env, "name", property.name, jsEffectInfoObj);
1230         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Set name failed");
1231         status = SetValueString(env, "category", property.category, jsEffectInfoObj);
1232         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Set category failed");
1233         status = SetValueInt32(env, "flag", property.flag, jsEffectInfoObj);
1234         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Set flag failed");
1235         napi_set_element(env, result, position, jsEffectInfoObj);
1236         position++;
1237     }
1238     return napi_ok;
1239 }
1240 
GetEffectPropertyArray(napi_env env,AudioEffectPropertyArray & effectArray,napi_value in)1241 napi_status NapiParamUtils::GetEffectPropertyArray(napi_env env, AudioEffectPropertyArray &effectArray,
1242     napi_value in)
1243 {
1244     uint32_t arrayLen = 0;
1245     napi_status status = napi_get_array_length(env, in, &arrayLen);
1246     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get subKeys length failed");
1247 
1248     for (size_t i = 0; i < arrayLen; i++) {
1249         napi_value element = nullptr;
1250         napi_get_element(env, in, i, &element);
1251 
1252         AudioEffectProperty prop;
1253         napi_value propValue = nullptr;
1254 
1255         status = napi_get_named_property(env, element, "effectClass", &propValue);
1256         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Get effectClass failed");
1257         prop.effectClass = GetStringArgument(env, propValue);
1258 
1259         status = napi_get_named_property(env, element, "effectProp", &propValue);
1260         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Get effectProp failed");
1261         prop.effectProp = GetStringArgument(env, propValue);
1262 
1263         effectArray.property.push_back(prop);
1264     }
1265 
1266     int32_t size = effectArray.property.size();
1267     CHECK_AND_RETURN_RET_LOG(size > 0 && size <= AUDIO_EFFECT_COUNT_UPPER_LIMIT,
1268                              napi_invalid_arg, "Audio effect property array size invalid");
1269 
1270     std::set<std::string> classSet;
1271     for (int32_t i = 0; i < size; i++) {
1272         if (effectArray.property[i].effectClass != "" && effectArray.property[i].effectProp != "") {
1273             classSet.insert(effectArray.property[i].effectClass);
1274         }
1275     }
1276     CHECK_AND_RETURN_RET_LOG(size == static_cast<int32_t>(classSet.size()), napi_invalid_arg,
1277         "Audio enhance property array exist duplicate data");
1278 
1279     return napi_ok;
1280 }
1281 
GetEnhancePropertyArray(napi_env env,AudioEnhancePropertyArray & enhanceArray,napi_value in)1282 napi_status NapiParamUtils::GetEnhancePropertyArray(napi_env env, AudioEnhancePropertyArray &enhanceArray,
1283     napi_value in)
1284 {
1285     uint32_t arrayLen = 0;
1286     napi_status status = napi_get_array_length(env, in, &arrayLen);
1287     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get subKeys length failed");
1288 
1289     for (size_t i = 0; i < arrayLen; i++) {
1290         napi_value element = nullptr;
1291         napi_get_element(env, in, i, &element);
1292 
1293         AudioEnhanceProperty prop;
1294         napi_value propValue = nullptr;
1295 
1296         status = napi_get_named_property(env, element, "enhanceClass", &propValue);
1297         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Get enhanceClass failed");
1298         prop.enhanceClass = GetStringArgument(env, propValue);
1299 
1300         status = napi_get_named_property(env, element, "enhanceProp", &propValue);
1301         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Get enhanceProp failed");
1302         prop.enhanceProp = GetStringArgument(env, propValue);
1303 
1304         enhanceArray.property.push_back(prop);
1305     }
1306 
1307     int32_t size = enhanceArray.property.size();
1308     CHECK_AND_RETURN_RET_LOG(size > 0 && size <= AUDIO_EFFECT_COUNT_UPPER_LIMIT,
1309                              napi_invalid_arg, "Audio enhance property array size invalid");
1310 
1311     std::set<std::string> classSet;
1312     for (int32_t i = 0; i < size; i++) {
1313         if (enhanceArray.property[i].enhanceClass != "" && enhanceArray.property[i].enhanceProp != "") {
1314             classSet.insert(enhanceArray.property[i].enhanceClass);
1315         }
1316     }
1317     CHECK_AND_RETURN_RET_LOG(size == static_cast<int32_t>(classSet.size()), napi_invalid_arg,
1318         "Audio enhance property array exist duplicate data");
1319 
1320     return napi_ok;
1321 }
1322 
SetEnhanceProperty(const napi_env & env,const AudioEnhancePropertyArray & enhanceArray,napi_value & result)1323 napi_status NapiParamUtils::SetEnhanceProperty(const napi_env &env, const AudioEnhancePropertyArray &enhanceArray,
1324     napi_value &result)
1325 {
1326     int32_t position = 0;
1327     napi_value jsEnhanceInfoObj = nullptr;
1328     napi_status status = napi_create_array_with_length(env, enhanceArray.property.size(), &result);
1329     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get create array failed");
1330     for (const auto &property : enhanceArray.property) {
1331         napi_create_object(env, &jsEnhanceInfoObj);
1332         status = SetValueString(env, "enhanceClass", property.enhanceClass, jsEnhanceInfoObj);
1333         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Set enhanceClass failed");
1334         status = SetValueString(env, "enhanceProp", property.enhanceProp, jsEnhanceInfoObj);
1335         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Set enhanceProp failed");
1336         napi_set_element(env, result, position, jsEnhanceInfoObj);
1337         position++;
1338     }
1339     return napi_ok;
1340 }
1341 
SetEffectProperty(const napi_env & env,const AudioEffectPropertyArray & effectArray,napi_value & result)1342 napi_status NapiParamUtils::SetEffectProperty(const napi_env &env, const AudioEffectPropertyArray &effectArray,
1343     napi_value &result)
1344 {
1345     int32_t position = 0;
1346     napi_value jsEffectInfoObj = nullptr;
1347     napi_status status = napi_create_array_with_length(env, effectArray.property.size(), &result);
1348     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get create array failed");
1349     for (const auto &property : effectArray.property) {
1350         napi_create_object(env, &jsEffectInfoObj);
1351         status = SetValueString(env, "effectClass", property.effectClass, jsEffectInfoObj);
1352         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Set effectClass failed");
1353         status = SetValueString(env, "effectProp", property.effectProp, jsEffectInfoObj);
1354         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Set effectProp failed");
1355         napi_set_element(env, result, position, jsEffectInfoObj);
1356         position++;
1357     }
1358     return napi_ok;
1359 }
1360 
GetAudioSessionStrategy(const napi_env & env,AudioSessionStrategy & audioSessionStrategy,napi_value in)1361 napi_status NapiParamUtils::GetAudioSessionStrategy(const napi_env &env,
1362     AudioSessionStrategy &audioSessionStrategy, napi_value in)
1363 {
1364     int32_t intValue = {0};
1365     napi_status status = napi_generic_failure;
1366     status = GetValueInt32(env, "concurrencyMode", intValue, in);
1367     if (status == napi_ok) {
1368         audioSessionStrategy.concurrencyMode = static_cast<AudioConcurrencyMode>(intValue);
1369         return napi_ok;
1370     } else {
1371         AUDIO_ERR_LOG("invaild concurrencyMode");
1372         return napi_generic_failure;
1373     }
1374 }
1375 
SetAudioSessionDeactiveEvent(const napi_env & env,const AudioSessionDeactiveEvent & deactiveEvent,napi_value & result)1376 napi_status NapiParamUtils::SetAudioSessionDeactiveEvent(
1377     const napi_env &env, const AudioSessionDeactiveEvent &deactiveEvent, napi_value &result)
1378 {
1379     napi_create_object(env, &result);
1380     SetValueInt32(env, "reason", static_cast<int32_t>(deactiveEvent.deactiveReason), result);
1381     return napi_ok;
1382 }
1383 
CheckArgType(napi_env env,napi_value arg,napi_valuetype expectedType)1384 bool NapiParamUtils::CheckArgType(napi_env env, napi_value arg, napi_valuetype expectedType)
1385 {
1386     napi_valuetype valueType = napi_undefined;
1387     napi_typeof(env, arg, &valueType);
1388     if (valueType != expectedType) {
1389         AUDIO_ERR_LOG("the type of parameter is invalid");
1390         return false;
1391     }
1392     return true;
1393 }
1394 } // namespace AudioStandard
1395 } // namespace OHOS
1396