This commit is contained in:
wangjiyu 2025-04-16 15:40:46 +08:00
parent b04c665ac6
commit fe9c640c5b
1 changed files with 146 additions and 146 deletions

View File

@ -303,165 +303,165 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
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();
try { //try {
// 2. 输入参数校验 // // 2. 输入参数校验
std::cout << "[2] 检查输入参数..." << std::endl; // std::cout << "[2] 检查输入参数..." << std::endl;
std::cout << " dataCount: " << audioFrame.dataCount // std::cout << " dataCount: " << audioFrame.dataCount
<< " (max: " << std::numeric_limits<npy_intp>::max() << ")" << std::endl; // << " (max: " << std::numeric_limits<npy_intp>::max() << ")" << std::endl;
if (!audioFrame.data || audioFrame.dataCount <= 0) { // if (!audioFrame.data || audioFrame.dataCount <= 0) {
std::cout << "[ERROR] 无效音频数据指针或长度" << std::endl; // std::cout << "[ERROR] 无效音频数据指针或长度" << std::endl;
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;
char shm_name[32]; // char shm_name[32];
//snprintf(shm_name, sizeof(shm_name), "/audio_shm_%d", getpid()); // //snprintf(shm_name, sizeof(shm_name), "/audio_shm_%d", getpid());
snprintf(shm_name, sizeof(shm_name), "/audio_shm_test"); // snprintf(shm_name, sizeof(shm_name), "/audio_shm_test");
int fd = shm_open(shm_name, O_CREAT | O_RDWR, 0666); // int fd = shm_open(shm_name, O_CREAT | O_RDWR, 0666);
if (fd == -1) { // if (fd == -1) {
std::cout << "[ERROR] shm_open失败: " << strerror(errno) << std::endl; // std::cout << "[ERROR] shm_open失败: " << strerror(errno) << std::endl;
throw std::runtime_error("Failed to create shared memory"); // throw std::runtime_error("Failed to create shared memory");
} // }
std::cout << " 共享内存fd: " << fd << " 名称: " << shm_name << std::endl; // std::cout << " 共享内存fd: " << fd << " 名称: " << shm_name << std::endl;
// 4. 设置共享内存大小 // // 4. 设置共享内存大小
std::cout << "[4] 设置共享内存大小..." << std::endl; // std::cout << "[4] 设置共享内存大小..." << std::endl;
if (ftruncate(fd, data_size) == -1) { // if (ftruncate(fd, data_size) == -1) {
close(fd); // close(fd);
std::cout << "[ERROR] ftruncate失败: " << strerror(errno) << std::endl; // std::cout << "[ERROR] ftruncate失败: " << strerror(errno) << std::endl;
throw std::runtime_error("Failed to resize shared memory"); // throw std::runtime_error("Failed to resize shared memory");
} // }
std::cout << " 内存大小: " << data_size << " bytes" << std::endl; // std::cout << " 内存大小: " << data_size << " bytes" << std::endl;
// 5. 内存映射 // // 5. 内存映射
std::cout << "[5] 内存映射..." << std::endl; // std::cout << "[5] 内存映射..." << std::endl;
void* ptr = mmap(NULL, data_size, PROT_WRITE, MAP_SHARED, fd, 0); // void* ptr = mmap(NULL, data_size, PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) { // if (ptr == MAP_FAILED) {
close(fd); // close(fd);
std::cout << "[ERROR] mmap失败: " << strerror(errno) << std::endl; // std::cout << "[ERROR] mmap失败: " << strerror(errno) << std::endl;
throw std::runtime_error("Failed to map shared memory"); // throw std::runtime_error("Failed to map shared memory");
} // }
std::cout << " 映射地址: " << ptr << std::endl; // std::cout << " 映射地址: " << ptr << std::endl;
namespace py = boost::python; // namespace py = boost::python;
namespace np = boost::python::numpy; // namespace np = boost::python::numpy;
// 6. 拷贝数据到共享内存 // // 6. 拷贝数据到共享内存
std::cout << "[6] 拷贝音频数据到共享内存..." << std::endl; // std::cout << "[6] 拷贝音频数据到共享内存..." << std::endl;
memcpy(ptr, audioFrame.data, data_size); // memcpy(ptr, audioFrame.data, data_size);
std::cout << "step1" << std::endl; // std::cout << "step1" << std::endl;
/* // /*
npy_intp shape[1] = { static_cast<npy_intp>(audioFrame.dataCount) }; // npy_intp shape[1] = { static_cast<npy_intp>(audioFrame.dataCount) };
std::cout << "step2" << std::endl; // std::cout << "step2" << std::endl;
np::dtype dtype = np::dtype::get_builtin<int16_t>(); // np::dtype dtype = np::dtype::get_builtin<int16_t>();
std::cout << "step3" << std::endl; // std::cout << "step3" << std::endl;
np::ndarray audioArray = np::from_data( // np::ndarray audioArray = np::from_data(
audioFrame.data, // 数据指针 // audioFrame.data, // 数据指针
dtype, // 数据类型 (int16) // dtype, // 数据类型 (int16)
py::make_tuple(shape[0]), // 形状 (1D) // py::make_tuple(shape[0]), // 形状 (1D)
py::make_tuple(sizeof(int16_t)), // 步长 // py::make_tuple(sizeof(int16_t)), // 步长
py::object() // 所有者Python管理 // py::object() // 所有者Python管理
); // );
*/ // */
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_( // // pyCallback_(
// audioArray, // numpy 数组 // // audioArray, // numpy 数组
// data_size, // 数据大小 // // data_size, // 数据大小
// audioFrame.dataCount, // // audioFrame.dataCount,
// audioFrame.sampleRate, // // audioFrame.sampleRate,
// audioFrame.numChannels, // // audioFrame.numChannels,
// audioFrame.channelIndex // // audioFrame.channelIndex
// ); // // );
// */ // // */
// std::cout << " after callback" << std::endl; // // std::cout << " after callback" << std::endl;
// if (PyErr_Occurred()) { // // if (PyErr_Occurred()) {
// PyObject *type, *value, *traceback; // // PyObject *type, *value, *traceback;
// PyErr_Fetch(&type, &value, &traceback); // // PyErr_Fetch(&type, &value, &traceback);
// if (value) { // // if (value) {
// PyObject* str = PyObject_Str(value); // // PyObject* str = PyObject_Str(value);
// if (str) { // // if (str) {
// std::cerr << "Python Error: " << PyUnicode_AsUTF8(str) << std::endl; // // std::cerr << "Python Error: " << PyUnicode_AsUTF8(str) << std::endl;
// Py_DECREF(str); // // Py_DECREF(str);
// } // // }
// } // // }
// Py_XDECREF(type); // // Py_XDECREF(type);
// Py_XDECREF(value); // // Py_XDECREF(value);
// Py_XDECREF(traceback); // // Py_XDECREF(traceback);
// //PyErr_Print(); // // //PyErr_Print();
// throw std::runtime_error("Python callback error"); // // 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;
munmap(ptr, data_size); // munmap(ptr, data_size);
close(fd); // close(fd);
shm_unlink(shm_name); // shm_unlink(shm_name);
std::cout << "[9] 释放GIL..." << std::endl; // std::cout << "[9] 释放GIL..." << std::endl;
//PyGILState_Release(gstate); // //PyGILState_Release(gstate);
std::cout << "=== 音频处理完成 ===" << std::endl; // std::cout << "=== 音频处理完成 ===" << std::endl;
} catch (const std::exception& e) { //} catch (const std::exception& e) {
std::cout << "[EXCEPTION] 异常捕获: " << e.what() << std::endl; // std::cout << "[EXCEPTION] 异常捕获: " << e.what() << std::endl;
//PyGILState_Release(gstate); // //PyGILState_Release(gstate);
std::cerr << "Audio process error: " << e.what() << std::endl; // std::cerr << "Audio process error: " << e.what() << std::endl;
} //}
} }
void RTCContext::onProducer(uint32_t msgId, mrtc::MRTCProducerInfo& info) void RTCContext::onProducer(uint32_t msgId, mrtc::MRTCProducerInfo& info)