diff --git a/rtc_plugins.cpp b/rtc_plugins.cpp index b70d486..e08b4e3 100644 --- a/rtc_plugins.cpp +++ b/rtc_plugins.cpp @@ -1,5 +1,6 @@ #include #include +#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #include #include @@ -33,24 +34,23 @@ int initSend(const char* destRoomId, const int16_t destChannelIndex) { } } py::object create_numpy_array() { - // 初始化 NumPy C API(必须调用) - import_array(); + // 确保已调用 import_array() + if (PyArray_API == nullptr) { + throw std::runtime_error("NumPy API not initialized"); + } - // 创建 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 - ); + PyObject* py_array = PyArray_SimpleNewFromData(1, dims, NPY_INT32, data); + if (!py_array) { + throw std::runtime_error("Failed to create NumPy array"); + } - // 转换为 boost::python::object - py::object py_data = py::handle<>(py_array); - - // 注意:需确保 data 的生命周期长于 py_data - return py_data; + return py::object(py::handle<>(py_array)); } + int sendCustomAudioData(const int16_t destChannelIndex, py::object pD, int32_t sampleRate, uint64_t channelNum, uint64_t dataLen) { try {