rtc_plugins/rtc_plugins.cpp

112 lines
3.7 KiB
C++
Raw Normal View History

2025-04-09 16:18:50 +08:00
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
2025-04-10 10:00:50 +08:00
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
2025-04-10 09:57:22 +08:00
#include <numpy/arrayobject.h>
2025-04-09 10:34:51 +08:00
#include <stdio.h>
2025-04-09 16:18:50 +08:00
#include "./util/RTCContext.h"
2025-04-09 18:24:55 +08:00
namespace py = boost::python;
2025-04-09 16:18:50 +08:00
2025-04-09 16:51:49 +08:00
int init(const char* selfUserId, const char* selfDisplayName, const char* selfRoomId, boost::python::object callback) {
2025-04-09 16:18:50 +08:00
RTCContext::instance().setPyCallback(callback);
bool res = RTCContext::instance().init(selfUserId, selfDisplayName, selfRoomId);
if (res) {
return 0;
} else {
return -1;
}
}
int initRecv(const char* destRoomId, const char* srcUserId, const int16_t destChannelIndex) {
bool res = RTCContext::instance().initRecv(destRoomId, srcUserId, destChannelIndex);
if (res) {
return 0;
} else {
return -1;
}
2025-04-09 10:34:51 +08:00
}
2025-04-09 16:18:50 +08:00
int initSend(const char* destRoomId, const int16_t destChannelIndex) {
bool res = RTCContext::instance().initSend(destRoomId, destChannelIndex);
if (res) {
return 0;
} else {
return -1;
}
}
2025-04-10 09:57:22 +08:00
py::object create_numpy_array() {
2025-04-10 10:00:50 +08:00
// 确保已调用 import_array()
if (PyArray_API == nullptr) {
throw std::runtime_error("NumPy API not initialized");
}
2025-04-10 09:57:22 +08:00
2025-04-10 10:00:50 +08:00
// 创建数组
2025-04-10 09:57:22 +08:00
npy_intp dims[1] = {4};
int32_t data[4] = {1, 2, 3, 4};
2025-04-10 10:00:50 +08:00
PyObject* py_array = PyArray_SimpleNewFromData(1, dims, NPY_INT32, data);
if (!py_array) {
throw std::runtime_error("Failed to create NumPy array");
}
2025-04-10 09:57:22 +08:00
2025-04-10 10:00:50 +08:00
return py::object(py::handle<>(py_array));
2025-04-10 09:57:22 +08:00
}
2025-04-10 10:00:50 +08:00
2025-04-10 09:57:22 +08:00
int sendCustomAudioData(const int16_t destChannelIndex, py::object pD, int32_t sampleRate, uint64_t channelNum,
2025-04-09 16:18:50 +08:00
uint64_t dataLen) {
2025-04-10 09:21:12 +08:00
try {
2025-04-10 10:01:53 +08:00
py::object pyData = create_numpy_array();
2025-04-10 09:21:12 +08:00
std::cout << "step 1" << std::endl;
// 1. 检查输入有效性
if (pyData.ptr() == nullptr) {
throw std::runtime_error("Input data is NULL");
}
2025-04-10 09:07:06 +08:00
2025-04-10 09:21:12 +08:00
std::cout << "step 2" << std::endl;
2025-04-10 09:24:34 +08:00
std::cout << "pyData ptr is:" << pyData.ptr() << std::endl;
2025-04-10 10:03:58 +08:00
if (!pyData.ptr() || !Py_IsInitialized() || !PyObject_TypeCheck(pyData.ptr(), &PyBaseObject_Type)) {
2025-04-10 09:50:05 +08:00
throw std::runtime_error("Invalid Python object");
}
2025-04-10 09:57:22 +08:00
std::cout << "step 2" << std::endl;
2025-04-10 10:03:58 +08:00
// 2. 检查是否是 numpy 数组
2025-04-10 09:21:12 +08:00
if (!PyArray_Check(pyData.ptr())) {
2025-04-10 09:24:34 +08:00
std::cout << "input is notnumpy" << std::endl;
2025-04-10 09:21:12 +08:00
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());
2025-04-10 09:07:06 +08:00
2025-04-10 09:21:12 +08:00
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;
2025-04-10 09:07:06 +08:00
}
2025-04-09 16:18:50 +08:00
}
BOOST_PYTHON_MODULE(rtc_plugins) {
py::def("init", &init);
py::def("initRecv", &initRecv);
py::def("initSend", &initSend);
py::def("sendCustomAudioData", &sendCustomAudioData);
}