当前位置:   article > 正文

HarmonyOS ArkTS与c++交互通信_napi arkts 回调

napi arkts 回调

一、创建Native C++ Module

1、右键项目->new->module

如图:

2、修改build-profile.json5配置

  1. "externalNativeOptions": {
  2. "path": "./src/main/cpp/CMakeLists.txt",
  3. "arguments": "-v -DOHOS_STL=c++_shared",
  4. "abiFilters": [
  5. // "armeabi-v7a",
  6. // "x86_64",
  7. "arm64-v8a"
  8. ],
  9. "cppFlags": ""
  10. }

3、CMakeLists.txt 配置如下:

  1. # the minimum version of CMake.
  2. cmake_minimum_required(VERSION 3.4.1)
  3. project(MyApplication43)
  4. set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
  5. include_directories(${NATIVERENDER_ROOT_PATH}
  6. ${NATIVERENDER_ROOT_PATH}/include)
  7. add_library(application SHARED SRRtcVideoEngineNapi.cpp SRRtcRoomCallBackNapi.cpp)
  8. target_link_libraries(application PUBLIC libace_napi.z.so)

二、代码实现

1、主调接口实现

NAPI中缓存回调接口的变量,便于后面回调给TS

  1. napi_value SRRtcVideoEngineNapi::setRRoomCallBack(napi_env env, napi_callback_info info) {
  2. LogE("setRRoomCallBack---一个参数:ISRoomCallBack");
  3. size_t argc = 1;
  4. napi_value args[1] = {nullptr};
  5. napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  6. if (status != napi_ok) {
  7. napi_throw_error(env, "", "");
  8. return nullptr;
  9. }
  10. // 缓存回调函数全局变量,回调ets用
  11. if (SRGlobalvar::napi_CallbackReference == nullptr) {
  12. LogE("setRRoomCallBack===new NapiCallBack()");
  13. SRGlobalvar::napi_CallbackReference = new NapiCallBack(); // 创建缓存函数
  14. }
  15. napi_create_reference(env, args[0], 1, &SRGlobalvar::napi_CallbackReference->roomCallBack_napi);
  16. SRGlobalvar::napi_CallbackReference->env = env;
  17. // 调用 底层sdk : RRoomControlMgr.setCallBack
  18. RResult rResult = sr_engineSdk->setRRoomCallBackRtcEngine();
  19. return SRGlobalvar::returnResult(env, rResult);
  20. }

2、回调接口实现

通过缓存的env,callback对象,调用napi_call_function方法将数据传回给ts

  1. void SRRtcRoomCallBackNapi::onRoomJoinConfirm(RResult rResult, const RRoomInfo &roomInfo) {
  2. // 处理 onRoomJoinConfirm 通知
  3. LogE("回调消息---SRRtcRoomCallBack:onRoomJoinConfirm ");
  4. // 转换N-API对象
  5. napi_value roomInfo_napi = SRGlobalvar::convertToSRRoomInfo(SRGlobalvar::napi_CallbackReference->env, roomInfo);
  6. napi_value rResult_napi = SRGlobalvar::convertToSRResult(SRGlobalvar::napi_CallbackReference->env, rResult);
  7. // 传递给TS
  8. napi_value callback;
  9. napi_get_reference_value(SRGlobalvar::napi_CallbackReference->env,
  10. SRGlobalvar::napi_CallbackReference->roomCallBack_napi, &callback);
  11. napi_value jsMethod;
  12. napi_get_named_property(SRGlobalvar::napi_CallbackReference->env, callback, "onRoomJoinConfirm", &jsMethod);
  13. napi_value argv[] = {rResult_napi, roomInfo_napi};
  14. napi_value callbackResult = nullptr;
  15. napi_call_function(SRGlobalvar::napi_CallbackReference->env, nullptr, jsMethod, 2, argv, &callbackResult);
  16. }

三、ets的接收c++传回的数据

1、index.d.ts 代码增加接口

function setRRoomCallBack(sroomCallback: ISRoomCallBack): SRReult

2、回调接口SRoomCallBack.ets

  1. export class SRoomCallBack implements ISRoomCallBack {
  2. onRoomJoinConfirm(rResult: SRReult, roomInfo: SRRoomInfo) {
  3. SRLog.i(TAG, `onRoomJoinConfirm==回调测试完成=rResult:${JsonUtil.jsonToString(rResult)}\n roomInfo:${JsonUtil.jsonToString(roomInfo)}`)
  4. }
  5. }

3、调用

  1. import srrtcNapi from 'librtcvideo.so';
  2. setRRoomCallBackRtcEngine(callback: ISRoomCallBack) {
  3. let srResult = srrtcNapi.setRRoomCallBack(callback)
  4. SRLog.i(TAG, "setRRoomCallBackRtcEngine===srresult:" + JsonUtil.jsonToString(srResult))
  5. }

                
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号