From 68ccd03ec3c7c1dbf42496d5f4cea158e27024a1 Mon Sep 17 00:00:00 2001 From: wangjiyu Date: Thu, 10 Apr 2025 11:15:04 +0800 Subject: [PATCH] debug --- util/RTCContext.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/util/RTCContext.cpp b/util/RTCContext.cpp index 8ad2b19..a980b71 100644 --- a/util/RTCContext.cpp +++ b/util/RTCContext.cpp @@ -36,6 +36,7 @@ void RTCContext::onSoundLevelUpdate(const char* roomId, const char* peerId, uint { std::cout << "RTCContext::onSoundLevelUpdate()" << std::endl; } +/* void RTCContext::onAudioProcess(const char* roomId, const char* peerId, mrtc::MRTCAudioFrame& audioFrame, mrtc::MRTCAudioSourceType audioSourceType) { @@ -55,6 +56,70 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId, std::cout << "step3" << std::endl; } } +*/ +void RTCContext::onAudioProcess(const char* roomId, const char* peerId, + mrtc::MRTCAudioFrame& audioFrame, + mrtc::MRTCAudioSourceType audioSourceType) +{ + namespace py = boost::python; + + // 1. 获取 GIL(绝对必要) + py::gil_scoped_acquire gil; + + 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; + return; + } + + std::cout << "step1" << std::endl; + // 3. 检查 NumPy API 状态(关键!) + if (PyArray_API == nullptr) { + std::cerr << "NumPy API not initialized" << std::endl; + return; + } + + 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; + } + std::cout << "step3" << std::endl; + + // 5. 拷贝数据(避免外部内存问题) + void* array_data = PyArray_DATA((PyArrayObject*)pyArray); + std::cout << "step4" << std::endl; + std::memcpy(array_data, audioFrame.data, audioFrame.dataCount * sizeof(int16_t)); + std::cout << "step5" << std::endl; + + // 6. 执行回调 + if (!py_callback_.is_none()) { + py_callback_( + py::handle<>(pyArray), // 自动管理引用 + audioFrame.dataCount, + audioFrame.sampleRate, + audioFrame.numChannels, + audioFrame.channelIndex + ); + } + std::cout << "step6" << std::endl; + + // 7. 释放数组(py::handle 会管理引用,此处可省略) + Py_DECREF(pyArray); + std::cout << "step7" << std::endl; + + } catch (const std::exception& e) { + std::cerr << "Audio process error: " << e.what() << std::endl; + PyErr_Print(); + } +} void RTCContext::onProducer(uint32_t msgId, mrtc::MRTCProducerInfo& info) { std::cout << "-----------------------------------" << std::endl;