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}") print(f"resend fail, ret:{ret}")
else: else:
print("resend succ") print("resend succ")
size = rtc_plugins.getSize() #size = rtc_plugins.getSize()
print(f"data size:{size}") #print(f"data size:{size}")
#frame = rtc_plugins.getNumpyData() ##frame = rtc_plugins.getNumpyData()
frame = rtc_plugins.getListData() #frame = rtc_plugins.getListData()
print(f"get frame:{frame}") #print(f"get frame:{frame}")
dataCount = rtc_plugins.getDataCount() #dataCount = rtc_plugins.getDataCount()
print(f"data count:{dataCount}") #print(f"data count:{dataCount}")
time.sleep(0.005) time.sleep(3)

View File

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

View File

@ -310,13 +310,20 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
mrtc::MRTCAudioFrame& audioFrame, mrtc::MRTCAudioFrame& audioFrame,
mrtc::MRTCAudioSourceType audioSourceType) 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; //namespace py = boost::python;
//std::cout << "=== 开始音频处理(共享内存版) ===" << std::endl; //std::cout << "=== 开始音频处理(共享内存版) ===" << std::endl;
//std::cout << "audioFrame:" << audioFrame.dataCount << "," << audioFrame.sampleRate << "," << //std::cout << "audioFrame:" << audioFrame.dataCount << "," << audioFrame.sampleRate << "," <<
// audioFrame.numChannels << "," << audioFrame.channelIndex << std::endl; // audioFrame.numChannels << "," << audioFrame.channelIndex << std::endl;
//printTimestamp(); //printTimestamp();
setData(audioFrame); //setData(audioFrame);
// 1. 获取GIL // 1. 获取GIL
//std::cout << "[1] 获取GIL锁..." << std::endl; //std::cout << "[1] 获取GIL锁..." << std::endl;
////PyGILState_STATE gstate = PyGILState_Ensure(); ////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"); // 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. 创建共享内存 // // 3. 创建共享内存
// std::cout << "[3] 创建共享内存..." << std::endl; // std::cout << "[3] 创建共享内存..." << std::endl;
@ -387,80 +394,68 @@ 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), // 共享内存名称 result, // 共享内存名称
// // data_size, // 数据大小 data_size, // 数据大小
// // audioFrame.dataCount, audioFrame.dataCount,
// // audioFrame.sampleRate, audioFrame.sampleRate,
// // audioFrame.numChannels, audioFrame.numChannels,
// // audioFrame.channelIndex 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::cout << " after callback" << std::endl;
// // std::cerr << "[PYTHON ERROR] "; if (PyErr_Occurred()) {
// // PyErr_Print(); // 自动打印到stderr PyObject *type, *value, *traceback;
// // // 可选:获取更详细的错误信息 PyErr_Fetch(&type, &value, &traceback);
// // if (PyErr_Occurred()) { if (value) {
// // PyObject *type, *value, *traceback; PyObject* str = PyObject_Str(value);
// // PyErr_Fetch(&type, &value, &traceback); if (str) {
// // std::cerr << "Details: " std::cerr << "Python Error: " << PyUnicode_AsUTF8(str) << std::endl;
// // << PyUnicode_AsUTF8(PyObject_Str(value)) << std::endl; Py_DECREF(str);
// // PyErr_Restore(type, value, traceback); }
// // } }
// // Py_DECREF(pyCallback_.ptr()); Py_XDECREF(type);
// // } catch (...) { Py_XDECREF(value);
// // std::cout << "[ERROR] 回调执行失败" << std::endl; Py_XDECREF(traceback);
// // munmap(ptr, data_size); //PyErr_Print();
// // close(fd); throw std::runtime_error("Python callback error");
// // shm_unlink(shm_name); }
// // Py_DECREF(pyCallback_.ptr()); std::cout << " 回调执行成功" << std::endl;
// // throw;
// // } } catch (const py::error_already_set& e) {
// // Py_DECREF(pyCallback_.ptr()); std::cerr << "[PYTHON ERROR] ";
// //} else { PyErr_Print(); // 自动打印到stderr
// // std::cout << "[7] 无回调函数设置" << std::endl; // 可选:获取更详细的错误信息
// //} 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. 释放资源 // // 8. 释放资源
// std::cout << "[8] 释放共享内存资源..." << std::endl; // std::cout << "[8] 释放共享内存资源..." << std::endl;