audio_trans/src/util/RTCContext.cpp

189 lines
6.6 KiB
C++
Raw Normal View History

2025-04-07 11:31:21 +08:00
#include "RTCContext.h"
#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()";
std::lock_guard<std::mutex> lock(mutex_);
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;
2025-04-07 18:02:49 +08:00
std::lock_guard<std::mutex> lock(mutex_);
isOnConsumer = true;
2025-04-07 11:31:21 +08:00
//LOG_DEBUG << "RTCContext::onConsumer()";
}
void RTCContext::onRender(const char* roomId, const char* peerId,
RTCENGINE_NAMESPACE::MRTCVideoSourceType sourceType, const RTCENGINE_NAMESPACE::MRTCVideoFrame& videoFrame) {
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()";
}
void RTCContext::onCallBackCustomData(RTCENGINE_NAMESPACE::MRTCCustomDataObject object) {
//LOG_DEBUG << "RTCContext::onCallBackCustomData(), obj:" << object.peerId << "," << object.data << "," << object.data_length;
LOG_DEBUG << "RTCContext::onCallBackCustomData()";
}
void RTCContext::onSoundLevelUpdate(const char* roomId, const char* peerId, uint16_t audioSourceType,
uint8_t channelIndex, uint16_t volume, int32_t vad)
{
LOG_DEBUG << "RTCContext::onSoundLevelUpdate()";
}
void RTCContext::onAudioProcess(const char* roomId, const char* peerId,
mrtc::MRTCAudioFrame& audioFrame, mrtc::MRTCAudioSourceType audioSourceType)
{
LOG_DEBUG << "-----------------------------------";
LOG_DEBUG << "dataCount:" << audioFrame.dataCount << audioSourceType;
//sendAudioData(audioFrame.channelIndex, audioFrame.data, audioFrame.sampleRate, audioFrame.numChannels,
// audioFrame.dataCount);
}
void RTCContext::onProducer(uint32_t msgId, mrtc::MRTCProducerInfo& info)
{
LOG_DEBUG << "-----------------------------------";
LOG_DEBUG << "RTCContext::onProducer()";
}
bool RTCContext::init()
{
mrtc::IMRTCEngineFactory * rtcFactory = mrtc::getMRTCEngineFactory();
if (!rtcFactory)
{
return false;
}
rtcEngine_ = rtcFactory->produceMRTCEngine();
if (!rtcEngine_)
{
return false;
}
mrtc::MRTCEngineConfig engineConfig;
strcpy(engineConfig.domain, domain);
strcpy(engineConfig.applicationId, appid);
strcpy(engineConfig.appSecrectKey, appSecrectKey);
engineConfig.port = port;
if (0 != rtcEngine_->init(engineConfig, this))
{
LOG_DEBUG << "RTCContext::instance().init() failed";
return false;
}
if (0 != rtcEngine_->setUserInfo(inUserId, inDisplayName, inRoomId))
{
LOG_DEBUG << "RTCContext::instance().setUserInfo() inUser failed";
return false;
}
mrtc::MRTCJoinAuthority authority;
strcpy(authority.applicationId, appid);
strcpy(authority.appSecretKey, appSecrectKey);
mrtc::MRTCJoinConfig loginConfig;
if (0!= rtcEngine_->joinRoom(authority, loginConfig))
{
LOG_DEBUG << "RTCContext::instance().joinRoom() failed";
return false;
}
if (0 != rtcEngine_->registerListener(mrtc::MRTCListenerType::TYPE_LISTENER_ROOM, this))
{
LOG_DEBUG << "RTCContext::instance().registerListener() failed";
return false;
}
if (0 != rtcEngine_->registerListener(mrtc::MRTCListenerType::TYPE_LISTENER_CONSUMER, this))
{
LOG_DEBUG << "RTCContext::instance().registerListener() failed";
return false;
}
return true;
}
bool RTCContext::initRecv()
{
2025-04-07 18:02:49 +08:00
while (!isOnConsumer)
2025-04-07 11:31:21 +08:00
{
2025-04-07 18:02:49 +08:00
LOG_DEBUG << "wait for OnConsumer";
2025-04-07 11:31:21 +08:00
sleep(3);
}
LOG_DEBUG << "registerSoundLevelListener";
int16_t ret1 = rtcEngine_->registerSoundLevelListener(mrtc::TYPE_AUDIO_SOURCE_CUSTOM, inRoomId,
inUserId, inChannelIndex, 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);
if (0 != ret2)
{
LOG_DEBUG << "RTCContext::instance().muteAudio() failed, ret:" << ret2;
return false;
}
LOG_DEBUG << "init recv succ";
return true;
}
bool RTCContext::initSend()
{
while (!isOnRoom)
{
2025-04-07 18:02:49 +08:00
LOG_DEBUG << "wait for OnRoom";
2025-04-07 11:31:21 +08:00
sleep(3);
}
LOG_DEBUG << "join multi room";
int16_t ret1 = rtcEngine_->joinMultiRoom(outRoomId);
if (ret1 != 0)
{
LOG_DEBUG << "joinMultiRoom fail, ret:" << ret1;
return false;
}
mrtc::MRTCAudioOption option;
strcpy(option.dstRoomId, outRoomId);
option.channelIndex = outChannelIndex;
LOG_DEBUG << "startCustomAudio";
int16_t ret2 = rtcEngine_->startCustomAudio(option);
if (ret2 != 0)
{
LOG_DEBUG << "startCustomAudio fail, ret:" << ret2;
return false;
}
LOG_DEBUG << "init send succ";
return true;
}
void RTCContext::destorySend(bool isIn)
{
int16_t channelIndex = isIn ? inChannelIndex : outChannelIndex;
rtcEngine_->stopCustomAudio(channelIndex);
}
int16_t RTCContext::sendAudioData(uint8_t channelIndex, const void* pData, int32_t nSampleRate, uint64_t nNumberOfChannels, uint64_t dataLength)
{
std::lock_guard<std::mutex> lock(mutex_);
if (pData_)
{
return rtcEngine_->sendCustomAudioData(channelIndex, pData, nSampleRate, nNumberOfChannels, dataLength);
}
return 0;
}
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_);
return rtcEngine_->sendCustomAudioData(channelIndex, customData, sampleRate, channelNum, dataLen);
}
mrtc::IMRTCEngine* RTCContext::getRtcEngine() const
{
std::lock_guard<std::mutex> lock(mutex_);
return rtcEngine_;
}
void* RTCContext::getpData() const
{
std::lock_guard<std::mutex> lock(mutex_);
return pData_;
}
void RTCContext::setpData(void* pData)
{
std::lock_guard<std::mutex> lock(mutex_);
pData_ = pData;
}