213 lines
7.7 KiB
C++
213 lines
7.7 KiB
C++
#include "RTCContext.h"
|
|
|
|
#include "drogon/drogon_test.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()";
|
|
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 << "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) {
|
|
LOG_DEBUG << "RTCContext::onRender()";
|
|
}
|
|
void RTCContext::onCallBackMessage(uint32_t msgId, const char* msg) {
|
|
|
|
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;
|
|
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(const char* selfUserId, const char* selfDisplayName, const char* selfRoomId)
|
|
{
|
|
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(selfUserId, selfDisplayName, selfRoomId))
|
|
{
|
|
LOG_DEBUG << "RTCContext::instance().setUserInfo() 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(const char* destRoomId, const char* srcUserId, const int16_t destChannelIndex)
|
|
{
|
|
while (!isOnConsumer_)
|
|
{
|
|
LOG_DEBUG << "wait for OnConsumer";
|
|
sleep(3);
|
|
}
|
|
return true;
|
|
|
|
}
|
|
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);
|
|
}
|
|
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;
|
|
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)
|
|
{
|
|
LOG_DEBUG << "startCustomAudio fail, ret:" << ret2;
|
|
return false;
|
|
}
|
|
LOG_DEBUG << "init send succ";
|
|
return true;
|
|
}
|
|
|
|
void RTCContext::destorySend(const int16_t selfChannelIndex)
|
|
{
|
|
rtcEngine_->stopCustomAudio(selfChannelIndex);
|
|
}
|
|
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)
|
|
{
|
|
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
|
|
{
|
|
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;
|
|
} |