This commit is contained in:
wangjiyu 2025-04-16 15:30:55 +08:00
parent 088f373770
commit 0042f506c2
4 changed files with 61 additions and 0 deletions

View File

@ -47,6 +47,9 @@ int initSend(const char* srcRoomId, const char* destRoomId, const int16_t destCh
return -1;
}
}
int getSize() {
return RTCContext::instance().getSize();
}
py::object create_int16_array() {
// 1. 定义数组维度1维长度为 4
@ -196,6 +199,7 @@ BOOST_PYTHON_MODULE(rtc_plugins) {
py::def("initRecv", &initRecv);
py::def("initSend", &initSend);
py::def("sendCustomAudioData", &sendCustomAudioData);
py::def("getSize", &getSize);
} catch (...) {
PyErr_SetString(PyExc_RuntimeError, "Module initialization failed");
}

View File

@ -56,4 +56,6 @@ while True:
print(f"resend fail, ret:{ret}")
else:
print("resend succ")
size = rtc_plugins.getSize()
print(f"data size:{size}")
time.sleep(30)

View File

@ -301,6 +301,7 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
std::cout << "audioFrame:" << audioFrame.dataCount << "," << audioFrame.sampleRate << "," <<
audioFrame.numChannels << "," << audioFrame.channelIndex << std::endl;
setData(audioFrame);
// 1. 获取GIL
std::cout << "[1] 获取GIL锁..." << std::endl;
//PyGILState_STATE gstate = PyGILState_Ensure();
@ -654,3 +655,37 @@ void RTCContext::setNumpyApi(void **numpyApi) {
numpyApi_ = numpyApi;
std::cout << "setNupyApi, numpyApi_:" << numpyApi_[93] << std::endl;
}
void RTCContext::setData(const mrtc::MRTCAudioFrame& frame) {
std::lock_guard<std::mutex> lock(dataMutex_);
if (dataSize_ == totalSize_) {
bottom_ = (bottom_ + 1) % totalSize_;
dataSize_--;
}
RetAudioFrame newFrame;
newFrame.dataCount = frame.dataCount;
newFrame.sampleRate = frame.sampleRate;
newFrame.numChannels = frame.numChannels;
newFrame.channelIndex = frame.channelIndex;
newFrame.data = std::make_unique<int16_t[]>(frame.dataCount);
std::memcpy(newFrame.data.get(), frame.data, frame.dataCount* sizeof(int16_t));
data_[head_] = std::move(newFrame);
head_ = (head_ + 1) % totalSize_;
dataSize_++;
}
RetAudioFrame RTCContext::getData() {
std::lock_guard<std::mutex> lock(dataMutex_);
if (dataSize_ > 0) {
RetAudioFrame frame = std::move(data_[bottom_]); // 移动而非拷贝
bottom_ = (bottom_ + 1) % totalSize_;
dataSize_--;
return frame; // 返回值优化(RVO)会生效
}
return {}; // 返回空对象
}
int16_t RTCContext::getSize() {
std::lock_guard<std::mutex> lock(dataMutex_);
return dataSize;
}

View File

@ -35,6 +35,16 @@ namespace fs = std::filesystem;
#define ENV_PRODUCT
//#define SEND_MODE
// 音频数据帧
struct RetAudioFrame final
{
std::unique_ptr<int16_t[]> data;
int dataCount = 0;
int sampleRate = 48000;
int numChannels = 1;
int channelIndex = 0;
};
class RTCContext :
public RTCENGINE_NAMESPACE::IMRTCRoomCallBack,
public RTCENGINE_NAMESPACE::IMRTCConsumerCallBack,
@ -85,6 +95,7 @@ public:
bool init(const char* selfUserId, const char* selfDisplayName, const char* selfRoomId);
bool initRecv(const char* destRoomId, const char* srcUserId, const int16_t destChannelIndex);
bool initSend(const char* srcRoomId, const char* destRoomId, const int16_t destChannelIndex, const uint8_t channelNum);
int16_t getSize();
void* getpData() const;
void setpData(void* pData);
@ -99,6 +110,7 @@ public:
private:
RTCContext()
{
data_.resize(totalSize_);
}
mutable std::mutex mutex_;
mrtc::IMRTCEngine * rtcEngine_ = nullptr;
@ -109,6 +121,12 @@ private:
bool isMultiRoom_ = false;
boost::python::object pyCallback_;
void ** numpyApi_;
std::vector<RetAudioFrame> data_;
mutable std::mutex dataMutex_;
const int16_t totalSize_ = 100;
int16_t dataSize_ = 0;
int16_t bottom_ = 0;
int16_t head_= 0;
void onRoom(uint32_t typeId, RTCENGINE_NAMESPACE::MRTCRoomInfo& roomInfo);
void onConsumer(uint32_t msgId, const char* roomId, const char* peerId, RTCENGINE_NAMESPACE::MRTCConsumerInfo& consumerInfo);
void onRender(const char* roomId, const char* peerId,
@ -119,4 +137,6 @@ private:
void onAudioProcess(const char* roomId, const char* peerId,
mrtc::MRTCAudioFrame& audioFrame, mrtc::MRTCAudioSourceType audioSourceType);
void onProducer(uint32_t msgId, mrtc::MRTCProducerInfo& info);
void setData(const mrtc::MRTCAudioFrame& frame);
RetAudioFrame getData();
};