HarmonyOS ArkTS与c++交互通信
一、创建Native C++ Module
1、右键项目->new->module
如图:
2、修改build-profile.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、回调接口实现
通过缓存的env,callback对象,调用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))
}