rtc_plugins/rtc_plugins.cpp

62 lines
2.0 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-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-09 18:24:55 +08:00
int sendCustomAudioData(const int16_t destChannelIndex, py::object pyData, int32_t sampleRate, uint64_t channelNum,
2025-04-09 16:18:50 +08:00
uint64_t dataLen) {
2025-04-10 09:07:06 +08:00
// 检查是否为 numpy 数组
if (!PyArray_Check(pyData.ptr())) {
PyErr_SetString(PyExc_TypeError, "Expected a numpy array");
throw py::error_already_set();
}
// 检查数据类型是否为 int16
PyArrayObject* npArray = (PyArrayObject*)pyData.ptr();
if (PyArray_TYPE(npArray) != NPY_INT16) {
PyErr_SetString(PyExc_TypeError, "Array must be of type int16 (np.int16)");
throw py::error_already_set();
}
void* dataPtr = PyArray_DATA(npArray);
if (dataPtr == nullptr) {
PyErr_SetString(PyExc_ValueError, "Invalid data pointer");
throw py::error_already_set();
}
2025-04-09 18:26:11 +08:00
return RTCContext::instance().sendCustomAudioData(destChannelIndex, dataPtr, sampleRate, channelNum, dataLen);
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);
}