2025-05-03 21:51:02 +08:00
|
|
|
|
#include "util/RTCContext.h"
|
|
|
|
|
#include <pybind11/pybind11.h>
|
|
|
|
|
#include <pybind11/numpy.h> // pybind11 的 NumPy 支持
|
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-04 10:58:59 +08:00
|
|
|
|
int init(const char* selfUserId, const char* selfDisplayName, const char* selfRoomId) {
|
2025-04-09 16:18:50 +08:00
|
|
|
|
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-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-04 11:14:22 +08:00
|
|
|
|
py::array_t<int16_t> contiguous = py::array::ensure(inputArray);
|
|
|
|
|
if (!contiguous) throw py::value_error("Array conversion failed");
|
|
|
|
|
auto buf = contiguous.request();
|
2025-05-03 21:51:02 +08:00
|
|
|
|
if (buf.size != dataLen) {
|
|
|
|
|
throw py::value_error("Array length does not match dataLen");
|
2025-04-10 10:37:11 +08:00
|
|
|
|
}
|
2025-05-04 11:14:22 +08:00
|
|
|
|
std::vector<int16_t> localCopy(static_cast<int16_t*>(buf.ptr),
|
|
|
|
|
static_cast<int16_t*>(buf.ptr) + buf.size);
|
2025-05-03 21:51:02 +08:00
|
|
|
|
|
|
|
|
|
return RTCContext::instance().sendCustomAudioData(
|
2025-05-04 11:14:22 +08:00
|
|
|
|
destChannelIndex, localCopy.data(), sampleRate, channelNum, dataLen
|
2025-05-03 21:51:02 +08:00
|
|
|
|
);
|
2025-05-04 11:14:22 +08:00
|
|
|
|
//return RTCContext::instance().sendCustomAudioData(
|
|
|
|
|
// destChannelIndex, buf.ptr, sampleRate, channelNum, dataLen
|
|
|
|
|
//);
|
2025-05-03 21:51:02 +08:00
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
// 可选:暴露 RetAudioFrame 类(需额外绑定)
|
|
|
|
|
py::class_<RetAudioFrame>(m, "RetAudioFrame")
|
2025-05-03 23:32:21 +08:00
|
|
|
|
// 定义 data 属性(包含 getter 和 setter)
|
|
|
|
|
.def_property("data",
|
|
|
|
|
// Getter:返回 NumPy 数组
|
|
|
|
|
[](RetAudioFrame& self) {
|
|
|
|
|
return py::array_t<short>(
|
|
|
|
|
{self.dataCount}, // 数组形状
|
|
|
|
|
{sizeof(short)}, // 步长
|
|
|
|
|
self.data.get() // 数据指针(原始内存)
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
// Setter:从 NumPy 数组复制数据
|
|
|
|
|
[](RetAudioFrame& self, py::array_t<short> arr) {
|
|
|
|
|
auto buf = arr.request(); // 获取数组信息
|
|
|
|
|
self.data.reset(new short[buf.size]); // 重新分配内存
|
|
|
|
|
std::memcpy(
|
|
|
|
|
self.data.get(), // 目标指针
|
|
|
|
|
buf.ptr, // 源数据指针
|
|
|
|
|
buf.size * sizeof(short) // 数据大小
|
|
|
|
|
);
|
|
|
|
|
self.dataCount = buf.size; // 更新数据长度
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.def_readwrite("dataCount", &RetAudioFrame::dataCount)
|
|
|
|
|
.def_readwrite("sampleRate", &RetAudioFrame::sampleRate)
|
|
|
|
|
.def_readwrite("numChannels", &RetAudioFrame::numChannels)
|
|
|
|
|
.def_readwrite("channelIndex", &RetAudioFrame::channelIndex);
|
2025-05-03 21:51:02 +08:00
|
|
|
|
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
|
|
|
|
}
|