debug
This commit is contained in:
parent
b04c665ac6
commit
fe9c640c5b
|
@ -303,165 +303,165 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
|
|||
|
||||
setData(audioFrame);
|
||||
// 1. 获取GIL
|
||||
std::cout << "[1] 获取GIL锁..." << std::endl;
|
||||
//PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
//std::cout << "[1] 获取GIL锁..." << std::endl;
|
||||
////PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
|
||||
try {
|
||||
// 2. 输入参数校验
|
||||
std::cout << "[2] 检查输入参数..." << std::endl;
|
||||
std::cout << " dataCount: " << audioFrame.dataCount
|
||||
<< " (max: " << std::numeric_limits<npy_intp>::max() << ")" << std::endl;
|
||||
//try {
|
||||
// // 2. 输入参数校验
|
||||
// std::cout << "[2] 检查输入参数..." << std::endl;
|
||||
// std::cout << " dataCount: " << audioFrame.dataCount
|
||||
// << " (max: " << std::numeric_limits<npy_intp>::max() << ")" << std::endl;
|
||||
|
||||
if (!audioFrame.data || audioFrame.dataCount <= 0) {
|
||||
std::cout << "[ERROR] 无效音频数据指针或长度" << std::endl;
|
||||
throw std::invalid_argument("Invalid audio frame data");
|
||||
}
|
||||
// if (!audioFrame.data || audioFrame.dataCount <= 0) {
|
||||
// std::cout << "[ERROR] 无效音频数据指针或长度" << std::endl;
|
||||
// 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;
|
||||
char shm_name[32];
|
||||
//snprintf(shm_name, sizeof(shm_name), "/audio_shm_%d", getpid());
|
||||
snprintf(shm_name, sizeof(shm_name), "/audio_shm_test");
|
||||
// // 3. 创建共享内存
|
||||
// std::cout << "[3] 创建共享内存..." << std::endl;
|
||||
// char shm_name[32];
|
||||
// //snprintf(shm_name, sizeof(shm_name), "/audio_shm_%d", getpid());
|
||||
// snprintf(shm_name, sizeof(shm_name), "/audio_shm_test");
|
||||
|
||||
int fd = shm_open(shm_name, O_CREAT | O_RDWR, 0666);
|
||||
if (fd == -1) {
|
||||
std::cout << "[ERROR] shm_open失败: " << strerror(errno) << std::endl;
|
||||
throw std::runtime_error("Failed to create shared memory");
|
||||
}
|
||||
std::cout << " 共享内存fd: " << fd << " 名称: " << shm_name << std::endl;
|
||||
// int fd = shm_open(shm_name, O_CREAT | O_RDWR, 0666);
|
||||
// if (fd == -1) {
|
||||
// std::cout << "[ERROR] shm_open失败: " << strerror(errno) << std::endl;
|
||||
// throw std::runtime_error("Failed to create shared memory");
|
||||
// }
|
||||
// std::cout << " 共享内存fd: " << fd << " 名称: " << shm_name << std::endl;
|
||||
|
||||
// 4. 设置共享内存大小
|
||||
std::cout << "[4] 设置共享内存大小..." << std::endl;
|
||||
if (ftruncate(fd, data_size) == -1) {
|
||||
close(fd);
|
||||
std::cout << "[ERROR] ftruncate失败: " << strerror(errno) << std::endl;
|
||||
throw std::runtime_error("Failed to resize shared memory");
|
||||
}
|
||||
std::cout << " 内存大小: " << data_size << " bytes" << std::endl;
|
||||
// // 4. 设置共享内存大小
|
||||
// std::cout << "[4] 设置共享内存大小..." << std::endl;
|
||||
// if (ftruncate(fd, data_size) == -1) {
|
||||
// close(fd);
|
||||
// std::cout << "[ERROR] ftruncate失败: " << strerror(errno) << std::endl;
|
||||
// throw std::runtime_error("Failed to resize shared memory");
|
||||
// }
|
||||
// std::cout << " 内存大小: " << data_size << " bytes" << std::endl;
|
||||
|
||||
// 5. 内存映射
|
||||
std::cout << "[5] 内存映射..." << std::endl;
|
||||
void* ptr = mmap(NULL, data_size, PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (ptr == MAP_FAILED) {
|
||||
close(fd);
|
||||
std::cout << "[ERROR] mmap失败: " << strerror(errno) << std::endl;
|
||||
throw std::runtime_error("Failed to map shared memory");
|
||||
}
|
||||
std::cout << " 映射地址: " << ptr << std::endl;
|
||||
// // 5. 内存映射
|
||||
// std::cout << "[5] 内存映射..." << std::endl;
|
||||
// void* ptr = mmap(NULL, data_size, PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
// if (ptr == MAP_FAILED) {
|
||||
// close(fd);
|
||||
// std::cout << "[ERROR] mmap失败: " << strerror(errno) << std::endl;
|
||||
// throw std::runtime_error("Failed to map shared memory");
|
||||
// }
|
||||
// std::cout << " 映射地址: " << ptr << std::endl;
|
||||
|
||||
namespace py = boost::python;
|
||||
namespace np = boost::python::numpy;
|
||||
// 6. 拷贝数据到共享内存
|
||||
std::cout << "[6] 拷贝音频数据到共享内存..." << std::endl;
|
||||
memcpy(ptr, audioFrame.data, data_size);
|
||||
std::cout << "step1" << std::endl;
|
||||
/*
|
||||
npy_intp shape[1] = { static_cast<npy_intp>(audioFrame.dataCount) };
|
||||
std::cout << "step2" << std::endl;
|
||||
np::dtype dtype = np::dtype::get_builtin<int16_t>();
|
||||
std::cout << "step3" << std::endl;
|
||||
np::ndarray audioArray = np::from_data(
|
||||
audioFrame.data, // 数据指针
|
||||
dtype, // 数据类型 (int16)
|
||||
py::make_tuple(shape[0]), // 形状 (1D)
|
||||
py::make_tuple(sizeof(int16_t)), // 步长
|
||||
py::object() // 所有者(Python管理)
|
||||
);
|
||||
*/
|
||||
std::cout << " 数据拷贝完成" << std::endl;
|
||||
// namespace py = boost::python;
|
||||
// namespace np = boost::python::numpy;
|
||||
// // 6. 拷贝数据到共享内存
|
||||
// std::cout << "[6] 拷贝音频数据到共享内存..." << std::endl;
|
||||
// memcpy(ptr, audioFrame.data, data_size);
|
||||
// std::cout << "step1" << std::endl;
|
||||
// /*
|
||||
// npy_intp shape[1] = { static_cast<npy_intp>(audioFrame.dataCount) };
|
||||
// std::cout << "step2" << std::endl;
|
||||
// np::dtype dtype = np::dtype::get_builtin<int16_t>();
|
||||
// std::cout << "step3" << std::endl;
|
||||
// np::ndarray audioArray = np::from_data(
|
||||
// audioFrame.data, // 数据指针
|
||||
// dtype, // 数据类型 (int16)
|
||||
// py::make_tuple(shape[0]), // 形状 (1D)
|
||||
// py::make_tuple(sizeof(int16_t)), // 步长
|
||||
// py::object() // 所有者(Python管理)
|
||||
// );
|
||||
// */
|
||||
// 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_(
|
||||
// // 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;
|
||||
munmap(ptr, data_size);
|
||||
close(fd);
|
||||
shm_unlink(shm_name);
|
||||
// // 8. 释放资源
|
||||
// std::cout << "[8] 释放共享内存资源..." << std::endl;
|
||||
// munmap(ptr, data_size);
|
||||
// close(fd);
|
||||
// shm_unlink(shm_name);
|
||||
|
||||
std::cout << "[9] 释放GIL..." << std::endl;
|
||||
//PyGILState_Release(gstate);
|
||||
std::cout << "=== 音频处理完成 ===" << std::endl;
|
||||
// std::cout << "[9] 释放GIL..." << std::endl;
|
||||
// //PyGILState_Release(gstate);
|
||||
// std::cout << "=== 音频处理完成 ===" << std::endl;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cout << "[EXCEPTION] 异常捕获: " << e.what() << std::endl;
|
||||
//PyGILState_Release(gstate);
|
||||
std::cerr << "Audio process error: " << e.what() << std::endl;
|
||||
}
|
||||
//} catch (const std::exception& e) {
|
||||
// std::cout << "[EXCEPTION] 异常捕获: " << e.what() << std::endl;
|
||||
// //PyGILState_Release(gstate);
|
||||
// std::cerr << "Audio process error: " << e.what() << std::endl;
|
||||
//}
|
||||
}
|
||||
|
||||
void RTCContext::onProducer(uint32_t msgId, mrtc::MRTCProducerInfo& info)
|
||||
|
|
Loading…
Reference in New Issue