2025-05-03 21:51:02 +08:00
|
|
|
|
#define IMPLEMENT_NUMPY_API
|
|
|
|
|
#include "util/RTCContext.h"
|
|
|
|
|
#include <pybind11/pybind11.h>
|
|
|
|
|
#include <pybind11/numpy.h> // pybind11 的 NumPy 支持
|
|
|
|
|
#include <pybind11/detail/common.h>
|
2025-04-10 16:54:02 +08:00
|
|
|
|
|
2025-05-03 21:51:02 +08:00
|
|
|
|
namespace py = pybind11;
|
2025-04-10 18:14:30 +08:00
|
|
|
|
#include "util/RTCContext.h"
|
2025-04-10 17:47:00 +08:00
|
|
|
|
|
2025-05-03 21:51:02 +08:00
|
|
|
|
// 初始化 NumPy(适配 pybind11)
|
|
|
|
|
void init_numpy() {
|
|
|
|
|
if (import_array() < 0) {
|
|
|
|
|
throw py::bind_already_set(); // 自动捕获 NumPy 初始化错误
|
|
|
|
|
}
|
|
|
|
|
std::cout << "NumPy API addr: " << PyArray_API << std::endl;
|
|
|
|
|
}
|
2025-04-09 16:18:50 +08:00
|
|
|
|
|
2025-05-03 21:51:02 +08:00
|
|
|
|
int init(const char* selfUserId, const char* selfDisplayName, const char* selfRoomId, py::object callback) {
|
2025-04-10 16:40:29 +08:00
|
|
|
|
if (!PyArray_API) {
|
|
|
|
|
std::cout << "PyArray_API is null in outer init" << std::endl;
|
|
|
|
|
} else {
|
|
|
|
|
std::cout << "PyArray_API is not null in outer init" << std::endl;
|
|
|
|
|
}
|
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) {
|
2025-04-10 16:40:29 +08:00
|
|
|
|
if (!PyArray_API) {
|
|
|
|
|
std::cout << "PyArray_API is null in outer initRecv" << std::endl;
|
|
|
|
|
} else {
|
|
|
|
|
std::cout << "PyArray_API is not null in outer initRecv" << std::endl;
|
|
|
|
|
}
|
2025-04-09 16:18:50 +08:00
|
|
|
|
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-15 23:21:48 +08:00
|
|
|
|
int initSend(const char* srcRoomId, const char* destRoomId, const int16_t destChannelIndex, const int16_t channelNum) {
|
|
|
|
|
bool res = RTCContext::instance().initSend(srcRoomId, destRoomId, destChannelIndex, channelNum);
|
2025-04-09 16:18:50 +08:00
|
|
|
|
if (res) {
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-03 21:51:02 +08:00
|
|
|
|
// NumPy 数据交互(关键修改)
|
|
|
|
|
py::array_t<int16_t> getNumpyData() {
|
|
|
|
|
return py::array_t<int16_t>(
|
|
|
|
|
RTCContext::instance().getNumpyData() // 假设返回的是已有 NumPy 数组
|
2025-04-10 10:29:57 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
2025-04-10 10:38:57 +08:00
|
|
|
|
|
2025-05-03 21:51:02 +08:00
|
|
|
|
int sendCustomAudioData(int16_t destChannelIndex, py::array_t<int16_t> inputArray,
|
2025-04-10 10:37:11 +08:00
|
|
|
|
int32_t sampleRate, uint64_t channelNum, uint64_t dataLen) {
|
2025-05-03 21:51:02 +08:00
|
|
|
|
py::gil_scoped_release release;
|
2025-04-10 10:37:11 +08:00
|
|
|
|
|
2025-05-03 21:51:02 +08:00
|
|
|
|
auto buf = inputArray.request();
|
|
|
|
|
if (buf.size != dataLen) {
|
|
|
|
|
throw py::value_error("Array length does not match dataLen");
|
2025-04-10 10:37:11 +08:00
|
|
|
|
}
|
2025-05-03 21:51:02 +08:00
|
|
|
|
|
|
|
|
|
return RTCContext::instance().sendCustomAudioData(
|
|
|
|
|
destChannelIndex, buf.ptr, sampleRate, channelNum, dataLen
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
py::list getListData() {
|
|
|
|
|
return RTCContext::instance().getListData();
|
|
|
|
|
}
|
|
|
|
|
int getSize() {
|
|
|
|
|
return RTCContext::instance().getSize();
|
2025-04-10 10:37:11 +08:00
|
|
|
|
}
|
2025-04-16 15:59:45 +08:00
|
|
|
|
RetAudioFrame getData() {
|
|
|
|
|
return RTCContext::instance().getData();
|
|
|
|
|
}
|
2025-05-03 21:51:02 +08:00
|
|
|
|
int16_t getDataCount() {
|
|
|
|
|
return RTCContext::instance().getDataCount();
|
2025-04-09 16:18:50 +08:00
|
|
|
|
}
|
2025-04-10 10:16:21 +08:00
|
|
|
|
|
2025-05-03 21:51:02 +08:00
|
|
|
|
PYBIND11_MODULE(rtc_plugins, m) {
|
|
|
|
|
init_numpy();
|
|
|
|
|
// 可选:暴露 RetAudioFrame 类(需额外绑定)
|
|
|
|
|
py::class_<RetAudioFrame>(m, "RetAudioFrame")
|
|
|
|
|
.def_readwrite("data", &RetAudioFrame::data)
|
|
|
|
|
.def_readwrite("dataCount", &RetAudioFrame::dataCount)
|
|
|
|
|
.def_readwrite("sampleRate", &RetAudioFrame::sampleRate)
|
|
|
|
|
.def_readwrite("numChannels", &RetAudioFrame::numChannels)
|
|
|
|
|
.def_readwrite("channelIndex", &RetAudioFrame::channelIndex);
|
|
|
|
|
m.def("init", &init);
|
|
|
|
|
m.def("initRecv", &initRecv);
|
|
|
|
|
m.def("initSend", &initSend);
|
|
|
|
|
m.def("sendCustomAudioData", &sendCustomAudioData);
|
|
|
|
|
m.def("getSize", &getSize);
|
|
|
|
|
m.def("getData", &getData);
|
|
|
|
|
m.def("getNumpyData", &getNumpyData);
|
|
|
|
|
m.def("getListData", &getListData);
|
|
|
|
|
m.def("getDataCount", &getDataCount);
|
2025-04-10 10:23:57 +08:00
|
|
|
|
|
2025-04-10 16:26:44 +08:00
|
|
|
|
|
2025-04-09 16:18:50 +08:00
|
|
|
|
}
|