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