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 utils for napi interface. 15 * Author: zhangjingnan 16 * Create: 2022-7-11 17 */ 18 19 #ifndef NAPI_CASTENGINE_UTILS_H 20 #define NAPI_CASTENGINE_UTILS_H 21 22 #include <list> 23 #include <uv.h> 24 #include <variant> 25 #include "napi/native_api.h" 26 #include "napi/native_node_api.h" 27 #include "cast_engine_log.h" 28 #include "cast_engine_common.h" 29 #include "i_cast_session.h" 30 #include "oh_remote_control_event.h" 31 32 #define CHECK_ARGS_RETURN_VOID(context, condition, message, code) \ 33 do { \ 34 if (!(condition)) { \ 35 (context)->status = napi_invalid_arg; \ 36 (context)->errMessage = std::string(message); \ 37 (context)->errCode = code; \ 38 CLOGE("(" #condition ") failed: " message); \ 39 return; \ 40 } \ 41 } while (0) 42 43 #define CHECK_STATUS_RETURN_VOID(context, message, code) \ 44 do { \ 45 if ((context)->status != napi_ok) { \ 46 (context)->errMessage = std::string(message); \ 47 (context)->errCode = code; \ 48 CLOGE("(context->status == napi_ok) failed: " message); \ 49 return; \ 50 } \ 51 } while (0) 52 53 #define CHECK_RETURN_VOID(condition, message) \ 54 do { \ 55 if (!(condition)) { \ 56 CLOGE("(" #condition ") failed: " message); \ 57 return; \ 58 } \ 59 } while (0) 60 61 namespace OHOS { 62 namespace CastEngine { 63 namespace CastEngineClient { 64 constexpr int32_t CALLBACK_ARGC_ZERO = 0; 65 constexpr int32_t CALLBACK_ARGC_ONE = 1; 66 constexpr int32_t CALLBACK_ARGC_TWO = 2; 67 constexpr int32_t CALLBACK_ARGC_THERE = 3; 68 constexpr int32_t CALLBACK_ARGC_FOUR = 4; 69 70 std::string ParseString(napi_env env, napi_value args); 71 int32_t ParseInt32(napi_env env, napi_value args); 72 uint32_t ParseUint32(napi_env env, napi_value args); 73 bool ParseBool(napi_env env, napi_value args); 74 std::string JsObjectToString(napi_env env, napi_value &object, const char *fieldStr); 75 int32_t JsObjectToInt32(napi_env env, napi_value &object, const char *fieldStr); 76 bool JsObjectToBool(napi_env env, napi_value &object, const char *fieldStr); 77 uint32_t JsObjectToUint32(napi_env env, napi_value &object, const char *fieldStr); 78 double JsObjectToDouble(napi_env env, napi_value &object, const char *fieldStr); 79 int64_t JsObjectToInt64(napi_env env, napi_value &object, const char *fieldStr); 80 81 napi_value ConvertDeviceListToJS(napi_env env, const std::vector<CastRemoteDevice> &devices); 82 napi_value ConvertDeviceStateInfoToJS(napi_env env, const DeviceStateInfo &stateEvent); 83 napi_value ConvertMediaInfoToJS(napi_env env, const MediaInfo &mediaInfo); 84 napi_value ConvertMediaInfoHolderToJS(napi_env env, const MediaInfoHolder &mediaInfoHolder); 85 napi_value ConvertCastRemoteDeviceToJS(napi_env env, const CastRemoteDevice &castRemoteDevice); 86 87 CastRemoteDevice GetCastRemoteDeviceFromJS(napi_env env, napi_value &object); 88 CastSessionProperty GetCastSessionPropertyFromJS(napi_env env, napi_value &object); 89 AudioProperty GetAudioPropertyFromJS(napi_env env, napi_value &object); 90 VideoProperty GetVideoPropertyFromJS(napi_env env, napi_value &object); 91 WindowProperty GetWindowPropertyFromJS(napi_env env, napi_value &object); 92 bool GetMediaInfoFromJS(napi_env env, napi_value &object, MediaInfo &mediaInfo); 93 bool GetProtocolTypesFromJS(napi_env env, napi_value &object, int &protocolTypes); 94 bool GetMediaInfoHolderFromJS(napi_env env, napi_value &object, MediaInfoHolder &mediaInfoHolder); 95 96 bool GetJSFuncParams(napi_env env, napi_callback_info info, napi_value argv[], size_t expectedArgc, 97 napi_valuetype expectedTypes[]); 98 bool CheckJSParamsType(napi_env env, napi_value argv[], size_t expectedArgc, napi_valuetype expectedTypes[]); 99 void CallJSFunc(napi_env env, napi_ref func, size_t argc, napi_value argv[]); 100 napi_value GetUndefinedValue(napi_env env); 101 bool Equals(napi_env env, napi_value value, napi_ref copy); 102 napi_status GetRefByCallback(napi_env env, std::list<napi_ref> &callbackList, napi_value callback, 103 napi_ref &callbackRef); 104 } // namespace CastEngineClient 105 } // namespace CastEngine 106 } // namespace OHOS 107 #endif 108