一、创建Native C++ Module

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

如图

2、修改buildprofile.json5配置

"externalNativeOptions": {
      "path": "./src/main/cpp/CMakeLists.txt",
      "arguments": "-v -DOHOS_STL=c++_shared",
      "abiFilters": [
//        "armeabi-v7a",
//        "x86_64",
        "arm64-v8a"
      ],
      "cppFlags": ""
    }

3、CMakeLists.txt 配置如下

# the minimum version of CMake.
cmake_minimum_required(VERSION 3.4.1)
project(MyApplication43)

set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})

include_directories(${NATIVERENDER_ROOT_PATH}
                    ${NATIVERENDER_ROOT_PATH}/include)

add_library(application SHARED SRRtcVideoEngineNapi.cpp SRRtcRoomCallBackNapi.cpp)
target_link_libraries(application PUBLIC libace_napi.z.so)

二、代码实现

1、主调接口实现

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

napi_value SRRtcVideoEngineNapi::setRRoomCallBack(napi_env env, napi_callback_info info) {
    LogE("setRRoomCallBack---一个参数:ISRoomCallBack");
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok) {
        napi_throw_error(env, "", "");
        return nullptr;
    }
    // 缓存回调函数全局变量,回调ets用
    if (SRGlobalvar::napi_CallbackReference == nullptr) {
        LogE("setRRoomCallBack===new NapiCallBack()");
        SRGlobalvar::napi_CallbackReference = new NapiCallBack(); // 创建缓存函数
    }
    napi_create_reference(env, args[0], 1, &SRGlobalvar::napi_CallbackReference->roomCallBack_napi);
    SRGlobalvar::napi_CallbackReference->env = env;
    // 调用 底层sdk : RRoomControlMgr.setCallBack
    RResult rResult = sr_engineSdk->setRRoomCallBackRtcEngine();
    return SRGlobalvar::returnResult(env, rResult);
}

2、回调接口实现

通过缓存的envcallback对象调用napi_call_function法将数据传回给ts

void SRRtcRoomCallBackNapi::onRoomJoinConfirm(RResult rResult, const RRoomInfo &roomInfo) {
    // 处理 onRoomJoinConfirm 通知
    LogE("回调消息---SRRtcRoomCallBack:onRoomJoinConfirm ");
    // 转换N-API对象
    napi_value roomInfo_napi = SRGlobalvar::convertToSRRoomInfo(SRGlobalvar::napi_CallbackReference->env, roomInfo);
    napi_value rResult_napi = SRGlobalvar::convertToSRResult(SRGlobalvar::napi_CallbackReference->env, rResult);
    // 传递给TS
    napi_value callback;
    napi_get_reference_value(SRGlobalvar::napi_CallbackReference->env,
                             SRGlobalvar::napi_CallbackReference->roomCallBack_napi, &callback);
    napi_value jsMethod;
    napi_get_named_property(SRGlobalvar::napi_CallbackReference->env, callback, "onRoomJoinConfirm", &jsMethod);
    napi_value argv[] = {rResult_napi, roomInfo_napi};
    napi_value callbackResult = nullptr;
    napi_call_function(SRGlobalvar::napi_CallbackReference->env, nullptr, jsMethod, 2, argv, &callbackResult);
}

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

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

function setRRoomCallBack(sroomCallback: ISRoomCallBack): SRReult

2、回调接口SRoomCallBack.ets

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

  }
}

3、调用

import srrtcNapi from 'librtcvideo.so';

  setRRoomCallBackRtcEngine(callback: ISRoomCallBack) {
    let srResult = srrtcNapi.setRRoomCallBack(callback)
    SRLog.i(TAG, "setRRoomCallBackRtcEngine===srresult:" + JsonUtil.jsonToString(srResult))
  }

原文地址:https://blog.csdn.net/licui1000/article/details/134729566

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_22114.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注