1 /*
2  * Copyright (c) 2023 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 {
GetUndefinedValue(napi_env env)25 napi_value NapiParamUtils::GetUndefinedValue(napi_env env)
26 {
27     napi_value result {};
28     napi_get_undefined(env, &result);
29     return result;
30 }
31 
GetParam(const napi_env & env,napi_callback_info info,size_t & argc,napi_value * args)32 napi_status NapiParamUtils::GetParam(const napi_env &env, napi_callback_info info, size_t &argc, napi_value *args)
33 {
34     napi_value thisVar = nullptr;
35     void *data;
36     return napi_get_cb_info(env, info, &argc, args, &thisVar, &data);
37 }
38 
GetValueInt32(const napi_env & env,int32_t & value,napi_value in)39 napi_status NapiParamUtils::GetValueInt32(const napi_env &env, int32_t &value, napi_value in)
40 {
41     napi_status status = napi_get_value_int32(env, in, &value);
42     CHECK_AND_RETURN_RET_PRELOG(status == napi_ok, status, "GetValueInt32 napi_get_value_int32 failed");
43     return status;
44 }
45 
SetValueInt32(const napi_env & env,const int32_t & value,napi_value & result)46 napi_status NapiParamUtils::SetValueInt32(const napi_env &env, const int32_t &value, napi_value &result)
47 {
48     napi_status status = napi_create_int32(env, value, &result);
49     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32 napi_create_int32 failed");
50     return status;
51 }
52 
GetValueInt32(const napi_env & env,const std::string & fieldStr,int32_t & value,napi_value in)53 napi_status NapiParamUtils::GetValueInt32(const napi_env &env, const std::string &fieldStr,
54     int32_t &value, napi_value in)
55 {
56     napi_value jsValue = nullptr;
57     napi_status status = napi_get_named_property(env, in, fieldStr.c_str(), &jsValue);
58     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueInt32 napi_get_named_property failed");
59     status = GetValueInt32(env, value, jsValue);
60     return status;
61 }
62 
SetValueInt32(const napi_env & env,const std::string & fieldStr,const int32_t value,napi_value & result)63 napi_status NapiParamUtils::SetValueInt32(const napi_env &env, const std::string &fieldStr,
64     const int32_t value, napi_value &result)
65 {
66     napi_value jsValue = nullptr;
67     napi_status status = SetValueInt32(env, value, jsValue);
68     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32 napi_create_int32 failed");
69     status = napi_set_named_property(env, result, fieldStr.c_str(), jsValue);
70     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32 napi_create_int32 failed");
71     return status;
72 }
73 
GetValueUInt32(const napi_env & env,uint32_t & value,napi_value in)74 napi_status NapiParamUtils::GetValueUInt32(const napi_env &env, uint32_t &value, napi_value in)
75 {
76     napi_status status = napi_get_value_uint32(env, in, &value);
77     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueUInt32 napi_get_value_uint32 failed");
78     return status;
79 }
80 
SetValueUInt32(const napi_env & env,const uint32_t & value,napi_value & result)81 napi_status NapiParamUtils::SetValueUInt32(const napi_env &env, const uint32_t &value, napi_value &result)
82 {
83     napi_status status = napi_create_uint32(env, value, &result);
84     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueUInt32 napi_create_uint32 failed");
85     return status;
86 }
87 
GetValueDouble(const napi_env & env,double & value,napi_value in)88 napi_status NapiParamUtils::GetValueDouble(const napi_env &env, double &value, napi_value in)
89 {
90     napi_status status = napi_get_value_double(env, in, &value);
91     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueDouble napi_get_value_double failed");
92     return status;
93 }
94 
SetValueDouble(const napi_env & env,const double & value,napi_value & result)95 napi_status NapiParamUtils::SetValueDouble(const napi_env &env, const double &value, napi_value &result)
96 {
97     napi_status status = napi_create_double(env, value, &result);
98     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueDouble napi_create_double failed");
99     return status;
100 }
101 
GetValueDouble(const napi_env & env,const std::string & fieldStr,double & value,napi_value in)102 napi_status NapiParamUtils::GetValueDouble(const napi_env &env, const std::string &fieldStr,
103     double &value, napi_value in)
104 {
105     napi_value jsValue = nullptr;
106     napi_status status = napi_get_named_property(env, in, fieldStr.c_str(), &jsValue);
107     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueDouble napi_get_named_property failed");
108     status = GetValueDouble(env, value, jsValue);
109     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueDouble failed");
110     return status;
111 }
112 
SetValueDouble(const napi_env & env,const std::string & fieldStr,const double value,napi_value & result)113 napi_status NapiParamUtils::SetValueDouble(const napi_env &env, const std::string &fieldStr,
114     const double value, napi_value &result)
115 {
116     napi_value jsValue = nullptr;
117     napi_status status = SetValueDouble(env, value, jsValue);
118     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueDouble SetValueDouble failed");
119     status = napi_set_named_property(env, result, fieldStr.c_str(), jsValue);
120     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueDouble napi_set_named_property failed");
121     return status;
122 }
123 
GetPropertyString(napi_env env,napi_value value,const std::string & fieldStr)124 std::string NapiParamUtils::GetPropertyString(napi_env env, napi_value value, const std::string &fieldStr)
125 {
126     std::string invalid = "";
127     bool exist = false;
128     napi_status status = napi_has_named_property(env, value, fieldStr.c_str(), &exist);
129     if (status != napi_ok || !exist) {
130         AUDIO_ERR_LOG("can not find %{public}s property", fieldStr.c_str());
131         return invalid;
132     }
133 
134     napi_value item = nullptr;
135     if (napi_get_named_property(env, value, fieldStr.c_str(), &item) != napi_ok) {
136         AUDIO_ERR_LOG("get %{public}s property fail", fieldStr.c_str());
137         return invalid;
138     }
139 
140     return GetStringArgument(env, item);
141 }
142 
GetStringArgument(napi_env env,napi_value value)143 std::string NapiParamUtils::GetStringArgument(napi_env env, napi_value value)
144 {
145     std::string strValue = "";
146     size_t bufLength = 0;
147     napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &bufLength);
148     if (status == napi_ok && bufLength > 0 && bufLength < PATH_MAX) {
149         strValue.reserve(bufLength + 1);
150         strValue.resize(bufLength);
151         status = napi_get_value_string_utf8(env, value, strValue.data(), bufLength + 1, &bufLength);
152         if (status == napi_ok) {
153             AUDIO_DEBUG_LOG("argument = %{public}s", strValue.c_str());
154         }
155     }
156     return strValue;
157 }
158 
SetValueString(const napi_env & env,const std::string & stringValue,napi_value & result)159 napi_status NapiParamUtils::SetValueString(const napi_env &env, const std::string &stringValue, napi_value &result)
160 {
161     napi_status status = napi_create_string_utf8(env, stringValue.c_str(), NAPI_AUTO_LENGTH, &result);
162     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueString napi_create_string_utf8 failed");
163     return status;
164 }
165 
SetValueString(const napi_env & env,const std::string & fieldStr,const std::string & stringValue,napi_value & result)166 napi_status NapiParamUtils::SetValueString(const napi_env &env, const std::string &fieldStr,
167     const std::string &stringValue, napi_value &result)
168 {
169     napi_value value = nullptr;
170     napi_status status = SetValueString(env, stringValue.c_str(), value);
171     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueString failed");
172     status = napi_set_named_property(env, result, fieldStr.c_str(), value);
173     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueString napi_set_named_property failed");
174     return status;
175 }
176 
GetValueBoolean(const napi_env & env,bool & boolValue,napi_value in)177 napi_status NapiParamUtils::GetValueBoolean(const napi_env &env, bool &boolValue, napi_value in)
178 {
179     napi_status status = napi_get_value_bool(env, in, &boolValue);
180     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueBoolean napi_get_boolean failed");
181     return status;
182 }
183 
SetValueBoolean(const napi_env & env,const bool boolValue,napi_value & result)184 napi_status NapiParamUtils::SetValueBoolean(const napi_env &env, const bool boolValue, napi_value &result)
185 {
186     napi_status status = napi_get_boolean(env, boolValue, &result);
187     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueBoolean napi_get_boolean failed");
188     return status;
189 }
190 
GetValueBoolean(const napi_env & env,const std::string & fieldStr,bool & boolValue,napi_value in)191 napi_status NapiParamUtils::GetValueBoolean(const napi_env &env, const std::string &fieldStr,
192     bool &boolValue, napi_value in)
193 {
194     napi_value jsValue = nullptr;
195     napi_status status = napi_get_named_property(env, in, fieldStr.c_str(), &jsValue);
196     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueBoolean napi_get_named_property failed");
197     status = GetValueBoolean(env, boolValue, jsValue);
198     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueBoolean failed");
199     return status;
200 }
201 
SetValueBoolean(const napi_env & env,const std::string & fieldStr,const bool boolValue,napi_value & result)202 napi_status NapiParamUtils::SetValueBoolean(const napi_env &env, const std::string &fieldStr,
203     const bool boolValue, napi_value &result)
204 {
205     napi_value value = nullptr;
206     napi_status status = SetValueBoolean(env, boolValue, value);
207     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueBoolean SetValueBoolean failed");
208     napi_set_named_property(env, result, fieldStr.c_str(), value);
209     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueBoolean napi_get_boolean failed");
210     return status;
211 }
212 
GetValueInt64(const napi_env & env,int64_t & value,napi_value in)213 napi_status NapiParamUtils::GetValueInt64(const napi_env &env, int64_t &value, napi_value in)
214 {
215     napi_status status = napi_get_value_int64(env, in, &value);
216     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueInt64 napi_get_value_int64 failed");
217     return status;
218 }
219 
SetValueInt64(const napi_env & env,const int64_t & value,napi_value & result)220 napi_status NapiParamUtils::SetValueInt64(const napi_env &env, const int64_t &value, napi_value &result)
221 {
222     napi_status status = napi_create_int64(env, value, &result);
223     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt64 napi_create_int64 failed");
224     return status;
225 }
226 
GetValueInt64(const napi_env & env,const std::string & fieldStr,int64_t & value,napi_value in)227 napi_status NapiParamUtils::GetValueInt64(const napi_env &env, const std::string &fieldStr,
228     int64_t &value, 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, "GetValueInt64 napi_get_named_property failed");
233     status = GetValueInt64(env, value, jsValue);
234     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetValueInt64 failed");
235     return status;
236 }
237 
SetValueInt64(const napi_env & env,const std::string & fieldStr,const int64_t value,napi_value & result)238 napi_status NapiParamUtils::SetValueInt64(const napi_env &env, const std::string &fieldStr,
239     const int64_t value, napi_value &result)
240 {
241     napi_value jsValue = nullptr;
242     napi_status status = SetValueInt64(env, value, jsValue);
243     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt64 failed");
244     status = napi_set_named_property(env, result, fieldStr.c_str(), jsValue);
245     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt64 napi_set_named_property failed");
246     return status;
247 }
248 
GetArrayBuffer(const napi_env & env,void * & data,size_t & length,napi_value in)249 napi_status NapiParamUtils::GetArrayBuffer(const napi_env &env, void* &data, size_t &length, napi_value in)
250 {
251     napi_status status = napi_get_arraybuffer_info(env, in, &data, &length);
252     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetArrayBuffer napi_get_arraybuffer_info failed");
253     return status;
254 }
255 
CreateArrayBuffer(const napi_env & env,const std::string & fieldStr,size_t bufferLen,uint8_t * bufferData,napi_value & result)256 napi_status NapiParamUtils::CreateArrayBuffer(const napi_env &env, const std::string &fieldStr, size_t bufferLen,
257     uint8_t *bufferData, napi_value &result)
258 {
259     napi_value value = nullptr;
260 
261     napi_status status = napi_create_arraybuffer(env, bufferLen, (void**)&bufferData, &value);
262     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "napi_create_arraybuffer failed");
263     status = napi_set_named_property(env, result, fieldStr.c_str(), value);
264     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "napi_set_named_property failed");
265 
266     return status;
267 }
268 
CreateArrayBuffer(const napi_env & env,const size_t bufferLen,const uint8_t * bufferData,napi_value & result)269 napi_status NapiParamUtils::CreateArrayBuffer(const napi_env &env, const size_t bufferLen,
270     const uint8_t *bufferData, napi_value &result)
271 {
272     uint8_t *native = nullptr;
273     napi_status status = napi_create_arraybuffer(env, bufferLen, reinterpret_cast<void **>(&native), &result);
274     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "napi_create_arraybuffer failed");
275     if (memcpy_s(native, bufferLen, bufferData, bufferLen)) {
276         result = nullptr;
277     }
278     return status;
279 }
280 
ConvertDeviceInfoToAudioDeviceDescriptor(sptr<AudioDeviceDescriptor> audioDeviceDescriptor,const DeviceInfo & deviceInfo)281 void NapiParamUtils::ConvertDeviceInfoToAudioDeviceDescriptor(sptr<AudioDeviceDescriptor> audioDeviceDescriptor,
282     const DeviceInfo &deviceInfo)
283 {
284     CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "audioDeviceDescriptor is nullptr");
285     audioDeviceDescriptor->deviceRole_ = deviceInfo.deviceRole;
286     audioDeviceDescriptor->deviceType_ = deviceInfo.deviceType;
287     audioDeviceDescriptor->deviceId_ = deviceInfo.deviceId;
288     audioDeviceDescriptor->channelMasks_ = deviceInfo.channelMasks;
289     audioDeviceDescriptor->channelIndexMasks_ = deviceInfo.channelIndexMasks;
290     audioDeviceDescriptor->deviceName_ = deviceInfo.deviceName;
291     audioDeviceDescriptor->macAddress_ = deviceInfo.macAddress;
292     audioDeviceDescriptor->interruptGroupId_ = deviceInfo.interruptGroupId;
293     audioDeviceDescriptor->volumeGroupId_ = deviceInfo.volumeGroupId;
294     audioDeviceDescriptor->networkId_ = deviceInfo.networkId;
295     audioDeviceDescriptor->displayName_ = deviceInfo.displayName;
296     audioDeviceDescriptor->audioStreamInfo_.samplingRate = deviceInfo.audioStreamInfo.samplingRate;
297     audioDeviceDescriptor->audioStreamInfo_.encoding = deviceInfo.audioStreamInfo.encoding;
298     audioDeviceDescriptor->audioStreamInfo_.format = deviceInfo.audioStreamInfo.format;
299     audioDeviceDescriptor->audioStreamInfo_.channels = deviceInfo.audioStreamInfo.channels;
300 }
301 
GetRendererOptions(const napi_env & env,AudioRendererOptions * opts,napi_value in)302 napi_status NapiParamUtils::GetRendererOptions(const napi_env &env, AudioRendererOptions *opts, napi_value in)
303 {
304     napi_value res = nullptr;
305 
306     napi_status status = napi_get_named_property(env, in, "rendererInfo", &res);
307     if (status == napi_ok) {
308         GetRendererInfo(env, &(opts->rendererInfo), res);
309     }
310 
311     status = napi_get_named_property(env, in, "streamInfo", &res);
312     if (status == napi_ok) {
313         GetStreamInfo(env, &(opts->streamInfo), res);
314     }
315 
316     int32_t intValue = {};
317     status = GetValueInt32(env, "privacyType", intValue, in);
318     if (status == napi_ok) {
319         opts->privacyType = static_cast<AudioPrivacyType>(intValue);
320     }
321 
322     return napi_ok;
323 }
324 
GetRendererInfo(const napi_env & env,AudioRendererInfo * rendererInfo,napi_value in)325 napi_status NapiParamUtils::GetRendererInfo(const napi_env &env, AudioRendererInfo *rendererInfo, napi_value in)
326 {
327     napi_valuetype valueType = napi_undefined;
328     napi_typeof(env, in, &valueType);
329     CHECK_AND_RETURN_RET_LOG(valueType == napi_object, napi_invalid_arg,
330         "GetRendererInfo failed, vauleType is not object");
331 
332     int32_t intValue = {0};
333     napi_status status = GetValueInt32(env, "content", intValue, in);
334     if (status == napi_ok) {
335         rendererInfo->contentType = static_cast<ContentType>(intValue);
336     }
337 
338     status = GetValueInt32(env, "usage", intValue, in);
339     if (status == napi_ok) {
340         if (NapiAudioEnum::IsLegalInputArgumentStreamUsage(intValue)) {
341             rendererInfo->streamUsage = static_cast<StreamUsage>(intValue);
342         } else {
343             rendererInfo->streamUsage = StreamUsage::STREAM_USAGE_INVALID;
344         }
345     }
346 
347     GetValueInt32(env, "rendererFlags", rendererInfo->rendererFlags, in);
348 
349     return napi_ok;
350 }
351 
SetRendererInfo(const napi_env & env,const AudioRendererInfo & rendererInfo,napi_value & result)352 napi_status NapiParamUtils::SetRendererInfo(const napi_env &env, const AudioRendererInfo &rendererInfo,
353     napi_value &result)
354 {
355     napi_status status = napi_create_object(env, &result);
356     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetRendererInfo napi_create_object failed");
357     SetValueInt32(env, "content", static_cast<int32_t>(rendererInfo.contentType), result);
358     SetValueInt32(env, "usage", static_cast<int32_t>(rendererInfo.streamUsage), result);
359     SetValueInt32(env, "rendererFlags", rendererInfo.rendererFlags, result);
360 
361     return napi_ok;
362 }
363 
GetStreamInfo(const napi_env & env,AudioStreamInfo * streamInfo,napi_value in)364 napi_status NapiParamUtils::GetStreamInfo(const napi_env &env, AudioStreamInfo *streamInfo, napi_value in)
365 {
366     int32_t intValue = {0};
367     napi_status status = GetValueInt32(env, "samplingRate", intValue, in);
368     if (status == napi_ok) {
369         if (intValue >= SAMPLE_RATE_8000 && intValue <= SAMPLE_RATE_192000) {
370             streamInfo->samplingRate = static_cast<AudioSamplingRate>(intValue);
371         } else {
372             AUDIO_ERR_LOG("invaild samplingRate");
373             return napi_generic_failure;
374         }
375     }
376 
377     status = GetValueInt32(env, "channels", intValue, in);
378     if (status == napi_ok) {
379         streamInfo->channels = static_cast<AudioChannel>(intValue);
380     }
381 
382     status = GetValueInt32(env, "sampleFormat", intValue, in);
383     if (status == napi_ok) {
384         streamInfo->format = static_cast<OHOS::AudioStandard::AudioSampleFormat>(intValue);
385     }
386 
387     status = GetValueInt32(env, "encodingType", intValue, in);
388     if (status == napi_ok) {
389         streamInfo->encoding = static_cast<AudioEncodingType>(intValue);
390     }
391 
392     int64_t int64Value = 0;
393     status = GetValueInt64(env, "channelLayout", int64Value, in);
394     if (status == napi_ok) {
395         streamInfo->channelLayout = static_cast<AudioChannelLayout>(int64Value);
396     }
397 
398     return napi_ok;
399 }
400 
SetStreamInfo(const napi_env & env,const AudioStreamInfo & streamInfo,napi_value & result)401 napi_status NapiParamUtils::SetStreamInfo(const napi_env &env, const AudioStreamInfo &streamInfo, napi_value &result)
402 {
403     napi_status status = napi_create_object(env, &result);
404     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetStreamInfo napi_create_object failed");
405     SetValueInt32(env, "samplingRate", static_cast<int32_t>(streamInfo.samplingRate), result);
406     SetValueInt32(env, "channels", static_cast<int32_t>(streamInfo.channels), result);
407     SetValueInt32(env, "sampleFormat", static_cast<int32_t>(streamInfo.format), result);
408     SetValueInt32(env, "encodingType", static_cast<int32_t>(streamInfo.encoding), result);
409     SetValueInt64(env, "channelLayout", static_cast<uint64_t>(streamInfo.channelLayout), result);
410 
411     return napi_ok;
412 }
413 
SetValueInt32Element(const napi_env & env,const std::string & fieldStr,const std::vector<int32_t> & values,napi_value & result)414 napi_status NapiParamUtils::SetValueInt32Element(const napi_env &env, const std::string &fieldStr,
415     const std::vector<int32_t> &values, napi_value &result)
416 {
417     napi_value jsValues = nullptr;
418     napi_status status = napi_create_array_with_length(env, values.size(), &jsValues);
419     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32Element napi_create_array_with_length failed");
420     size_t count = 0;
421     for (const auto &value : values) {
422         napi_value jsValue = nullptr;
423         status = SetValueInt32(env, value, jsValue);
424         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32Element SetValueInt32 failed");
425         status = napi_set_element(env, jsValues, count, jsValue);
426         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32Element napi_set_element failed");
427         count++;
428     }
429     status = napi_set_named_property(env, result, fieldStr.c_str(), jsValues);
430     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueInt32Element napi_set_named_property failed");
431     return status;
432 }
433 
SetDeviceDescriptor(const napi_env & env,const AudioDeviceDescriptor & deviceInfo,napi_value & result)434 napi_status NapiParamUtils::SetDeviceDescriptor(const napi_env &env, const AudioDeviceDescriptor &deviceInfo,
435     napi_value &result)
436 {
437     (void)napi_create_object(env, &result);
438     SetValueInt32(env, "deviceRole", static_cast<int32_t>(deviceInfo.deviceRole_), result);
439     SetValueInt32(env, "deviceType", static_cast<int32_t>(deviceInfo.deviceType_), result);
440     SetValueInt32(env, "id", static_cast<int32_t>(deviceInfo.deviceId_), result);
441     SetValueString(env, "name", deviceInfo.deviceName_, result);
442     SetValueString(env, "address", deviceInfo.macAddress_, result);
443     SetValueString(env, "networkId", deviceInfo.networkId_, result);
444     SetValueString(env, "displayName", deviceInfo.displayName_, result);
445     SetValueInt32(env, "interruptGroupId", static_cast<int32_t>(deviceInfo.interruptGroupId_), result);
446     SetValueInt32(env, "volumeGroupId", static_cast<int32_t>(deviceInfo.volumeGroupId_), result);
447 
448     napi_value value = nullptr;
449     napi_value sampleRates;
450     size_t size = deviceInfo.audioStreamInfo_.samplingRate.size();
451     napi_create_array_with_length(env, size, &sampleRates);
452     size_t count = 0;
453     for (const auto &samplingRate : deviceInfo.audioStreamInfo_.samplingRate) {
454         napi_create_int32(env, samplingRate, &value);
455         napi_set_element(env, sampleRates, count, value);
456         count++;
457     }
458     napi_set_named_property(env, result, "sampleRates", sampleRates);
459 
460     napi_value channelCounts;
461     size = deviceInfo.audioStreamInfo_.channels.size();
462     napi_create_array_with_length(env, size, &channelCounts);
463     count = 0;
464     for (const auto &channels : deviceInfo.audioStreamInfo_.channels) {
465         napi_create_int32(env, channels, &value);
466         napi_set_element(env, channelCounts, count, value);
467         count++;
468     }
469     napi_set_named_property(env, result, "channelCounts", channelCounts);
470 
471     std::vector<int32_t> channelMasks_;
472     channelMasks_.push_back(deviceInfo.channelMasks_);
473     SetValueInt32Element(env, "channelMasks", channelMasks_, result);
474 
475     std::vector<int32_t> channelIndexMasks_;
476     channelIndexMasks_.push_back(deviceInfo.channelIndexMasks_);
477     SetValueInt32Element(env, "channelIndexMasks", channelIndexMasks_, result);
478 
479     std::vector<int32_t> encoding;
480     encoding.push_back(deviceInfo.audioStreamInfo_.encoding);
481     SetValueInt32Element(env, "encodingTypes", encoding, result);
482 
483     return napi_ok;
484 }
485 
SetDeviceDescriptors(const napi_env & env,const std::vector<sptr<AudioDeviceDescriptor>> & deviceDescriptors,napi_value & result)486 napi_status NapiParamUtils::SetDeviceDescriptors(const napi_env &env,
487     const std::vector<sptr<AudioDeviceDescriptor>> &deviceDescriptors, napi_value &result)
488 {
489     napi_status status = napi_create_array_with_length(env, deviceDescriptors.size(), &result);
490     for (size_t i = 0; i < deviceDescriptors.size(); i++) {
491         if (deviceDescriptors[i] != nullptr) {
492             napi_value valueParam = nullptr;
493             SetDeviceDescriptor(env, deviceDescriptors[i], valueParam);
494             napi_set_element(env, result, i, valueParam);
495         }
496     }
497     return status;
498 }
SetAudioSpatialEnabledStateForDevice(const napi_env & env,const AudioSpatialEnabledStateForDevice audioSpatialEnabledStateForDevice,napi_value & result)499 napi_status NapiParamUtils::SetAudioSpatialEnabledStateForDevice(const napi_env &env,
500     const AudioSpatialEnabledStateForDevice audioSpatialEnabledStateForDevice, napi_value &result)
501 {
502     (void)napi_create_object(env, &result);
503     napi_value jsArray;
504     NapiParamUtils::SetDeviceDescriptor(env, audioSpatialEnabledStateForDevice.deviceDescriptor, jsArray);
505     napi_set_named_property(env, result, "deviceDescriptor", jsArray);
506 
507     NapiParamUtils::SetValueBoolean(env, "enabled", audioSpatialEnabledStateForDevice.enabled, result);
508     return napi_ok;
509 }
510 
SetValueDeviceInfo(const napi_env & env,const DeviceInfo & deviceInfo,napi_value & result)511 napi_status NapiParamUtils::SetValueDeviceInfo(const napi_env &env, const DeviceInfo &deviceInfo, napi_value &result)
512 {
513     std::vector<sptr<AudioDeviceDescriptor>> deviceDescriptors;
514     sptr<AudioDeviceDescriptor> audioDeviceDescriptor = new(std::nothrow) AudioDeviceDescriptor();
515     CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptor != nullptr, napi_generic_failure,
516         "audioDeviceDescriptor malloc failed");
517     ConvertDeviceInfoToAudioDeviceDescriptor(audioDeviceDescriptor, deviceInfo);
518     deviceDescriptors.push_back(std::move(audioDeviceDescriptor));
519     SetDeviceDescriptors(env, deviceDescriptors, result);
520     return napi_ok;
521 }
522 
SetInterruptEvent(const napi_env & env,const InterruptEvent & interruptEvent,napi_value & result)523 napi_status NapiParamUtils::SetInterruptEvent(const napi_env &env, const InterruptEvent &interruptEvent,
524     napi_value &result)
525 {
526     napi_create_object(env, &result);
527     SetValueInt32(env, "eventType", static_cast<int32_t>(interruptEvent.eventType), result);
528     SetValueInt32(env, "forceType", static_cast<int32_t>(interruptEvent.forceType), result);
529     SetValueInt32(env, "hintType", static_cast<int32_t>(interruptEvent.hintType), result);
530     return napi_ok;
531 }
532 
SetNativeAudioRendererDataInfo(const napi_env & env,const AudioRendererDataInfo & audioRendererDataInfo,napi_value & result)533 napi_status NapiParamUtils::SetNativeAudioRendererDataInfo(const napi_env &env,
534     const AudioRendererDataInfo &audioRendererDataInfo, napi_value &result)
535 {
536     napi_status status = napi_create_object(env, &result);
537 
538     SetValueInt32(env, "flag", static_cast<int32_t>(audioRendererDataInfo.flag), result);
539     CreateArrayBuffer(env, "buffer", audioRendererDataInfo.flag, audioRendererDataInfo.buffer, result);
540 
541     return status;
542 }
543 
GetCapturerInfo(const napi_env & env,AudioCapturerInfo * capturerInfo,napi_value in)544 napi_status NapiParamUtils::GetCapturerInfo(const napi_env &env, AudioCapturerInfo *capturerInfo, napi_value in)
545 {
546     int32_t intValue = {0};
547     napi_status status = NapiParamUtils::GetValueInt32(env, "source", intValue, in);
548     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetCapturerInfo GetValueInt32 source failed");
549     CHECK_AND_RETURN_RET_LOG(NapiAudioEnum::IsLegalCapturerType(intValue),
550         napi_generic_failure, "Invailed captureType");
551     capturerInfo->sourceType = static_cast<SourceType>(intValue);
552 
553     status = NapiParamUtils::GetValueInt32(env, "capturerFlags", capturerInfo->capturerFlags, in);
554     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetCapturerInfo GetValueInt32 capturerFlags failed");
555     return status;
556 }
557 
SetCapturerInfo(const napi_env & env,const AudioCapturerInfo & capturerInfo,napi_value & result)558 napi_status NapiParamUtils::SetCapturerInfo(const napi_env &env,
559     const AudioCapturerInfo &capturerInfo, napi_value &result)
560 {
561     napi_status status = napi_create_object(env, &result);
562     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetCapturerInfo napi_create_object failed");
563     SetValueInt32(env, "source", static_cast<int32_t>(capturerInfo.sourceType), result);
564     SetValueInt32(env, "capturerFlags", static_cast<int32_t>(capturerInfo.capturerFlags), result);
565 
566     return napi_ok;
567 }
568 
GetCaptureFilterOptionsVector(const napi_env & env,CaptureFilterOptions * filterOptions,napi_value in)569 napi_status NapiParamUtils::GetCaptureFilterOptionsVector(const napi_env &env,
570     CaptureFilterOptions *filterOptions, napi_value in)
571 {
572     napi_value usagesValue = nullptr;
573     napi_status status = napi_get_named_property(env, in, "usages", &usagesValue);
574     CHECK_AND_RETURN_RET_LOG(status == napi_ok, napi_ok, "GetUsages failed");
575 
576     uint32_t arrayLen = 0;
577     napi_get_array_length(env, usagesValue, &arrayLen);
578 
579     if (arrayLen == 0) {
580         filterOptions->usages = {};
581         AUDIO_INFO_LOG("ParseCaptureFilterOptions get empty usage");
582         return napi_ok;
583     }
584 
585     for (size_t i = 0; i < static_cast<size_t>(arrayLen); i++) {
586         napi_value element;
587         if (napi_get_element(env, usagesValue, i, &element) == napi_ok) {
588             int32_t val = {0};
589             napi_get_value_int32(env, element, &val);
590             filterOptions->usages.emplace_back(static_cast<StreamUsage>(val));
591         }
592     }
593 
594     return napi_ok;
595 }
596 
GetPlaybackCaptureConfig(const napi_env & env,AudioPlaybackCaptureConfig * captureConfig,napi_value in)597 napi_status NapiParamUtils::GetPlaybackCaptureConfig(const napi_env &env,
598     AudioPlaybackCaptureConfig *captureConfig, napi_value in)
599 {
600     napi_value res = nullptr;
601 
602     if (napi_get_named_property(env, in, "filterOptions", &res) == napi_ok) {
603         return GetCaptureFilterOptionsVector(env, &(captureConfig->filterOptions), res);
604     }
605 
606     return NapiParamUtils::GetCaptureFilterOptionsVector(env, &(captureConfig->filterOptions), res);
607 }
608 
GetCapturerOptions(const napi_env & env,AudioCapturerOptions * opts,napi_value in)609 napi_status NapiParamUtils::GetCapturerOptions(const napi_env &env, AudioCapturerOptions *opts, napi_value in)
610 {
611     napi_value result = nullptr;
612     napi_status status = napi_get_named_property(env, in, "streamInfo", &result);
613     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get streamInfo name failed");
614 
615     status = GetStreamInfo(env, &(opts->streamInfo), result);
616     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "ParseStreamInfo failed");
617 
618     status = napi_get_named_property(env, in, "capturerInfo", &result);
619     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get capturerInfo name failed");
620 
621     status = GetCapturerInfo(env, &(opts->capturerInfo), result);
622     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "ParseCapturerInfo failed");
623 
624     if (napi_get_named_property(env, in, "playbackCaptureConfig", &result) == napi_ok) {
625         return GetPlaybackCaptureConfig(env, &(opts->playbackCaptureConfig), result);
626     }
627 
628     AUDIO_INFO_LOG("ParseCapturerOptions, without playbackCaptureConfig");
629     return napi_ok;
630 }
631 
SetAudioCapturerChangeInfoDescriptors(const napi_env & env,const AudioCapturerChangeInfo & changeInfo,napi_value & result)632 napi_status NapiParamUtils::SetAudioCapturerChangeInfoDescriptors(const napi_env &env,
633     const AudioCapturerChangeInfo &changeInfo, napi_value &result)
634 {
635     napi_status status = napi_create_object(env, &result);
636     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "audioDeviceDescriptor malloc failed");
637     SetValueInt32(env, "streamId", changeInfo.sessionId, result);
638     SetValueInt32(env, "clientUid", changeInfo.clientUID, result);
639     SetValueInt32(env, "capturerState", static_cast<int32_t>(changeInfo.capturerState), result);
640     SetValueBoolean(env, "muted", changeInfo.muted, result);
641 
642     napi_value jsCapInfoObj = nullptr;
643     SetCapturerInfo(env, changeInfo.capturerInfo, jsCapInfoObj);
644     napi_set_named_property(env, result, "capturerInfo", jsCapInfoObj);
645 
646     napi_value deviceInfo = nullptr;
647     SetValueDeviceInfo(env, changeInfo.inputDeviceInfo, deviceInfo);
648     napi_set_named_property(env, result, "deviceDescriptors", deviceInfo);
649     return status;
650 }
651 
SetMicrophoneDescriptor(const napi_env & env,const sptr<MicrophoneDescriptor> & micDesc,napi_value & result)652 napi_status NapiParamUtils::SetMicrophoneDescriptor(const napi_env &env, const sptr<MicrophoneDescriptor> &micDesc,
653     napi_value &result)
654 {
655     napi_create_object(env, &result);
656     napi_value jsPositionObj = nullptr;
657     napi_value jsOrientationObj = nullptr;
658 
659     SetValueInt32(env, "id", micDesc->micId_, result);
660     SetValueInt32(env, "deviceType", static_cast<int32_t>(micDesc->deviceType_), result);
661     SetValueInt32(env, "groupId", micDesc->groupId_, result);
662     SetValueInt32(env, "sensitivity", micDesc->sensitivity_, result);
663     napi_create_object(env, &jsPositionObj);
664     SetValueDouble(env, "x", micDesc->position_.x, jsPositionObj);
665     SetValueDouble(env, "y", micDesc->position_.y, jsPositionObj);
666     SetValueDouble(env, "z", micDesc->position_.z, jsPositionObj);
667     napi_set_named_property(env, result, "position", jsPositionObj);
668 
669     napi_create_object(env, &jsOrientationObj);
670     SetValueDouble(env, "x", micDesc->orientation_.x, jsOrientationObj);
671     SetValueDouble(env, "y", micDesc->orientation_.y, jsOrientationObj);
672     SetValueDouble(env, "z", micDesc->orientation_.z, jsOrientationObj);
673     napi_set_named_property(env, result, "orientation", jsOrientationObj);
674     return napi_ok;
675 }
676 
SetMicrophoneDescriptors(const napi_env & env,const std::vector<sptr<MicrophoneDescriptor>> & micDescs,napi_value & result)677 napi_status NapiParamUtils::SetMicrophoneDescriptors(const napi_env &env,
678     const std::vector<sptr<MicrophoneDescriptor>> &micDescs, napi_value &result)
679 {
680     napi_status status = napi_create_array_with_length(env, micDescs.size(), &result);
681     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "audioDeviceDescriptor malloc failed");
682     int32_t index = 0;
683     for (const auto &micDesc : micDescs) {
684         napi_value valueParam = nullptr;
685         SetMicrophoneDescriptor(env, micDesc, valueParam);
686         napi_set_element(env, result, index, valueParam);
687         index++;
688     }
689     return status;
690 }
691 
SetValueMicStateChange(const napi_env & env,const MicStateChangeEvent & micStateChangeEvent,napi_value & result)692 napi_status NapiParamUtils::SetValueMicStateChange(const napi_env &env, const MicStateChangeEvent &micStateChangeEvent,
693     napi_value &result)
694 {
695     napi_create_object(env, &result);
696     NapiParamUtils::SetValueBoolean(env, "mute", micStateChangeEvent.mute, result);
697     return napi_ok;
698 }
699 
SetVolumeGroupInfos(const napi_env & env,const std::vector<sptr<VolumeGroupInfo>> & volumeGroupInfos,napi_value & result)700 napi_status NapiParamUtils::SetVolumeGroupInfos(const napi_env &env,
701     const std::vector<sptr<VolumeGroupInfo>> &volumeGroupInfos, napi_value &result)
702 {
703     napi_value valueParam = nullptr;
704     napi_status status = napi_create_array_with_length(env, volumeGroupInfos.size(), &result);
705     for (size_t i = 0; i < volumeGroupInfos.size(); i++) {
706         if (volumeGroupInfos[i] != nullptr) {
707             (void)napi_create_object(env, &valueParam);
708             SetValueString(env, "networkId", static_cast<std::string>(
709                 volumeGroupInfos[i]->networkId_), valueParam);
710             SetValueInt32(env, "groupId", static_cast<int32_t>(
711                 volumeGroupInfos[i]->volumeGroupId_), valueParam);
712             SetValueInt32(env, "mappingId", static_cast<int32_t>(
713                 volumeGroupInfos[i]->mappingId_), valueParam);
714             SetValueString(env, "groupName", static_cast<std::string>(
715                 volumeGroupInfos[i]->groupName_), valueParam);
716             SetValueInt32(env, "ConnectType", static_cast<int32_t>(
717                 volumeGroupInfos[i]->connectType_), valueParam);
718             napi_set_element(env, result, i, valueParam);
719         }
720     }
721     return status;
722 }
723 
SetValueVolumeEvent(const napi_env & env,const VolumeEvent & volumeEvent,napi_value & result)724 napi_status NapiParamUtils::SetValueVolumeEvent(const napi_env& env, const VolumeEvent &volumeEvent,
725     napi_value &result)
726 {
727     napi_create_object(env, &result);
728     SetValueInt32(env, "volumeType",
729         NapiAudioEnum::GetJsAudioVolumeType(static_cast<AudioStreamType>(volumeEvent.volumeType)), result);
730     SetValueInt32(env, "volume", static_cast<int32_t>(volumeEvent.volume), result);
731     SetValueBoolean(env, "updateUi", volumeEvent.updateUi, result);
732     SetValueInt32(env, "volumeGroupId", volumeEvent.volumeGroupId, result);
733     SetValueString(env, "networkId", volumeEvent.networkId, result);
734     return napi_ok;
735 }
736 
GetAudioDeviceDescriptor(const napi_env & env,sptr<AudioDeviceDescriptor> & selectedAudioDevice,bool & argTransFlag,napi_value in)737 napi_status NapiParamUtils::GetAudioDeviceDescriptor(const napi_env &env,
738     sptr<AudioDeviceDescriptor> &selectedAudioDevice, bool &argTransFlag, napi_value in)
739 {
740     int32_t intValue = {0};
741     argTransFlag = true;
742     bool hasDeviceRole = true;
743     bool hasNetworkId  = true;
744     napi_status status = napi_has_named_property(env, in, "deviceRole", &hasDeviceRole);
745     status = napi_has_named_property(env, in, "networkId", &hasNetworkId);
746     if ((!hasDeviceRole) || (!hasNetworkId)) {
747         argTransFlag = false;
748         return status;
749     }
750 
751     status = GetValueInt32(env, "deviceRole", intValue, in);
752     if (status == napi_ok) {
753         if (std::find(DEVICE_ROLE_SET.begin(), DEVICE_ROLE_SET.end(), intValue) == DEVICE_ROLE_SET.end()) {
754             argTransFlag = false;
755             return status;
756         }
757         selectedAudioDevice->deviceRole_ = static_cast<DeviceRole>(intValue);
758     }
759 
760     status = GetValueInt32(env, "deviceType", intValue, in);
761     if (status == napi_ok) {
762         if (std::find(DEVICE_TYPE_SET.begin(), DEVICE_TYPE_SET.end(), intValue) == DEVICE_TYPE_SET.end()) {
763             argTransFlag = false;
764             return status;
765         }
766         selectedAudioDevice->deviceType_ = static_cast<DeviceType>(intValue);
767     }
768 
769     selectedAudioDevice->networkId_ = GetPropertyString(env, in, "networkId");
770 
771     selectedAudioDevice->displayName_ = GetPropertyString(env, in, "displayName");
772 
773     status = GetValueInt32(env, "interruptGroupId", intValue, in);
774     if (status == napi_ok) {
775         selectedAudioDevice->interruptGroupId_ = intValue;
776     }
777 
778     status = GetValueInt32(env, "volumeGroupId", intValue, in);
779     if (status == napi_ok) {
780         selectedAudioDevice->volumeGroupId_ = intValue;
781     }
782 
783     selectedAudioDevice->macAddress_ = GetPropertyString(env, in, "address");
784 
785     status = GetValueInt32(env, "id", intValue, in);
786     if (status == napi_ok) {
787         selectedAudioDevice->deviceId_ = intValue;
788     }
789 
790     return napi_ok;
791 }
792 
GetAudioDeviceDescriptorVector(const napi_env & env,std::vector<sptr<AudioDeviceDescriptor>> & deviceDescriptorsVector,bool & argTransFlag,napi_value in)793 napi_status NapiParamUtils::GetAudioDeviceDescriptorVector(const napi_env &env,
794     std::vector<sptr<AudioDeviceDescriptor>> &deviceDescriptorsVector, bool &argTransFlag, napi_value in)
795 {
796     uint32_t arrayLen = 0;
797     napi_get_array_length(env, in, &arrayLen);
798     if (arrayLen == 0) {
799         deviceDescriptorsVector = {};
800         AUDIO_INFO_LOG("Error: AudioDeviceDescriptor vector is NULL!");
801     }
802 
803     for (size_t i = 0; i < arrayLen; i++) {
804         napi_value element;
805         napi_get_element(env, in, i, &element);
806         sptr<AudioDeviceDescriptor> selectedAudioDevice = new(std::nothrow) AudioDeviceDescriptor();
807         GetAudioDeviceDescriptor(env, selectedAudioDevice, argTransFlag, element);
808         if (!argTransFlag) {
809             return napi_ok;
810         }
811         deviceDescriptorsVector.push_back(selectedAudioDevice);
812     }
813     return napi_ok;
814 }
815 
GetAudioCapturerFilter(const napi_env & env,sptr<AudioCapturerFilter> & audioCapturerFilter,napi_value in)816 napi_status NapiParamUtils::GetAudioCapturerFilter(const napi_env &env, sptr<AudioCapturerFilter> &audioCapturerFilter,
817     napi_value in)
818 {
819     int32_t intValue = {0};
820     audioCapturerFilter = new(std::nothrow) AudioCapturerFilter();
821 
822     napi_status status = GetValueInt32(env, "uid", intValue, in);
823     if (status == napi_ok) {
824         audioCapturerFilter->uid = intValue;
825     }
826 
827     return napi_ok;
828 }
829 
GetAudioCapturerInfo(const napi_env & env,AudioCapturerInfo * capturerInfo,napi_value in)830 napi_status NapiParamUtils::GetAudioCapturerInfo(const napi_env &env, AudioCapturerInfo *capturerInfo, napi_value in)
831 {
832     napi_valuetype valueType = napi_undefined;
833     napi_typeof(env, in, &valueType);
834     CHECK_AND_RETURN_RET_LOG(valueType == napi_object, napi_invalid_arg,
835         "GetRendererInfo failed, vauleType is not object");
836 
837     int32_t intValue = {0};
838     napi_value tempValue = nullptr;
839     napi_status status = napi_get_named_property(env, in, "source", &tempValue);
840     if (status == napi_ok) {
841         GetValueInt32(env, intValue, tempValue);
842         if (NapiAudioEnum::IsValidSourceType(intValue)) {
843             capturerInfo->sourceType = static_cast<SourceType>(intValue);
844         } else {
845             capturerInfo->sourceType = SourceType::SOURCE_TYPE_INVALID;
846         }
847     }
848 
849     status = GetValueInt32(env, "capturerFlags", intValue, in);
850     if (status == napi_ok) {
851         capturerInfo->capturerFlags = intValue;
852     }
853 
854     return napi_ok;
855 }
856 
GetAudioRendererFilter(const napi_env & env,sptr<AudioRendererFilter> & audioRendererFilter,bool & argTransFlag,napi_value in)857 napi_status NapiParamUtils::GetAudioRendererFilter(const napi_env &env, sptr<AudioRendererFilter> &audioRendererFilter,
858     bool &argTransFlag, napi_value in)
859 {
860     napi_value tempValue = nullptr;
861     int32_t intValue = {0};
862     argTransFlag = true;
863     audioRendererFilter = new(std::nothrow) AudioRendererFilter();
864 
865     napi_status status = GetValueInt32(env, "uid", intValue, in);
866     if (status == napi_ok) {
867         audioRendererFilter->uid = intValue;
868     }
869 
870     if (napi_get_named_property(env, in, "rendererInfo", &tempValue) == napi_ok) {
871         GetRendererInfo(env, &(audioRendererFilter->rendererInfo), tempValue);
872     }
873 
874     status = GetValueInt32(env, "rendererId", intValue, in);
875     if (status == napi_ok) {
876         audioRendererFilter->streamId = intValue;
877     }
878 
879     return napi_ok;
880 }
881 
SetValueDeviceChangeAction(const napi_env & env,const DeviceChangeAction & action,napi_value & result)882 napi_status NapiParamUtils::SetValueDeviceChangeAction(const napi_env& env, const DeviceChangeAction &action,
883     napi_value &result)
884 {
885     napi_create_object(env, &result);
886     NapiParamUtils::SetValueInt32(env, "type", static_cast<int32_t>(action.type), result);
887 
888     napi_value jsArray;
889     NapiParamUtils::SetDeviceDescriptors(env, action.deviceDescriptors, jsArray);
890     napi_set_named_property(env, result, "deviceDescriptors", jsArray);
891     return napi_ok;
892 }
893 
SetValueBlockedDeviceAction(const napi_env & env,const MicrophoneBlockedInfo & action,napi_value & result)894 napi_status NapiParamUtils::SetValueBlockedDeviceAction(const napi_env& env, const MicrophoneBlockedInfo &action,
895     napi_value &result)
896 {
897     napi_create_object(env, &result);
898     NapiParamUtils::SetValueInt32(env, "blockStatus", static_cast<int32_t>(action.blockStatus), result);
899 
900     napi_value jsArray;
901     NapiParamUtils::SetDeviceDescriptors(env, action.devices, jsArray);
902     napi_set_named_property(env, result, "devices", jsArray);
903     return napi_ok;
904 }
905 
SetRendererChangeInfos(const napi_env & env,const std::vector<std::unique_ptr<AudioRendererChangeInfo>> & changeInfos,napi_value & result)906 napi_status NapiParamUtils::SetRendererChangeInfos(const napi_env &env,
907     const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &changeInfos, napi_value &result)
908 {
909     int32_t position = 0;
910     napi_value jsChangeInfoObj = nullptr;
911     napi_value jsRenInfoObj = nullptr;
912     napi_create_array_with_length(env, changeInfos.size(), &result);
913     for (const auto &changeInfo : changeInfos) {
914         if (changeInfo) {
915             napi_create_object(env, &jsChangeInfoObj);
916             SetValueInt32(env, "streamId", changeInfo->sessionId, jsChangeInfoObj);
917             SetValueInt32(env, "rendererState", static_cast<int32_t>(changeInfo->rendererState), jsChangeInfoObj);
918             SetValueInt32(env, "clientUid", changeInfo->clientUID, jsChangeInfoObj);
919             SetRendererInfo(env, changeInfo->rendererInfo, jsRenInfoObj);
920             napi_set_named_property(env, jsChangeInfoObj, "rendererInfo", jsRenInfoObj);
921             napi_value deviceInfo = nullptr;
922             SetValueDeviceInfo(env, changeInfo->outputDeviceInfo, deviceInfo);
923             napi_set_named_property(env, jsChangeInfoObj, "deviceDescriptors", deviceInfo);
924             napi_set_element(env, result, position, jsChangeInfoObj);
925             position++;
926         }
927     }
928     return napi_ok;
929 }
930 
SetCapturerChangeInfos(const napi_env & env,const std::vector<std::unique_ptr<AudioCapturerChangeInfo>> & changeInfos,napi_value & result)931 napi_status NapiParamUtils::SetCapturerChangeInfos(const napi_env &env,
932     const std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &changeInfos, napi_value &result)
933 {
934     int32_t position = 0;
935     napi_value jsChangeInfoObj = nullptr;
936     napi_create_array_with_length(env, changeInfos.size(), &result);
937     for (const auto &changeInfo : changeInfos) {
938         if (changeInfo) {
939             SetAudioCapturerChangeInfoDescriptors(env, *changeInfo, jsChangeInfoObj);
940             napi_set_element(env, result, position, jsChangeInfoObj);
941             position++;
942         }
943     }
944     return napi_ok;
945 }
946 
SetEffectInfo(const napi_env & env,const AudioSceneEffectInfo & audioSceneEffectInfo,napi_value & result)947 napi_status NapiParamUtils::SetEffectInfo(const napi_env &env,
948     const AudioSceneEffectInfo &audioSceneEffectInfo, napi_value &result)
949 {
950     int32_t position = 0;
951     napi_value jsEffectInofObj = nullptr;
952     napi_create_array_with_length(env, audioSceneEffectInfo.mode.size(), &result);
953     napi_create_object(env, &jsEffectInofObj);
954     for (const auto &mode : audioSceneEffectInfo.mode) {
955         SetValueUInt32(env, mode, jsEffectInofObj);
956         napi_set_element(env, result, position, jsEffectInofObj);
957         position++;
958     }
959     return napi_ok;
960 }
961 
GetAudioInterrupt(const napi_env & env,AudioInterrupt & audioInterrupt,napi_value in)962 napi_status NapiParamUtils::GetAudioInterrupt(const napi_env &env, AudioInterrupt &audioInterrupt,
963     napi_value in)
964 {
965     int32_t propValue = -1;
966     napi_status status = NapiParamUtils::GetValueInt32(env, "contentType", propValue, in);
967     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetAudioInterrupt: Failed to retrieve contentType");
968     audioInterrupt.contentType = static_cast<ContentType>(propValue);
969 
970     status = NapiParamUtils::GetValueInt32(env, "streamUsage", propValue, in);
971     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetAudioInterrupt: Failed to retrieve streamUsage");
972     audioInterrupt.streamUsage = static_cast<StreamUsage>(propValue);
973 
974     status = NapiParamUtils::GetValueBoolean(env, "pauseWhenDucked", audioInterrupt.pauseWhenDucked, in);
975     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "GetAudioInterrupt: Failed to retrieve pauseWhenDucked");
976     audioInterrupt.audioFocusType.streamType = AudioSystemManager::GetStreamType(audioInterrupt.contentType,
977         audioInterrupt.streamUsage);
978     return status;
979 }
980 
SetValueInterruptAction(const napi_env & env,const InterruptAction & interruptAction,napi_value & result)981 napi_status NapiParamUtils::SetValueInterruptAction(const napi_env &env, const InterruptAction &interruptAction,
982     napi_value &result)
983 {
984     napi_create_object(env, &result);
985     SetValueInt32(env, "actionType", static_cast<int32_t>(interruptAction.actionType), result);
986     SetValueInt32(env, "type", static_cast<int32_t>(interruptAction.interruptType), result);
987     SetValueInt32(env, "hint", static_cast<int32_t>(interruptAction.interruptHint), result);
988     SetValueBoolean(env, "activated", interruptAction.activated, result);
989     return napi_ok;
990 }
991 
GetSpatialDeviceState(napi_env env,AudioSpatialDeviceState * spatialDeviceState,napi_value in)992 napi_status NapiParamUtils::GetSpatialDeviceState(napi_env env, AudioSpatialDeviceState *spatialDeviceState,
993     napi_value in)
994 {
995     napi_value res = nullptr;
996     int32_t intValue = {0};
997     napi_valuetype valueType = napi_undefined;
998     napi_status status = napi_get_named_property(env, in, "address", &res);
999     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Get address name failed");
1000     napi_typeof(env, res, &valueType);
1001     CHECK_AND_RETURN_RET_LOG(valueType == napi_string, napi_invalid_arg, "Get address type failed");
1002     spatialDeviceState->address = NapiParamUtils::GetStringArgument(env, res);
1003 
1004     status = GetValueBoolean(env, "isSpatializationSupported", spatialDeviceState->isSpatializationSupported, in);
1005     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Get isSpatializationSupported failed");
1006 
1007     status = GetValueBoolean(env, "isHeadTrackingSupported", spatialDeviceState->isHeadTrackingSupported, in);
1008     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Get isHeadTrackingSupported failed");
1009 
1010     status = GetValueInt32(env, "spatialDeviceType", intValue, in);
1011     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "Get spatialDeviceType failed");
1012     CHECK_AND_RETURN_RET_LOG((intValue >= EARPHONE_TYPE_NONE) && (intValue <= EARPHONE_TYPE_OTHERS),
1013         napi_invalid_arg, "Get spatialDeviceType failed");
1014     spatialDeviceState->spatialDeviceType = static_cast<AudioSpatialDeviceType>(intValue);
1015 
1016     return napi_ok;
1017 }
1018 
GetExtraParametersSubKV(napi_env env,std::vector<std::pair<std::string,std::string>> & subKV,napi_value in)1019 napi_status NapiParamUtils::GetExtraParametersSubKV(napi_env env,
1020     std::vector<std::pair<std::string, std::string>> &subKV, napi_value in)
1021 {
1022     napi_value jsProNameList = nullptr;
1023     uint32_t jsProCount = 0;
1024     napi_status status = napi_get_property_names(env, in, &jsProNameList);
1025     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get property name failed");
1026     status = napi_get_array_length(env, jsProNameList, &jsProCount);
1027     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get subKeys length failed");
1028 
1029     napi_value jsProName = nullptr;
1030     napi_value jsProValue = nullptr;
1031     for (uint32_t i = 0; i < jsProCount; i++) {
1032         status = napi_get_element(env, jsProNameList, i, &jsProName);
1033         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get sub key failed");
1034 
1035         std::string strProName = NapiParamUtils::GetStringArgument(env, jsProName);
1036         status = napi_get_named_property(env, in, strProName.c_str(), &jsProValue);
1037         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "get sub value failed");
1038 
1039         subKV.push_back(std::make_pair(strProName, NapiParamUtils::GetStringArgument(env, jsProValue)));
1040     }
1041 
1042     return napi_ok;
1043 }
1044 
GetExtraParametersVector(const napi_env & env,std::vector<std::string> & subKeys,napi_value in)1045 napi_status NapiParamUtils::GetExtraParametersVector(const napi_env &env,
1046     std::vector<std::string> &subKeys, napi_value in)
1047 {
1048     uint32_t arrayLen = 0;
1049     napi_get_array_length(env, in, &arrayLen);
1050 
1051     for (uint32_t i = 0; i < arrayLen; i++) {
1052         napi_value element;
1053         if (napi_get_element(env, in, i, &element) == napi_ok) {
1054             subKeys.push_back(GetStringArgument(env, element));
1055         }
1056     }
1057 
1058     return napi_ok;
1059 }
1060 
SetExtraAudioParametersInfo(const napi_env & env,const std::vector<std::pair<std::string,std::string>> & extraParameters,napi_value & result)1061 napi_status NapiParamUtils::SetExtraAudioParametersInfo(const napi_env &env,
1062     const std::vector<std::pair<std::string, std::string>> &extraParameters, napi_value &result)
1063 {
1064     napi_status status = napi_create_object(env, &result);
1065     CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "malloc array buffer failed");
1066 
1067     for (auto it = extraParameters.begin(); it != extraParameters.end(); it++) {
1068         status = SetValueString(env, it->first, it->second, result);
1069         CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "SetValueString failed");
1070     }
1071 
1072     return status;
1073 }
1074 
GetAudioSessionStrategy(const napi_env & env,AudioSessionStrategy & audioSessionStrategy,napi_value in)1075 napi_status NapiParamUtils::GetAudioSessionStrategy(const napi_env &env,
1076     AudioSessionStrategy &audioSessionStrategy, napi_value in)
1077 {
1078     int32_t intValue = {0};
1079     napi_status status = napi_generic_failure;
1080     status = GetValueInt32(env, "concurrencyMode", intValue, in);
1081     if (status == napi_ok) {
1082         audioSessionStrategy.concurrencyMode = static_cast<AudioConcurrencyMode>(intValue);
1083         return napi_ok;
1084     } else {
1085         AUDIO_ERR_LOG("invaild concurrencyMode");
1086         return napi_generic_failure;
1087     }
1088 }
1089 
SetAudioSessionDeactiveEvent(const napi_env & env,const AudioSessionDeactiveEvent & deactiveEvent,napi_value & result)1090 napi_status NapiParamUtils::SetAudioSessionDeactiveEvent(
1091     const napi_env &env, const AudioSessionDeactiveEvent &deactiveEvent, napi_value &result)
1092 {
1093     napi_create_object(env, &result);
1094     SetValueInt32(env, "reason", static_cast<int32_t>(deactiveEvent.deactiveReason), result);
1095     return napi_ok;
1096 }
1097 } // namespace AudioStandard
1098 } // namespace OHOS
1099