diff --git a/main.cpp b/main.cpp index 0cde465d549b3267e4683a915b8b3b25782db78e..43dfdee97cade9b3f77b47e752459d84afab827f 100644 --- a/main.cpp +++ b/main.cpp @@ -89,22 +89,7 @@ int main(int argc, char *argv[]) printf("--roomid:%s,--audio;%s,--video:%s\n", g_roomid.c_str(), g_audioOprate.c_str(), g_videoOprate.c_str()); RtcClient mcc; int res = 0; - res = mcc.load(g_url, g_token,true); - printf("============================================================res:%d...\n", res); - if (res < 0) - return -1; - - char quit; - if (g_roomid.empty()) - { - mcc.ScheduleRoom(); - } - else - { - printf("===========================joinRoom=======================res:%d...\n", res); - mcc.joinRoom(g_roomid, "AITestUserId", "AITestUserName"); - printf("===========================joinRoom= end======================res:%d...\n", res); - } + mcc.loadAndJoinRoom(g_url, token, true, g_roomid, "test_user_id", "test_user_name", 10); sleep(3); mcc.subAudioStream("n2m-u-8EC0D9D36BDA35474389D1CA18F93981DB81"); while (true) diff --git a/rect.py b/rect.py index f6da9ac79e3e878311ae36ff7ce24f4c057c66b1..85a8af3c7e9b301f4013fec065be7e18e3529f03 100644 --- a/rect.py +++ b/rect.py @@ -20,13 +20,13 @@ roomid = '20589675' import cv2 import numpy as np + client = RtcClient() time.sleep(2) -ret = client.load(url,ak,sk) -time.sleep(2) ## 加入房间 -client.joinRoom(roomid,'90091','ai_agent') +client.loadAndJoinRoom(url, token, True, roomid, '90091', 'ai_agent', 10) +time.sleep(2) # 按帧发送视频 for i in range(200): @@ -36,12 +36,14 @@ for i in range(200): client.publishVedioStream(w,h, frame.tobytes(), len(frame.tobytes())) client.leave(0) -print('leave room') + time.sleep(4) -client.joinRoom(roomid,'90091','ai_agent') + +client.loadAndJoinRoom(url,token, True, roomid, '90091', 'ai_agent', 10) print('after joinroom') for i in range(200): frame = cv2.imread(r"human.png") h,w = frame.shape[:2] time.sleep(0.04) - client.publishVedioStream(w,h, frame.tobytes(), len(frame.tobytes())) \ No newline at end of file + client.publishVedioStream(w,h, frame.tobytes(), len(frame.tobytes())) +client.leave(0) \ No newline at end of file diff --git a/rtcclient.cpp b/rtcclient.cpp index 46ce472bcb1388d1fbff8271a435d77a22b06b81..158d1d2e430c1bf3085f0e7f99dcb1546928243c 100644 --- a/rtcclient.cpp +++ b/rtcclient.cpp @@ -120,6 +120,70 @@ int RtcClient::joinRoom(std::string roomid, std::string userid, std::string user cout << "END join room ,roomid=" << roomid << "roomdcode=" << ret << endl; return ret; } + +int RtcClient::loadAndJoinRoom(std::string url, std::string token, bool enablelog, std::string roomid, std::string userid, std::string username, int waitseonds) +{ + std::lock_guard gurad(m_mtx); + cout << "AVDEngine start" << endl; + int result = 0; + + m_isJoinSuccess = 0; + m_isJoind = true; + m_user.userId = userid; + m_user.userName = username; + m_roomid = roomid; + m_fakeCamera.id = userid + "_virturevideoid"; + m_fakeCamera.name = "fakevideoin"; + + // 设置日志文件 + if (enablelog) { + rtc::IAVDEngine::Instance()->setLogParams("verbose realtstamp", "mclient.log"); + } + + // 设备必须在初始化引擎前设置 + GlobalDeviceManager::SetAudioInterface(0, 0); + + // 设置引擎选项 + rtc::IAVDEngine::Instance()->setOption(eo_video_codec_priority, &g_videocodec); + rtc::IAVDEngine::Instance()->setOption(eo_camera_capability_default, &m_cameraCap); + + // 设置叁体向我们鉴权,该选项开启后,必须使用uap的token,不需要携带Bearer + rtc::IAVDEngine::Instance()->setCustomerTokenEnabled(true); + + // 初始化 + result = rtc::IAVDEngine::Instance()->init(this, url, token); + cout << "url: " << url << endl; + cout << "token: " << token << endl; + + if (result != AVD_OK) + { + cout << "AVDEngine registration failed!" << endl; + cout << "url: " << url << endl; + cout << "token: " << token << endl; + return -1; + } + cout << "AVDEngine initialization successful !!" << endl; + + cout << "join room ,roomid=" << roomid << endl; + cout << "join room ,userId=" << userid << endl; + cout << "join room ,username=" << username << endl; + cout << "Wait for join room" << endl; + //循环等待m_isInitSuccess变为true,直到超时退出循环 + auto start = std::chrono::steady_clock::now(); + std::chrono::milliseconds timeout(1000 * waitseonds); // 10秒超时 + while (m_isJoinSuccess == 0 && (std::chrono::steady_clock::now() - start <= timeout)) { + // 等待一段时间再检查,以减少CPU占用 + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + + if (m_isJoinSuccess == 1) { + std::cout << "Join room success!!" << std::endl; + return 0; + } + std::cerr << "Join room failed with result: " << m_isJoinSuccess << std::endl; + return -1; +} + int RtcClient::leave(int reason) { cout << "leave room " << endl; @@ -392,19 +456,24 @@ int RtcClient::JoinRoomInternal() void RtcClient::onInitResult(Result result) { - cout << "result = " << result << endl; - cout << "This is the callback after InitEngine is initialized !!!" << endl; - if (result == AVD_OK) - { + std::cout << "Initialization result = " << static_cast(result) << std::endl; + std::cout << "This is the callback after InitEngine is initialized !!!" << std::endl; + if (result == AVD_OK) { m_isInitSuccess = true; + + // 如果初始化成功并且房间ID不为空,则尝试加入房间 + if (!m_roomid.empty()) { + JoinRoomInternal(); + } + + // 如果初始化成功且需要创建测试房间,则安排房间创建 + if (m_createRoomForTest) { + ScheduleRoom(); + } } - if (m_isInitSuccess && !m_roomid.empty()) - { - JoinRoomInternal(); - } - if (m_isInitSuccess && m_createRoomForTest) - { - ScheduleRoom(); + else { + // 处理初始化失败的情况(如果需要) + std::cerr << "Initialization failed with result: " << static_cast(result) << std::endl; } } @@ -412,17 +481,19 @@ void RtcClient::onJoinResult(Result result) { if (result != 0) { + m_isJoinSuccess = 2; cout << "Join failed" << endl; } else { + m_isJoinSuccess = 1; cout << "Joinf success ,roomid=" << m_roomid << endl; if (m_audio) { std::cout << "openMicrophone" << endl; m_audio->openMicrophone(); } - // 导入和导出视 + // 导入和导出视频 if (m_video) { std::cout << "publishLocalCamera" << endl; @@ -597,6 +668,7 @@ PYBIND11_MODULE(rtc_client, m) py::arg("url"), py::arg("token"), py::arg("enablelog") = false) .def("uninit", &RtcClient::uninit) .def("joinRoom", &RtcClient::joinRoom) + .def("loadAndJoinRoom", &RtcClient::loadAndJoinRoom) .def("leave", &RtcClient::leave) .def("sendPrivateMessage", &RtcClient::sendPrivateMessage) .def("sendPublicMessage", &RtcClient::sendPublicMessage) diff --git a/rtcclient.h b/rtcclient.h index eb8a87a0fa10a4dd3cfe755f8ee544238e05c34e..b0bfdd63670c38ff102687bb0c88d9346368a637 100644 --- a/rtcclient.h +++ b/rtcclient.h @@ -30,6 +30,7 @@ public: int load(std::string url, std::string token, bool enablelog) override; void uninit(); int joinRoom(std::string roomid, std::string selfUserId, std::string selfUserName) override; + int loadAndJoinRoom(std::string url, std::string token, bool enablelog, std::string roomid, std::string selfUserId, std::string selfUserName, int waitSeconds) override; int leave(int reason) override; int sendPrivateMessage(int msgType, std::string message, std::string targetUserId) override; int sendPublicMessage(int msgType, std::string message) override; @@ -87,6 +88,7 @@ public: AudioDeviceOutDumy *m_audioDeviceOut; AudioPcmOut *m_audioPcmOut; bool m_isInitSuccess; + int m_isJoinSuccess; uint32 m_callid; std::mutex m_mtx; diff --git a/rtcinterface.h b/rtcinterface.h index e443dbb17f12e260575bb075ce8ebdb28c731060..33ab518a10ba79b50d9dfa78ec7e2b3f75f3c5c9 100644 --- a/rtcinterface.h +++ b/rtcinterface.h @@ -17,6 +17,7 @@ public: //virtual int load(std::string url, std::string appkey, std::string secretkey, bool enablelog) = 0; virtual int load(std::string url, std::string token, bool enablelog) = 0; virtual int joinRoom(std::string roomid, std::string selfUserId, std::string selfUserName) = 0; + virtual int loadAndJoinRoom(std::string url, std::string token, bool enablelog, std::string roomid, std::string selfUserId, std::string selfUserName, int waitseonds) = 0; virtual int leave(int reason) = 0; virtual int sendPrivateMessage(int msgType, std::string message, std::string targetUserId) = 0; virtual int sendPublicMessage(int msgType, std::string message) = 0;