Compare commits

...

4 Commits

Author SHA1 Message Date
wangjiyu 7740e1b220 debug 2025-05-03 20:03:22 +08:00
wangjiyu df4a1193b6 debug 2025-05-03 20:01:47 +08:00
wangjiyu a2f2082f54 debug 2025-05-03 19:59:46 +08:00
wangjiyu bbac7269cd debug 2025-05-03 19:45:44 +08:00
5 changed files with 73 additions and 34 deletions

View File

@ -32,7 +32,7 @@ int main()
LOG_DEBUG << "RTCContext init failed";
return -1;
}
if (!RTCContext::instance().initSend(destRoomId, destChannelIndex)) {
if (!RTCContext::instance().initSend(srcRoomId, destRoomId, destChannelIndex, 1)) {
LOG_DEBUG << "RTCContext initSend failed";
return -1;
}

View File

@ -20,7 +20,7 @@ int main()
LOG_DEBUG << "RTCContext init failed";
return -1;
}
if (!RTCContext::instance().initSend(destRoomId, destChannelIndex)) {
if (!RTCContext::instance().initSend(srcRoomId, destRoomId, destChannelIndex, 1)) {
LOG_DEBUG << "RTCContext initSend failed";
return -1;
}

View File

@ -2,9 +2,6 @@
#include "drogon/drogon_test.h"
#include <iostream>
#include <unistd.h>
void RTCContext::onRoom(uint32_t typeId, RTCENGINE_NAMESPACE::MRTCRoomInfo& roomInfo) {
//LOG_DEBUG << "RTCContext::onRoom():" << roomInfo.roomId << "," << roomInfo.displayName << "," << roomInfo.userId << "," << roomInfo.message;
LOG_DEBUG << "RTCContext::onRoom()";
@ -12,10 +9,30 @@ 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) {
LOG_DEBUG << "RTCContext::onConsumer():" << consumerInfo.roomId << "," << consumerInfo.displayName << "," << consumerInfo.channelIndex;
//LOG_DEBUG << "RTCContext::onConsumer()";
LOG_DEBUG << "RTCContext::onConsumer():msgId:" << msgId << ", roomId:" << consumerInfo.roomId << ", displayName:"
<< consumerInfo.displayName << ", channelIndex" << consumerInfo.channelIndex;
std::lock_guard<std::mutex> lock(mutex_);
isOnConsumer_ = true;
//LOG_DEBUG << "RTCContext::onConsumer()";
LOG_DEBUG << "registerSoundLevelListener";
int16_t ret1 = rtcEngine_->registerSoundLevelListener(mrtc::TYPE_AUDIO_SOURCE_CUSTOM, roomId,
peerId, consumerInfo.channelIndex, this);
if (0 != ret1)
{
LOG_DEBUG << "RTCContext::instance().registerSoundLevelListener() inUser failed, ret:" << ret1;
return;
}
LOG_DEBUG << "muteAudio";
int16_t ret2 = rtcEngine_->muteAudio(roomId, peerId, mrtc::TYPE_AUDIO_SOURCE_CUSTOM,
false, consumerInfo.channelIndex);
if (0 != ret2)
{
LOG_DEBUG << "RTCContext::instance().muteAudio() failed, ret:" << ret2;
return;
}
LOG_DEBUG << "init recv succ";
}
void RTCContext::onRender(const char* roomId, const char* peerId,
RTCENGINE_NAMESPACE::MRTCVideoSourceType sourceType, const RTCENGINE_NAMESPACE::MRTCVideoFrame& videoFrame) {
@ -111,43 +128,35 @@ bool RTCContext::initRecv(const char* destRoomId, const char* srcUserId, const i
LOG_DEBUG << "wait for OnConsumer";
sleep(3);
}
LOG_DEBUG << "registerSoundLevelListener";
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(destRoomId, srcUserId, mrtc::TYPE_AUDIO_SOURCE_CUSTOM, false, destChannelIndex);
if (0 != ret2)
{
LOG_DEBUG << "RTCContext::instance().muteAudio() failed, ret:" << ret2;
return false;
}
LOG_DEBUG << "init recv succ";
return true;
}
bool RTCContext::initSend(const char* destRoomId, const int16_t destChannelIndex)
bool RTCContext::initSend(const char* srcRoomId, const char* destRoomId, const int16_t destChannelIndex,
const uint8_t channelNum)
{
while (!isOnRoom_)
{
LOG_DEBUG << "wait for OnRoom";
sleep(3);
}
LOG_DEBUG << "join multi room";
int16_t ret1 = rtcEngine_->joinMultiRoom(destRoomId);
if (ret1 != 0)
{
LOG_DEBUG << "joinMultiRoom fail, ret:" << ret1;
return false;
if (std::string(srcRoomId) != std::string(destRoomId)) {
isMultiRoom_ = true;
LOG_DEBUG << "join multi room";
int16_t ret1 = rtcEngine_->joinMultiRoom(destRoomId);
if (ret1 != 0) {
LOG_DEBUG << "joinMultiRoom fail, ret:" << ret1;
return false;
}
} else {
isMultiRoom_ = false;
}
mrtc::MRTCAudioOption option;
strcpy(option.dstRoomId, destRoomId);
if (std::string(srcRoomId) != std::string(destRoomId)) {
strcpy(option.dstRoomId, destRoomId);
}
option.channelIndex = destChannelIndex;
option.channel = channelNum;
LOG_DEBUG << "startCustomAudio";
int16_t ret2 = rtcEngine_->startCustomAudio(option);
if (ret2 != 0)
@ -175,11 +184,16 @@ int16_t RTCContext::sendAudioData(uint8_t channelIndex, const void* pData, int32
int16_t RTCContext::sendCustomAudioData(const int16_t channelIndex, void* customData, int32_t sampleRate,
uint64_t channelNum, uint64_t dataLen)
{
while(!isOnRoom_ || !isJoinMultiRoom_) {
while(!isOnRoom_ || (isMultiRoom_ && !isJoinMultiRoom_)) {
LOG_DEBUG << "wait for room and multi room before send";
sleep(3);
}
std::lock_guard<std::mutex> lock(mutex_);
if (customData == nullptr) {
LOG_DEBUG << "customData is null";
return -1;
}
LOG_DEBUG << "customData addr is:" << customData;
return rtcEngine_->sendCustomAudioData(channelIndex, customData, sampleRate, channelNum, dataLen);
}
mrtc::IMRTCEngine* RTCContext::getRtcEngine() const

View File

@ -4,10 +4,27 @@
#include <mutex>
#include <cstring>
#include <filesystem> // C++17
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
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,
@ -39,7 +56,7 @@ public:
mrtc::IMRTCEngine* getRtcEngine() const;
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* destRoomId, const int16_t destChannelIndex);
bool initSend(const char* srcRoomId, const char* destRoomId, const int16_t destChannelIndex, const uint8_t channelNum);
void* getpData() const;
void setpData(void* pData);
@ -59,6 +76,7 @@ private:
bool isOnRoom_ = false;
bool isOnConsumer_ = false;
bool isJoinMultiRoom_ = false;
bool isMultiRoom_ = false;
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,

View File

@ -118,8 +118,15 @@ int main()
LOG_DEBUG << "RTCContext init failed";
return -1;
}
if (!RTCContext::instance().initSend(destRoomId, destChannelIndex)) {
if (!RTCContext::instance().initSend(srcRoomId, destRoomId, destChannelIndex, 1)) {
LOG_DEBUG << "RTCContext initSend failed";
return -1;
}
app().getLoop()->loop();
app().setLogLevel(trantor::Logger::kDebug);
app().run();
LOG_INFO << "bye!";
return 0;
}