This commit is contained in:
wangjiyu 2025-04-10 22:06:08 +08:00
parent 85528e9023
commit 0f7c98f2f2
1 changed files with 21 additions and 1 deletions

View File

@ -155,6 +155,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
exit(0);
}
*/
void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
mrtc::MRTCAudioFrame& audioFrame,
mrtc::MRTCAudioSourceType audioSourceType)
@ -173,11 +174,13 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
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<npy_intp>::max()) {
std::cout << "[ERROR] 数据长度超过最大值" << std::endl;
PyGILState_Release(gstate);
throw std::overflow_error("Audio frame size exceeds maximum limit");
}
@ -191,6 +194,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
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");
}
std::cout << " 模块导入成功: " << numpy_module << std::endl;
@ -201,6 +205,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
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;
@ -210,7 +215,18 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
PyObject* py_dims = PyTuple_New(1);
PyTuple_SetItem(py_dims, 0, PyLong_FromLong(dims[0]));
PyObject* dtype = PyArray_DescrFromType(NPY_INT16);
// 修正点显式处理dtype类型转换
PyArray_Descr* np_dtype = PyArray_DescrFromType(NPY_INT16);
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");
}
PyObject* dtype = reinterpret_cast<PyObject*>(np_dtype);
PyObject* args = PyTuple_Pack(2, py_dims, dtype);
std::cout << " 参数构建完成: " << args << std::endl;
@ -229,6 +245,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
if (!pyArray) {
std::cout << "[ERROR] 数组创建失败" << std::endl;
PyGILState_Release(gstate);
throw std::bad_alloc();
}
@ -237,6 +254,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
if (reinterpret_cast<uintptr_t>(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");
}
@ -262,6 +280,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
} catch (...) {
std::cout << "[ERROR] 回调执行异常" << std::endl;
Py_DECREF(pyArray);
PyGILState_Release(gstate);
throw;
}
} else {
@ -282,6 +301,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
std::cerr << "Audio process error: " << e.what() << std::endl;
}
}
void RTCContext::onProducer(uint32_t msgId, mrtc::MRTCProducerInfo& info)
{
std::cout << "-----------------------------------" << std::endl;