This commit is contained in:
wangjiyu 2025-05-28 15:26:24 +08:00
parent 392ec2416b
commit 49c540a78e
1 changed files with 42 additions and 24 deletions

View File

@ -1,4 +1,5 @@
#include "RTCContext.h"
#include <fstream>
void RTCContext::onRoom(uint32_t typeId, RTCENGINE_NAMESPACE::MRTCRoomInfo& roomInfo) {
//std::cout << "RTCContext::onRoom():" << roomInfo.roomId << "," << roomInfo.displayName << "," << roomInfo.userId << "," << roomInfo.message;
@ -7,31 +8,32 @@ void RTCContext::onRoom(uint32_t typeId, RTCENGINE_NAMESPACE::MRTCRoomInfo& room
isOnRoom_ = true;
}
void RTCContext::onConsumer(uint32_t msgId, const char* roomId, const char* peerId, RTCENGINE_NAMESPACE::MRTCConsumerInfo& consumerInfo) {
//std::cout << "RTCContext::onConsumer()" << std::endl;
std::cout << "RTCContext::onConsumer()" << std::endl;
std::cout << "RTCContext::onConsumer():msgId:" << msgId << ", roomId:" << consumerInfo.roomId << ", displayName:"
<< consumerInfo.displayName << ", channelIndex" << (int)consumerInfo.channelIndex;
<< consumerInfo.displayName << ", type:" << consumerInfo.audioSourceType << ", channelIndex:" << (int)consumerInfo.channelIndex
<< std::endl;
if (msgId == RTCENGINE_NAMESPACE::MRTCConsumerStatusType::TYPE_STATUS_CONSUMER_START && std::string(consumerInfo.kind) == "audio") {
std::lock_guard <std::mutex> lock(mutex_);
isOnConsumer_ = true;
std::cout << "registerSoundLevelListener" << std::endl;
int16_t ret1 = rtcEngine_->registerSoundLevelListener(mrtc::TYPE_AUDIO_SOURCE_CUSTOM, roomId,
int16_t ret1 = rtcEngine_->registerSoundLevelListener(consumerInfo.audioSourceType, roomId,
peerId, consumerInfo.channelIndex, this);
if (0 != ret1)
{
if (0 != ret1) {
std::cout << "RTCContext::instance().registerSoundLevelListener() inUser failed, ret:" << ret1;
return;
}
std::cout << "muteAudio" << std::endl;
int16_t ret2 = rtcEngine_->muteAudio(roomId, peerId, mrtc::TYPE_AUDIO_SOURCE_CUSTOM,
int16_t ret2 = rtcEngine_->muteAudio(roomId, peerId, consumerInfo.audioSourceType,
false, consumerInfo.channelIndex);
if (0 != ret2)
{
if (0 != ret2) {
std::cout << "RTCContext::instance().muteAudio() failed, ret:" << ret2;
return;
}
std::cout << "init recv succ" << std::endl;
}
}
void RTCContext::onRender(const char* roomId, const char* peerId,
RTCENGINE_NAMESPACE::MRTCVideoSourceType sourceType, const RTCENGINE_NAMESPACE::MRTCVideoFrame& videoFrame) {
std::cout << "RTCContext::onRender()" << std::endl;
@ -54,7 +56,7 @@ void RTCContext::onCallBackCustomData(RTCENGINE_NAMESPACE::MRTCCustomDataObject
void RTCContext::onSoundLevelUpdate(const char* roomId, const char* peerId, uint16_t audioSourceType,
uint8_t channelIndex, uint16_t volume, int32_t vad)
{
std::cout << "RTCContext::onSoundLevelUpdate()" << std::endl;
//std::cout << "RTCContext::onSoundLevelUpdate()" << std::endl;
}
void printTimestamp() {
@ -75,6 +77,21 @@ void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
mrtc::MRTCAudioFrame& audioFrame,
mrtc::MRTCAudioSourceType audioSourceType)
{
//std::cout << "onAudioProcess, roomId:" << roomId << ", peerId:" << peerId << ", type:" << audioSourceType
// << ", datacount:" << audioFrame.dataCount << std::endl;
if (audioFrame.data == nullptr || audioFrame.dataCount <= 0) {
return;
}
// 以二进制追加模式打开文件
//std::ofstream outFile("audio_data.pcm", std::ios::binary | std::ios::app);
//if (!outFile.is_open()) {
// std::cerr << "Failed to open file for writing!" << std::endl;
// return;
//}
//// 写入数据int16_t格式
//outFile.write(reinterpret_cast<const char*>(audioFrame.data),
// audioFrame.dataCount);
setData(audioFrame);
}
@ -237,7 +254,8 @@ void RTCContext::setData(const mrtc::MRTCAudioFrame& frame) {
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));
//std::memcpy(newFrame.data.get(), frame.data, frame.dataCount* sizeof(int16_t));
std::memcpy(newFrame.data.get(), frame.data, frame.dataCount);
data_[head_] = std::move(newFrame);
head_ = (head_ + 1) % totalSize_;
dataSize_++;
@ -280,7 +298,7 @@ py::list RTCContext::getListData() {
RetAudioFrame frame = getData();
py::list result;
if (frame.data) {
for (int i = 0; i < frame.dataCount; i++) {
for (int i = 0; i < frame.dataCount/sizeof(int16_t); i++) {
result.append(frame.data.get()[i]);
}
}