fix rtc send bug

This commit is contained in:
wangjiyu 2025-04-09 08:45:07 +08:00
parent bf66197322
commit 338be05383
5 changed files with 63 additions and 54 deletions

View File

@ -4,14 +4,17 @@ using namespace drogon;
int doSend(void* pdata, int32_t sampleRate, uint64_t channelNum, uint64_t dataLen)
{
LOG_DEBUG << "Sending custom audio data...";
int16_t retSend = RTCContext::instance().sendCustomAudioData(RTCContext::instance().outChannelIndex,
pdata, sampleRate, channelNum, dataLen);
int16_t retSend = RTCContext::instance().sendCustomAudioData(RTCContext::instance().destChannelIndex,
pdata, sampleRate, channelNum, dataLen);
if (0!= retSend)
{
LOG_DEBUG << "RTCContext::instance().sendCustomAudioData() failed, retSend:" << retSend;
return -1;
} else
{
LOG_DEBUG << "Sending custom audio data succ";
}
return 0;
}
int main()

View File

@ -27,19 +27,15 @@ int main()
do
{
if (RTCContext::instance().isOnRoom)
int16_t retSend = RTCContext::instance().sendCustomAudioData(RTCContext::instance().destChannelIndex,
(void*)"aaaaaaaaaaaaaaaaaa", 48000, 1, 10);
if (0!= retSend)
{
LOG_DEBUG << "Sending custom audio data...";
int16_t retSend = RTCContext::instance().sendCustomAudioData(RTCContext::instance().outChannelIndex,
(void*)"aaaaaaaaaaaaaaaaaaaaaaaaaaaa", 48000, 1, 10);
if (0!= retSend)
{
LOG_DEBUG << "RTCContext::instance().sendCustomAudioData() failed, retSend:" << retSend;
return -1;
} else
{
LOG_DEBUG << "Sending custom audio data succ";
}
LOG_DEBUG << "RTCContext::instance().sendCustomAudioData() failed, retSend:" << retSend;
return -1;
} else
{
LOG_DEBUG << "Sending custom audio data succ";
}
sleep(3);

View File

@ -22,8 +22,15 @@ void RTCContext::onRender(const char* roomId, const char* peerId,
LOG_DEBUG << "RTCContext::onRender()";
}
void RTCContext::onCallBackMessage(uint32_t msgId, const char* msg) {
//LOG_DEBUG << "RTCContext::onCallBackMessage(), msgId:" << msgId << ", msg:" << msg;
LOG_DEBUG << "RTCContext::onCallBackMessage()";
std::lock_guard<std::mutex> lock(mutex_);
if (msgId == (uint32_t)mrtc::JOIN_MULTI_ROOM_SUCCESS) {
LOG_DEBUG << "receive join multi room callback" << msgId;
isJoinMultiRoom = true;
}
LOG_DEBUG << "RTCContext::onCallBackMessage(), msgId:" << msgId << ", msg:" << msg;
//LOG_DEBUG << "RTCContext::onCallBackMessage()";
}
void RTCContext::onCallBackCustomData(RTCENGINE_NAMESPACE::MRTCCustomDataObject object) {
//LOG_DEBUG << "RTCContext::onCallBackCustomData(), obj:" << object.peerId << "," << object.data << "," << object.data_length;
@ -69,9 +76,13 @@ bool RTCContext::init()
LOG_DEBUG << "RTCContext::instance().init() failed";
return false;
}
if (0 != rtcEngine_->setUserInfo(inUserId, inDisplayName, inRoomId))
#ifdef SEND_MODE
if (0 != rtcEngine_->setUserInfo(srcUserId, srcDisplayName, srcRoomId))
#else
if (0 != rtcEngine_->setUserInfo(destUserId, destDisplayName, destRoomId))
#endif
{
LOG_DEBUG << "RTCContext::instance().setUserInfo() inUser failed";
LOG_DEBUG << "RTCContext::instance().setUserInfo() failed";
return false;
}
@ -105,15 +116,15 @@ bool RTCContext::init()
sleep(3);
}
LOG_DEBUG << "registerSoundLevelListener";
int16_t ret1 = rtcEngine_->registerSoundLevelListener(mrtc::TYPE_AUDIO_SOURCE_CUSTOM, inRoomId,
inUserId, inChannelIndex, this);
int16_t ret1 = rtcEngine_->registerSoundLevelListener(mrtc::TYPE_AUDIO_SOURCE_CUSTOM, destRoomId,
srcUserId, destChannelIndex, this);
if (0 != ret1)
{
LOG_DEBUG << "RTCContext::instance().registerSoundLevelListener() inUser failed, ret:" << ret1;
return false;
}
LOG_DEBUG << "muteAudio";
int16_t ret2 = rtcEngine_->muteAudio(inRoomId, inUserId, mrtc::TYPE_AUDIO_SOURCE_CUSTOM, false, inChannelIndex);
int16_t ret2 = rtcEngine_->muteAudio(destRoomId, srcUserId, mrtc::TYPE_AUDIO_SOURCE_CUSTOM, false, destChannelIndex);
if (0 != ret2)
{
LOG_DEBUG << "RTCContext::instance().muteAudio() failed, ret:" << ret2;
@ -131,7 +142,7 @@ bool RTCContext::init()
sleep(3);
}
LOG_DEBUG << "join multi room";
int16_t ret1 = rtcEngine_->joinMultiRoom(outRoomId);
int16_t ret1 = rtcEngine_->joinMultiRoom(destRoomId);
if (ret1 != 0)
{
LOG_DEBUG << "joinMultiRoom fail, ret:" << ret1;
@ -139,8 +150,8 @@ bool RTCContext::init()
}
mrtc::MRTCAudioOption option;
strcpy(option.dstRoomId, outRoomId);
option.channelIndex = outChannelIndex;
strcpy(option.dstRoomId, destRoomId);
option.channelIndex = destChannelIndex;
LOG_DEBUG << "startCustomAudio";
int16_t ret2 = rtcEngine_->startCustomAudio(option);
if (ret2 != 0)
@ -152,10 +163,9 @@ bool RTCContext::init()
return true;
}
void RTCContext::destorySend(bool isIn)
void RTCContext::destorySend()
{
int16_t channelIndex = isIn ? inChannelIndex : outChannelIndex;
rtcEngine_->stopCustomAudio(channelIndex);
rtcEngine_->stopCustomAudio(destChannelIndex);
}
int16_t RTCContext::sendAudioData(uint8_t channelIndex, const void* pData, int32_t nSampleRate, uint64_t nNumberOfChannels, uint64_t dataLength)
{
@ -169,7 +179,11 @@ bool RTCContext::init()
int16_t RTCContext::sendCustomAudioData(const int16_t channelIndex, void* customData, int32_t sampleRate,
uint64_t channelNum, uint64_t dataLen)
{
std::lock_guard<std::mutex> lock(mutex_);
while(!isOnRoom || !isJoinMultiRoom) {
LOG_DEBUG << "wait for room and multi room before send";
sleep(3);
}
std::lock_guard<std::mutex> lock(mutex_);
return rtcEngine_->sendCustomAudioData(channelIndex, customData, sampleRate, channelNum, dataLen);
}
mrtc::IMRTCEngine* RTCContext::getRtcEngine() const

View File

@ -1,11 +1,9 @@
#include <iostream>
#include "IMRTCEngineFactory.hpp"
#include "IMRTCEngine.hpp"
#include "MRTCEngineDefine.hpp"
#include "IMRTCEngineFactory.hpp"
#include <mutex>
#include <cstring>
#include <filesystem> // C++17
#include "trantor/utils/Logger.h"
namespace fs = std::filesystem;
#define ENV_PRODUCT
//#define SEND_MODE
@ -31,24 +29,18 @@ public:
const unsigned short port = 34443;
#endif
#ifdef SEND_MODE
const char* inUserId = "inuser1";
#else
const char* inUserId = "inuser2";
#endif
const char* srcUserId = "srcUser1";
const char* destUserId = "destUser1";
const char* inDisplayName = "indisplayName";
const char* outRoomId = "outRoomId1";
const int16_t outChannelIndex = 43;
#ifdef SEND_MODE
const char* inRoomId = "inRoomId1";
const int16_t inChannelIndex = 42;
#else
const char* inRoomId = outRoomId;
const int16_t inChannelIndex = outChannelIndex;
#endif
const char* srcDisplayName = "srcDisplayName1";
const char* destDisplayName = "destDisplayName1";
const char* srcRoomId = "srcRoom1";
const char* destRoomId = "destRoomId1";
const int16_t srcChannelIndex = 42;
const int16_t destChannelIndex = 43;
bool isOnRoom = false;
bool isOnConsumer = false;
bool isJoinMultiRoom = false;
static RTCContext& instance()
{
@ -68,7 +60,7 @@ public:
int16_t sendAudioData(uint8_t channelIndex = 0, const void* pData = nullptr, int32_t nSampleRate = 48000, uint64_t nNumberOfChannels = 2, uint64_t dataLength = 0);
int16_t sendCustomAudioData(const int16_t channelIndex, void* customData, int32_t sampleRate,
uint64_t channelNum, uint64_t dataLen);
void destorySend(bool isIn);
void destorySend();
private:
RTCContext()

View File

@ -35,7 +35,6 @@ int16_t sendRTC(std::string &&message) {
LOG_DEBUG << "message is empty";
return 0;
}
LOG_DEBUG << "@@@@@@@@@@@@@@@@@@@@ begin @@@@@@@@@@@@@@@@@@@" << message;
mrtc::IMRTCEngine *rtcEngine = RTCContext::instance().getRtcEngine();
if (!rtcEngine)
{
@ -43,6 +42,7 @@ int16_t sendRTC(std::string &&message) {
return -1;
}
/*
int32_t nCount = 0;
if (0 != rtcEngine->getUserCount(nCount))
{
@ -51,13 +51,17 @@ int16_t sendRTC(std::string &&message) {
}
LOG_DEBUG << "nCount: " << nCount;
if (0 != RTCContext::instance().sendCustomAudioData(RTCContext::instance().outChannelIndex,
(void*)"aaaaaaaaaaaaaaaaaaaaaaaaaaaa", 48000, 1, 10))
*/
int16_t retSend = RTCContext::instance().sendCustomAudioData(RTCContext::instance().destChannelIndex,
(void*)"aaaaaaaaaaaaaaaaaa", 48000, 1, 10);
if (0!= retSend)
{
LOG_DEBUG << "RTCContext::instance().sendCustomAudioData() failed";
return -1;
LOG_DEBUG << "RTCContext::instance().sendCustomAudioData() failed, retSend:" << retSend;
return -1;
} else
{
LOG_DEBUG << "Sending custom audio data succ";
}
LOG_DEBUG << "@@@@@@@@@@@@@@@@@@@@ end @@@@@@@@@@@@@@@@@@@" << message;
return 0;
}