From d1525f6dfa5c8cb49313330de051891dff873fe2 Mon Sep 17 00:00:00 2001 From: wangjiyu Date: Thu, 10 Apr 2025 22:20:33 +0800 Subject: [PATCH] debug --- util/RTCContext.cpp | 117 ++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 79 deletions(-) diff --git a/util/RTCContext.cpp b/util/RTCContext.cpp index c1df4cb..1c1d06b 100644 --- a/util/RTCContext.cpp +++ b/util/RTCContext.cpp @@ -163,116 +163,76 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId, namespace py = boost::python; std::cout << "=== 开始音频处理 ===" << std::endl; + // 1. 获取GIL + std::cout << "[1] 获取GIL锁..." << std::endl; PyGILState_STATE gstate = PyGILState_Ensure(); - std::cout << "[1] GIL 已获取" << std::endl; try { - // 1. 输入参数校验 + // 2. 输入参数校验 std::cout << "[2] 检查输入参数..." << std::endl; std::cout << " dataCount: " << audioFrame.dataCount << " (max: " << std::numeric_limits::max() << ")" << std::endl; if (!audioFrame.data || audioFrame.dataCount <= 0) { std::cout << "[ERROR] 无效音频数据指针或长度" << std::endl; - PyGILState_Release(gstate); throw std::invalid_argument("Invalid audio frame data"); } if (audioFrame.dataCount > std::numeric_limits::max()) { std::cout << "[ERROR] 数据长度超过最大值" << std::endl; - PyGILState_Release(gstate); throw std::overflow_error("Audio frame size exceeds maximum limit"); } - // 2. 准备NumPy数组参数 + // 3. 准备数组维度 std::cout << "[3] 准备数组维度..." << std::endl; npy_intp dims[1] = {static_cast(audioFrame.dataCount)}; - std::cout << " 维度设置完成: " << dims[0] << std::endl; + std::cout << " 维度设置完成: [" << dims[0] << "]" << std::endl; - // 3. 获取NumPy模块 - std::cout << "[4] 导入numpy.core.multiarray模块..." << std::endl; - PyObject* numpy_module = PyImport_ImportModule("numpy.core.multiarray"); - if (!numpy_module) { - std::cout << "[ERROR] 无法导入numpy模块" << std::endl; - PyGILState_Release(gstate); - throw std::runtime_error("Failed to import numpy.core"); + // 4. 检查NumPy API状态 + std::cout << "[4] 检查NumPy API状态..." << std::endl; + std::cout << " numpyApi_ 地址: " << numpyApi_ << std::endl; + if (!numpyApi_) { + throw std::runtime_error("NumPy C-API not initialized"); } - std::cout << " 模块导入成功: " << numpy_module << std::endl; - // 4. 获取empty函数 - std::cout << "[5] 获取numpy.empty函数..." << std::endl; - PyObject* empty_func = PyObject_GetAttrString(numpy_module, "empty"); - if (!empty_func) { - Py_DECREF(numpy_module); - std::cout << "[ERROR] 无法获取empty函数" << std::endl; - PyGILState_Release(gstate); - throw std::runtime_error("Failed to get numpy.empty"); - } - std::cout << " 函数获取成功: " << empty_func << std::endl; + // 5. 获取PyArray_SimpleNew函数 + std::cout << "[5] 获取PyArray_SimpleNew函数..." << std::endl; + using PyArray_SimpleNew_t = PyObject*(*)(int, npy_intp*, int); + PyArray_SimpleNew_t PyArray_SimpleNew = + reinterpret_cast(numpyApi_[93]); + std::cout << " 函数地址: " << (void*)PyArray_SimpleNew << std::endl; - // 5. 构建参数元组 - std::cout << "[6] 构建参数元组..." << std::endl; - PyObject* py_dims = PyTuple_New(1); - std::cout << "step1, " << py_dims << std::endl; - PyTuple_SetItem(py_dims, 0, PyLong_FromLong(dims[0])); - std::cout << "step2" << std::endl; - - // 修正点:显式处理dtype类型转换 - PyArray_Descr* np_dtype = PyArray_DescrFromType(NPY_INT16); - std::cout << "step3" << std::endl; - if (!np_dtype) { - Py_DECREF(py_dims); - Py_DECREF(empty_func); - Py_DECREF(numpy_module); - std::cout << "[ERROR] 无法创建dtype" << std::endl; - PyGILState_Release(gstate); - throw std::runtime_error("Failed to create numpy dtype"); - } - std::cout << "step4" << std::endl; - PyObject* dtype = reinterpret_cast(np_dtype); - std::cout << "step5" << std::endl; - - PyObject* args = PyTuple_Pack(2, py_dims, dtype); - std::cout << " 参数构建完成: " << args << std::endl; - - // 6. 调用函数创建数组 - std::cout << "[7] 调用numpy.empty创建数组..." << std::endl; - PyObject* pyArray = PyObject_Call(empty_func, args, nullptr); - std::cout << " 数组创建结果: " << pyArray << std::endl; - - // 7. 清理临时对象 - std::cout << "[8] 清理临时Python对象..." << std::endl; - Py_DECREF(args); - Py_DECREF(py_dims); - Py_DECREF(dtype); - Py_DECREF(empty_func); - Py_DECREF(numpy_module); + // 6. 创建NumPy数组 + std::cout << "[6] 创建NumPy数组..." << std::endl; + PyObject* pyArray = PyArray_SimpleNew(1, dims, NPY_INT16); + std::cout << " 数组地址: " << pyArray << std::endl; if (!pyArray) { - std::cout << "[ERROR] 数组创建失败" << std::endl; - PyGILState_Release(gstate); throw std::bad_alloc(); } - // 8. 检查内存对齐 - std::cout << "[9] 检查内存对齐..." << std::endl; + // 7. 检查内存对齐 + std::cout << "[7] 检查内存对齐..." << std::endl; + std::cout << " 音频数据地址: " << (void*)audioFrame.data + << " 对齐要求: " << alignof(int16_t) << std::endl; + if (reinterpret_cast(audioFrame.data) % alignof(int16_t) != 0) { Py_DECREF(pyArray); - std::cout << "[ERROR] 内存未对齐" << std::endl; - PyGILState_Release(gstate); throw std::runtime_error("Unaligned audio data pointer"); } - // 9. 拷贝音频数据 - std::cout << "[10] 拷贝音频数据到NumPy数组..." << std::endl; + // 8. 拷贝数据 + std::cout << "[8] 拷贝音频数据..." << std::endl; + std::cout << " 目标地址: " << PyArray_DATA((PyArrayObject*)pyArray) + << " 字节数: " << audioFrame.dataCount * sizeof(int16_t) << std::endl; + std::memcpy(PyArray_DATA((PyArrayObject*)pyArray), audioFrame.data, audioFrame.dataCount * sizeof(int16_t)); - std::cout << " 数据拷贝完成" << std::endl; - // 10. 执行Python回调 + // 9. 执行回调 if (!pyCallback_.is_none()) { - std::cout << "[11] 准备执行Python回调..." << std::endl; + std::cout << "[9] 准备执行Python回调..." << std::endl; try { pyCallback_( py::handle<>(pyArray), @@ -281,21 +241,20 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId, audioFrame.numChannels, audioFrame.channelIndex ); - std::cout << " 回调执行完成" << std::endl; + std::cout << " 回调执行成功" << std::endl; } catch (...) { - std::cout << "[ERROR] 回调执行异常" << std::endl; + std::cout << "[ERROR] 回调执行失败" << std::endl; Py_DECREF(pyArray); - PyGILState_Release(gstate); throw; } } else { - std::cout << "[11] 无回调函数设置" << std::endl; + std::cout << "[9] 无回调函数设置" << std::endl; } - // 11. 清理资源 - std::cout << "[12] 释放Python数组资源..." << std::endl; + // 10. 释放资源 + std::cout << "[10] 释放资源..." << std::endl; Py_DECREF(pyArray); - std::cout << "[13] 释放GIL..." << std::endl; + std::cout << "[11] 释放GIL..." << std::endl; PyGILState_Release(gstate); std::cout << "=== 音频处理完成 ===" << std::endl;