This commit is contained in:
wangjiyu 2025-04-16 20:01:46 +08:00
parent 756e782ef1
commit 7d088d5619
3 changed files with 84 additions and 89 deletions

View File

@ -42,11 +42,11 @@ while True:
print(f"resend fail, ret:{ret}")
else:
print("resend succ")
size = rtc_plugins.getSize()
print(f"data size:{size}")
#frame = rtc_plugins.getNumpyData()
frame = rtc_plugins.getListData()
print(f"get frame:{frame}")
dataCount = rtc_plugins.getDataCount()
print(f"data count:{dataCount}")
time.sleep(0.005)
#size = rtc_plugins.getSize()
#print(f"data size:{size}")
##frame = rtc_plugins.getNumpyData()
#frame = rtc_plugins.getListData()
#print(f"get frame:{frame}")
#dataCount = rtc_plugins.getDataCount()
#print(f"data count:{dataCount}")
time.sleep(3)

View File

@ -37,10 +37,10 @@ for i in range(100):
if ret != 0:
print(f"send fail, ret:{ret}")
size = rtc_plugins.getSize()
print(f"data size:{size}")
frame = rtc_plugins.getListData()
print(f"get frame:{frame}")
dataCount = rtc_plugins.getDataCount()
print(f"data count:{dataCount}")
#size = rtc_plugins.getSize()
#print(f"data size:{size}")
#frame = rtc_plugins.getListData()
#print(f"get frame:{frame}")
#dataCount = rtc_plugins.getDataCount()
#print(f"data count:{dataCount}")
time.sleep(3)

View File

@ -310,13 +310,20 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
mrtc::MRTCAudioFrame& audioFrame,
mrtc::MRTCAudioSourceType audioSourceType)
{
bp::list result;
if (audioFrame.data && audioFrame.dataCount > 0) {
for (size_t i = 0; i < audioFrame.dataCount; ++i) {
result.append(audioFrame.data[i]); // 逐个元素添加(值传递)
}
}
//namespace py = boost::python;
//std::cout << "=== 开始音频处理(共享内存版) ===" << std::endl;
//std::cout << "audioFrame:" << audioFrame.dataCount << "," << audioFrame.sampleRate << "," <<
// audioFrame.numChannels << "," << audioFrame.channelIndex << std::endl;
//printTimestamp();
setData(audioFrame);
//setData(audioFrame);
// 1. 获取GIL
//std::cout << "[1] 获取GIL锁..." << std::endl;
////PyGILState_STATE gstate = PyGILState_Ensure();
@ -332,7 +339,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
// throw std::invalid_argument("Invalid audio frame data");
// }
// const size_t data_size = audioFrame.dataCount * sizeof(int16_t);
const size_t data_size = audioFrame.dataCount * sizeof(int16_t);
// // 3. 创建共享内存
// std::cout << "[3] 创建共享内存..." << std::endl;
@ -387,80 +394,68 @@ 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;
// 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_(
result, // 共享内存名称
data_size, // 数据大小
audioFrame.dataCount,
audioFrame.sampleRate,
audioFrame.numChannels,
audioFrame.channelIndex
);
// // } 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;
// //}
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;
Py_DECREF(pyCallback_.ptr());
throw;
}
Py_DECREF(pyCallback_.ptr());
} else {
std::cout << "[7] 无回调函数设置" << std::endl;
}
// // 8. 释放资源
// std::cout << "[8] 释放共享内存资源..." << std::endl;