This commit is contained in:
wangjiyu 2025-04-10 17:47:00 +08:00
parent b39a0657bc
commit c98b283bdf
5 changed files with 42 additions and 11 deletions

View File

@ -17,7 +17,8 @@ add_executable(rtc_plugins
util/RTCContext.cpp
rtc_plugins.cpp
util/RTCContext.cpp
util/numpyConfig.h)
util/numpyConfig.h
util/numpyStub.h)
add_library(rtc_plugins_lib SHARED rtc_plugins.cpp
util/RTCContext.cpp)

View File

@ -1,8 +1,18 @@
// rtc_plugins.cpp
#include "./util/numpyConfig.h"
//#include "./util/numpyConfig.h"
#include "./util/RTCContext.h"
#define PY_ARRAY_UNIQUE_SYMBOL RTC_PLUGINS_ARRAY_API_IMPL // 内部实现专用符号
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
// 提供转换接口
void** get_numpy_api() {
return (void**)RTC_PLUGINS_ARRAY_API_IMPL;
}
namespace py = boost::python;

View File

@ -72,10 +72,13 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
<< " (max: " << std::numeric_limits<npy_intp>::max() << ")" << std::endl;
if (!PyArray_API) {
auto numpyApi = RTCContext::numpy_api();
if (!numpyApi) {
PyGILState_Release(gstate);
throw std::runtime_error("NumPy C-API not initialized. Call import_array() in module init");
}
using PyArray_SimpleNew_t = PyObject*(*)(int, npy_intp*, int);
auto PyArray_SimpleNew = reinterpret_cast<PyArray_SimpleNew_t>(numpy_api[93]);
// 3. 严格校验输入数据
if (!audioFrame.data || audioFrame.dataCount <= 0) {
@ -92,13 +95,11 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
// 5. 创建NumPy数组带内存保护
PyObject* pyArray = nullptr;
{
pyArray = PyArray_SimpleNew(1, dims, NPY_INT16);
if (!pyArray) {
PyGILState_Release(gstate);
throw std::bad_alloc();
}
}
// 6. 安全拷贝数据(带对齐检查)
if (reinterpret_cast<uintptr_t>(audioFrame.data) % alignof(int16_t) != 0) {

View File

@ -2,8 +2,7 @@
#pragma once
//#include "numpyConfig.h"
extern void* RTC_PLUGINS_ARRAY_API[];
#include "numpyStub.h"
#include "IMRTCEngine.hpp"
#include "MRTCEngineDefine.hpp"
@ -57,6 +56,13 @@ public:
static RTCContext instance;
return instance;
}
static void** numpy_api() {
static void** api = [](){
// 延迟获取实际指针
return reinterpret_cast<void**>(RTC_PLUGINS_ARRAY_API);
}();
return api;
}
RTCContext(const RTCContext&) = delete;
RTCContext& operator=(const RTCContext&) = delete;
mrtc::IMRTCEngine* getRtcEngine() const;

13
util/numpyStub.h Normal file
View File

@ -0,0 +1,13 @@
// numpy_stub.h
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
// 声明为不完整类型(阻止链接器匹配)
extern struct NumPyAPIWrapper* RTC_PLUGINS_ARRAY_API;
#ifdef __cplusplus
}
#endif