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