rtc_plugins/rtc_plugins.cpp

105 lines
3.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "util/RTCContext.h"
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h> // pybind11 的 NumPy 支持
namespace py = pybind11;
#include "util/RTCContext.h"
int init(const char* selfUserId, const char* selfDisplayName, const char* selfRoomId, py::object 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;
}
}
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);
if (res) {
return 0;
} else {
return -1;
}
}
// NumPy 数据交互(关键修改)
py::array_t<int16_t> getNumpyData() {
return py::array_t<int16_t>(
RTCContext::instance().getNumpyData() // 假设返回的是已有 NumPy 数组
);
}
int sendCustomAudioData(int16_t destChannelIndex, py::array_t<int16_t> inputArray,
int32_t sampleRate, uint64_t channelNum, uint64_t dataLen) {
py::gil_scoped_release release;
auto buf = inputArray.request();
if (buf.size != dataLen) {
throw py::value_error("Array length does not match dataLen");
}
return RTCContext::instance().sendCustomAudioData(
destChannelIndex, buf.ptr, sampleRate, channelNum, dataLen
);
}
py::list getListData() {
return RTCContext::instance().getListData();
}
int getSize() {
return RTCContext::instance().getSize();
}
RetAudioFrame getData() {
return RTCContext::instance().getData();
}
int16_t getDataCount() {
return RTCContext::instance().getDataCount();
}
PYBIND11_MODULE(rtc_plugins, m) {
// 可选:暴露 RetAudioFrame 类(需额外绑定)
py::class_<RetAudioFrame>(m, "RetAudioFrame")
// 定义 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);
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);
}