• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #include <cstdio>
17 
18 #include "napi_utils.h"
19 #include "securec.h"
20 #include "avsession_log.h"
21 #include "av_session.h"
22 #include "napi_avcall_meta_data.h"
23 #include "napi_avcall_state.h"
24 #include "napi_meta_data.h"
25 #include "napi_playback_state.h"
26 #include "napi_media_description.h"
27 #include "napi_queue_item.h"
28 #include "native_engine/native_value.h"
29 #include "native_engine/native_engine.h"
30 #include "extension_context.h"
31 #include "ability_context.h"
32 #include "napi_common_want.h"
33 #include "napi_media_info_holder.h"
34 #include "pixel_map_napi.h"
35 #include "avsession_pixel_map_adapter.h"
36 #include "curl/curl.h"
37 #include "image_source.h"
38 #include "pixel_map.h"
39 #include "napi_avsession_controller.h"
40 
41 namespace OHOS::AVSession {
42 static constexpr int32_t STR_MAX_LENGTH = 40960;
43 static constexpr size_t STR_TAIL_LENGTH = 1;
44 
WriteCallback(std::uint8_t * ptr,size_t size,size_t nmemb,std::vector<std::uint8_t> * imgBuffer)45 size_t NapiUtils::WriteCallback(std::uint8_t *ptr, size_t size, size_t nmemb, std::vector<std::uint8_t> *imgBuffer)
46 {
47     size_t realsize = size * nmemb;
48     imgBuffer->reserve(realsize + imgBuffer->capacity());
49     for (size_t i = 0; i < realsize; i++) {
50         imgBuffer->push_back(ptr[i]);
51     }
52     return realsize;
53 }
54 
CurlSetRequestOptions(std::vector<std::uint8_t> & imgBuffer,const std::string uri)55 bool NapiUtils::CurlSetRequestOptions(std::vector<std::uint8_t>& imgBuffer, const std::string uri)
56 {
57     CURL *easyHandle_ = curl_easy_init();
58     if (easyHandle_) {
59         // set request options
60         curl_easy_setopt(easyHandle_, CURLOPT_URL, uri.c_str());
61         curl_easy_setopt(easyHandle_, CURLOPT_CONNECTTIMEOUT, NapiUtils::TIME_OUT_SECOND);
62         curl_easy_setopt(easyHandle_, CURLOPT_SSL_VERIFYPEER, 0L);
63         curl_easy_setopt(easyHandle_, CURLOPT_SSL_VERIFYHOST, 0L);
64         curl_easy_setopt(easyHandle_, CURLOPT_CAINFO, "/etc/ssl/certs/" "cacert.pem");
65         curl_easy_setopt(easyHandle_, CURLOPT_HTTPGET, 1L);
66         curl_easy_setopt(easyHandle_, CURLOPT_WRITEFUNCTION, WriteCallback);
67         curl_easy_setopt(easyHandle_, CURLOPT_WRITEDATA, &imgBuffer);
68 
69         // perform request
70         CURLcode res = curl_easy_perform(easyHandle_);
71         if (res != CURLE_OK) {
72             SLOGI("DoDownload curl easy_perform failure: %{public}s\n", curl_easy_strerror(res));
73             curl_easy_cleanup(easyHandle_);
74             easyHandle_ = nullptr;
75             return false;
76         } else {
77             int64_t httpCode = 0;
78             curl_easy_getinfo(easyHandle_, CURLINFO_RESPONSE_CODE, &httpCode);
79             SLOGI("DoDownload Http result " "%{public}" PRId64, httpCode);
80             CHECK_AND_RETURN_RET_LOG(httpCode < NapiUtils::HTTP_ERROR_CODE, false, "recv Http ERROR");
81             curl_easy_cleanup(easyHandle_);
82             easyHandle_ = nullptr;
83             return true;
84         }
85     }
86     return false;
87 }
88 
DoDownloadInCommon(std::shared_ptr<Media::PixelMap> & pixelMap,const std::string uri)89 bool NapiUtils::DoDownloadInCommon(std::shared_ptr<Media::PixelMap>& pixelMap, const std::string uri)
90 {
91     SLOGI("DoDownloadInCommon with uri");
92 
93     std::vector<std::uint8_t> imgBuffer(0);
94     if (CurlSetRequestOptions(imgBuffer, uri) == true) {
95         std::uint8_t* buffer = (std::uint8_t*) calloc(imgBuffer.size(), sizeof(uint8_t));
96         if (buffer == nullptr) {
97             SLOGE("buffer malloc fail");
98             free(buffer);
99             return false;
100         }
101         std::copy(imgBuffer.begin(), imgBuffer.end(), buffer);
102         uint32_t errorCode = 0;
103         Media::SourceOptions opts;
104         SLOGD("DoDownload get size %{public}d", static_cast<int>(imgBuffer.size()));
105         auto imageSource = Media::ImageSource::CreateImageSource(buffer, imgBuffer.size(), opts, errorCode);
106         free(buffer);
107         if (errorCode || !imageSource) {
108             SLOGE("DoDownload create imageSource fail: %{public}u", errorCode);
109             return false;
110         }
111         Media::DecodeOptions decodeOpts;
112         pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
113         if (errorCode || pixelMap == nullptr) {
114             SLOGE("DoDownload creatPix fail: %{public}u, %{public}d", errorCode, static_cast<int>(pixelMap != nullptr));
115             return false;
116         }
117         return true;
118     }
119     return false;
120 }
121 
ConvertSessionType(const std::string & typeString)122 int32_t NapiUtils::ConvertSessionType(const std::string& typeString)
123 {
124     if (typeString == "audio") {
125         return AVSession::SESSION_TYPE_AUDIO;
126     } else if (typeString == "video") {
127         return AVSession::SESSION_TYPE_VIDEO;
128     } else if (typeString == "voice_call") {
129         return AVSession::SESSION_TYPE_VOICE_CALL;
130     } else if (typeString == "video_call") {
131         return AVSession::SESSION_TYPE_VIDEO_CALL;
132     } else {
133         return AVSession::SESSION_TYPE_INVALID;
134     }
135 }
136 
ConvertSessionType(int32_t type)137 std::string NapiUtils::ConvertSessionType(int32_t type)
138 {
139     if (type == AVSession::SESSION_TYPE_AUDIO) {
140         return "audio";
141     } else if (type == AVSession::SESSION_TYPE_VIDEO) {
142         return "video";
143     } else if (type == AVSession::SESSION_TYPE_VOICE_CALL) {
144         return "voice_call";
145     } else if (type == AVSession::SESSION_TYPE_VIDEO_CALL) {
146         return "video_call";
147     } else {
148         return "";
149     }
150 }
151 
152 /* napi_value <-> bool */
GetValue(napi_env env,napi_value in,bool & out)153 napi_status NapiUtils::GetValue(napi_env env, napi_value in, bool& out)
154 {
155     return napi_get_value_bool(env, in, &out);
156 }
157 
SetValue(napi_env env,const bool & in,napi_value & out)158 napi_status NapiUtils::SetValue(napi_env env, const bool& in, napi_value& out)
159 {
160     return napi_get_boolean(env, in, &out);
161 }
162 
163 /* napi_value <-> int32_t */
GetValue(napi_env env,napi_value in,int32_t & out)164 napi_status NapiUtils::GetValue(napi_env env, napi_value in, int32_t& out)
165 {
166     return napi_get_value_int32(env, in, &out);
167 }
168 
SetValue(napi_env env,const int32_t & in,napi_value & out)169 napi_status NapiUtils::SetValue(napi_env env, const int32_t& in, napi_value& out)
170 {
171     return napi_create_int32(env, in, &out);
172 }
173 
174 /* napi_value <-> uint32_t */
GetValue(napi_env env,napi_value in,uint32_t & out)175 napi_status NapiUtils::GetValue(napi_env env, napi_value in, uint32_t& out)
176 {
177     return napi_get_value_uint32(env, in, &out);
178 }
179 
SetValue(napi_env env,const uint32_t & in,napi_value & out)180 napi_status NapiUtils::SetValue(napi_env env, const uint32_t& in, napi_value& out)
181 {
182     return napi_create_uint32(env, in, &out);
183 }
184 
185 /* napi_value <-> int64_t */
GetValue(napi_env env,napi_value in,int64_t & out)186 napi_status NapiUtils::GetValue(napi_env env, napi_value in, int64_t& out)
187 {
188     return napi_get_value_int64(env, in, &out);
189 }
190 
SetValue(napi_env env,const int64_t & in,napi_value & out)191 napi_status NapiUtils::SetValue(napi_env env, const int64_t& in, napi_value& out)
192 {
193     return napi_create_int64(env, in, &out);
194 }
195 
196 /* napi_value <-> double */
GetValue(napi_env env,napi_value in,double & out)197 napi_status NapiUtils::GetValue(napi_env env, napi_value in, double& out)
198 {
199     return napi_get_value_double(env, in, &out);
200 }
201 
SetValue(napi_env env,const double & in,napi_value & out)202 napi_status NapiUtils::SetValue(napi_env env, const double& in, napi_value& out)
203 {
204     return napi_create_double(env, in, &out);
205 }
206 
207 /* napi_value <-> std::string */
GetValue(napi_env env,napi_value in,std::string & out)208 napi_status NapiUtils::GetValue(napi_env env, napi_value in, std::string& out)
209 {
210     napi_valuetype type = napi_undefined;
211     napi_status status = napi_typeof(env, in, &type);
212     CHECK_RETURN((status == napi_ok) && (type == napi_string), "invalid type", napi_invalid_arg);
213 
214     size_t maxLen = STR_MAX_LENGTH;
215     status = napi_get_value_string_utf8(env, in, nullptr, 0, &maxLen);
216     if (status != napi_ok || maxLen >= STR_MAX_LENGTH) {
217         return napi_invalid_arg;
218     }
219 
220     char buf[STR_MAX_LENGTH + STR_TAIL_LENGTH] {};
221     size_t len = 0;
222     status = napi_get_value_string_utf8(env, in, buf, maxLen + STR_TAIL_LENGTH, &len);
223     if (status == napi_ok) {
224         out = std::string(buf);
225     }
226     return status;
227 }
228 
SetValue(napi_env env,const std::string & in,napi_value & out)229 napi_status NapiUtils::SetValue(napi_env env, const std::string& in, napi_value& out)
230 {
231     return napi_create_string_utf8(env, in.c_str(), in.size(), &out);
232 }
233 
234 /* napi_value <-> AppExecFwk::ElementName */
SetValue(napi_env env,const AppExecFwk::ElementName & in,napi_value & out)235 napi_status NapiUtils::SetValue(napi_env env, const AppExecFwk::ElementName& in, napi_value& out)
236 {
237     napi_status status = napi_create_object(env, &out);
238     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
239 
240     napi_value property = nullptr;
241     status = SetValue(env, in.GetDeviceID(), property);
242     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
243     status = napi_set_named_property(env, out, "deviceId", property);
244     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
245 
246     status = SetValue(env, in.GetBundleName(), property);
247     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
248     status = napi_set_named_property(env, out, "bundleName", property);
249     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
250 
251     status = SetValue(env, in.GetAbilityName(), property);
252     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
253     status = napi_set_named_property(env, out, "abilityName", property);
254     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
255 
256     return napi_ok;
257 }
258 
JudgeNumString(const std::string & str)259 bool NapiUtils::JudgeNumString(const std::string& str)
260 {
261     std::string tempStr = str;
262     return all_of(tempStr.begin(), tempStr.end(), [](char ch) {
263         return isdigit(ch);
264     });
265 }
266 
SetOutPutDeviceIdValue(napi_env env,const std::vector<std::string> & in,napi_value & out)267 napi_status NapiUtils::SetOutPutDeviceIdValue(napi_env env, const std::vector<std::string>& in, napi_value& out)
268 {
269     napi_status status = napi_create_array_with_length(env, in.size(), &out);
270     CHECK_RETURN(status == napi_ok, "create array failed!", status);
271     int index = 0;
272     for (auto& item : in) {
273         napi_value element = nullptr;
274         if (!JudgeNumString(item)) {
275             SLOGE("item is not num string");
276             return napi_invalid_arg;
277         }
278         SetValue(env, static_cast<int32_t>(std::stoi(item)), element);
279         status = napi_set_element(env, out, index++, element);
280         CHECK_RETURN((status == napi_ok), "napi_set_element failed!", status);
281     }
282     return status;
283 }
284 
285 /* napi_value <-> AVSessionDescriptor */
SetValue(napi_env env,const AVSessionDescriptor & in,napi_value & out)286 napi_status NapiUtils::SetValue(napi_env env, const AVSessionDescriptor& in, napi_value& out)
287 {
288     napi_status status = napi_create_object(env, &out);
289     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
290 
291     napi_value property = nullptr;
292     status = SetValue(env, in.sessionId_, property);
293     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
294     status = napi_set_named_property(env, out, "sessionId", property);
295     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
296 
297     status = SetValue(env, ConvertSessionType(in.sessionType_), property);
298     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
299     status = napi_set_named_property(env, out, "type", property);
300     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
301 
302     status = SetValue(env, in.sessionTag_, property);
303     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
304     status = napi_set_named_property(env, out, "sessionTag", property);
305     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
306 
307     status = SetValue(env, in.elementName_, property);
308     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
309     status = napi_set_named_property(env, out, "elementName", property);
310     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
311 
312     status = SetValue(env, in.isActive_, property);
313     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
314     status = napi_set_named_property(env, out, "isActive", property);
315     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
316 
317     status = SetValue(env, in.isTopSession_, property);
318     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
319     status = napi_set_named_property(env, out, "isTopSession", property);
320     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
321 
322     status = SetValue(env, in.outputDeviceInfo_, property);
323     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
324     status = napi_set_named_property(env, out, "outputDevice", property);
325     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
326 
327     return napi_ok;
328 }
329 
330 /* napi_value <-> AVQueueInfo */
SetValue(napi_env env,const AVQueueInfo & in,napi_value & out)331 napi_status NapiUtils::SetValue(napi_env env, const AVQueueInfo& in, napi_value& out)
332 {
333     napi_status status = napi_create_object(env, &out);
334     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
335 
336     napi_value property = nullptr;
337     status = SetValue(env, in.GetBundleName(), property);
338     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
339     status = napi_set_named_property(env, out, "bundleName", property);
340     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
341 
342     status = SetValue(env, in.GetAVQueueName(), property);
343     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
344     status = napi_set_named_property(env, out, "avQueueName", property);
345     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
346 
347     status = SetValue(env, in.GetAVQueueId(), property);
348     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
349     status = napi_set_named_property(env, out, "avQueueId", property);
350     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
351 
352     auto pixelMap = in.GetAVQueueImage();
353     if (pixelMap != nullptr) {
354         SLOGD(" napi setvalue has avqueueimage");
355         property = Media::PixelMapNapi::CreatePixelMap(env, AVSessionPixelMapAdapter::ConvertFromInner(pixelMap));
356         status = napi_set_named_property(env, out, "avQueueImage", property);
357         CHECK_RETURN(status == napi_ok, "set property failed", status);
358     }
359 
360     auto uri = in.GetAVQueueImageUri();
361     if (!uri.empty()) {
362         SLOGD(" napi setvalue has avqueueimageuri");
363         status = NapiUtils::SetValue(env, uri, property);
364         CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
365         status = napi_set_named_property(env, out, "avQueueImage", property);
366         CHECK_RETURN(status == napi_ok, "set property failed", status);
367     }
368 
369     return napi_ok;
370 }
371 
372 /* napi_value <-> MMI::KeyEvent::KeyItem */
GetValue(napi_env env,napi_value in,MMI::KeyEvent::KeyItem & out)373 napi_status NapiUtils::GetValue(napi_env env, napi_value in, MMI::KeyEvent::KeyItem& out)
374 {
375     int32_t code {};
376     auto status = GetNamedProperty(env, in, "code", code);
377     CHECK_RETURN(status == napi_ok, "get code property failed", status);
378     SLOGI("code=%{public}d", code);
379     out.SetKeyCode(code);
380 
381     int64_t pressedTime {};
382     status = GetNamedProperty(env, in, "pressedTime", pressedTime);
383     CHECK_RETURN(status == napi_ok, "get pressedTime property failed", status);
384     SLOGI("pressedTime=%{public}" PRIu64, pressedTime);
385     out.SetDownTime(pressedTime);
386 
387     int32_t deviceId {};
388     status = GetNamedProperty(env, in, "deviceId", deviceId);
389     CHECK_RETURN(status == napi_ok, "get deviceId property failed", status);
390     out.SetDeviceId(deviceId);
391     out.SetPressed(true);
392 
393     return status;
394 }
395 
SetValue(napi_env env,const std::optional<MMI::KeyEvent::KeyItem> in,napi_value & out)396 napi_status NapiUtils::SetValue(napi_env env, const std::optional<MMI::KeyEvent::KeyItem> in, napi_value& out)
397 {
398     auto status = napi_create_object(env, &out);
399     CHECK_RETURN(status == napi_ok, "create object failed", status);
400 
401     napi_value code {};
402     status = SetValue(env, in->GetKeyCode(), code);
403     CHECK_RETURN((status == napi_ok) && (code != nullptr), "create property failed", status);
404     status = napi_set_named_property(env, out, "code", code);
405     CHECK_RETURN(status == napi_ok, "set property failed", status);
406 
407     napi_value pressedTime {};
408     status = SetValue(env, in->GetDownTime(), pressedTime);
409     CHECK_RETURN((status == napi_ok) && (pressedTime != nullptr), "create property failed", status);
410     status = napi_set_named_property(env, out, "pressedTime", pressedTime);
411     CHECK_RETURN(status == napi_ok, "set property failed", status);
412 
413     napi_value deviceId {};
414     status = SetValue(env, in->GetDeviceId(), deviceId);
415     CHECK_RETURN((status == napi_ok) && (deviceId != nullptr), "create property failed", status);
416     status = napi_set_named_property(env, out, "deviceId", deviceId);
417     CHECK_RETURN(status == napi_ok, "set property failed", status);
418 
419     return status;
420 }
421 
422 /* napi_value <-> MMI::KeyEvent */
GetValue(napi_env env,napi_value in,std::shared_ptr<MMI::KeyEvent> & out)423 napi_status NapiUtils::GetValue(napi_env env, napi_value in, std::shared_ptr<MMI::KeyEvent>& out)
424 {
425     napi_valuetype valueType = napi_undefined;
426     auto status = napi_typeof(env, in, &valueType);
427     CHECK_RETURN((status == napi_ok) && (valueType == napi_object), "object type invalid", status);
428 
429     out = MMI::KeyEvent::Create();
430     CHECK_RETURN(out != nullptr, "create keyEvent failed", napi_generic_failure);
431 
432     int32_t action {};
433     status = GetNamedProperty(env, in, "action", action);
434     CHECK_RETURN(status == napi_ok, "get action property failed", napi_generic_failure);
435     SLOGI("action=%{public}d", action);
436     action += KEYEVENT_ACTION_JS_NATIVE_DELTA;
437     out->SetKeyAction(action);
438 
439     MMI::KeyEvent::KeyItem key;
440     status = GetNamedProperty(env, in, "key", key);
441     CHECK_RETURN(status == napi_ok, "get action property failed", napi_generic_failure);
442     out->SetKeyCode(key.GetKeyCode());
443 
444     napi_value keyItems {};
445     status = napi_get_named_property(env, in, "keys", &keyItems);
446     CHECK_RETURN((status == napi_ok) && (keyItems != nullptr), "get keys property failed", status);
447 
448     uint32_t length {};
449     status = napi_get_array_length(env, keyItems, &length);
450     CHECK_RETURN(status == napi_ok, "get array length failed", status);
451 
452     for (uint32_t i = 0; i < length; ++i) {
453         napi_value keyItem {};
454         status = napi_get_element(env, keyItems, i, &keyItem);
455         CHECK_RETURN((status == napi_ok) && (keyItem != nullptr), "get element failed", status);
456         MMI::KeyEvent::KeyItem item;
457         status = GetValue(env, keyItem, item);
458         CHECK_RETURN(status == napi_ok, "get KeyItem failed", status);
459         if ((key.GetKeyCode() == item.GetKeyCode()) && (action == MMI::KeyEvent::KEY_ACTION_UP)) {
460             item.SetPressed(false);
461         }
462         out->AddKeyItem(item);
463     }
464 
465     return napi_ok;
466 }
467 
SetValue(napi_env env,const std::shared_ptr<MMI::KeyEvent> & in,napi_value & out)468 napi_status NapiUtils::SetValue(napi_env env, const std::shared_ptr<MMI::KeyEvent>& in, napi_value& out)
469 {
470     CHECK_RETURN(in != nullptr, "key event is nullptr", napi_generic_failure);
471 
472     auto status = napi_create_object(env, &out);
473     CHECK_RETURN(status == napi_ok, "create object failed", status);
474 
475     napi_value action {};
476     status = SetValue(env, in->GetKeyAction() - KEYEVENT_ACTION_JS_NATIVE_DELTA, action);
477     CHECK_RETURN((status == napi_ok) && (action != nullptr), "create action property failed", status);
478     status = napi_set_named_property(env, out, "action", action);
479     CHECK_RETURN(status == napi_ok, "set action property failed", status);
480 
481     napi_value key {};
482     CHECK_RETURN(in->GetKeyItem(), "get key item failed", napi_generic_failure);
483     status = SetValue(env, in->GetKeyItem(), key);
484     CHECK_RETURN((status == napi_ok) && (key != nullptr), "create key property failed", status);
485     status = napi_set_named_property(env, out, "key", key);
486     CHECK_RETURN(status == napi_ok, "set key property failed", status);
487 
488     napi_value keys {};
489     status = napi_create_array(env, &keys);
490     CHECK_RETURN(status == napi_ok, "create array failed", status);
491 
492     uint32_t idx = 0;
493     std::vector<MMI::KeyEvent::KeyItem> keyItems = in->GetKeyItems();
494     for (const auto& keyItem : keyItems) {
495         napi_value item {};
496         status = SetValue(env, keyItem, item);
497         CHECK_RETURN((status == napi_ok) && (item != nullptr), "create keyItem failed", status);
498 
499         status = napi_set_element(env, keys, idx, item);
500         CHECK_RETURN(status == napi_ok, "set element failed", status);
501         ++idx;
502     }
503 
504     status = napi_set_named_property(env, out, "keys", keys);
505     CHECK_RETURN(status == napi_ok, "set keys property failed", status);
506     return status;
507 }
508 
509 /* napi_value <-> AbilityRuntime::WantAgent::WantAgent */
GetValue(napi_env env,napi_value in,AbilityRuntime::WantAgent::WantAgent * & out)510 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AbilityRuntime::WantAgent::WantAgent*& out)
511 {
512     auto status = napi_unwrap(env, in, reinterpret_cast<void**>(&out));
513     CHECK_RETURN(status == napi_ok, "unwrap failed", napi_invalid_arg);
514     return status;
515 }
516 
SetValue(napi_env env,AbilityRuntime::WantAgent::WantAgent & in,napi_value & out)517 napi_status NapiUtils::SetValue(napi_env env, AbilityRuntime::WantAgent::WantAgent& in, napi_value& out)
518 {
519     auto status = napi_create_object(env, &out);
520     CHECK_RETURN(status == napi_ok, "create object failed", napi_generic_failure);
521     auto finalizecb = [](napi_env env, void* data, void* hint) {};
522     status = napi_wrap(env, out, static_cast<void*>(&in), finalizecb, nullptr, nullptr);
523     CHECK_RETURN(status == napi_ok, "wrap object failed", napi_generic_failure);
524     return status;
525 }
526 
SetValue(napi_env env,AbilityRuntime::WantAgent::WantAgent * in,napi_value & out)527 napi_status NapiUtils::SetValue(napi_env env, AbilityRuntime::WantAgent::WantAgent* in, napi_value& out)
528 {
529     auto status = napi_create_object(env, &out);
530     CHECK_RETURN(status == napi_ok, "create object failed", napi_generic_failure);
531     auto finalizecb = [](napi_env env, void* data, void* hint) {};
532     status = napi_wrap(env, out, static_cast<void*>(in), finalizecb, nullptr, nullptr);
533     CHECK_RETURN(status == napi_ok, "wrap object failed", napi_generic_failure);
534     return status;
535 }
536 
537 /* napi_value <-> AAFwk::WantParams */
GetValue(napi_env env,napi_value in,AAFwk::WantParams & out)538 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AAFwk::WantParams& out)
539 {
540     auto status = AppExecFwk::UnwrapWantParams(env, in, out);
541     CHECK_RETURN(status == true, "unwrap object failed", napi_generic_failure);
542     return napi_ok;
543 }
544 
SetValue(napi_env env,const AAFwk::WantParams & in,napi_value & out)545 napi_status NapiUtils::SetValue(napi_env env, const AAFwk::WantParams& in, napi_value& out)
546 {
547     auto status = napi_create_object(env, &out);
548     CHECK_RETURN(status == napi_ok, "create object failed", napi_generic_failure);
549     out = AppExecFwk::WrapWantParams(env, in);
550     return status;
551 }
552 
553 /* napi_value <-> AVCallMetaData */
GetValue(napi_env env,napi_value in,AVCallMetaData & out)554 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AVCallMetaData& out)
555 {
556     return NapiAVCallMetaData::GetValue(env, in, out);
557 }
558 
SetValue(napi_env env,const AVCallMetaData & in,napi_value & out)559 napi_status NapiUtils::SetValue(napi_env env, const AVCallMetaData& in, napi_value& out)
560 {
561     return NapiAVCallMetaData::SetValue(env, in, out);
562 }
563 
564 /* napi_value <-> AVCallState */
GetValue(napi_env env,napi_value in,AVCallState & out)565 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AVCallState& out)
566 {
567     return NapiAVCallState::GetValue(env, in, out);
568 }
569 
SetValue(napi_env env,const AVCallState & in,napi_value & out)570 napi_status NapiUtils::SetValue(napi_env env, const AVCallState& in, napi_value& out)
571 {
572     return NapiAVCallState::SetValue(env, in, out);
573 }
574 
575 /* napi_value <-> AVMetaData */
GetValue(napi_env env,napi_value in,AVMetaData & out)576 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AVMetaData& out)
577 {
578     return NapiMetaData::GetValue(env, in, out);
579 }
580 
SetValue(napi_env env,const AVMetaData & in,napi_value & out)581 napi_status NapiUtils::SetValue(napi_env env, const AVMetaData& in, napi_value& out)
582 {
583     return NapiMetaData::SetValue(env, in, out);
584 }
585 
586 /* napi_value <-> AVMediaDescription */
GetValue(napi_env env,napi_value in,AVMediaDescription & out)587 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AVMediaDescription& out)
588 {
589     return NapiMediaDescription::GetValue(env, in, out);
590 }
591 
SetValue(napi_env env,const AVMediaDescription & in,napi_value & out)592 napi_status NapiUtils::SetValue(napi_env env, const AVMediaDescription& in, napi_value& out)
593 {
594     return NapiMediaDescription::SetValue(env, in, out);
595 }
596 
597 /* napi_value <-> AVQueueItem */
GetValue(napi_env env,napi_value in,AVQueueItem & out)598 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AVQueueItem& out)
599 {
600     return NapiQueueItem::GetValue(env, in, out);
601 }
602 
SetValue(napi_env env,const AVQueueItem & in,napi_value & out)603 napi_status NapiUtils::SetValue(napi_env env, const AVQueueItem& in, napi_value& out)
604 {
605     return NapiQueueItem::SetValue(env, in, out);
606 }
607 
608 /* napi_value <-> std::vector<AVQueueItem> */
GetValue(napi_env env,napi_value in,std::vector<AVQueueItem> & out)609 napi_status NapiUtils::GetValue(napi_env env, napi_value in, std::vector<AVQueueItem>& out)
610 {
611     uint32_t length {};
612     auto status = napi_get_array_length(env, in, &length);
613     CHECK_RETURN(status == napi_ok, "get AVQueueItem array length failed", status);
614     for (uint32_t i = 0; i < length; ++i) {
615         napi_value element {};
616         status = napi_get_element(env, in, i, &element);
617         CHECK_RETURN((status == napi_ok) && (element != nullptr), "get element failed", status);
618         AVQueueItem descriptor;
619         status = GetValue(env, element, descriptor);
620         out.push_back(descriptor);
621     }
622     return status;
623 }
624 
SetValue(napi_env env,const std::vector<AVQueueItem> & in,napi_value & out)625 napi_status NapiUtils::SetValue(napi_env env, const std::vector<AVQueueItem>& in, napi_value& out)
626 {
627     SLOGD("napi_value <- std::vector<std::string>");
628     napi_status status = napi_create_array_with_length(env, in.size(), &out);
629     CHECK_RETURN(status == napi_ok, "create AVQueueItem array failed!", status);
630     int index = 0;
631     for (auto& item : in) {
632         napi_value element = nullptr;
633         SetValue(env, item, element);
634         status = napi_set_element(env, out, index++, element);
635         CHECK_RETURN((status == napi_ok), "napi_set_element failed!", status);
636     }
637     return status;
638 }
639 
640 /* napi_value <-> AVPlaybackState */
GetValue(napi_env env,napi_value in,AVPlaybackState & out)641 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AVPlaybackState& out)
642 {
643     return NapiPlaybackState::GetValue(env, in, out);
644 }
645 
SetValue(napi_env env,const AVPlaybackState & in,napi_value & out)646 napi_status NapiUtils::SetValue(napi_env env, const AVPlaybackState& in, napi_value& out)
647 {
648     return NapiPlaybackState::SetValue(env, in, out);
649 }
650 
651 /* napi_value <-> AVCastPlayerState */
GetValue(napi_env env,napi_value in,AVCastPlayerState & out)652 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AVCastPlayerState& out)
653 {
654     napi_valuetype type = napi_undefined;
655     napi_status status = napi_typeof(env, in, &type);
656     CHECK_RETURN((status == napi_ok) && (type == napi_string), "invalid type", napi_invalid_arg);
657 
658     size_t maxLen = STR_MAX_LENGTH;
659     status = napi_get_value_string_utf8(env, in, nullptr, 0, &maxLen);
660     if (maxLen >= STR_MAX_LENGTH) {
661         return napi_invalid_arg;
662     }
663 
664     char buf[STR_MAX_LENGTH + STR_TAIL_LENGTH] {};
665     size_t len = 0;
666     status = napi_get_value_string_utf8(env, in, buf, maxLen + STR_TAIL_LENGTH, &len);
667     if (status == napi_ok) {
668         AVCastPlayerState castPlayerState;
669         castPlayerState.castPlayerState_ = std::string(buf);
670         out = castPlayerState;
671     }
672     return status;
673 }
674 
SetValue(napi_env env,const AVCastPlayerState & in,napi_value & out)675 napi_status NapiUtils::SetValue(napi_env env, const AVCastPlayerState& in, napi_value& out)
676 {
677     return napi_create_string_utf8(env, in.castPlayerState_.c_str(), in.castPlayerState_.size(), &out);
678 }
679 
680 /* napi_value <-> std::vector<std::string> */
GetValue(napi_env env,napi_value in,std::vector<std::string> & out)681 napi_status NapiUtils::GetValue(napi_env env, napi_value in, std::vector<std::string>& out)
682 {
683     SLOGD("napi_value -> std::vector<std::string>");
684     out.clear();
685     bool isArray = false;
686     napi_is_array(env, in, &isArray);
687     CHECK_RETURN(isArray, "not an array", napi_invalid_arg);
688 
689     uint32_t length = 0;
690     napi_status status = napi_get_array_length(env, in, &length);
691     CHECK_RETURN((status == napi_ok) && (length > 0 || length == 0), "get_array failed!", napi_invalid_arg);
692     for (uint32_t i = 0; i < length; ++i) {
693         napi_value item = nullptr;
694         status = napi_get_element(env, in, i, &item);
695         CHECK_RETURN((item != nullptr) && (status == napi_ok), "no element", napi_invalid_arg);
696         std::string value;
697         status = GetValue(env, item, value);
698         CHECK_RETURN(status == napi_ok, "not a string", napi_invalid_arg);
699         out.push_back(value);
700     }
701     return status;
702 }
703 
SetValue(napi_env env,const std::vector<std::string> & in,napi_value & out)704 napi_status NapiUtils::SetValue(napi_env env, const std::vector<std::string>& in, napi_value& out)
705 {
706     SLOGD("napi_value <- std::vector<std::string>");
707     napi_status status = napi_create_array_with_length(env, in.size(), &out);
708     CHECK_RETURN(status == napi_ok, "create array failed!", status);
709     int index = 0;
710     for (auto& item : in) {
711         napi_value element = nullptr;
712         SetValue(env, item, element);
713         status = napi_set_element(env, out, index++, element);
714         CHECK_RETURN((status == napi_ok), "napi_set_element failed!", status);
715     }
716     return status;
717 }
718 
719 /* napi_value <-> std::vector<uint8_t> */
GetValue(napi_env env,napi_value in,std::vector<uint8_t> & out)720 napi_status NapiUtils::GetValue(napi_env env, napi_value in, std::vector<uint8_t>& out)
721 {
722     out.clear();
723     SLOGD("napi_value -> std::vector<uint8_t> ");
724     napi_typedarray_type type = napi_biguint64_array;
725     size_t length = 0;
726     napi_value buffer = nullptr;
727     size_t offset = 0;
728     void* data = nullptr;
729     napi_status status = napi_get_typedarray_info(env, in, &type, &length, &data, &buffer, &offset);
730     SLOGD("array type=%{public}d length=%{public}d offset=%{public}d", static_cast<int>(type), static_cast<int>(length),
731           static_cast<int>(offset));
732     CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg);
733     CHECK_RETURN(type == napi_uint8_array, "is not Uint8Array!", napi_invalid_arg);
734     CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg);
735     out.assign(static_cast<uint8_t*>(data), static_cast<uint8_t*>(data) + length);
736     return status;
737 }
738 
SetValue(napi_env env,const std::vector<uint8_t> & in,napi_value & out)739 napi_status NapiUtils::SetValue(napi_env env, const std::vector<uint8_t>& in, napi_value& out)
740 {
741     SLOGD("napi_value <- std::vector<uint8_t> ");
742     CHECK_RETURN(!in.empty(), "invalid std::vector<uint8_t>", napi_invalid_arg);
743     void* data = nullptr;
744     napi_value buffer = nullptr;
745     napi_status status = napi_create_arraybuffer(env, in.size(), &data, &buffer);
746     CHECK_RETURN((status == napi_ok), "create array buffer failed!", status);
747 
748     if (memcpy_s(data, in.size(), in.data(), in.size()) != EOK) {
749         SLOGE("memcpy_s not EOK");
750         return napi_invalid_arg;
751     }
752     status = napi_create_typedarray(env, napi_uint8_array, in.size(), buffer, 0, &out);
753     CHECK_RETURN((status == napi_ok), "napi_value <- std::vector<uint8_t> invalid value", status);
754     return status;
755 }
756 
757 /* napi_value <-> CastDisplayInfo */
SetValue(napi_env env,const CastDisplayInfo & in,napi_value & out)758 napi_status NapiUtils::SetValue(napi_env env, const CastDisplayInfo& in, napi_value& out)
759 {
760     auto status = napi_create_object(env, &out);
761     CHECK_RETURN(status == napi_ok, "create object failed", status);
762     napi_value displayState = nullptr;
763     napi_create_int32(env, static_cast<int>(in.displayState), &displayState);
764     status = napi_set_named_property(env, out, "state", displayState);
765     napi_value displayId = nullptr;
766     napi_create_int64(env, in.displayId, &displayId);
767     status = napi_set_named_property(env, out, "id", displayId);
768     napi_value name = nullptr;
769     napi_create_string_utf8(env, in.name.c_str(), in.name.size(), &name);
770     status = napi_set_named_property(env, out, "name", name);
771     napi_value width = nullptr;
772     napi_create_int32(env, in.width, &width);
773     status = napi_set_named_property(env, out, "width", width);
774     napi_value height = nullptr;
775     napi_create_int32(env, in.height, &height);
776     status = napi_set_named_property(env, out, "height", height);
777     CHECK_RETURN(status == napi_ok, "set property failed", status);
778     return status;
779 }
780 
781 /* napi_value <-> CastDisplayInfo Array */
SetValue(napi_env env,const std::vector<CastDisplayInfo> & in,napi_value & out)782 napi_status NapiUtils::SetValue(napi_env env, const std::vector<CastDisplayInfo>& in, napi_value& out)
783 {
784     auto status = napi_create_array_with_length(env, in.size(), &out);
785     CHECK_RETURN(status == napi_ok, "create CastDisplayInfo Array failed", status);
786     int index = 0;
787     for (auto& item : in) {
788         napi_value element = nullptr;
789         SetValue(env, item, element);
790         status = napi_set_element(env, out, index++, element);
791         CHECK_RETURN(status == napi_ok, "napi_set_element failed", status);
792     }
793     return status;
794 }
795 
796 /* napi_value <-> NapiAVCastPickerOptions */
GetValue(napi_env env,napi_value in,NapiAVCastPickerOptions & out)797 napi_status NapiUtils::GetValue(napi_env env, napi_value in, NapiAVCastPickerOptions& out)
798 {
799     napi_value value {};
800     auto status = napi_get_named_property(env, in, "sessionType", &value);
801     CHECK_RETURN(status == napi_ok, "get sessionType failed", status);
802     status = GetValue(env, value, out.sessionType);
803     CHECK_RETURN(status == napi_ok, "get sessionType value failed", status);
804 
805     return napi_ok;
806 }
807 
808 template <typename T>
TypedArray2Vector(uint8_t * data,size_t length,napi_typedarray_type type,std::vector<T> & out)809 void TypedArray2Vector(uint8_t* data, size_t length, napi_typedarray_type type, std::vector<T>& out)
810 {
811     auto convert = [&out](auto* data, size_t elements) {
812         for (size_t index = 0; index < elements; index++) {
813             out.push_back(static_cast<T>(data[index]));
814         }
815     };
816 
817     switch (type) {
818         case napi_int8_array:
819             convert(reinterpret_cast<int8_t*>(data), length);
820             break;
821         case napi_uint8_array:
822             convert(data, length);
823             break;
824         case napi_uint8_clamped_array:
825             convert(data, length);
826             break;
827         case napi_int16_array:
828             convert(reinterpret_cast<int16_t*>(data), length / sizeof(int16_t));
829             break;
830         case napi_uint16_array:
831             convert(reinterpret_cast<uint16_t*>(data), length / sizeof(uint16_t));
832             break;
833         case napi_int32_array:
834             convert(reinterpret_cast<int32_t*>(data), length / sizeof(int32_t));
835             break;
836         case napi_uint32_array:
837             convert(reinterpret_cast<uint32_t*>(data), length / sizeof(uint32_t));
838             break;
839         case napi_float32_array:
840             convert(reinterpret_cast<float*>(data), length / sizeof(float));
841             break;
842         case napi_float64_array:
843             convert(reinterpret_cast<double*>(data), length / sizeof(double));
844             break;
845         case napi_bigint64_array:
846             convert(reinterpret_cast<int64_t*>(data), length / sizeof(int64_t));
847             break;
848         case napi_biguint64_array:
849             convert(reinterpret_cast<uint64_t*>(data), length / sizeof(uint64_t));
850             break;
851         default:
852             CHECK_RETURN_VOID(false, "[FATAL] invalid napi_typedarray_type!");
853     }
854 }
855 
856 /* napi_value <-> std::vector<int32_t> */
GetValue(napi_env env,napi_value in,std::vector<int32_t> & out)857 napi_status NapiUtils::GetValue(napi_env env, napi_value in, std::vector<int32_t>& out)
858 {
859     out.clear();
860     SLOGD("napi_value -> std::vector<int32_t> ");
861     napi_typedarray_type type = napi_biguint64_array;
862     size_t length = 0;
863     napi_value buffer = nullptr;
864     size_t offset = 0;
865     uint8_t* data = nullptr;
866     napi_status status = napi_get_typedarray_info(env, in, &type, &length,
867                                                   reinterpret_cast<void**>(&data), &buffer, &offset);
868     SLOGD("array type=%{public}d length=%{public}d offset=%{public}d", static_cast<int>(type), static_cast<int>(length),
869           static_cast<int>(offset));
870     CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg);
871     CHECK_RETURN(type <= napi_int32_array, "is not int32 supported typed array!", napi_invalid_arg);
872     CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg);
873     TypedArray2Vector<int32_t>(data, length, type, out);
874     return status;
875 }
876 
SetValue(napi_env env,const std::vector<int32_t> & in,napi_value & out)877 napi_status NapiUtils::SetValue(napi_env env, const std::vector<int32_t>& in, napi_value& out)
878 {
879     SLOGD("napi_value <- std::vector<int32_t> ");
880     size_t bytes = in.size() * sizeof(int32_t);
881     CHECK_RETURN(bytes > 0, "invalid std::vector<int32_t>", napi_invalid_arg);
882     void* data = nullptr;
883     napi_value buffer = nullptr;
884     napi_status status = napi_create_arraybuffer(env, bytes, &data, &buffer);
885     CHECK_RETURN((status == napi_ok), "invalid buffer", status);
886 
887     if (memcpy_s(data, bytes, in.data(), bytes) != EOK) {
888         SLOGE("memcpy_s not EOK");
889         return napi_invalid_arg;
890     }
891     status = napi_create_typedarray(env, napi_int32_array, in.size(), buffer, 0, &out);
892     CHECK_RETURN((status == napi_ok), "invalid buffer", status);
893     return status;
894 }
895 
896 /* napi_value <-> std::vector<uint32_t> */
GetValue(napi_env env,napi_value in,std::vector<uint32_t> & out)897 napi_status NapiUtils::GetValue(napi_env env, napi_value in, std::vector<uint32_t>& out)
898 {
899     out.clear();
900     SLOGD("napi_value -> std::vector<uint32_t> ");
901     napi_typedarray_type type = napi_biguint64_array;
902     size_t length = 0;
903     napi_value buffer = nullptr;
904     size_t offset = 0;
905     uint8_t* data = nullptr;
906     napi_status status = napi_get_typedarray_info(env, in, &type, &length,
907                                                   reinterpret_cast<void**>(&data), &buffer, &offset);
908     SLOGD("napi_get_typedarray_info type=%{public}d", static_cast<int>(type));
909     CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg);
910     CHECK_RETURN((type <= napi_uint16_array) || (type == napi_uint32_array), "invalid type!", napi_invalid_arg);
911     CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg);
912     TypedArray2Vector<uint32_t>(data, length, type, out);
913     return status;
914 }
915 
SetValue(napi_env env,const std::vector<uint32_t> & in,napi_value & out)916 napi_status NapiUtils::SetValue(napi_env env, const std::vector<uint32_t>& in, napi_value& out)
917 {
918     SLOGD("napi_value <- std::vector<uint32_t> ");
919     size_t bytes = in.size() * sizeof(uint32_t);
920     CHECK_RETURN(bytes > 0, "invalid std::vector<uint32_t>", napi_invalid_arg);
921     void* data = nullptr;
922     napi_value buffer = nullptr;
923     napi_status status = napi_create_arraybuffer(env, bytes, &data, &buffer);
924     CHECK_RETURN((status == napi_ok), "invalid buffer", status);
925 
926     if (memcpy_s(data, bytes, in.data(), bytes) != EOK) {
927         SLOGE("memcpy_s not EOK");
928         return napi_invalid_arg;
929     }
930     status = napi_create_typedarray(env, napi_uint32_array, in.size(), buffer, 0, &out);
931     CHECK_RETURN((status == napi_ok), "invalid buffer", status);
932     return status;
933 }
934 
935 /* napi_value <-> std::vector<int64_t> */
GetValue(napi_env env,napi_value in,std::vector<int64_t> & out)936 napi_status NapiUtils::GetValue(napi_env env, napi_value in, std::vector<int64_t>& out)
937 {
938     out.clear();
939     SLOGD("napi_value -> std::vector<int64_t> ");
940     napi_typedarray_type type = napi_biguint64_array;
941     size_t length = 0;
942     napi_value buffer = nullptr;
943     size_t offset = 0;
944     uint8_t* data = nullptr;
945     napi_status status = napi_get_typedarray_info(env, in, &type, &length,
946                                                   reinterpret_cast<void**>(&data), &buffer, &offset);
947     SLOGD("array type=%{public}d length=%{public}d offset=%{public}d", static_cast<int>(type), static_cast<int>(length),
948           static_cast<int>(offset));
949     CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg);
950     CHECK_RETURN((type <= napi_uint32_array) || (type == napi_bigint64_array), "invalid type!", napi_invalid_arg);
951     CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg);
952     TypedArray2Vector<int64_t>(data, length, type, out);
953     return status;
954 }
955 
SetValue(napi_env env,const std::vector<int64_t> & in,napi_value & out)956 napi_status NapiUtils::SetValue(napi_env env, const std::vector<int64_t>& in, napi_value& out)
957 {
958     SLOGD("napi_value <- std::vector<int64_t> ");
959     size_t bytes = in.size() * sizeof(int64_t);
960     CHECK_RETURN(bytes > 0, "invalid std::vector<uint32_t>", napi_invalid_arg);
961     void* data = nullptr;
962     napi_value buffer = nullptr;
963     napi_status status = napi_create_arraybuffer(env, bytes, &data, &buffer);
964     CHECK_RETURN((status == napi_ok), "invalid buffer", status);
965 
966     if (memcpy_s(data, bytes, in.data(), bytes) != EOK) {
967         SLOGE("memcpy_s not EOK");
968         return napi_invalid_arg;
969     }
970     status = napi_create_typedarray(env, napi_bigint64_array, in.size(), buffer, 0, &out);
971     CHECK_RETURN((status == napi_ok), "invalid buffer", status);
972     return status;
973 }
974 /* napi_value <-> std::vector<double> */
GetValue(napi_env env,napi_value in,std::vector<double> & out)975 napi_status NapiUtils::GetValue(napi_env env, napi_value in, std::vector<double>& out)
976 {
977     out.clear();
978     bool isTypedArray = false;
979     napi_status status = napi_is_typedarray(env, in, &isTypedArray);
980     SLOGD("napi_value -> std::vector<double> input %{public}s a TypedArray", isTypedArray ? "is" : "is not");
981     CHECK_RETURN((status == napi_ok), "napi_is_typedarray failed!", status);
982     if (isTypedArray) {
983         SLOGD("napi_value -> std::vector<double> ");
984         napi_typedarray_type type = napi_biguint64_array;
985         size_t length = 0;
986         napi_value buffer = nullptr;
987         size_t offset = 0;
988         uint8_t* data = nullptr;
989         status = napi_get_typedarray_info(env, in, &type, &length, reinterpret_cast<void**>(&data), &buffer, &offset);
990         SLOGD("napi_get_typedarray_info status=%{public}d type=%{public}d", status, static_cast<int>(type));
991         CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg);
992         CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg);
993         TypedArray2Vector<double>(data, length, type, out);
994     } else {
995         bool isArray = false;
996         status = napi_is_array(env, in, &isArray);
997         SLOGD("napi_value -> std::vector<double> input %{public}s an Array", isArray ? "is" : "is not");
998         CHECK_RETURN((status == napi_ok) && isArray, "invalid data!", napi_invalid_arg);
999         uint32_t length = 0;
1000         status = napi_get_array_length(env, in, &length);
1001         CHECK_RETURN((status == napi_ok) && (length > 0), "invalid data!", napi_invalid_arg);
1002         for (uint32_t i = 0; i < length; ++i) {
1003             napi_value item = nullptr;
1004             status = napi_get_element(env, in, i, &item);
1005             CHECK_RETURN((item != nullptr) && (status == napi_ok), "no element", napi_invalid_arg);
1006             double vi = 0.0f;
1007             status = napi_get_value_double(env, item, &vi);
1008             CHECK_RETURN(status == napi_ok, "element not a double", napi_invalid_arg);
1009             out.push_back(vi);
1010         }
1011     }
1012     return status;
1013 }
1014 
SetValue(napi_env env,const std::vector<double> & in,napi_value & out)1015 napi_status NapiUtils::SetValue(napi_env env, const std::vector<double>& in, napi_value& out)
1016 {
1017     SLOGD("napi_value <- std::vector<double> ");
1018     (void)(env);
1019     (void)(in);
1020     (void)(out);
1021     CHECK_RETURN(false, "std::vector<double> to napi_value, unsupported!", napi_invalid_arg);
1022     return napi_invalid_arg;
1023 }
1024 
1025 /* std::vector<AVSessionDescriptor> <-> napi_value */
SetValue(napi_env env,const std::vector<AVSessionDescriptor> & in,napi_value & out)1026 napi_status NapiUtils::SetValue(napi_env env, const std::vector<AVSessionDescriptor>& in, napi_value& out)
1027 {
1028     SLOGD("napi_value <- std::vector<AVSessionDescriptor>  %{public}d", static_cast<int>(in.size()));
1029     napi_status status = napi_create_array_with_length(env, in.size(), &out);
1030     CHECK_RETURN((status == napi_ok), "create_array failed!", status);
1031     int index = 0;
1032     for (const auto& item : in) {
1033         napi_value entry = nullptr;
1034         SetValue(env, item, entry);
1035         napi_set_element(env, out, index++, entry);
1036     }
1037     return status;
1038 }
1039 
1040 /* std::vector<AVQueueInfo> <-> napi_value */
SetValue(napi_env env,const std::vector<AVQueueInfo> & in,napi_value & out)1041 napi_status NapiUtils::SetValue(napi_env env, const std::vector<AVQueueInfo>& in, napi_value& out)
1042 {
1043     SLOGD("napi_value <- std::vector<AVQueueInfo>  %{public}d", static_cast<int>(in.size()));
1044     napi_status status = napi_create_array_with_length(env, in.size(), &out);
1045     CHECK_RETURN((status == napi_ok), "create_array failed!", status);
1046     int index = 0;
1047     for (const auto& item : in) {
1048         napi_value entry = nullptr;
1049         SetValue(env, item, entry);
1050         napi_set_element(env, out, index++, entry);
1051     }
1052     return status;
1053 }
1054 
1055 /* napi_value <-> std::vector<ResolutionLevel> */
SetValue(napi_env env,const std::vector<ResolutionLevel> & in,napi_value & out)1056 napi_status NapiUtils::SetValue(napi_env env, const std::vector<ResolutionLevel>& in, napi_value& out)
1057 {
1058     SLOGD("napi_value <- std::vector<ResolutionLevel>  %{public}d", static_cast<int>(in.size()));
1059     napi_status status = napi_create_array_with_length(env, in.size(), &out);
1060     CHECK_RETURN((status == napi_ok), "create_array failed!", status);
1061     int index = 0;
1062     for (const auto& item : in) {
1063         napi_value entry = nullptr;
1064         SetValue(env, item, entry);
1065         status = napi_set_element(env, out, index++, entry);
1066         CHECK_RETURN(status == napi_ok, "napi_set_element failed", status);
1067     }
1068     return status;
1069 }
1070 
1071 /* napi_value <-> std::vector<HDRFormat> */
SetValue(napi_env env,const std::vector<HDRFormat> & in,napi_value & out)1072 napi_status NapiUtils::SetValue(napi_env env, const std::vector<HDRFormat>& in, napi_value& out)
1073 {
1074     SLOGD("napi_value <- std::vector<HDRFormat>  %{public}d", static_cast<int>(in.size()));
1075     napi_status status = napi_create_array_with_length(env, in.size(), &out);
1076     CHECK_RETURN((status == napi_ok), "create_array failed!", status);
1077     int index = 0;
1078     for (const auto& item : in) {
1079         napi_value entry = nullptr;
1080         SetValue(env, item, entry);
1081         status = napi_set_element(env, out, index++, entry);
1082         CHECK_RETURN(status == napi_ok, "napi_set_element failed", status);
1083     }
1084     return status;
1085 }
1086 
1087 /* napi_value <-> std::vector<float> */
SetValue(napi_env env,const std::vector<float> & in,napi_value & out)1088 napi_status NapiUtils::SetValue(napi_env env, const std::vector<float>& in, napi_value& out)
1089 {
1090     SLOGD("napi_value <- std::vector<float>  %{public}d", static_cast<int>(in.size()));
1091     napi_status status = napi_create_array_with_length(env, in.size(), &out);
1092     CHECK_RETURN((status == napi_ok), "create_array failed!", status);
1093     int index = 0;
1094     for (const auto& item : in) {
1095         napi_value entry = nullptr;
1096         SetValue(env, item, entry);
1097         status = napi_set_element(env, out, index++, entry);
1098         CHECK_RETURN(status == napi_ok, "napi_set_element failed", status);
1099     }
1100     return status;
1101 }
1102 
1103 /* napi_value <-> DeviceInfo */
SetValue(napi_env env,const DeviceInfo & in,napi_value & out)1104 napi_status NapiUtils::SetValue(napi_env env, const DeviceInfo& in, napi_value& out)
1105 {
1106     napi_status status = napi_create_object(env, &out);
1107     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
1108     napi_value property = nullptr;
1109     status = SetValue(env, in.castCategory_, property);
1110     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1111     status = napi_set_named_property(env, out, "castCategory", property);
1112     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1113 
1114     status = SetValue(env, in.deviceId_, property);
1115     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1116     status = napi_set_named_property(env, out, "deviceId", property);
1117     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1118 
1119     status = SetValue(env, in.manufacturer_, property);
1120     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1121     status = napi_set_named_property(env, out, "manufacturer", property);
1122     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1123 
1124     status = SetValue(env, in.modelName_, property);
1125     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1126     status = napi_set_named_property(env, out, "modelName", property);
1127     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1128 
1129     status = SetValue(env, in.deviceName_, property);
1130     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1131     status = napi_set_named_property(env, out, "deviceName", property);
1132     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1133 
1134     status = SetValue(env, in.deviceType_, property);
1135     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1136     status = napi_set_named_property(env, out, "deviceType", property);
1137     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1138 
1139     status = SetValue(env, in.ipAddress_, property);
1140     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1141     status = napi_set_named_property(env, out, "ipAddress", property);
1142     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1143 
1144     status = SetValue(env, in.networkId_, property);
1145     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1146     status = napi_set_named_property(env, out, "networkId", property);
1147     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1148 
1149     status = SetValue(env, in.providerId_, property);
1150     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1151     status = napi_set_named_property(env, out, "providerId", property);
1152     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1153 
1154     status = SetValue(env, in.supportedProtocols_, property);
1155     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1156     status = napi_set_named_property(env, out, "supportedProtocols", property);
1157     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1158 
1159     status = SetValue(env, in.authenticationStatus_, property);
1160     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1161     status = napi_set_named_property(env, out, "authenticationStatus", property);
1162     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1163 
1164     status = SetValue(env, in.supportedDrmCapabilities_, property);
1165     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1166     status = napi_set_named_property(env, out, "supportedDrmCapabilities", property);
1167     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1168 
1169     status = SetValue(env, in.isLegacy_, property);
1170     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1171     status = napi_set_named_property(env, out, "isLegacy", property);
1172     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1173 
1174     status = SetValue(env, in.mediumTypes_, property);
1175     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1176     status = napi_set_named_property(env, out, "mediumTypes", property);
1177     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1178     return napi_ok;
1179 }
1180 
1181 /* OutputDeviceInfo <-> napi_value */
SetValue(napi_env env,const OutputDeviceInfo & in,napi_value & out)1182 napi_status NapiUtils::SetValue(napi_env env, const OutputDeviceInfo& in, napi_value& out)
1183 {
1184     SLOGD("napi_value <- OutputDeviceInfo");
1185     napi_value temp {};
1186     napi_create_object(env, &out);
1187 
1188     napi_status status = napi_create_array_with_length(env, in.deviceInfos_.size(), &temp);
1189     CHECK_RETURN((status == napi_ok), "create_array failed!", status);
1190     int index = 0;
1191     SLOGD("The length of deviceInfos is %{public}d", static_cast<int32_t>(in.deviceInfos_.size()));
1192     for (const auto& item : in.deviceInfos_) {
1193         napi_value entry = nullptr;
1194         status = SetValue(env, item, entry);
1195         CHECK_RETURN((status == napi_ok) && (entry != nullptr), "create array failed!", status);
1196         napi_set_element(env, temp, index++, entry);
1197     }
1198     status = napi_set_named_property(env, out, "devices", temp);
1199     CHECK_RETURN((status == napi_ok), "set named property devices failed", status);
1200     return status;
1201 }
1202 
Unwrap(napi_env env,napi_value in,void ** out,napi_value constructor)1203 napi_status NapiUtils::Unwrap(napi_env env, napi_value in, void** out, napi_value constructor)
1204 {
1205     if (constructor != nullptr) {
1206         bool isInstance = false;
1207         napi_instanceof(env, in, constructor, &isInstance);
1208         if (!isInstance) {
1209             SLOGE("not a instance of *");
1210             return napi_invalid_arg;
1211         }
1212     }
1213     return napi_unwrap(env, in, out);
1214 }
1215 
Equals(napi_env env,napi_value value,napi_ref copy)1216 bool NapiUtils::Equals(napi_env env, napi_value value, napi_ref copy)
1217 {
1218     if (copy == nullptr) {
1219         return (value == nullptr);
1220     }
1221 
1222     napi_value copyValue = nullptr;
1223     napi_get_reference_value(env, copy, &copyValue);
1224     CHECK_RETURN((napi_get_reference_value(env, copy, &copyValue) == napi_ok),
1225                  "get ref value failed", napi_generic_failure);
1226     bool isEquals = false;
1227     CHECK_RETURN(napi_strict_equals(env, value, copyValue, &isEquals) == napi_ok,
1228                  "get equals result failed", napi_generic_failure);
1229     return isEquals;
1230 }
1231 
TypeCheck(napi_env env,napi_value value,napi_valuetype expectType)1232 bool NapiUtils::TypeCheck(napi_env env, napi_value value, napi_valuetype expectType)
1233 {
1234     napi_valuetype valueType = napi_undefined;
1235     napi_status status = napi_typeof(env, value, &valueType);
1236     if (status != napi_ok || valueType != expectType) {
1237         return false;
1238     }
1239     return true;
1240 }
1241 
GetUndefinedValue(napi_env env)1242 napi_value NapiUtils::GetUndefinedValue(napi_env env)
1243 {
1244     napi_value result {};
1245     napi_get_undefined(env, &result);
1246     return result;
1247 }
1248 
GetPropertyNames(napi_env env,napi_value in,std::vector<std::string> & out)1249 napi_status NapiUtils::GetPropertyNames(napi_env env, napi_value in, std::vector<std::string>& out)
1250 {
1251     napi_value names {};
1252     NAPI_CALL_BASE(env, napi_get_property_names(env, in, &names), napi_generic_failure);
1253     uint32_t length = 0;
1254     NAPI_CALL_BASE(env, napi_get_array_length(env, names, &length), napi_generic_failure);
1255 
1256     for (uint32_t index = 0; index < length; ++index) {
1257         napi_value name {};
1258         std::string nameString;
1259         if (napi_get_element(env, names, index, &name) != napi_ok) {
1260             continue;
1261         }
1262         if (GetValue(env, name, nameString) != napi_ok) {
1263             continue;
1264         }
1265         out.push_back(nameString);
1266     }
1267 
1268     return napi_ok;
1269 }
1270 
GetDateValue(napi_env env,napi_value value,double & result)1271 napi_status NapiUtils::GetDateValue(napi_env env, napi_value value, double& result)
1272 {
1273     CHECK_RETURN(env != nullptr, "env is nullptr", napi_invalid_arg);
1274     CHECK_RETURN(value != nullptr, "value is nullptr", napi_invalid_arg);
1275 
1276     SLOGD("GetDateValue in");
1277     bool isDate = false;
1278     napi_is_date(env, value, &isDate);
1279     if (isDate) {
1280         napi_status status = napi_get_date_value(env, value, &result);
1281         if (status != napi_ok) {
1282             SLOGE("get date error");
1283         }
1284         SLOGD("GetDateValue out");
1285         return napi_ok;
1286     } else {
1287         SLOGE("value is not date type");
1288         return napi_date_expected;
1289     }
1290 }
1291 
SetDateValue(napi_env env,double time,napi_value & result)1292 napi_status NapiUtils::SetDateValue(napi_env env, double time, napi_value& result)
1293 {
1294     CHECK_RETURN(env != nullptr, "env is nullptr", napi_invalid_arg);
1295 
1296     SLOGD("SetDateValue in");
1297     napi_status status = napi_create_date(env, time, &result);
1298     if (status != napi_ok) {
1299         SLOGE("create date error");
1300     }
1301     SLOGD("SetDateValue out");
1302     return napi_ok;
1303 }
1304 
GetRefByCallback(napi_env env,std::list<napi_ref> callbackList,napi_value callback,napi_ref & callbackRef)1305 napi_status NapiUtils::GetRefByCallback(napi_env env, std::list<napi_ref> callbackList, napi_value callback,
1306                                         napi_ref& callbackRef)
1307 {
1308     for (auto ref = callbackList.begin(); ref != callbackList.end(); ++ref) {
1309         if (Equals(env, callback, *ref)) {
1310             SLOGD("Callback has been matched");
1311             callbackRef = *ref;
1312             break;
1313         }
1314     }
1315     return napi_ok;
1316 }
1317 
1318 /* napi_value is napi stage context */
GetStageElementName(napi_env env,napi_value in,AppExecFwk::ElementName & out)1319 napi_status NapiUtils::GetStageElementName(napi_env env, napi_value in, AppExecFwk::ElementName& out)
1320 {
1321     std::shared_ptr<AbilityRuntime::Context> stageContext = AbilityRuntime::GetStageModeContext(env, in);
1322     CHECK_RETURN(stageContext != nullptr, "get StagContext failed", napi_generic_failure);
1323     std::shared_ptr <AppExecFwk::AbilityInfo> abilityInfo;
1324     auto abilityContext = AbilityRuntime::Context::ConvertTo<AbilityRuntime::AbilityContext>(stageContext);
1325     if (abilityContext != nullptr) {
1326         abilityInfo = abilityContext->GetAbilityInfo();
1327     } else {
1328         auto extensionContext = AbilityRuntime::Context::ConvertTo<AbilityRuntime::ExtensionContext>(stageContext);
1329         CHECK_RETURN(extensionContext != nullptr, "context ConvertTo AbilityContext and ExtensionContext fail",
1330                      napi_generic_failure);
1331         abilityInfo = extensionContext->GetAbilityInfo();
1332     }
1333     out.SetBundleName(abilityInfo->bundleName);
1334     out.SetAbilityName(abilityInfo->name);
1335     return napi_ok;
1336 }
1337 
GetFaElementName(napi_env env,AppExecFwk::ElementName & out)1338 napi_status NapiUtils::GetFaElementName(napi_env env, AppExecFwk::ElementName& out)
1339 {
1340     auto* ability = AbilityRuntime::GetCurrentAbility(env);
1341     CHECK_RETURN(ability != nullptr, "get feature ability failed", napi_generic_failure);
1342     auto want = ability->GetWant();
1343     CHECK_RETURN(want != nullptr, "get want failed", napi_generic_failure);
1344     out = want->GetElement();
1345     return napi_ok;
1346 }
1347 
GetValue(napi_env env,napi_value in,AppExecFwk::ElementName & out)1348 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AppExecFwk::ElementName& out)
1349 {
1350     bool isStageMode = false;
1351     CHECK_RETURN(AbilityRuntime::IsStageContext(env, in, isStageMode) == napi_ok, "get context type failed",
1352                  napi_generic_failure);
1353     if (isStageMode) {
1354         CHECK_RETURN(GetStageElementName(env, in, out) == napi_ok, "get StagContext failed", napi_generic_failure);
1355     } else {
1356         CHECK_RETURN(GetFaElementName(env, out) == napi_ok, "get FaContext failed", napi_generic_failure);
1357     }
1358     return napi_ok;
1359 }
1360 
GetValue(napi_env env,napi_value in,SessionToken & out)1361 napi_status NapiUtils::GetValue(napi_env env, napi_value in, SessionToken& out)
1362 {
1363     napi_value value {};
1364     auto status = napi_get_named_property(env, in, "sessionId", &value);
1365     CHECK_RETURN(status == napi_ok, "get SessionToken sessionId failed", status);
1366     status = GetValue(env, value, out.sessionId);
1367     CHECK_RETURN(status == napi_ok, "get SessionToken sessionId value failed", status);
1368 
1369     bool hasPid = false;
1370     NAPI_CALL_BASE(env, napi_has_named_property(env, in, "pid", &hasPid), napi_invalid_arg);
1371     if (hasPid) {
1372         status = napi_get_named_property(env, in, "pid", &value);
1373         CHECK_RETURN(status == napi_ok, "get SessionToken pid failed", status);
1374         status = GetValue(env, value, out.pid);
1375         CHECK_RETURN(status == napi_ok, "get SessionToken pid value failed", status);
1376     } else {
1377         out.pid = 0;
1378     }
1379 
1380     bool hasUid = false;
1381     NAPI_CALL_BASE(env, napi_has_named_property(env, in, "uid", &hasUid), napi_invalid_arg);
1382     if (hasUid) {
1383         status = napi_get_named_property(env, in, "uid", &value);
1384         CHECK_RETURN(status == napi_ok, "get SessionToken uid failed", status);
1385         status = GetValue(env, value, out.pid);
1386         CHECK_RETURN(status == napi_ok, "get SessionToken uid value failed", status);
1387     } else {
1388         out.uid = 0;
1389     }
1390     return napi_ok;
1391 }
1392 
GetValue(napi_env env,napi_value in,AudioStandard::DeviceRole & out)1393 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AudioStandard::DeviceRole& out)
1394 {
1395     int32_t deviceRole;
1396     auto status = GetValue(env, in, deviceRole);
1397     CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor deviceRole failed", status);
1398     out = static_cast<AudioStandard::DeviceRole>(deviceRole);
1399     return napi_ok;
1400 }
1401 
GetValue(napi_env env,napi_value in,AudioStandard::DeviceType & out)1402 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AudioStandard::DeviceType& out)
1403 {
1404     int32_t deviceType;
1405     auto status = GetValue(env, in, deviceType);
1406     CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor deviceType failed", status);
1407     out = static_cast<AudioStandard::DeviceType>(deviceType);
1408     return napi_ok;
1409 }
1410 
GetSampleRate(napi_env env,napi_value in,AudioStandard::AudioSamplingRate & out)1411 napi_status NapiUtils::GetSampleRate(napi_env env, napi_value in, AudioStandard::AudioSamplingRate& out)
1412 {
1413     napi_value value {};
1414     auto status = napi_get_named_property(env, in, "sampleRates", &value);
1415     uint32_t length {};
1416     napi_get_array_length(env, value, &length);
1417     CHECK_RETURN(status == napi_ok, "get array length failed", status);
1418     if (length > 0) {
1419         napi_value element {};
1420         status = napi_get_element(env, value, 0, &element);
1421         CHECK_RETURN((status == napi_ok) && (element != nullptr), "get element failed", status);
1422         int32_t samplingRate;
1423         status = GetValue(env, element, samplingRate);
1424         CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor audioStreamInfo_ samplingRate value failed", status);
1425         out = static_cast<AudioStandard::AudioSamplingRate>(samplingRate);
1426     }
1427     return status;
1428 }
1429 
GetChannels(napi_env env,napi_value in,AudioStandard::AudioChannel & out)1430 napi_status NapiUtils::GetChannels(napi_env env, napi_value in, AudioStandard::AudioChannel& out)
1431 {
1432     napi_value value {};
1433     auto status = napi_get_named_property(env, in, "channelCounts", &value);
1434     uint32_t length {};
1435     napi_get_array_length(env, value, &length);
1436     CHECK_RETURN(status == napi_ok, "get array length failed", status);
1437     if (length > 0) {
1438         napi_value element {};
1439         status = napi_get_element(env, value, 0, &element);
1440         CHECK_RETURN((status == napi_ok) && (element != nullptr), "get element failed", status);
1441         int32_t channel;
1442         status = GetValue(env, element, channel);
1443         CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor audioStreamInfo_ channels value failed", status);
1444         out = static_cast<AudioStandard::AudioChannel>(channel);
1445     }
1446     return status;
1447 }
1448 
GetChannelMasks(napi_env env,napi_value in,int32_t & out)1449 napi_status NapiUtils::GetChannelMasks(napi_env env, napi_value in, int32_t& out)
1450 {
1451     napi_value value {};
1452     auto status = napi_get_named_property(env, in, "channelMasks", &value);
1453     uint32_t length {};
1454     napi_get_array_length(env, value, &length);
1455     CHECK_RETURN(status == napi_ok, "get array length failed", status);
1456     if (length > 0) {
1457         napi_value element {};
1458         status = napi_get_element(env, value, 0, &element);
1459         CHECK_RETURN((status == napi_ok) && (element != nullptr), "get element failed", status);
1460         status = GetValue(env, element, out);
1461         CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor channelMasks_ value failed", status);
1462     }
1463     return status;
1464 }
1465 
GetValue(napi_env env,napi_value in,AudioStandard::AudioDeviceDescriptor & out)1466 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AudioStandard::AudioDeviceDescriptor& out)
1467 {
1468     napi_value value {};
1469     auto status = napi_get_named_property(env, in, "id", &value);
1470     CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor deviceId_ failed", status);
1471     status = GetValue(env, value, out.deviceId_);
1472     CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor deviceId_ value failed", status);
1473 
1474     status = napi_get_named_property(env, in, "name", &value);
1475     CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor deviceName_ failed", status);
1476     status = GetValue(env, value, out.deviceName_);
1477     CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor deviceName_ value failed", status);
1478 
1479     status = napi_get_named_property(env, in, "networkId", &value);
1480     CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor networkId failed", status);
1481     status = GetValue(env, value, out.networkId_);
1482     CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor networkId value failed", status);
1483 
1484     status = napi_get_named_property(env, in, "deviceRole", &value);
1485     CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor deviceRole_ failed", status);
1486     status = GetValue(env, value, out.deviceRole_);
1487     CHECK_RETURN(status == napi_ok, "get AudioDeviceDescriptor deviceRole_ value failed", status);
1488 
1489     if (napi_get_named_property(env, in, "address", &value) == napi_ok) {
1490         GetValue(env, value, out.macAddress_);
1491     }
1492 
1493     if (napi_get_named_property(env, in, "deviceType", &value) == napi_ok) {
1494         GetValue(env, value, out.deviceType_);
1495     }
1496 
1497     if (napi_get_named_property(env, in, "interruptGroupId", &value) == napi_ok) {
1498         GetValue(env, value, out.interruptGroupId_);
1499     }
1500 
1501     if (napi_get_named_property(env, in, "volumeGroupId", &value) == napi_ok) {
1502         GetValue(env, value, out.volumeGroupId_);
1503     }
1504 
1505     AudioStandard::AudioSamplingRate audioSamplingRate;
1506     GetSampleRate(env, in, audioSamplingRate);
1507     out.audioStreamInfo_.samplingRate = {audioSamplingRate};
1508 
1509     AudioStandard::AudioChannel audioChannel;
1510     GetChannels(env, in, audioChannel);
1511     out.audioStreamInfo_.channels = {audioChannel};
1512 
1513     GetChannelMasks(env, in, out.channelMasks_);
1514     return napi_ok;
1515 }
1516 
GetValue(napi_env env,napi_value in,std::vector<AudioStandard::AudioDeviceDescriptor> & out)1517 napi_status NapiUtils::GetValue(napi_env env, napi_value in, std::vector<AudioStandard::AudioDeviceDescriptor>& out)
1518 {
1519     uint32_t length {};
1520     auto status = napi_get_array_length(env, in, &length);
1521     CHECK_RETURN(status == napi_ok, "get array length failed", status);
1522     for (uint32_t i = 0; i < length; ++i) {
1523         napi_value element {};
1524         status = napi_get_element(env, in, i, &element);
1525         CHECK_RETURN((status == napi_ok) && (element != nullptr), "get element failed", status);
1526         AudioStandard::AudioDeviceDescriptor descriptor;
1527         status = GetValue(env, element, descriptor);
1528         out.push_back(descriptor);
1529     }
1530     return napi_ok;
1531 }
1532 
GetOptionalString(napi_env env,napi_value in,DeviceInfo & out)1533 napi_status NapiUtils::GetOptionalString(napi_env env, napi_value in, DeviceInfo& out)
1534 {
1535     napi_value value {};
1536     bool hasIpAddress = false;
1537     napi_has_named_property(env, in, "ipAddress", &hasIpAddress);
1538     if (hasIpAddress) {
1539         napi_status status = napi_get_named_property(env, in, "ipAddress", &value);
1540         CHECK_RETURN(status == napi_ok, "get DeviceInfo ipAddress failed", status);
1541 
1542         napi_valuetype type = napi_undefined;
1543         status = napi_typeof(env, value, &type);
1544         CHECK_RETURN((status == napi_ok) && type == napi_string, "invalid typ for ipAddress", napi_invalid_arg);
1545 
1546         size_t maxLen = STR_MAX_LENGTH;
1547         status = napi_get_value_string_utf8(env, value, nullptr, 0, &maxLen);
1548         if (maxLen == 0) {
1549             out.ipAddress_ = "";
1550         } else {
1551             if (maxLen < 0 || maxLen >= STR_MAX_LENGTH) {
1552                 return napi_invalid_arg;
1553             }
1554             char buf[STR_MAX_LENGTH + STR_TAIL_LENGTH] {};
1555             size_t len = 0;
1556             status = napi_get_value_string_utf8(env, value, buf, maxLen + STR_TAIL_LENGTH, &len);
1557             if (status == napi_ok) {
1558                 out.ipAddress_ = std::string(buf);
1559             }
1560         }
1561         CHECK_RETURN(status == napi_ok, "get DeviceInfo ipAddress value failed", status);
1562     } else {
1563         out.ipAddress_ = "";
1564     }
1565     return napi_ok;
1566 }
1567 
1568 /* napi_value -> DeviceInfo */
GetValue(napi_env env,napi_value in,DeviceInfo & out)1569 napi_status NapiUtils::GetValue(napi_env env, napi_value in, DeviceInfo& out)
1570 {
1571     napi_value value {};
1572     auto status = napi_get_named_property(env, in, "castCategory", &value);
1573     CHECK_RETURN(status == napi_ok, "get DeviceInfo castCategory_ failed", status);
1574     status = GetValue(env, value, out.castCategory_);
1575     CHECK_RETURN(status == napi_ok, "get DeviceInfo castCategory_ value failed", status);
1576     status = napi_get_named_property(env, in, "deviceId", &value);
1577     CHECK_RETURN(status == napi_ok, "get DeviceInfo deviceId_ failed", status);
1578     status = GetValue(env, value, out.deviceId_);
1579     CHECK_RETURN(status == napi_ok, "get DeviceInfo deviceId_ value failed", status);
1580     status = napi_get_named_property(env, in, "manufacturer", &value);
1581     CHECK_RETURN(status == napi_ok, "get DeviceInfo manufacturer_ failed", status);
1582     status = GetValue(env, value, out.manufacturer_);
1583     CHECK_RETURN(status == napi_ok, "get DeviceInfo manufacturer_ value failed", status);
1584     status = napi_get_named_property(env, in, "modelName", &value);
1585     CHECK_RETURN(status == napi_ok, "get DeviceInfo modelName_ failed", status);
1586     status = GetValue(env, value, out.modelName_);
1587     CHECK_RETURN(status == napi_ok, "get DeviceInfo modelName_ value failed", status);
1588     status = napi_get_named_property(env, in, "deviceName", &value);
1589     CHECK_RETURN(status == napi_ok, "get DeviceInfo deviceName_ failed", status);
1590     status = GetValue(env, value, out.deviceName_);
1591     CHECK_RETURN(status == napi_ok, "get DeviceInfo deviceName_ value failed", status);
1592     status = napi_get_named_property(env, in, "deviceType", &value);
1593     CHECK_RETURN(status == napi_ok, "get DeviceInfo deviceType_ failed", status);
1594     status = GetValue(env, value, out.deviceType_);
1595     CHECK_RETURN(status == napi_ok, "get DeviceInfo deviceType_ value failed", status);
1596     CHECK_RETURN(GetOptionalString(env, in, out) == napi_ok, "get DeviceInfo ip address value failed", status);
1597     status = napi_get_named_property(env, in, "networkId", &value);
1598     CHECK_RETURN(status == napi_ok, "get DeviceInfo networkId_ failed", status);
1599     status = GetValue(env, value, out.networkId_);
1600     CHECK_RETURN(status == napi_ok, "get DeviceInfo networkId_ value failed", status);
1601 
1602     bool hasKey = false;
1603     napi_has_named_property(env, in, "providerId", &hasKey);
1604     if (hasKey) {
1605         status = napi_get_named_property(env, in, "providerId", &value);
1606         CHECK_RETURN(status == napi_ok, "get DeviceInfo providerId failed", status);
1607         status = GetValue(env, value, out.providerId_);
1608         CHECK_RETURN(status == napi_ok, "get DeviceInfo providerId value failed", status);
1609     } else {
1610         out.providerId_ = 0;
1611     }
1612 
1613     status = ProcessDeviceInfoParams(env, in, out);
1614     CHECK_RETURN(status == napi_ok, "get DeviceInfo ProcessDeviceInfoParams failed", status);
1615 
1616     return napi_ok;
1617 }
1618 
ProcessDeviceInfoParams(napi_env env,napi_value in,DeviceInfo & out)1619 napi_status NapiUtils::ProcessDeviceInfoParams(napi_env env, napi_value in, DeviceInfo& out)
1620 {
1621     napi_value value {};
1622     bool hasKey = false;
1623     napi_status status = napi_ok;
1624     napi_has_named_property(env, in, "supportedProtocols", &hasKey);
1625     if (hasKey) {
1626         status = napi_get_named_property(env, in, "supportedProtocols", &value);
1627         CHECK_RETURN(status == napi_ok, "get DeviceInfo supportedProtocols failed", status);
1628         status = GetValue(env, value, out.supportedProtocols_);
1629         CHECK_RETURN(status == napi_ok, "get DeviceInfo supportedProtocols value failed", status);
1630     } else {
1631         out.supportedProtocols_ = ProtocolType::TYPE_CAST_PLUS_STREAM;
1632     }
1633     napi_has_named_property(env, in, "authenticationStatus", &hasKey);
1634     if (hasKey) {
1635         status = napi_get_named_property(env, in, "authenticationStatus", &value);
1636         CHECK_RETURN(status == napi_ok, "get DeviceInfo authenticationStatus failed", status);
1637         status = GetValue(env, value, out.authenticationStatus_);
1638         CHECK_RETURN(status == napi_ok, "get DeviceInfo authenticationStatus value failed", status);
1639     } else {
1640         out.authenticationStatus_ = 0;
1641     }
1642     napi_has_named_property(env, in, "supportedDrmCapabilities", &hasKey);
1643     if (hasKey) {
1644         status = napi_get_named_property(env, in, "supportedDrmCapabilities", &value);
1645         CHECK_RETURN(status == napi_ok, "get DeviceInfo supportedDrmCapabilities failed", status);
1646         status = GetValue(env, value, out.supportedDrmCapabilities_);
1647         CHECK_RETURN(status == napi_ok, "get DeviceInfo supportedDrmCapabilities value failed", status);
1648     }
1649     napi_has_named_property(env, in, "isLegacy", &hasKey);
1650     if (hasKey) {
1651         status = napi_get_named_property(env, in, "isLegacy", &value);
1652         CHECK_RETURN(status == napi_ok, "get DeviceInfo isLegacy failed", status);
1653         status = GetValue(env, value, out.isLegacy_);
1654         CHECK_RETURN(status == napi_ok, "get DeviceInfo isLegacy value failed", status);
1655     } else {
1656         out.isLegacy_ = false;
1657     }
1658     napi_has_named_property(env, in, "mediumTypes", &hasKey);
1659     if (hasKey) {
1660         status = napi_get_named_property(env, in, "mediumTypes", &value);
1661         CHECK_RETURN(status == napi_ok, "get DeviceInfo mediumTypes failed", status);
1662         status = GetValue(env, value, out.mediumTypes_);
1663         CHECK_RETURN(status == napi_ok, "get DeviceInfo mediumTypes value failed", status);
1664     } else {
1665         out.mediumTypes_ = COAP;
1666     }
1667     return napi_ok;
1668 }
1669 
1670 /* napi_value -> OutputDeviceInfo */
GetValue(napi_env env,napi_value in,OutputDeviceInfo & out)1671 napi_status NapiUtils::GetValue(napi_env env, napi_value in, OutputDeviceInfo& out)
1672 {
1673     napi_value devices = nullptr;
1674     bool hasProperty = false;
1675     NAPI_CALL_BASE(env, napi_has_named_property(env, in, "devices", &hasProperty), napi_invalid_arg);
1676     if (!hasProperty) {
1677         SLOGE("devices is not exit in OutputDeviceInfo");
1678         return napi_invalid_arg;
1679     }
1680     NAPI_CALL_BASE(env, napi_get_named_property(env, in, "devices", &devices), napi_invalid_arg);
1681     bool isArray = false;
1682     NAPI_CALL_BASE(env, napi_is_array(env, devices, &isArray), napi_invalid_arg);
1683     if (!isArray) {
1684         SLOGE("devices is not array");
1685         return napi_invalid_arg;
1686     }
1687 
1688     uint32_t arrLen = 0;
1689     NAPI_CALL_BASE(env, napi_get_array_length(env, devices, &arrLen), napi_invalid_arg);
1690     if (arrLen == 0) {
1691         SLOGE("devices len is invalid");
1692         return napi_invalid_arg;
1693     }
1694 
1695     for (uint32_t i = 0; i < arrLen; i++) {
1696         napi_value item = nullptr;
1697         NAPI_CALL_BASE(env, napi_get_element(env, devices, i, &item), napi_invalid_arg);
1698         DeviceInfo deviceInfo;
1699         napi_status status = GetValue(env, item, deviceInfo);
1700         CHECK_RETURN(status == napi_ok, "not is device info", status);
1701         out.deviceInfos_.push_back(deviceInfo);
1702     }
1703     return napi_ok;
1704 }
1705 
1706 /* napi_value -> MediaInfoHolder */
GetValue(napi_env env,napi_value in,MediaInfoHolder & out)1707 napi_status NapiUtils::GetValue(napi_env env, napi_value in, MediaInfoHolder& out)
1708 {
1709     return NapiMediaInfoHolder::GetValue(env, in, out);
1710 }
1711 
1712 /* napi_value <-> MediaInfoHolder */
SetValue(napi_env env,const MediaInfoHolder & in,napi_value & out)1713 napi_status NapiUtils::SetValue(napi_env env, const MediaInfoHolder& in, napi_value& out)
1714 {
1715     return NapiMediaInfoHolder::SetValue(env, in, out);
1716 }
1717 
1718 /* napi_value -> MediaInfo */
GetValue(napi_env env,napi_value in,MediaInfo & out)1719 napi_status NapiUtils::GetValue(napi_env env, napi_value in, MediaInfo& out)
1720 {
1721     napi_value value {};
1722     auto status = napi_get_named_property(env, in, "mediaId", &value);
1723     CHECK_RETURN(status == napi_ok, "get mediaId failed", status);
1724     status = GetValue(env, value, out.mediaId_);
1725     CHECK_RETURN(status == napi_ok, "get mediaId value failed", status);
1726 
1727     status = napi_get_named_property(env, in, "mediaUrl", &value);
1728     CHECK_RETURN(status == napi_ok, "get mediaUrl failed", status);
1729     status = GetValue(env, value, out.mediaUrl_);
1730     CHECK_RETURN(status == napi_ok, "get mediaUrl value failed", status);
1731 
1732     bool hasStartPosition = false;
1733     napi_has_named_property(env, in, "startPosition", &hasStartPosition);
1734     if (hasStartPosition) {
1735         status = napi_get_named_property(env, in, "startPosition", &value);
1736         CHECK_RETURN(status == napi_ok, "get MediaInfo startPosition failed", status);
1737         status = GetValue(env, value, out.startPosition_);
1738         CHECK_RETURN(status == napi_ok, "get MediaInfo startPosition value failed", status);
1739     } else {
1740         out.startPosition_ = -1;
1741     }
1742     return napi_ok;
1743 }
1744 
1745 /* napi_value <- MediaInfo */
SetValue(napi_env env,const MediaInfo & in,napi_value & out)1746 napi_status NapiUtils::SetValue(napi_env env, const MediaInfo& in, napi_value& out)
1747 {
1748     napi_status status = napi_create_object(env, &out);
1749     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
1750 
1751     napi_value property = nullptr;
1752     status = SetValue(env, in.mediaId_, property);
1753     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1754     status = napi_set_named_property(env, out, "mediaId", property);
1755     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1756 
1757     status = SetValue(env, in.mediaUrl_, property);
1758     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1759     status = napi_set_named_property(env, out, "mediaUrl", property);
1760     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1761 
1762     if (in.startPosition_ != -1) {
1763         status = SetValue(env, in.startPosition_, property);
1764         CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1765         status = napi_set_named_property(env, out, "mediaInfo", property);
1766         CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1767     }
1768 
1769     return napi_ok;
1770 }
1771 
1772 /* napi_value -> AVFileDescriptor */
GetValue(napi_env env,napi_value in,AVFileDescriptor & out)1773 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AVFileDescriptor& out)
1774 {
1775     napi_value value {};
1776     auto status = napi_ok;
1777     bool hasFd = false;
1778     napi_has_named_property(env, in, "fd", &hasFd);
1779     if (hasFd) {
1780         status = napi_get_named_property(env, in, "fd", &value);
1781         CHECK_RETURN(status == napi_ok, "get fd failed", status);
1782         status = GetValue(env, value, out.fd_);
1783         CHECK_RETURN(status == napi_ok, "get fd value failed", status);
1784     } else {
1785         out.fd_ = 0;
1786     }
1787 
1788     bool hasOffset = false;
1789     napi_has_named_property(env, in, "offset", &hasOffset);
1790     if (hasOffset) {
1791         status = napi_get_named_property(env, in, "offset", &value);
1792         CHECK_RETURN(status == napi_ok, "get offset failed", status);
1793         status = GetValue(env, value, out.offset_);
1794         CHECK_RETURN(status == napi_ok, "get offset value failed", status);
1795     } else {
1796         out.offset_ = 0;
1797     }
1798 
1799     bool hasLength = false;
1800     napi_has_named_property(env, in, "length", &hasLength);
1801     if (hasLength) {
1802         status = napi_get_named_property(env, in, "length", &value);
1803         CHECK_RETURN(status == napi_ok, "get length failed", status);
1804         status = GetValue(env, value, out.length_);
1805         CHECK_RETURN(status == napi_ok, "get length value failed", status);
1806     } else {
1807         out.length_ = -1;
1808     }
1809     return napi_ok;
1810 }
1811 
1812 /* napi_value <- AVFileDescriptor */
SetValue(napi_env env,const AVFileDescriptor & in,napi_value & out)1813 napi_status NapiUtils::SetValue(napi_env env, const AVFileDescriptor& in, napi_value& out)
1814 {
1815     napi_status status = napi_create_object(env, &out);
1816     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
1817 
1818     napi_value property = nullptr;
1819     status = SetValue(env, in.fd_, property);
1820     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1821     status = napi_set_named_property(env, out, "fd", property);
1822     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1823 
1824     if (in.offset_ != 0) {
1825         status = SetValue(env, in.offset_, property);
1826         CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1827         status = napi_set_named_property(env, out, "offset", property);
1828         CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1829     }
1830 
1831     if (in.length_ != -1) {
1832         status = SetValue(env, in.length_, property);
1833         CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1834         status = napi_set_named_property(env, out, "length", property);
1835         CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1836     }
1837     return napi_ok;
1838 }
1839 
GetValue(napi_env env,napi_value in,DistributedSessionType & out)1840 napi_status NapiUtils::GetValue(napi_env env, napi_value in, DistributedSessionType& out)
1841 {
1842     napi_value value {};
1843     auto status = napi_ok;
1844     bool hasSessionType = false;
1845     int32_t type = -1;
1846     napi_has_named_property(env, in, "distributedSessionType", &hasSessionType);
1847     if (hasSessionType) {
1848         status = napi_get_named_property(env, in, "distributedSessionType", &value);
1849         CHECK_RETURN(status == napi_ok, "get sessionType failed", status);
1850         status = GetValue(env, value, type);
1851         CHECK_RETURN(status == napi_ok, "get sessionType value failed", status);
1852     }
1853     out = DistributedSessionType(type);
1854     return napi_ok;
1855 }
SetValue(napi_env env,const std::vector<std::shared_ptr<AVSessionController>> & in,napi_value & out)1856 napi_status NapiUtils::SetValue(
1857     napi_env env, const std::vector<std::shared_ptr<AVSessionController>>& in, napi_value& out)
1858 {
1859     SLOGD("napi_value <- std::vector<std::shared_ptr<AVSessionController>>  %{public}d", static_cast<int>(in.size()));
1860     napi_status status = napi_create_array_with_length(env, in.size(), &out);
1861     CHECK_RETURN((status == napi_ok), "create_array failed!", status);
1862     int index = 0;
1863     for (const auto& item : in) {
1864         napi_value entry = nullptr;
1865         NapiAVSessionController::NewInstance(env, item, entry);
1866         napi_set_element(env, out, index++, entry);
1867     }
1868     return status;
1869 }
1870 
1871 /* napi_value -> AVDataSrcDescriptor */
GetValue(napi_env env,napi_value in,AVDataSrcDescriptor & out)1872 napi_status NapiUtils::GetValue(napi_env env, napi_value in, AVDataSrcDescriptor& out)
1873 {
1874     napi_value value {};
1875     auto status = napi_ok;
1876     bool hasFileSize = false;
1877     status = napi_has_named_property(env, in, "fileSize", &hasFileSize);
1878     CHECK_RETURN(status == napi_ok && hasFileSize, "get fileSize failed!", status);
1879     status = napi_get_named_property(env, in, "fileSize", &value);
1880     CHECK_RETURN(status == napi_ok, "get fileSize failed", status);
1881     status = GetValue(env, value, out.fileSize);
1882     CHECK_RETURN(status == napi_ok, "get fileSize value failed", status);
1883 
1884     bool hasCallback = false;
1885     status = napi_has_named_property(env, in, "callback", &hasCallback);
1886     CHECK_RETURN(status == napi_ok && hasCallback, "get callback failed!", status);
1887     out.hasCallback = hasCallback;
1888 
1889     return napi_ok;
1890 }
1891 
1892 /* napi_value <- AVDataSrcDescriptor */
SetValue(napi_env env,const AVDataSrcDescriptor & in,napi_value & out)1893 napi_status NapiUtils::SetValue(napi_env env, const AVDataSrcDescriptor& in, napi_value& out)
1894 {
1895     napi_status status = napi_create_object(env, &out);
1896     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
1897 
1898     napi_value property = nullptr;
1899     status = SetValue(env, in.fileSize, property);
1900     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create object failed", status);
1901     status = napi_set_named_property(env, out, "fileSize", property);
1902     CHECK_RETURN(status == napi_ok, "napi_set_named_property failed", status);
1903     return napi_ok;
1904 }
1905 
ThrowError(napi_env env,const char * napiMessage,int32_t napiCode)1906 napi_status NapiUtils::ThrowError(napi_env env, const char* napiMessage, int32_t napiCode)
1907 {
1908     napi_value message = nullptr;
1909     napi_value code = nullptr;
1910     napi_value result = nullptr;
1911     napi_create_string_utf8(env, napiMessage, NAPI_AUTO_LENGTH, &message);
1912     napi_create_error(env, nullptr, message, &result);
1913     napi_create_int32(env, napiCode, &code);
1914     napi_set_named_property(env, result, "code", code);
1915     napi_throw(env, result);
1916     return napi_ok;
1917 }
1918 }
1919