change init format

This commit is contained in:
wangjiyu 2025-04-09 14:49:22 +08:00
parent 338be05383
commit d16c76f693
6 changed files with 189 additions and 269 deletions

View File

@ -2,9 +2,9 @@
#include "../util/RTCContext.h"
using namespace drogon;
int doSend(void* pdata, int32_t sampleRate, uint64_t channelNum, uint64_t dataLen)
int doSend(const int16_t destChannelIndex, void* pdata, int32_t sampleRate, uint64_t channelNum, uint64_t dataLen)
{
int16_t retSend = RTCContext::instance().sendCustomAudioData(RTCContext::instance().destChannelIndex,
int16_t retSend = RTCContext::instance().sendCustomAudioData(destChannelIndex,
pdata, sampleRate, channelNum, dataLen);
if (0!= retSend)
{
@ -19,21 +19,23 @@ int doSend(void* pdata, int32_t sampleRate, uint64_t channelNum, uint64_t dataLe
}
int main()
{
mrtc::IMRTCEngine *rtcEngine = RTCContext::instance().getRtcEngine();
if (!rtcEngine)
{
LOG_DEBUG << "RTCContext::instance().getRtcEngine() failed";
const char* srcUserId = "srcUser1";
const char* destUserId = "destUser1";
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;
if (!RTCContext::instance().init(srcUserId, srcDisplayName, srcRoomId)) {
LOG_DEBUG << "RTCContext init failed";
return -1;
}
int32_t nCount = 0;
if (0 != rtcEngine->getUserCount(nCount))
{
LOG_DEBUG << "RTCContext::instance().getUserCount() failed";
if (!RTCContext::instance().initSend(destRoomId, destChannelIndex)) {
LOG_DEBUG << "RTCContext initSend failed";
return -1;
}
LOG_DEBUG << "nCount: " << nCount;
app().registerHandler(
"/",
[](const HttpRequestPtr &,
@ -69,7 +71,7 @@ int main()
resp->setBody(
"The server has calculated the file's MD5 hash to be " + md5);
//file.save();
doSend((void*)file.fileData(), sampleRate, channelNum, dataLen);
doSend(destChannelIndex, (void*)file.fileData(), sampleRate, channelNum, dataLen);
LOG_INFO << "The uploaded file has been send to RTC";
callback(resp);
},

View File

@ -8,131 +8,29 @@ using namespace drogon;
int main()
{
LOG_DEBUG << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++";
const char* srcUserId = "srcUser1";
const char* destUserId = "destUser1";
mrtc::IMRTCEngine *rtcEngine = RTCContext::instance().getRtcEngine();
if (!rtcEngine)
{
LOG_DEBUG << "RTCContext::instance().getRtcEngine() failed";
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;
if (!RTCContext::instance().init(destUserId, destDisplayName, destRoomId)) {
LOG_DEBUG << "RTCContext init failed";
return -1;
}
if (!RTCContext::instance().initRecv(destRoomId, srcUserId, destChannelIndex)) {
LOG_DEBUG << "RTCContext initRecv failed";
return -1;
}
/*
int32_t nCount = 0;
if (0 != rtcEngine->getUserCount(nCount))
{
LOG_DEBUG << "RTCContext::instance().getUserCount() failed";
return -1;
}
LOG_DEBUG << "nCount: " << nCount;
*/
app().getLoop()->loop();
app().setLogLevel(trantor::Logger::kDebug);
app().run();
LOG_INFO << "bye!";
/*
do
{
sleep(1);
} while (1);
*/
return 0;
/*
RTCImpl impl;
mrtc::IMRTCEngineFactory * rtcFactory = mrtc::getMRTCEngineFactory();
mrtc::IMRTCEngine *rtcEngine = rtcFactory->produceMRTCEngine();
mrtc::MRTCEngineConfig engineConfig;
strcpy(engineConfig.domain, "localhost");
strcpy(engineConfig.applicationId, "appid");
strcpy(engineConfig.appSecrectKey, "appsecret");
engineConfig.port = 34443;
rtcEngine->init(engineConfig, impl);
const char* userId = "user";
const char* displayName = "displayName";
const char* inRoomId = "inRoomId";
const char* outRoomId = "outRoomId";
const int16_t channelIndex = 40;
rtcEngine->setUserInfo(userId, displayName, inRoomId);
rtcEngine->registerListener(mrtc::MRTCListenerType::TYPE_LISTENER_ROOM, impl);
rtcEngine->registerListener(mrtc::MRTCListenerType::TYPE_LISTENER_CONSUMER, impl);
rtcEngine->registerSoundLevelListener(mrtc::TYPE_AUDIO_SOURCE_MIC, inRoomId, userId, channelIndex, impl);
//rtcEngine->muteAudio(inRoomId, userId, mrtc::MRTCAudioSourceType::TYPE_AUDIO_SOURCE_MIC, false, channelIndex);
mrtc::MRTCAudioOption option;
option.channelIndex = channelIndex;
strcpy(option.dstRoomId, outRoomId);
rtcEngine->joinMultiRoom(outRoomId);
rtcEngine->startCustomAudio(option);
rtcEngine->sendCustomAudioData(channelIndex, "");
rtcEngine->stopCustomAudio();
*/
// `registerHandler()` adds a handler to the desired path. The handler is
// responsible for generating a HTTP response upon an HTTP request being
// sent to Drogon
/*
app().registerHandler(
"/",
[](const HttpRequestPtr &,
std::function<void(const HttpResponsePtr &)> &&callback) {
auto resp = HttpResponse::newHttpResponse();
resp->setBody("Hello, World!");
callback(resp);
},
{Get});
// `registerHandler()` also supports parsing and passing the path as
// parameters to the handler. Parameters are specified using {}. The text
// inside the {} does not correspond to the index of parameter passed to the
// handler (nor it has any meaning). Instead, it is only to make it easier
// for users to recognize the function of each parameter.
app().registerHandler(
"/user/{user-name}",
[](const HttpRequestPtr &,
std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &name) {
auto resp = HttpResponse::newHttpResponse();
resp->setBody("Hello, " + name + "!");
callback(resp);
},
{Get});
// You can also specify that the parameter is in the query section of the
// URL!
app().registerHandler(
"/hello?user={user-name}",
[](const HttpRequestPtr &,
std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &name) {
auto resp = HttpResponse::newHttpResponse();
resp->setBody("Hello, " + name + "!");
callback(resp);
},
{Get});
// Or, if you want to, instead of asking drogon to parse it for you. You can
// parse the request yourselves.
app().registerHandler(
"/hello_user",
[](const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback) {
auto resp = HttpResponse::newHttpResponse();
auto name = req->getOptionalParameter<std::string>("user");
if (!name)
resp->setBody("Please tell me your name");
else
resp->setBody("Hello, " + name.value() + "!");
callback(resp);
},
{Get});
// Ask Drogon to listen on 127.0.0.1 port 8848. Drogon supports listening
// on multiple IP addresses by adding multiple listeners. For example, if
// you want the server also listen on 127.0.0.1 port 5555. Just add another
// line of addListener("127.0.0.1", 5555)
LOG_INFO << "Server running on 127.0.0.1:8848";
app().addListener("127.0.0.1", 8848).run();
*/
}

View File

@ -7,27 +7,26 @@ using namespace drogon;
int main()
{
LOG_DEBUG << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++";
const char* srcUserId = "srcUser1";
const char* destUserId = "destUser1";
mrtc::IMRTCEngine *rtcEngine = RTCContext::instance().getRtcEngine();
if (!rtcEngine)
{
LOG_DEBUG << "RTCContext::instance().getRtcEngine() failed";
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;
if (!RTCContext::instance().init(srcUserId, srcDisplayName, srcRoomId)) {
LOG_DEBUG << "RTCContext init failed";
return -1;
}
/*
int32_t nCount = 0;
if (0 != rtcEngine->getUserCount(nCount))
{
LOG_DEBUG << "RTCContext::instance().getUserCount() failed";
if (!RTCContext::instance().initSend(destRoomId, destChannelIndex)) {
LOG_DEBUG << "RTCContext initSend failed";
return -1;
}
LOG_DEBUG << "nCount: " << nCount;
*/
do
{
int16_t retSend = RTCContext::instance().sendCustomAudioData(RTCContext::instance().destChannelIndex,
int16_t retSend = RTCContext::instance().sendCustomAudioData(destChannelIndex,
(void*)"aaaaaaaaaaaaaaaaaa", 48000, 1, 10);
if (0!= retSend)
{

View File

@ -9,12 +9,12 @@ void RTCContext::onRoom(uint32_t typeId, RTCENGINE_NAMESPACE::MRTCRoomInfo& room
//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;
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;
std::lock_guard<std::mutex> lock(mutex_);
isOnConsumer = true;
isOnConsumer_ = true;
//LOG_DEBUG << "RTCContext::onConsumer()";
}
void RTCContext::onRender(const char* roomId, const char* peerId,
@ -26,7 +26,7 @@ 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;
isJoinMultiRoom_ = true;
}
LOG_DEBUG << "RTCContext::onCallBackMessage(), msgId:" << msgId << ", msg:" << msg;
@ -54,7 +54,7 @@ void RTCContext::onProducer(uint32_t msgId, mrtc::MRTCProducerInfo& info)
LOG_DEBUG << "-----------------------------------";
LOG_DEBUG << "RTCContext::onProducer()";
}
bool RTCContext::init()
bool RTCContext::init(const char* selfUserId, const char* selfDisplayName, const char* selfRoomId)
{
mrtc::IMRTCEngineFactory * rtcFactory = mrtc::getMRTCEngineFactory();
if (!rtcFactory)
@ -76,6 +76,7 @@ bool RTCContext::init()
LOG_DEBUG << "RTCContext::instance().init() failed";
return false;
}
/*
#ifdef SEND_MODE
if (0 != rtcEngine_->setUserInfo(srcUserId, srcDisplayName, srcRoomId))
#else
@ -85,7 +86,13 @@ bool RTCContext::init()
LOG_DEBUG << "RTCContext::instance().setUserInfo() 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);
@ -108,9 +115,9 @@ bool RTCContext::init()
return true;
}
bool RTCContext::initRecv()
bool RTCContext::initRecv(const char* destRoomId, const char* srcUserId, const int16_t destChannelIndex)
{
while (!isOnConsumer)
while (!isOnConsumer_)
{
LOG_DEBUG << "wait for OnConsumer";
sleep(3);
@ -134,9 +141,9 @@ bool RTCContext::init()
return true;
}
bool RTCContext::initSend()
bool RTCContext::initSend(const char* destRoomId, const int16_t destChannelIndex)
{
while (!isOnRoom)
while (!isOnRoom_)
{
LOG_DEBUG << "wait for OnRoom";
sleep(3);
@ -163,9 +170,9 @@ bool RTCContext::init()
return true;
}
void RTCContext::destorySend()
void RTCContext::destorySend(const int16_t selfChannelIndex)
{
rtcEngine_->stopCustomAudio(destChannelIndex);
rtcEngine_->stopCustomAudio(selfChannelIndex);
}
int16_t RTCContext::sendAudioData(uint8_t channelIndex, const void* pData, int32_t nSampleRate, uint64_t nNumberOfChannels, uint64_t dataLength)
{
@ -179,7 +186,7 @@ bool RTCContext::init()
int16_t RTCContext::sendCustomAudioData(const int16_t channelIndex, void* customData, int32_t sampleRate,
uint64_t channelNum, uint64_t dataLen)
{
while(!isOnRoom || !isJoinMultiRoom) {
while(!isOnRoom_ || !isJoinMultiRoom_) {
LOG_DEBUG << "wait for room and multi room before send";
sleep(3);
}

View File

@ -29,6 +29,7 @@ public:
const unsigned short port = 34443;
#endif
/*
const char* srcUserId = "srcUser1";
const char* destUserId = "destUser1";
@ -38,9 +39,7 @@ public:
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()
{
@ -50,9 +49,9 @@ public:
RTCContext(const RTCContext&) = delete;
RTCContext& operator=(const RTCContext&) = delete;
mrtc::IMRTCEngine* getRtcEngine() const;
bool init();
bool initRecv();
bool initSend();
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);
void* getpData() const;
void setpData(void* pData);
@ -60,21 +59,26 @@ 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();
void destorySend(const int16_t selfChannelIndex);
private:
RTCContext()
{
/*
init();
#ifdef SEND_MODE
initSend();
#else
initRecv();
#endif
*/
}
mutable std::mutex mutex_;
mrtc::IMRTCEngine * rtcEngine_ = nullptr;
void* pData_ = nullptr;
bool isOnRoom_ = false;
bool isOnConsumer_ = false;
bool isJoinMultiRoom_ = 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

@ -21,6 +21,15 @@ class WebSocket2RTC : public drogon::WebSocketController<WebSocket2RTC>
WS_PATH_LIST_END
private:
PubSubService<std::string> chatRooms_;
const char* srcUserId = "srcUser1";
const char* destUserId = "destUser1";
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;
};
struct Subscriber
@ -29,30 +38,14 @@ struct Subscriber
drogon::SubscriberID id_;
};
int16_t sendRTC(std::string &&message) {
int16_t sendRTC(const int16_t destChannelIndex, std::string &&message) {
if (message.empty())
{
LOG_DEBUG << "message is empty";
return 0;
}
mrtc::IMRTCEngine *rtcEngine = RTCContext::instance().getRtcEngine();
if (!rtcEngine)
{
LOG_DEBUG << "RTCContext::instance().getRtcEngine() failed";
return -1;
}
/*
int32_t nCount = 0;
if (0 != rtcEngine->getUserCount(nCount))
{
LOG_DEBUG << "RTCContext::instance().getUserCount() failed";
return -1;
}
LOG_DEBUG << "nCount: " << nCount;
*/
int16_t retSend = RTCContext::instance().sendCustomAudioData(RTCContext::instance().destChannelIndex,
int16_t retSend = RTCContext::instance().sendCustomAudioData(destChannelIndex,
(void*)"aaaaaaaaaaaaaaaaaa", 48000, 1, 10);
if (0!= retSend)
{
@ -80,7 +73,7 @@ void WebSocket2RTC::handleNewMessage(const WebSocketConnectionPtr &wsConnPtr,
auto &s = wsConnPtr->getContextRef<Subscriber>();
chatRooms_.publish(s.chatRoomName_, message);
}
if (0 != sendRTC(std::move(message))) {
if (0 != sendRTC(destChannelIndex, std::move(message))) {
LOG_ERROR << "sendRTC() failed";
}
}
@ -111,5 +104,22 @@ void WebSocket2RTC::handleNewConnection(const HttpRequestPtr &req,
int main()
{
const char* srcUserId = "srcUser1";
const char* destUserId = "destUser1";
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;
app().addListener("127.0.0.1", 8848).run();
if (!RTCContext::instance().init(srcUserId, srcDisplayName, srcRoomId)) {
LOG_DEBUG << "RTCContext init failed";
return -1;
}
if (!RTCContext::instance().initSend(destRoomId, destChannelIndex)) {
LOG_DEBUG << "RTCContext initSend failed";
return -1;
}
}