This commit is contained in:
wangjiyu 2025-04-15 16:23:43 +08:00
parent 7086e4cd21
commit dc9dd314d6
2 changed files with 5 additions and 67 deletions

View File

@ -107,62 +107,6 @@ int sendCustomAudioData(int16_t destChannelIndex, py::object pD,
}
}
/*
int sendCustomAudioData(const int16_t destChannelIndex, py::object pyData, int32_t sampleRate, uint64_t channelNum,
uint64_t dataLen) {
try {
//py::object pyData = create_int16_array();
std::cout << "step 1" << std::endl;
// 1. 检查输入有效性
if (pyData.ptr() == nullptr) {
throw std::runtime_error("Input data is NULL");
}
std::cout << "step 2" << std::endl;
std::cout << "pyData ptr is:" << pyData.ptr() << std::endl;
if (!pyData.ptr() || !Py_IsInitialized() || !PyObject_TypeCheck(pyData.ptr(), &PyBaseObject_Type)) {
throw std::runtime_error("Invalid Python object");
}
std::cout << "step 2" << std::endl;
// 2. 检查是否是 numpy 数组
if (!PyArray_Check(pyData.ptr())) {
std::cout << "input is notnumpy" << std::endl;
throw std::runtime_error("Input is not a numpy array");
}
std::cout << "step 3" << std::endl;
// 3. 转换为 PyArrayObject
PyArrayObject* npArray = reinterpret_cast<PyArrayObject*>(pyData.ptr());
std::cout << "step 4" << std::endl;
// 4. 检查数据类型是否为 int16
if (PyArray_TYPE(npArray) != NPY_INT16) {
throw std::runtime_error("Array must be of type int16 (np.int16)");
}
std::cout << "step 5" << std::endl;
// 5. 检查数据是否连续
if (!PyArray_ISCONTIGUOUS(npArray)) {
throw std::runtime_error("Array must be contiguous in memory");
}
std::cout << "step 6" << std::endl;
// 6. 获取数据指针
void* dataPtr = PyArray_DATA(npArray);
if (dataPtr == nullptr) {
throw std::runtime_error("Invalid data pointer");
}
std::cout << "step 7" << std::endl;
return RTCContext::instance().sendCustomAudioData(destChannelIndex, dataPtr, sampleRate, channelNum, dataLen);
} catch (const std::exception& e) {
std::cout << "error:" << e.what() << std::endl;
return -1;
}
}
*/
void init_numpy() {
// 直接调用底层函数,绕过宏的问题
if (_import_array() < 0) {
@ -184,14 +128,6 @@ BOOST_PYTHON_MODULE(rtc_plugins) {
std::cout << "set numpyApi succ:" << numpyApi[93] << std::endl;
}
/*
if (!PyArray_API) {
std::cout << "PyArray_API is null" << std::endl;
} else {
std::cout << "PyArray_API is not null" << std::endl;
}
*/
py::def("init", &init);
py::def("initRecv", &initRecv);
py::def("initSend", &initSend);

View File

@ -71,7 +71,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
std::cerr << "Python 解释器未初始化!" << std::endl;
return;
}
PyGILState_STATE gstate = PyGILState_Ensure();
//PyGILState_STATE gstate = PyGILState_Ensure();
try {
// 2. 输入参数校验
@ -115,6 +115,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
Py_DECREF(repr); // 必须手动释放
}
PyGILState_STATE gstate = PyGILState_Ensure();
pyCallback_(
audioArray, // numpy 数组
data_size, // 数据大小
@ -123,6 +124,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
audioFrame.numChannels,
audioFrame.channelIndex
);
PyGILState_Release(gstate);
std::cout << " after callback" << std::endl;
if (PyErr_Occurred()) {
PyObject *type, *value, *traceback;
@ -169,12 +171,12 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
std::cout << "[8] 释放共享内存资源..." << std::endl;
std::cout << "[9] 释放GIL..." << std::endl;
PyGILState_Release(gstate);
//PyGILState_Release(gstate);
std::cout << "=== 音频处理完成 ===" << std::endl;
} catch (const std::exception& e) {
std::cout << "[EXCEPTION] 异常捕获: " << e.what() << std::endl;
PyGILState_Release(gstate);
//PyGILState_Release(gstate);
std::cerr << "Audio process error: " << e.what() << std::endl;
}
}