debug
This commit is contained in:
parent
4947927a74
commit
c37c0656a7
|
@ -64,6 +64,10 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
|
||||||
namespace py = boost::python;
|
namespace py = boost::python;
|
||||||
|
|
||||||
// 1. 获取 GIL(绝对必要)
|
// 1. 获取 GIL(绝对必要)
|
||||||
|
if (!Py_IsInitialized()) {
|
||||||
|
std::cerr << "Python interpreter not initialized" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
PyGILState_STATE gstate = PyGILState_Ensure();
|
PyGILState_STATE gstate = PyGILState_Ensure();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -71,41 +75,50 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
|
||||||
std::cout << "dataCount:" << audioFrame.dataCount << std::endl;
|
std::cout << "dataCount:" << audioFrame.dataCount << std::endl;
|
||||||
std::cout << "dataCount value: " << audioFrame.dataCount
|
std::cout << "dataCount value: " << audioFrame.dataCount
|
||||||
<< " (max: " << std::numeric_limits<npy_intp>::max() << ")" << std::endl;
|
<< " (max: " << std::numeric_limits<npy_intp>::max() << ")" << std::endl;
|
||||||
// 在调用 PyArray_SimpleNew 前插入
|
|
||||||
|
|
||||||
if (!PyArray_API) {
|
if (!PyArray_API) {
|
||||||
std::cerr << "FATAL: NumPy API not initialized! Addr: " << PyArray_API << std::endl;
|
PyGILState_Release(gstate);
|
||||||
abort(); // 立即终止以暴露问题
|
throw std::runtime_error("NumPy C-API not initialized. Call import_array() in module init");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "step0" << std::endl;
|
// 3. 严格校验输入数据
|
||||||
if (!PyArray_SimpleNew) {
|
|
||||||
std::cerr << "FATAL: PyArray_SimpleNew symbol missing!" << std::endl;
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
std::cout << "step0.1" << std::endl;
|
|
||||||
if (!audioFrame.data || audioFrame.dataCount <= 0) {
|
if (!audioFrame.data || audioFrame.dataCount <= 0) {
|
||||||
std::cerr << "Invalid audio frame data" << std::endl;
|
|
||||||
PyGILState_Release(gstate);
|
PyGILState_Release(gstate);
|
||||||
return;
|
throw std::invalid_argument("Invalid audio frame data");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 安全创建维度数组(带边界检查)
|
||||||
|
if (audioFrame.dataCount > std::numeric_limits<npy_intp>::max()) {
|
||||||
|
PyGILState_Release(gstate);
|
||||||
|
throw std::overflow_error("Audio frame size exceeds maximum limit");
|
||||||
}
|
}
|
||||||
std::cout << "step1" << std::endl;
|
|
||||||
npy_intp dims[1] = {static_cast<npy_intp>(audioFrame.dataCount)};
|
npy_intp dims[1] = {static_cast<npy_intp>(audioFrame.dataCount)};
|
||||||
|
|
||||||
std::cout << "step2" << std::endl;
|
// 5. 创建NumPy数组(带内存保护)
|
||||||
PyObject* pyArray = PyArray_SimpleNew(1, dims, NPY_INT16);
|
PyObject* pyArray = nullptr;
|
||||||
std::cout << "step3" << std::endl;
|
{
|
||||||
|
py::gil_scoped_acquire gil; // 确保在创建数组时持有GIL
|
||||||
|
pyArray = PyArray_SimpleNew(1, dims, NPY_INT16);
|
||||||
if (!pyArray) {
|
if (!pyArray) {
|
||||||
PyGILState_Release(gstate);
|
PyGILState_Release(gstate);
|
||||||
throw std::runtime_error("Failed to create NumPy array");
|
throw std::bad_alloc();
|
||||||
}
|
}
|
||||||
std::cout << "step4" << std::endl;
|
}
|
||||||
std::memcpy(PyArray_DATA((PyArrayObject*)pyArray),
|
|
||||||
|
// 6. 安全拷贝数据(带对齐检查)
|
||||||
|
if (reinterpret_cast<uintptr_t>(audioFrame.data) % alignof(int16_t) != 0) {
|
||||||
|
Py_DECREF(pyArray);
|
||||||
|
PyGILState_Release(gstate);
|
||||||
|
throw std::runtime_error("Unaligned audio data pointer");
|
||||||
|
}
|
||||||
|
std::memcpy(PyArray_DATA(reinterpret_cast<PyArrayObject*>(pyArray)),
|
||||||
audioFrame.data,
|
audioFrame.data,
|
||||||
audioFrame.dataCount * sizeof(int16_t));
|
audioFrame.dataCount * sizeof(int16_t));
|
||||||
std::cout << "step5" << std::endl;
|
|
||||||
|
|
||||||
// 6. 执行回调
|
// 7. 执行回调(带引用计数保护)
|
||||||
if (!py_callback_.is_none()) {
|
if (!py_callback_.is_none()) {
|
||||||
|
try {
|
||||||
py_callback_(
|
py_callback_(
|
||||||
py::handle<>(pyArray), // 自动管理引用
|
py::handle<>(pyArray), // 自动管理引用
|
||||||
audioFrame.dataCount,
|
audioFrame.dataCount,
|
||||||
|
@ -113,12 +126,15 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
|
||||||
audioFrame.numChannels,
|
audioFrame.numChannels,
|
||||||
audioFrame.channelIndex
|
audioFrame.channelIndex
|
||||||
);
|
);
|
||||||
}
|
} catch (...) {
|
||||||
std::cout << "step6" << std::endl;
|
|
||||||
|
|
||||||
// 7. 释放数组(py::handle 会管理引用,此处可省略)
|
|
||||||
Py_DECREF(pyArray);
|
Py_DECREF(pyArray);
|
||||||
std::cout << "step7" << std::endl;
|
throw; // 重新抛出异常
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8. 释放资源
|
||||||
|
Py_DECREF(pyArray);
|
||||||
|
PyGILState_Release(gstate);
|
||||||
|
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
std::cerr << "Audio process error: " << e.what() << std::endl;
|
std::cerr << "Audio process error: " << e.what() << std::endl;
|
||||||
|
|
Loading…
Reference in New Issue