This commit is contained in:
wangjiyu 2025-04-10 11:34:01 +08:00
parent 9d53a7156f
commit 3667d3262b
1 changed files with 11 additions and 20 deletions

View File

@ -64,39 +64,30 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
namespace py = boost::python;
// 1. 获取 GIL绝对必要
py::gil_scoped_acquire gil;
PyGILState_STATE gstate = PyGILState_Ensure();
try {
std::cout << "-----------------------------------" << std::endl;
std::cout << "dataCount:" << audioFrame.dataCount << std::endl;
// 2. 严格校验输入数据
if (!audioFrame.data || audioFrame.dataCount <= 0) {
std::cerr << "Invalid audio frame data" << std::endl;
PyGILState_Release(gstate);
return;
}
std::cout << "step1" << std::endl;
// 3. 检查 NumPy API 状态(关键!)
if (PyArray_API == nullptr) {
std::cerr << "NumPy API not initialized" << std::endl;
return;
}
npy_intp dims[1] = {audioFrame.dataCount};
std::cout << "step2" << std::endl;
// 4. 安全创建 NumPy 数组(深拷贝方案)
npy_intp dims[1] = {audioFrame.dataCount};
PyObject* pyArray = PyArray_SimpleNew(1, dims, NPY_INT16); // 创建新数组
if (!pyArray) {
std::cerr << "Failed to create NumPy array" << std::endl;
return;
}
PyObject* pyArray = PyArray_SimpleNew(1, dims, NPY_INT16);
std::cout << "step3" << std::endl;
// 5. 拷贝数据(避免外部内存问题)
void* array_data = PyArray_DATA((PyArrayObject*)pyArray);
if (!pyArray) {
PyGILState_Release(gstate);
throw std::runtime_error("Failed to create NumPy array");
}
std::cout << "step4" << std::endl;
std::memcpy(array_data, audioFrame.data, audioFrame.dataCount * sizeof(int16_t));
std::memcpy(PyArray_DATA((PyArrayObject*)pyArray),
audioFrame.data,
audioFrame.dataCount * sizeof(int16_t));
std::cout << "step5" << std::endl;
// 6. 执行回调