1 /*
2 * Copyright (C) 2023-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 * Description: supply untils realization for napi interface.
15 * Author: zhangjingnan
16 * Create: 2022-7-11
17 */
18
19 #include <uv.h>
20 #include <memory>
21 #include "securec.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "cast_engine_log.h"
25 #include "cast_engine_common.h"
26 #include "napi_cast_session.h"
27 #include "napi_castengine_utils.h"
28
29 using namespace std;
30 using namespace OHOS::CastEngine;
31
32 namespace OHOS {
33 namespace CastEngine {
34 namespace CastEngineClient {
35 DEFINE_CAST_ENGINE_LABEL("Cast-Napi-Utils");
36
GetUndefinedValue(napi_env env)37 napi_value GetUndefinedValue(napi_env env)
38 {
39 napi_value result {};
40 napi_get_undefined(env, &result);
41 return result;
42 }
43
ParseString(napi_env env,napi_value args)44 string ParseString(napi_env env, napi_value args)
45 {
46 string result {};
47 if (args == nullptr) {
48 CLOGE("args is nullptr");
49 return result;
50 }
51 napi_valuetype valueType;
52 napi_status status = napi_typeof(env, args, &valueType);
53 CLOGD("param=%{public}d.", valueType);
54 if (status != napi_ok || valueType != napi_string) {
55 CLOGE("Wrong argument type. String expected.");
56 result = "";
57 return result;
58 }
59 size_t size = 0;
60 size_t bufSize = 0;
61
62 if (napi_get_value_string_utf8(env, args, nullptr, bufSize, &size) != napi_ok) {
63 CLOGE("can not get string size");
64 result = "";
65 return result;
66 }
67 result.reserve(size + 1);
68 result.resize(size);
69 if (napi_get_value_string_utf8(env, args, result.data(), (size + 1), &size) != napi_ok) {
70 CLOGE("can not get string value");
71 result = "";
72 return result;
73 }
74 return result;
75 }
76
ParseInt32(napi_env env,napi_value args)77 int32_t ParseInt32(napi_env env, napi_value args)
78 {
79 int32_t param = 0;
80 if (args == nullptr) {
81 CLOGE("args is nullptr");
82 return param;
83 }
84 napi_valuetype valueType;
85 napi_status status = napi_typeof(env, args, &valueType);
86 if (status != napi_ok) {
87 CLOGE("napi_typeof failed.");
88 return param;
89 }
90 CLOGD("param=%{public}d.", valueType);
91 if (valueType != napi_number) {
92 CLOGE("Wrong argument type. Int32 expected.");
93 return param;
94 }
95 status = napi_get_value_int32(env, args, ¶m);
96 if (status != napi_ok) {
97 CLOGE("napi_get_value_int32 failed.");
98 return param;
99 }
100 return param;
101 }
102
ParseUint32(napi_env env,napi_value args)103 uint32_t ParseUint32(napi_env env, napi_value args)
104 {
105 uint32_t param = 0;
106 if (args == nullptr) {
107 CLOGE("args is nullptr");
108 return param;
109 }
110 napi_valuetype valueType;
111 napi_status status = napi_typeof(env, args, &valueType);
112 if (status != napi_ok) {
113 CLOGE("napi_typeof failed.");
114 return param;
115 }
116 CLOGD("param=%{public}d.", valueType);
117 if (valueType != napi_number) {
118 CLOGE("Wrong argument type. Int32 expected.");
119 return param;
120 }
121 status = napi_get_value_uint32(env, args, ¶m);
122 if (status != napi_ok) {
123 CLOGE("napi_get_value_int32 failed.");
124 return param;
125 }
126 return param;
127 }
128
ParseBool(napi_env env,napi_value args)129 bool ParseBool(napi_env env, napi_value args)
130 {
131 bool param = false;
132 if (args == nullptr) {
133 CLOGE("args is nullptr");
134 return param;
135 }
136 napi_valuetype valueType;
137 napi_status status = napi_typeof(env, args, &valueType);
138 if (status != napi_ok) {
139 CLOGE("napi_typeof failed.");
140 return param;
141 }
142
143 CLOGD("param=%{public}d.", valueType);
144 if (valueType != napi_boolean) {
145 CLOGE("Wrong argument type. bool expected.");
146 return param;
147 }
148 status = napi_get_value_bool(env, args, ¶m);
149 if (status != napi_ok) {
150 CLOGE("napi_get_value_bool failed.");
151 return param;
152 }
153 return param;
154 }
155
ConvertDeviceListToJS(napi_env env,const vector<CastRemoteDevice> & devices)156 napi_value ConvertDeviceListToJS(napi_env env, const vector<CastRemoteDevice> &devices)
157 {
158 int count = 0;
159 napi_value devicesList = nullptr;
160 NAPI_CALL(env, napi_create_array(env, &devicesList));
161 for (CastRemoteDevice vec : devices) {
162 napi_value deviceResult = ConvertCastRemoteDeviceToJS(env, vec);
163 NAPI_CALL(env, napi_set_element(env, devicesList, count, deviceResult));
164 count++;
165 }
166
167 if (devicesList == nullptr) {
168 CLOGE("devicesList is null");
169 }
170 return devicesList;
171 }
172
ConvertCastSessionToJS(napi_env env,const shared_ptr<ICastSession> & castSession)173 napi_value ConvertCastSessionToJS(napi_env env, const shared_ptr<ICastSession> &castSession)
174 {
175 napi_value napiCastSession;
176 NapiCastSession::CreateNapiCastSession(env, castSession, napiCastSession);
177 if (napiCastSession == nullptr) {
178 CLOGE("napiCastSession is null");
179 }
180 return napiCastSession;
181 }
182
ConvertDeviceStateInfoToJS(napi_env env,const DeviceStateInfo & stateEvent)183 napi_value ConvertDeviceStateInfoToJS(napi_env env, const DeviceStateInfo &stateEvent)
184 {
185 napi_value stateEventCallback = nullptr;
186 NAPI_CALL(env, napi_create_object(env, &stateEventCallback));
187
188 napi_value deviceState = nullptr;
189 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(stateEvent.deviceState), &deviceState));
190 NAPI_CALL(env, napi_set_named_property(env, stateEventCallback, "deviceState", deviceState));
191
192 napi_value deviceId = nullptr;
193 NAPI_CALL(env, napi_create_string_utf8(env, stateEvent.deviceId.c_str(), NAPI_AUTO_LENGTH, &deviceId));
194 NAPI_CALL(env, napi_set_named_property(env, stateEventCallback, "deviceId", deviceId));
195
196 napi_value eventCode = nullptr;
197 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(stateEvent.eventCode), &eventCode));
198 NAPI_CALL(env, napi_set_named_property(env, stateEventCallback, "eventCode", eventCode));
199
200 return stateEventCallback;
201 }
202
GetCastRemoteDeviceFromJS(napi_env env,napi_value & object)203 CastRemoteDevice GetCastRemoteDeviceFromJS(napi_env env, napi_value &object)
204 {
205 CastRemoteDevice castRemoteDevice = CastRemoteDevice();
206
207 castRemoteDevice.deviceId = JsObjectToString(env, object, "deviceId");
208 castRemoteDevice.deviceName = JsObjectToString(env, object, "deviceName");
209 int32_t deviceTypeCallback = JsObjectToInt32(env, object, "deviceType");
210 DeviceType deviceType = static_cast<DeviceType>(deviceTypeCallback);
211 int32_t subDeviceTypeCallback = JsObjectToInt32(env, object, "subDeviceType");
212 SubDeviceType subDeviceType = static_cast<SubDeviceType>(subDeviceTypeCallback);
213 castRemoteDevice.ipAddress = JsObjectToString(env, object, "ipAddress");
214 int32_t channelTypeInt = JsObjectToInt32(env, object, "channelType");
215 ChannelType channelType = static_cast<ChannelType>(channelTypeInt);
216 castRemoteDevice.networkId = JsObjectToString(env, object, "networkId");
217 castRemoteDevice.deviceType = deviceType;
218 castRemoteDevice.subDeviceType = subDeviceType;
219 castRemoteDevice.channelType = channelType;
220 return castRemoteDevice;
221 }
222
GetWindowPropertyFromJS(napi_env env,napi_value & object)223 WindowProperty GetWindowPropertyFromJS(napi_env env, napi_value &object)
224 {
225 WindowProperty windowProperty = WindowProperty();
226
227 napi_value windowPropertyCallback = nullptr;
228 bool hasProperty = false;
229 napi_status status = napi_has_named_property(env, object, "windowProperty", &hasProperty);
230 if (status == napi_ok && hasProperty) {
231 status = napi_get_named_property(env, object, "windowProperty", &windowPropertyCallback);
232 if (status != napi_ok) {
233 CLOGE("napi_get_named_property failed.");
234 return windowProperty;
235 }
236 uint32_t width = JsObjectToUint32(env, windowPropertyCallback, "width");
237 uint32_t height = JsObjectToUint32(env, windowPropertyCallback, "height");
238 uint32_t startX = JsObjectToUint32(env, windowPropertyCallback, "startX");
239 uint32_t startY = JsObjectToUint32(env, windowPropertyCallback, "startY");
240
241 windowProperty.startX = startX;
242 windowProperty.startY = startY;
243 windowProperty.width = width;
244 windowProperty.height = height;
245 }
246 return windowProperty;
247 }
248
GetProtocolTypesFromJS(napi_env env,napi_value & object,int & protocolTypes)249 bool GetProtocolTypesFromJS(napi_env env, napi_value &object, int &protocolTypes)
250 {
251 bool isArray = false;
252 NAPI_CALL_BASE(env, napi_is_array(env, object, &isArray), false);
253 if (!isArray) {
254 CLOGE("protocolType is not array.");
255 return false;
256 }
257 uint32_t arrLen = 0;
258 NAPI_CALL_BASE(env, napi_get_array_length(env, object, &arrLen), false);
259 if (arrLen == 0) {
260 CLOGE("mediaInfoList len is invalid");
261 return false;
262 }
263 uint32_t ret = 0;
264 for (uint32_t i = 0; i < arrLen; i++) {
265 napi_value item = nullptr;
266 NAPI_CALL_BASE(env, napi_get_element(env, object, i, &item), false);
267 ret = ret | static_cast<uint32_t>(ParseInt32(env, item));
268 }
269 protocolTypes = static_cast<int>(ret);
270 CLOGI("GetProtocolTypesFromJS finished, protocolTypes: %{public}d", protocolTypes);
271 return true;
272 }
273
GetMediaInfoHolderFromJS(napi_env env,napi_value & object,MediaInfoHolder & mediaInfoHolder)274 bool GetMediaInfoHolderFromJS(napi_env env, napi_value &object, MediaInfoHolder &mediaInfoHolder)
275 {
276 napi_value mediaInfoList = nullptr;
277 bool hasProperty = false;
278
279 mediaInfoHolder.currentIndex = JsObjectToUint32(env, object, "currentIndex");
280 mediaInfoHolder.progressRefreshInterval = JsObjectToUint32(env, object, "progressRefreshInterval");
281 NAPI_CALL_BASE(env, napi_has_named_property(env, object, "mediaInfoList", &hasProperty), false);
282 if (!hasProperty) {
283 CLOGE("mediaInfoList is not exit");
284 return false;
285 }
286 NAPI_CALL_BASE(env, napi_get_named_property(env, object, "mediaInfoList", &mediaInfoList), false);
287 bool isArray = false;
288 NAPI_CALL_BASE(env, napi_is_array(env, mediaInfoList, &isArray), false);
289 if (!isArray) {
290 CLOGE("mediaInfoList is not array.");
291 return false;
292 }
293 uint32_t arrLen = 0;
294 NAPI_CALL_BASE(env, napi_get_array_length(env, mediaInfoList, &arrLen), false);
295 if (arrLen == 0) {
296 CLOGE("mediaInfoList len is invalid");
297 return false;
298 }
299 for (uint32_t i = 0; i < arrLen; i++) {
300 napi_value item = nullptr;
301 NAPI_CALL_BASE(env, napi_get_element(env, mediaInfoList, i, &item), false);
302 MediaInfo mediaInfo = MediaInfo{};
303 GetMediaInfoFromJS(env, item, mediaInfo);
304 mediaInfoHolder.mediaInfoList.push_back(mediaInfo);
305 }
306 return true;
307 }
308
GetMediaInfoFromJS(napi_env env,napi_value & object,MediaInfo & mediaInfo)309 bool GetMediaInfoFromJS(napi_env env, napi_value &object, MediaInfo &mediaInfo)
310 {
311 mediaInfo.mediaId = JsObjectToString(env, object, "mediaId");
312 mediaInfo.mediaName = JsObjectToString(env, object, "mediaName");
313 mediaInfo.mediaUrl = JsObjectToString(env, object, "mediaUrl");
314 mediaInfo.mediaType = JsObjectToString(env, object, "mediaType");
315 mediaInfo.albumCoverUrl = JsObjectToString(env, object, "albumCoverUrl");
316 mediaInfo.albumTitle = JsObjectToString(env, object, "albumTitle");
317 mediaInfo.mediaArtist = JsObjectToString(env, object, "mediaArtist");
318 mediaInfo.lrcUrl = JsObjectToString(env, object, "lrcUrl");
319 mediaInfo.lrcContent = JsObjectToString(env, object, "lrcContent");
320 mediaInfo.appIconUrl = JsObjectToString(env, object, "appIconUrl");
321 mediaInfo.appName = JsObjectToString(env, object, "appName");
322 mediaInfo.mediaSize = JsObjectToUint32(env, object, "mediaSize");
323 mediaInfo.startPosition = JsObjectToUint32(env, object, "startPosition");
324 mediaInfo.duration = JsObjectToUint32(env, object, "duration");
325 mediaInfo.closingCreditsPosition = JsObjectToUint32(env, object, "closingCreditsPosition");
326 return true;
327 }
328
GetCastSessionPropertyFromJS(napi_env env,napi_value & object)329 CastSessionProperty GetCastSessionPropertyFromJS(napi_env env, napi_value &object)
330 {
331 CastSessionProperty castSessionProperty = CastSessionProperty();
332
333 int32_t protocolTypeInt = JsObjectToInt32(env, object, "protocolType");
334 ProtocolType protocolType = static_cast<ProtocolType>(protocolTypeInt);
335 int32_t endTypeInt = JsObjectToInt32(env, object, "endType");
336 EndType endType = static_cast<EndType>(endTypeInt);
337 napi_value audioPropertyCallback = nullptr;
338 bool hasProperty = false;
339 napi_status status = napi_has_named_property(env, object, "audioProperty", &hasProperty);
340 if (status == napi_ok && hasProperty) {
341 status = napi_get_named_property(env, object, "audioProperty", &audioPropertyCallback);
342 if (status != napi_ok) {
343 CLOGE("napi_get_named_property failed.");
344 return castSessionProperty;
345 }
346 AudioProperty audioProperty = GetAudioPropertyFromJS(env, audioPropertyCallback);
347 castSessionProperty.audioProperty = audioProperty;
348 }
349
350 napi_value videoPropertyCallback = nullptr;
351 hasProperty = false;
352 status = napi_has_named_property(env, object, "videoProperty", &hasProperty);
353 if (status == napi_ok && hasProperty) {
354 status = napi_get_named_property(env, object, "videoProperty", &videoPropertyCallback);
355 if (status != napi_ok) {
356 CLOGE("napi_get_named_property failed.");
357 return castSessionProperty;
358 }
359 VideoProperty videoProperty = GetVideoPropertyFromJS(env, videoPropertyCallback);
360 castSessionProperty.videoProperty = videoProperty;
361 }
362 WindowProperty windowProperty = GetWindowPropertyFromJS(env, object);
363
364 castSessionProperty.protocolType = protocolType;
365 castSessionProperty.endType = endType;
366 castSessionProperty.windowProperty = windowProperty;
367 return castSessionProperty;
368 }
369
GetAudioPropertyFromJS(napi_env env,napi_value & object)370 AudioProperty GetAudioPropertyFromJS(napi_env env, napi_value &object)
371 {
372 AudioProperty audioProperty = AudioProperty();
373
374 uint32_t sampleRate = JsObjectToUint32(env, object, "sampleRate");
375 uint32_t channelConfig = JsObjectToUint32(env, object, "channelConfig");
376 uint32_t bitrate = JsObjectToUint32(env, object, "bitrate");
377 uint32_t codec = JsObjectToUint32(env, object, "codec");
378
379 audioProperty.sampleRate = sampleRate;
380 audioProperty.channelConfig = channelConfig;
381 audioProperty.bitrate = bitrate;
382 audioProperty.codec = codec;
383 return audioProperty;
384 }
385
GetVideoPropertyFromJS(napi_env env,napi_value & object)386 VideoProperty GetVideoPropertyFromJS(napi_env env, napi_value &object)
387 {
388 VideoProperty videoProperty = VideoProperty();
389 uint32_t videoWidth = JsObjectToUint32(env, object, "videoWidth");
390 uint32_t videoHeight = JsObjectToUint32(env, object, "videoHeight");
391 uint32_t fps = JsObjectToUint32(env, object, "fps");
392 int32_t codecTypeInt = JsObjectToInt32(env, object, "codecType");
393 VideoCodecType codecType = static_cast<VideoCodecType>(codecTypeInt);
394 uint32_t gop = JsObjectToUint32(env, object, "gop");
395 uint32_t bitrate = JsObjectToUint32(env, object, "bitrate");
396 uint32_t minBitrate = JsObjectToUint32(env, object, "minBitrate");
397 uint32_t maxBitrate = JsObjectToUint32(env, object, "maxBitrate");
398 uint32_t dpi = JsObjectToUint32(env, object, "dpi");
399 int32_t colorStandardInt = JsObjectToInt32(env, object, "colorStandard");
400 ColorStandard colorStandard = static_cast<ColorStandard>(colorStandardInt);
401 uint32_t screenWidth = JsObjectToUint32(env, object, "screenWidth");
402 uint32_t screenHeight = JsObjectToUint32(env, object, "screenHeight");
403 uint32_t profile = JsObjectToUint32(env, object, "profile");
404 uint32_t level = JsObjectToUint32(env, object, "level");
405
406 videoProperty.videoWidth = videoWidth;
407 videoProperty.videoHeight = videoHeight;
408 videoProperty.fps = fps;
409 videoProperty.codecType = codecType;
410 videoProperty.gop = gop;
411 videoProperty.bitrate = bitrate;
412 videoProperty.minBitrate = minBitrate;
413 videoProperty.maxBitrate = maxBitrate;
414 videoProperty.dpi = dpi;
415 videoProperty.colorStandard = colorStandard;
416 videoProperty.screenWidth = screenWidth;
417 videoProperty.screenHeight = screenHeight;
418 videoProperty.profile = profile;
419 videoProperty.level = level;
420 return videoProperty;
421 }
422
Equals(napi_env env,napi_value value,napi_ref copy)423 bool Equals(napi_env env, napi_value value, napi_ref copy)
424 {
425 if (copy == nullptr) {
426 return (value == nullptr);
427 }
428
429 napi_value copyValue = nullptr;
430 if (napi_get_reference_value(env, copy, ©Value) != napi_ok) {
431 CLOGE("get ref value failed");
432 return false;
433 }
434 bool isEquals = false;
435 if (napi_strict_equals(env, value, copyValue, &isEquals) != napi_ok) {
436 CLOGE("get equals result failed");
437 return false;
438 }
439 return isEquals;
440 }
441
GetRefByCallback(napi_env env,std::list<napi_ref> callbackList,napi_value callback,napi_ref & callbackRef)442 napi_status GetRefByCallback(napi_env env, std::list<napi_ref> callbackList, napi_value callback, napi_ref &callbackRef)
443 {
444 for (auto ref = callbackList.begin(); ref != callbackList.end(); ++ref) {
445 if (Equals(env, callback, *ref)) {
446 CLOGD("Callback has been matched");
447 callbackRef = *ref;
448 break;
449 }
450 }
451 return napi_ok;
452 }
453
ConvertCastRemoteDeviceToJS(napi_env env,const CastRemoteDevice & castRemoteDevice)454 napi_value ConvertCastRemoteDeviceToJS(napi_env env, const CastRemoteDevice &castRemoteDevice)
455 {
456 napi_value result = nullptr;
457 NAPI_CALL(env, napi_create_object(env, &result));
458 napi_value deviceId = nullptr;
459 NAPI_CALL(env, napi_create_string_utf8(env, castRemoteDevice.deviceId.c_str(), NAPI_AUTO_LENGTH, &deviceId));
460 NAPI_CALL(env, napi_set_named_property(env, result, "deviceId", deviceId));
461 napi_value deviceName = nullptr;
462 CLOGD("ConvertCastRemoteDeviceToJS deviceName %{public}s", castRemoteDevice.deviceName.c_str());
463 NAPI_CALL(env, napi_create_string_utf8(env, castRemoteDevice.deviceName.c_str(), NAPI_AUTO_LENGTH, &deviceName));
464 NAPI_CALL(env, napi_set_named_property(env, result, "deviceName", deviceName));
465 napi_value deviceType = nullptr;
466 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(castRemoteDevice.deviceType), &deviceType));
467 NAPI_CALL(env, napi_set_named_property(env, result, "deviceType", deviceType));
468 napi_value subDeviceType = nullptr;
469 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(castRemoteDevice.subDeviceType), &subDeviceType));
470 NAPI_CALL(env, napi_set_named_property(env, result, "subDeviceType", subDeviceType));
471 napi_value ipAddress = nullptr;
472 NAPI_CALL(env, napi_create_string_utf8(env, castRemoteDevice.ipAddress.c_str(), NAPI_AUTO_LENGTH, &ipAddress));
473 NAPI_CALL(env, napi_set_named_property(env, result, "ipAddress", ipAddress));
474 napi_value channelType = nullptr;
475 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(castRemoteDevice.channelType), &channelType));
476 NAPI_CALL(env, napi_set_named_property(env, result, "channelType", channelType));
477 napi_value networkId = nullptr;
478 NAPI_CALL(env, napi_create_string_utf8(env, castRemoteDevice.networkId.c_str(), NAPI_AUTO_LENGTH, &networkId));
479 NAPI_CALL(env, napi_set_named_property(env, result, "networkId", networkId));
480 return result;
481 }
482
JsObjectToString(napi_env env,napi_value & object,const char * fieldStr)483 string JsObjectToString(napi_env env, napi_value &object, const char *fieldStr)
484 {
485 string fieldRef {};
486 if (object == nullptr) {
487 CLOGE("args is nullptr");
488 return fieldRef;
489 }
490 napi_value field = nullptr;
491 bool hasProperty = false;
492
493 napi_status status = napi_has_named_property(env, object, fieldStr, &hasProperty);
494 if (status == napi_ok && hasProperty) {
495 status = napi_get_named_property(env, object, fieldStr, &field);
496 if (status != napi_ok) {
497 CLOGE("napi_get_named_property failed.");
498 return fieldRef;
499 }
500 fieldRef = ParseString(env, field);
501 return fieldRef;
502 } else {
503 CLOGE("Js obj to str no property: %{public}s", fieldStr);
504 }
505 return fieldRef;
506 }
507
JsObjectToInt32(napi_env env,napi_value & object,const char * fieldStr)508 int32_t JsObjectToInt32(napi_env env, napi_value &object, const char *fieldStr)
509 {
510 int32_t fieldRef = 0;
511 if (object == nullptr) {
512 CLOGE("args is nullptr");
513 return fieldRef;
514 }
515 bool hasProperty = false;
516 napi_status status = napi_has_named_property(env, object, fieldStr, &hasProperty);
517 if (status == napi_ok && hasProperty) {
518 napi_value field;
519 napi_valuetype valueType;
520 status = napi_get_named_property(env, object, fieldStr, &field);
521 if (status != napi_ok) {
522 CLOGE("napi_get_named_property failed.");
523 return fieldRef;
524 }
525 status = napi_typeof(env, field, &valueType);
526 if (status != napi_ok || valueType != napi_number) {
527 CLOGE("Wrong argument type. Number expected.");
528 return fieldRef;
529 }
530 status = napi_get_value_int32(env, field, &fieldRef);
531 if (status != napi_ok) {
532 CLOGE("napi_get_value_int32 failed");
533 return fieldRef;
534 }
535 return fieldRef;
536 } else {
537 CLOGE("Js to int32_t no property: %{public}s", fieldStr);
538 }
539 return fieldRef;
540 }
541
JsObjectToBool(napi_env env,napi_value & object,const char * fieldStr)542 bool JsObjectToBool(napi_env env, napi_value &object, const char *fieldStr)
543 {
544 bool fieldRef = false;
545 if (object == nullptr) {
546 CLOGE("args is nullptr");
547 return fieldRef;
548 }
549 bool hasProperty = false;
550 napi_status status = napi_has_named_property(env, object, fieldStr, &hasProperty);
551 if (status == napi_ok && hasProperty) {
552 napi_value field;
553 napi_valuetype valueType;
554 status = napi_get_named_property(env, object, fieldStr, &field);
555 if (status != napi_ok) {
556 CLOGE("napi_get_named_property failed.");
557 return fieldRef;
558 }
559 status = napi_typeof(env, field, &valueType);
560 if (status != napi_ok || valueType != napi_boolean) {
561 CLOGE("Wrong argument type. Bool expected.");
562 return fieldRef;
563 }
564 status = napi_get_value_bool(env, field, &fieldRef);
565 if (status != napi_ok) {
566 CLOGE("napi_get_value_bool failed");
567 return fieldRef;
568 }
569 return fieldRef;
570 } else {
571 CLOGE("Js to bool no property: %{public}s", fieldStr);
572 }
573 return fieldRef;
574 }
575
JsObjectToUint32(napi_env env,napi_value & object,const char * fieldStr)576 uint32_t JsObjectToUint32(napi_env env, napi_value &object, const char *fieldStr)
577 {
578 uint32_t fieldRef = 0;
579 if (object == nullptr) {
580 CLOGE("args is nullptr");
581 return fieldRef;
582 }
583 bool hasProperty = false;
584 napi_status status = napi_has_named_property(env, object, fieldStr, &hasProperty);
585 if (status == napi_ok && hasProperty) {
586 napi_value field;
587 napi_valuetype valueType;
588 status = napi_get_named_property(env, object, fieldStr, &field);
589 if (status != napi_ok) {
590 CLOGE("napi_get_named_property failed.");
591 return fieldRef;
592 }
593 status = napi_typeof(env, field, &valueType);
594 if (status != napi_ok || valueType != napi_number) {
595 CLOGE("Wrong argument type. Number expected.");
596 return fieldRef;
597 }
598 status = napi_get_value_uint32(env, field, &fieldRef);
599 if (status != napi_ok) {
600 CLOGE("napi_get_value_uint32 failed");
601 return fieldRef;
602 }
603 return fieldRef;
604 } else {
605 CLOGE("Js to uint32_t no property: %{public}s", fieldStr);
606 }
607 return fieldRef;
608 }
609
JsObjectToDouble(napi_env env,napi_value & object,const char * fieldStr)610 double JsObjectToDouble(napi_env env, napi_value &object, const char *fieldStr)
611 {
612 double fieldRef = 0;
613 if (object == nullptr) {
614 CLOGE("args is nullptr");
615 return fieldRef;
616 }
617 bool hasProperty = false;
618 napi_status status = napi_has_named_property(env, object, fieldStr, &hasProperty);
619 if (status == napi_ok && hasProperty) {
620 napi_value field;
621 napi_valuetype valueType;
622 status = napi_get_named_property(env, object, fieldStr, &field);
623 if (status != napi_ok) {
624 CLOGE("napi_get_named_property failed.");
625 return fieldRef;
626 }
627 status = napi_typeof(env, field, &valueType);
628 if (status != napi_ok || valueType != napi_number) {
629 CLOGE("Wrong argument type. Number expected.");
630 return fieldRef;
631 }
632 status = napi_get_value_double(env, field, &fieldRef);
633 if (status != napi_ok) {
634 CLOGE("napi_get_value_double failed");
635 return fieldRef;
636 }
637 return fieldRef;
638 } else {
639 CLOGE("Js to double no property: %{public}s", fieldStr);
640 }
641 return fieldRef;
642 }
643
JsObjectToInt64(napi_env env,napi_value & object,const char * fieldStr)644 int64_t JsObjectToInt64(napi_env env, napi_value &object, const char *fieldStr)
645 {
646 int64_t fieldRef = 0;
647 if (object == nullptr) {
648 CLOGE("args is nullptr");
649 return fieldRef;
650 }
651 bool hasProperty = false;
652 napi_status status = napi_has_named_property(env, object, fieldStr, &hasProperty);
653 if (status == napi_ok && hasProperty) {
654 napi_value field;
655 napi_valuetype valueType;
656 status = napi_get_named_property(env, object, fieldStr, &field);
657 if (status != napi_ok) {
658 CLOGE("napi_get_named_property failed");
659 return fieldRef;
660 }
661 status = napi_typeof(env, field, &valueType);
662 if (status != napi_ok || valueType != napi_number) {
663 CLOGE("Wrong argument type. Number expected.");
664 return fieldRef;
665 }
666 status = napi_get_value_int64(env, field, &fieldRef);
667 if (status != napi_ok) {
668 CLOGE("napi_get_value_int64 failed");
669 return fieldRef;
670 }
671 return fieldRef;
672 } else {
673 CLOGE("Js to int64_t no property: %{public}s", fieldStr);
674 }
675 return fieldRef;
676 }
677
ConvertMediaInfoToJS(napi_env env,const MediaInfo & mediaInfo)678 napi_value ConvertMediaInfoToJS(napi_env env, const MediaInfo &mediaInfo)
679 {
680 CLOGD("ConvertMediaInfoToJS start");
681 napi_value result = nullptr;
682 napi_value value = nullptr;
683 NAPI_CALL(env, napi_create_object(env, &result));
684 NAPI_CALL(env, napi_create_string_utf8(env, mediaInfo.mediaId.c_str(), NAPI_AUTO_LENGTH, &value));
685 NAPI_CALL(env, napi_set_named_property(env, result, "mediaId", value));
686 NAPI_CALL(env, napi_create_string_utf8(env, mediaInfo.mediaName.c_str(), NAPI_AUTO_LENGTH, &value));
687 NAPI_CALL(env, napi_set_named_property(env, result, "mediaName", value));
688 NAPI_CALL(env, napi_create_string_utf8(env, mediaInfo.mediaUrl.c_str(), NAPI_AUTO_LENGTH, &value));
689 NAPI_CALL(env, napi_set_named_property(env, result, "mediaUrl", value));
690 NAPI_CALL(env, napi_create_string_utf8(env, mediaInfo.mediaType.c_str(), NAPI_AUTO_LENGTH, &value));
691 NAPI_CALL(env, napi_set_named_property(env, result, "mediaType", value));
692 NAPI_CALL(env, napi_create_string_utf8(env, mediaInfo.albumCoverUrl.c_str(), NAPI_AUTO_LENGTH, &value));
693 NAPI_CALL(env, napi_set_named_property(env, result, "albumCoverUrl", value));
694 NAPI_CALL(env, napi_create_string_utf8(env, mediaInfo.albumTitle.c_str(), NAPI_AUTO_LENGTH, &value));
695 NAPI_CALL(env, napi_set_named_property(env, result, "albumTitle", value));
696 NAPI_CALL(env, napi_create_string_utf8(env, mediaInfo.mediaArtist.c_str(), NAPI_AUTO_LENGTH, &value));
697 NAPI_CALL(env, napi_set_named_property(env, result, "mediaArtist", value));
698 NAPI_CALL(env, napi_create_string_utf8(env, mediaInfo.lrcUrl.c_str(), NAPI_AUTO_LENGTH, &value));
699 NAPI_CALL(env, napi_set_named_property(env, result, "lrcUrl", value));
700 NAPI_CALL(env, napi_create_string_utf8(env, mediaInfo.lrcContent.c_str(), NAPI_AUTO_LENGTH, &value));
701 NAPI_CALL(env, napi_set_named_property(env, result, "lrcContent", value));
702 NAPI_CALL(env, napi_create_string_utf8(env, mediaInfo.appIconUrl.c_str(), NAPI_AUTO_LENGTH, &value));
703 NAPI_CALL(env, napi_set_named_property(env, result, "appIconUrl", value));
704 NAPI_CALL(env, napi_create_string_utf8(env, mediaInfo.appName.c_str(), NAPI_AUTO_LENGTH, &value));
705 NAPI_CALL(env, napi_set_named_property(env, result, "appName", value));
706
707 NAPI_CALL(env, napi_create_uint32(env, mediaInfo.mediaSize, &value));
708 NAPI_CALL(env, napi_set_named_property(env, result, "mediaSize", value));
709 NAPI_CALL(env, napi_create_uint32(env, mediaInfo.startPosition, &value));
710 NAPI_CALL(env, napi_set_named_property(env, result, "startPosition", value));
711 NAPI_CALL(env, napi_create_uint32(env, mediaInfo.duration, &value));
712 NAPI_CALL(env, napi_set_named_property(env, result, "duration", value));
713 NAPI_CALL(env, napi_create_uint32(env, mediaInfo.closingCreditsPosition, &value));
714 NAPI_CALL(env, napi_set_named_property(env, result, "closingCreditsPosition", value));
715 CLOGD("ConvertMediaInfoToJS end");
716
717 return result;
718 }
719
ConvertMediaInfoHolderToJS(napi_env env,const MediaInfoHolder & mediaInfoHolder)720 napi_value ConvertMediaInfoHolderToJS(napi_env env, const MediaInfoHolder &mediaInfoHolder)
721 {
722 CLOGD("ConvertMediaInfoHolderToJS start");
723 napi_value result = nullptr;
724 napi_value value = nullptr;
725 size_t len = mediaInfoHolder.mediaInfoList.size();
726 if (len == 0) {
727 CLOGE("mediaInfoList len is invalid");
728 return result;
729 }
730 NAPI_CALL(env, napi_create_object(env, &result));
731 NAPI_CALL(env, napi_create_uint32(env, mediaInfoHolder.currentIndex, &value));
732 NAPI_CALL(env, napi_set_named_property(env, result, "currentIndex", value));
733 NAPI_CALL(env, napi_create_uint32(env, mediaInfoHolder.progressRefreshInterval, &value));
734 NAPI_CALL(env, napi_set_named_property(env, result, "progressRefreshInterval", value));
735 NAPI_CALL(env, napi_create_array_with_length(env, len, &value));
736 for (size_t i = 0; i < len; i++) {
737 napi_value mediaInfo = ConvertMediaInfoToJS(env, mediaInfoHolder.mediaInfoList[i]);
738 NAPI_CALL(env, napi_set_element(env, value, i, mediaInfo));
739 }
740 NAPI_CALL(env, napi_set_named_property(env, result, "mediaInfoList", value));
741 CLOGD("ConvertMediaInfoHolderToJS end");
742 return result;
743 }
744
CallJSFunc(napi_env env,napi_ref func,size_t argc,napi_value argv[])745 void CallJSFunc(napi_env env, napi_ref func, size_t argc, napi_value argv[])
746 {
747 napi_value callback = nullptr;
748 napi_value undefined = nullptr;
749 napi_value callResult = nullptr;
750 if (napi_get_undefined(env, &undefined) != napi_ok) {
751 CLOGE("napi_get_undefined failed");
752 return;
753 }
754 if (napi_get_reference_value(env, func, &callback) != napi_ok) {
755 CLOGE("napi_get_reference_value failed");
756 return;
757 }
758 if (napi_call_function(env, undefined, callback, argc, argv, &callResult) != napi_ok) {
759 CLOGE("napi_call_function failed");
760 }
761 }
762
GetJSFuncParams(napi_env env,napi_callback_info info,napi_value argv[],size_t expectedArgc,napi_valuetype expectedTypes[])763 bool GetJSFuncParams(napi_env env, napi_callback_info info, napi_value argv[], size_t expectedArgc,
764 napi_valuetype expectedTypes[])
765 {
766 napi_value thisVar = nullptr;
767 size_t argc = expectedArgc;
768 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
769 if (status != napi_ok || argc != expectedArgc) {
770 CLOGE("napi_get_cb_info failed");
771 return false;
772 }
773
774 for (size_t i = 0; i < expectedArgc; i++) {
775 napi_valuetype valueType = napi_undefined;
776 if (napi_typeof(env, argv[i], &valueType) != napi_ok) {
777 CLOGE("napi_typeof failed");
778 return false;
779 }
780 if (valueType != expectedTypes[i]) {
781 CLOGE("Wrong argument type. type:%d expected", expectedTypes[i]);
782 return false;
783 }
784 }
785
786 return true;
787 }
788
CheckJSParamsType(napi_env env,napi_value argv[],size_t expectedArgc,napi_valuetype expectedTypes[])789 bool CheckJSParamsType(napi_env env, napi_value argv[], size_t expectedArgc, napi_valuetype expectedTypes[])
790 {
791 for (size_t i = 0; i < expectedArgc; i++) {
792 napi_valuetype valueType = napi_undefined;
793 if (napi_typeof(env, argv[i], &valueType) != napi_ok) {
794 CLOGE("napi_typeof failed");
795 return false;
796 }
797 if (valueType != expectedTypes[i]) {
798 CLOGE("Wrong argument type. type:%d expected", expectedTypes[i]);
799 return false;
800 }
801 }
802
803 return true;
804 }
805 } // namespace CastEngineClient
806 } // namespace CastEngine
807 } // namespace OHOS
808