From a47809f3157123af44b8d5e6d046ed72cde43c9d Mon Sep 17 00:00:00 2001 From: wangjiyu Date: Thu, 10 Apr 2025 09:57:22 +0800 Subject: [PATCH] debug --- rtc_plugins.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/rtc_plugins.cpp b/rtc_plugins.cpp index 14c700a..b70d486 100644 --- a/rtc_plugins.cpp +++ b/rtc_plugins.cpp @@ -1,5 +1,6 @@ #include #include +#include #include @@ -31,9 +32,29 @@ int initSend(const char* destRoomId, const int16_t destChannelIndex) { return -1; } } -int sendCustomAudioData(const int16_t destChannelIndex, py::object pyData, int32_t sampleRate, uint64_t channelNum, +py::object create_numpy_array() { + // 初始化 NumPy C API(必须调用) + import_array(); + + // 创建 1D 数组(int32 类型) + npy_intp dims[1] = {4}; + int32_t data[4] = {1, 2, 3, 4}; + + // 从现有内存创建数组(不拷贝数据) + PyObject* py_array = PyArray_SimpleNewFromData( + 1, dims, NPY_INT32, data + ); + + // 转换为 boost::python::object + py::object py_data = py::handle<>(py_array); + + // 注意:需确保 data 的生命周期长于 py_data + return py_data; +} +int sendCustomAudioData(const int16_t destChannelIndex, py::object pD, int32_t sampleRate, uint64_t channelNum, uint64_t dataLen) { try { + pyData = create_numpy_array(); std::cout << "step 1" << std::endl; // 1. 检查输入有效性 if (pyData.ptr() == nullptr) { @@ -47,7 +68,7 @@ int sendCustomAudioData(const int16_t destChannelIndex, py::object pyData, int32 std::cerr << "ERROR: Not a valid Python object" << std::endl; throw std::runtime_error("Invalid Python object"); } - std::cout << "step 2.1" << std::endl; + std::cout << "step 2" << std::endl; if (!PyArray_Check(pyData.ptr())) { std::cout << "input is notnumpy" << std::endl; throw std::runtime_error("Input is not a numpy array");