This commit is contained in:
wangjiyu 2025-04-10 09:21:12 +08:00
parent d7b0bc221c
commit 317a16849e
2 changed files with 40 additions and 28 deletions

View File

@ -33,36 +33,48 @@ int initSend(const char* destRoomId, const int16_t destChannelIndex) {
} }
int sendCustomAudioData(const int16_t destChannelIndex, py::object pyData, int32_t sampleRate, uint64_t channelNum, int sendCustomAudioData(const int16_t destChannelIndex, py::object pyData, int32_t sampleRate, uint64_t channelNum,
uint64_t dataLen) { uint64_t dataLen) {
std::cout << "step1" << std::endl; try {
// 检查是否为 numpy 数组 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;
// 2. 检查是否是 numpy 数组
if (!PyArray_Check(pyData.ptr())) { if (!PyArray_Check(pyData.ptr())) {
std::cout << "check fail, ptr:" << pyData.ptr() << std::endl; throw std::runtime_error("Input is not a numpy array");
PyErr_SetString(PyExc_TypeError, "Expected a numpy array");
throw py::error_already_set();
} }
std::cout << "step2" << std::endl;
// 检查数据类型是否为 int16 std::cout << "step 3" << std::endl;
PyArrayObject* npArray = (PyArrayObject*)pyData.ptr(); // 3. 转换为 PyArrayObject
PyArrayObject* npArray = reinterpret_cast<PyArrayObject*>(pyData.ptr());
std::cout << "step 4" << std::endl;
// 4. 检查数据类型是否为 int16
if (PyArray_TYPE(npArray) != NPY_INT16) { if (PyArray_TYPE(npArray) != NPY_INT16) {
PyErr_SetString(PyExc_TypeError, "Array must be of type int16 (np.int16)"); throw std::runtime_error("Array must be of type int16 (np.int16)");
throw py::error_already_set();
}
std::cout << "step3" << std::endl;
if (npArray == nullptr) {
std::cout << "npArray is null" << std::endl;
PyErr_SetString(PyExc_TypeError, "npArray is null");
throw py::error_already_set();
} }
std::cout << "step4" << std::endl; 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); void* dataPtr = PyArray_DATA(npArray);
if (dataPtr == nullptr) { if (dataPtr == nullptr) {
PyErr_SetString(PyExc_ValueError, "Invalid data pointer"); throw std::runtime_error("Invalid data pointer");
throw py::error_already_set();
} }
std::cout << "step5" << std::endl; std::cout << "step 7" << std::endl;
return RTCContext::instance().sendCustomAudioData(destChannelIndex, dataPtr, sampleRate, channelNum, dataLen); return RTCContext::instance().sendCustomAudioData(destChannelIndex, dataPtr, sampleRate, channelNum, dataLen);
} catch (const std::exception& e) {
std::cout << "error:" << e.what() << std::endl;
return -1;
}
} }
BOOST_PYTHON_MODULE(rtc_plugins) { BOOST_PYTHON_MODULE(rtc_plugins) {

View File

@ -21,7 +21,7 @@ ret = rtc_plugins.initSend(destRoomId, destChannelIndex)
if ret != 0: if ret != 0:
print(f"initSend fail, ret:{ret}") print(f"initSend fail, ret:{ret}")
exit(1) exit(1)
audioData = np.zeros(16, dtype=np.int16) audioData = np.array([0, 1, -1, 0], dtype=np.int16)
while True: while True:
ret = rtc_plugins.sendCustomAudioData(destChannelIndex, audioData, 48000, 1, len(audioData)) ret = rtc_plugins.sendCustomAudioData(destChannelIndex, audioData, 48000, 1, len(audioData))
if ret != 0: if ret != 0: