This commit is contained in:
wangjiyu 2025-04-10 20:51:28 +08:00
parent 0ea089203c
commit ba09525535
3 changed files with 31 additions and 12 deletions

View File

@ -174,18 +174,23 @@ void init_numpy() {
BOOST_PYTHON_MODULE(rtc_plugins) {
try {
init_numpy();
void** numpy_api = (void**)PyArray_API;
if (!numpy_api || !numpy_api[93]) { // 93是PyArray_SimpleNew的偏移量
void** numpyApi = (void**)PyArray_API;
if (!numpyApi || !numpyApi[93]) { // 93是PyArray_SimpleNew的偏移量
std::cout << "NumPy API corrupt! Key functions missing." << std::endl;
PyErr_Print();
throw std::runtime_error("Invalid NumPy API state");
} else {
RTCContext::instance().setNumpyApi(numpyApi);
std::cout << "set numpyApi succ" << std::endl;
}
/*
if (!PyArray_API) {
std::cout << "PyArray_API is null" << std::endl;
} else {
std::cout << "PyArray_API is not null" << std::endl;
}
*/
py::def("init", &init);
py::def("initRecv", &initRecv);

View File

@ -72,7 +72,13 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
<< " (max: " << std::numeric_limits<npy_intp>::max() << ")" << std::endl;
auto numpyApi = RTCContext::numpy_api();
void** numpyApi = numpyApi_;
if (!numpyApi || !numpyApi[93]) { // 93是PyArray_SimpleNew的偏移量
std::cout << "NumPy API corrupt! Key functions missing." << std::endl;
} else {
std::cout << "PyArray_API is not null in init" << std::endl;
}
//auto numpyApi = RTCContext::numpy_api();
std::cout << "step1" << std::endl;
if (!numpyApi) {
PyGILState_Release(gstate);
@ -81,7 +87,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
std::cout << "step2" << std::endl;
using PyArray_SimpleNew_t = PyObject*(*)(int, npy_intp*, int);
std::cout << "step3" << std::endl;
auto PyArray_SimpleNew = reinterpret_cast<PyArray_SimpleNew_t>(numpy_api[93]);
auto PyArray_SimpleNew = reinterpret_cast<PyArray_SimpleNew_t>(numpyApi[93]);
std::cout << "step4" << std::endl;
// 3. 严格校验输入数据
@ -123,9 +129,9 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
std::cout << "step11" << std::endl;
// 7. 执行回调(带引用计数保护)
if (!py_callback_.is_none()) {
if (!pyCallback_.is_none()) {
try {
py_callback_(
pyCallback_(
py::handle<>(pyArray), // 自动管理引用
audioFrame.dataCount,
audioFrame.sampleRate,
@ -158,8 +164,9 @@ void RTCContext::onProducer(uint32_t msgId, mrtc::MRTCProducerInfo& info)
}
bool RTCContext::init(const char* selfUserId, const char* selfDisplayName, const char* selfRoomId)
{
if (!PyArray_API) {
std::cout << "PyArray_API is null in init" << std::endl;
void** numpyApi = numpyApi_;
if (!numpyApi || !numpyApi[93]) { // 93是PyArray_SimpleNew的偏移量
std::cout << "NumPy API corrupt! Key functions missing." << std::endl;
} else {
std::cout << "PyArray_API is not null in init" << std::endl;
}
@ -213,8 +220,9 @@ bool RTCContext::init(const char* selfUserId, const char* selfDisplayName, const
}
bool RTCContext::initRecv(const char* destRoomId, const char* srcUserId, const int16_t destChannelIndex)
{
if (!PyArray_API) {
std::cout << "PyArray_API is null in init" << std::endl;
void** numpyApi = numpyApi_;
if (!numpyApi || !numpyApi[93]) { // 93是PyArray_SimpleNew的偏移量
std::cout << "NumPy API corrupt! Key functions missing." << std::endl;
} else {
std::cout << "PyArray_API is not null in init" << std::endl;
}
@ -316,5 +324,9 @@ void RTCContext::setpData(void* pData)
}
void RTCContext::setPyCallback(boost::python::object callback) {
std::lock_guard<std::mutex> lock(mutex_);
py_callback_ = callback;
pyCallback_ = callback;
}
void RTCContext::setNumpyApi(void **numpyApi) {
std::lock_guard<std::mutex> lock(mutex_);
numpyApi_ = numpyApi;
}

View File

@ -85,6 +85,7 @@ public:
void* getpData() const;
void setpData(void* pData);
void setPyCallback(boost::python::object callback);
void setNumpyApi(void** numpyApi);
int16_t sendAudioData(uint8_t channelIndex = 0, const void* pData = nullptr, int32_t nSampleRate = 48000, uint64_t nNumberOfChannels = 2, uint64_t dataLength = 0);
int16_t sendCustomAudioData(const int16_t channelIndex, void* customData, int32_t sampleRate,
@ -101,7 +102,8 @@ private:
bool isOnRoom_ = false;
bool isOnConsumer_ = false;
bool isJoinMultiRoom_ = false;
boost::python::object py_callback_;
boost::python::object pyCallback_;
void ** numpyApi_;
void onRoom(uint32_t typeId, RTCENGINE_NAMESPACE::MRTCRoomInfo& roomInfo);
void onConsumer(uint32_t msgId, const char* roomId, const char* peerId, RTCENGINE_NAMESPACE::MRTCConsumerInfo& consumerInfo);
void onRender(const char* roomId, const char* peerId,