diff --git a/CMakeLists.txt b/CMakeLists.txt index f3552a5..3126a48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/rtc_plugins.cpp b/rtc_plugins.cpp index e0b5edf..1a0cf09 100644 --- a/rtc_plugins.cpp +++ b/rtc_plugins.cpp @@ -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 + +// 提供转换接口 +void** get_numpy_api() { + return (void**)RTC_PLUGINS_ARRAY_API_IMPL; +} + namespace py = boost::python; diff --git a/util/RTCContext.cpp b/util/RTCContext.cpp index a58e289..b4b777c 100644 --- a/util/RTCContext.cpp +++ b/util/RTCContext.cpp @@ -72,10 +72,13 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId, << " (max: " << std::numeric_limits::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(numpy_api[93]); // 3. 严格校验输入数据 if (!audioFrame.data || audioFrame.dataCount <= 0) { @@ -92,12 +95,10 @@ 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(); - } + pyArray = PyArray_SimpleNew(1, dims, NPY_INT16); + if (!pyArray) { + PyGILState_Release(gstate); + throw std::bad_alloc(); } // 6. 安全拷贝数据(带对齐检查) diff --git a/util/RTCContext.h b/util/RTCContext.h index d55ac11..d93697c 100644 --- a/util/RTCContext.h +++ b/util/RTCContext.h @@ -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(RTC_PLUGINS_ARRAY_API); + }(); + return api; + } RTCContext(const RTCContext&) = delete; RTCContext& operator=(const RTCContext&) = delete; mrtc::IMRTCEngine* getRtcEngine() const; diff --git a/util/numpyStub.h b/util/numpyStub.h new file mode 100644 index 0000000..1f46e76 --- /dev/null +++ b/util/numpyStub.h @@ -0,0 +1,13 @@ +// numpy_stub.h +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +// 声明为不完整类型(阻止链接器匹配) +extern struct NumPyAPIWrapper* RTC_PLUGINS_ARRAY_API; + +#ifdef __cplusplus +} +#endif \ No newline at end of file