This commit is contained in:
wangjiyu 2025-04-10 11:15:04 +08:00
parent 05fa5fc274
commit 68ccd03ec3
1 changed files with 65 additions and 0 deletions

View File

@ -36,6 +36,7 @@ void RTCContext::onSoundLevelUpdate(const char* roomId, const char* peerId, uint
{ {
std::cout << "RTCContext::onSoundLevelUpdate()" << std::endl; std::cout << "RTCContext::onSoundLevelUpdate()" << std::endl;
} }
/*
void RTCContext::onAudioProcess(const char* roomId, const char* peerId, void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
mrtc::MRTCAudioFrame& audioFrame, mrtc::MRTCAudioSourceType audioSourceType) mrtc::MRTCAudioFrame& audioFrame, mrtc::MRTCAudioSourceType audioSourceType)
{ {
@ -55,6 +56,70 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
std::cout << "step3" << std::endl; 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) void RTCContext::onProducer(uint32_t msgId, mrtc::MRTCProducerInfo& info)
{ {
std::cout << "-----------------------------------" << std::endl; std::cout << "-----------------------------------" << std::endl;