diff --git a/util/RTCContext.cpp b/util/RTCContext.cpp index a12c180..4de5bfa 100644 --- a/util/RTCContext.cpp +++ b/util/RTCContext.cpp @@ -1,4 +1,5 @@ #include "RTCContext.h" +#include void RTCContext::onRoom(uint32_t typeId, RTCENGINE_NAMESPACE::MRTCRoomInfo& roomInfo) { //std::cout << "RTCContext::onRoom():" << roomInfo.roomId << "," << roomInfo.displayName << "," << roomInfo.userId << "," << roomInfo.message; @@ -7,30 +8,31 @@ 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; - std::lock_guard lock(mutex_); - isOnConsumer_ = true; - std::cout << "registerSoundLevelListener" << std::endl; - int16_t ret1 = rtcEngine_->registerSoundLevelListener(mrtc::TYPE_AUDIO_SOURCE_CUSTOM, roomId, - peerId, consumerInfo.channelIndex, this); - if (0 != ret1) - { - std::cout << "RTCContext::instance().registerSoundLevelListener() inUser failed, ret:" << ret1; - return; - } + << 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 lock(mutex_); + isOnConsumer_ = true; + std::cout << "registerSoundLevelListener" << std::endl; + int16_t ret1 = rtcEngine_->registerSoundLevelListener(consumerInfo.audioSourceType, roomId, + peerId, consumerInfo.channelIndex, this); + 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, - false, consumerInfo.channelIndex); - if (0 != ret2) - { - std::cout << "RTCContext::instance().muteAudio() failed, ret:" << ret2; - return; - } + std::cout << "muteAudio" << std::endl; + int16_t ret2 = rtcEngine_->muteAudio(roomId, peerId, consumerInfo.audioSourceType, + false, consumerInfo.channelIndex); + if (0 != ret2) { + std::cout << "RTCContext::instance().muteAudio() failed, ret:" << ret2; + return; + } - std::cout << "init recv succ" << std::endl; + 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) { @@ -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(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(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]); } }