This commit is contained in:
wangjiyu 2025-04-16 11:26:13 +08:00
parent 847aad603e
commit 6f24ab5105
1 changed files with 72 additions and 74 deletions

View File

@ -372,81 +372,79 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
std::cout << " 数据拷贝完成" << std::endl;
// 7. 执行回调
if (!pyCallback_.is_none()) {
std::cout << "[7] 准备执行Python回调..." << std::endl;
// 增加引用计数防止提前释放
Py_INCREF(pyCallback_.ptr());
try {
std::cout << " pyCallback_ type: " << Py_TYPE(pyCallback_.ptr())->tp_name << std::endl;
PyObject* repr = PyObject_Repr(pyCallback_.ptr());
if (repr) {
std::cout << " pyCallback_ repr: " << PyUnicode_AsUTF8(repr) << std::endl;
Py_DECREF(repr); // 必须手动释放
}
// 传递共享内存信息
/*
pyCallback_(
py::str(shm_name), // 共享内存名称
data_size, // 数据大小
audioFrame.dataCount,
audioFrame.sampleRate,
audioFrame.numChannels,
audioFrame.channelIndex
);
*/
/*
pyCallback_(
audioArray, // numpy 数组
data_size, // 数据大小
audioFrame.dataCount,
audioFrame.sampleRate,
audioFrame.numChannels,
audioFrame.channelIndex
);
*/
std::cout << " after callback" << std::endl;
if (PyErr_Occurred()) {
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
if (value) {
PyObject* str = PyObject_Str(value);
if (str) {
std::cerr << "Python Error: " << PyUnicode_AsUTF8(str) << std::endl;
Py_DECREF(str);
}
}
Py_XDECREF(type);
Py_XDECREF(value);
Py_XDECREF(traceback);
//PyErr_Print();
throw std::runtime_error("Python callback error");
}
std::cout << " 回调执行成功" << std::endl;
//if (!pyCallback_.is_none()) {
// std::cout << "[7] 准备执行Python回调..." << std::endl;
// // 增加引用计数防止提前释放
// Py_INCREF(pyCallback_.ptr());
// try {
// std::cout << " pyCallback_ type: " << Py_TYPE(pyCallback_.ptr())->tp_name << std::endl;
// PyObject* repr = PyObject_Repr(pyCallback_.ptr());
// if (repr) {
// std::cout << " pyCallback_ repr: " << PyUnicode_AsUTF8(repr) << std::endl;
// Py_DECREF(repr); // 必须手动释放
// }
// // 传递共享内存信息
// pyCallback_(
// py::str(shm_name), // 共享内存名称
// data_size, // 数据大小
// audioFrame.dataCount,
// audioFrame.sampleRate,
// audioFrame.numChannels,
// audioFrame.channelIndex
// );
// /*
// pyCallback_(
// audioArray, // numpy 数组
// data_size, // 数据大小
// audioFrame.dataCount,
// audioFrame.sampleRate,
// audioFrame.numChannels,
// audioFrame.channelIndex
// );
// */
// std::cout << " after callback" << std::endl;
// if (PyErr_Occurred()) {
// PyObject *type, *value, *traceback;
// PyErr_Fetch(&type, &value, &traceback);
// if (value) {
// PyObject* str = PyObject_Str(value);
// if (str) {
// std::cerr << "Python Error: " << PyUnicode_AsUTF8(str) << std::endl;
// Py_DECREF(str);
// }
// }
// Py_XDECREF(type);
// Py_XDECREF(value);
// Py_XDECREF(traceback);
// //PyErr_Print();
// throw std::runtime_error("Python callback error");
// }
// std::cout << " 回调执行成功" << std::endl;
} catch (const py::error_already_set& e) {
std::cerr << "[PYTHON ERROR] ";
PyErr_Print(); // 自动打印到stderr
// 可选:获取更详细的错误信息
if (PyErr_Occurred()) {
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
std::cerr << "Details: "
<< PyUnicode_AsUTF8(PyObject_Str(value)) << std::endl;
PyErr_Restore(type, value, traceback);
}
Py_DECREF(pyCallback_.ptr());
} catch (...) {
std::cout << "[ERROR] 回调执行失败" << std::endl;
munmap(ptr, data_size);
close(fd);
shm_unlink(shm_name);
Py_DECREF(pyCallback_.ptr());
throw;
}
Py_DECREF(pyCallback_.ptr());
} else {
std::cout << "[7] 无回调函数设置" << std::endl;
}
// } catch (const py::error_already_set& e) {
// std::cerr << "[PYTHON ERROR] ";
// PyErr_Print(); // 自动打印到stderr
// // 可选:获取更详细的错误信息
// if (PyErr_Occurred()) {
// PyObject *type, *value, *traceback;
// PyErr_Fetch(&type, &value, &traceback);
// std::cerr << "Details: "
// << PyUnicode_AsUTF8(PyObject_Str(value)) << std::endl;
// PyErr_Restore(type, value, traceback);
// }
// Py_DECREF(pyCallback_.ptr());
// } catch (...) {
// std::cout << "[ERROR] 回调执行失败" << std::endl;
// munmap(ptr, data_size);
// close(fd);
// shm_unlink(shm_name);
// Py_DECREF(pyCallback_.ptr());
// throw;
// }
// Py_DECREF(pyCallback_.ptr());
//} else {
// std::cout << "[7] 无回调函数设置" << std::endl;
//}
// 8. 释放资源
std::cout << "[8] 释放共享内存资源..." << std::endl;