From 95d2d7497974b1e9d26d645a39530e151acb09ee Mon Sep 17 00:00:00 2001 From: dijunkun Date: Mon, 9 Oct 2023 14:11:29 +0800 Subject: [PATCH] Eable leave connection --- .../remote_desk/remote_desk_gui/main.cpp | 155 +++++------------- .../remote_desk_server/remote_desk_server.cpp | 4 +- src/interface/x.h | 2 + src/pc/peer_connection.cpp | 14 +- src/pc/peer_connection.h | 8 +- src/ringbuffer/ringbuffer.h | 6 + src/rtc/x_inner.cpp | 11 ++ src/ws/ws_core.cpp | 4 +- 8 files changed, 83 insertions(+), 121 deletions(-) diff --git a/application/remote_desk/remote_desk_gui/main.cpp b/application/remote_desk/remote_desk_gui/main.cpp index eec0dd6..e884a8c 100644 --- a/application/remote_desk/remote_desk_gui/main.cpp +++ b/application/remote_desk/remote_desk_gui/main.cpp @@ -49,6 +49,8 @@ std::string window_title = "Remote Desk Client"; int thread_exit = 0; PeerPtr *peer = nullptr; +bool joined = false; +bool received_frame = false; typedef enum { mouse = 0, keyboard } ControlType; typedef enum { move = 0, left_down, left_up, right_down, right_up } MouseFlag; @@ -72,30 +74,6 @@ typedef struct { }; } RemoteAction; -inline void FreshVideo() { - sdlRect.x = 0; - sdlRect.y = 0; - sdlRect.w = screen_w; - sdlRect.h = screen_h; - - SDL_UpdateTexture(sdlTexture, NULL, dst_buffer, pixel_w); - SDL_RenderClear(sdlRenderer); - SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect); - SDL_RenderPresent(sdlRenderer); - - frame_count++; - end_time = SDL_GetTicks(); - elapsed_time = end_time - start_time; - if (elapsed_time >= 1000) { - fps = frame_count / (elapsed_time / 1000); - frame_count = 0; - window_title = "Remote Desk Client FPS [" + std::to_string(fps) + "]"; - // For MacOS, UI frameworks can only be called from the main thread - SDL_SetWindowTitle(window, window_title.c_str()); - start_time = end_time; - } -} - inline int ProcessMouseKeyEven(SDL_Event &ev) { float ratio = 1280.0 / screen_w; @@ -192,6 +170,7 @@ void ReceiveVideoBuffer(const char *data, size_t size, const char *user_id, SDL_Event event; event.type = REFRESH_EVENT; SDL_PushEvent(&event); + received_frame = true; } void ReceiveAudioBuffer(const char *data, size_t size, const char *user_id, @@ -302,7 +281,7 @@ int main() { std::string user_id = "C-" + std::string(GetMac(mac_addr)); peer = CreatePeer(¶ms); - JoinConnection(peer, transmission_id.c_str(), user_id.c_str()); + // JoinConnection(peer, transmission_id.c_str(), user_id.c_str()); // Setup SDL if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != @@ -377,11 +356,15 @@ int main() { SDL_Event event; while (SDL_PollEvent(&event)) { ImGui_ImplSDL2_ProcessEvent(&event); - if (event.type == SDL_QUIT) done = true; - if (event.type == SDL_WINDOWEVENT && - event.window.event == SDL_WINDOWEVENT_CLOSE && - event.window.windowID == SDL_GetWindowID(window)) + if (event.type == SDL_QUIT) { done = true; + } else if (event.type == SDL_WINDOWEVENT && + event.window.event == SDL_WINDOWEVENT_CLOSE && + event.window.windowID == SDL_GetWindowID(window)) { + done = true; + } else { + ProcessMouseKeyEven(event); + } } // Start the Dear ImGui frame @@ -392,102 +375,42 @@ int main() { ImGui::BeginMainMenuBar(); if (ImGui::BeginMenu("Main Menu", true)) { - if (ImGui::MenuItem("Connect")) { - if (ImGui::Button("Button", ImVec2(100, 50))) { - LOG_ERROR("!!!!!!!!!!") - } - ImGui::SameLine(); + if (ImGui::MenuItem("Connect") && !joined) { + JoinConnection(peer, transmission_id.c_str(), user_id.c_str()); + joined = true; } ImGui::Separator(); if (ImGui::MenuItem("Disconnect")) { + LeaveConnection(peer); + joined = false; } ImGui::EndMenu(); } - ImGui::Separator(); + // ImGui::Separator(); - if (ImGui::BeginMenu("Second Menu", true)) { - if (ImGui::MenuItem("Item 1", "item 1")) { - } - ImGui::Separator(); - if (ImGui::MenuItem("Item 2", "item 2")) { - } - ImGui::EndMenu(); - } + // if (ImGui::BeginMenu("Second Menu", true)) { + // if (ImGui::MenuItem("Item 1", "item 1")) { + // } + // ImGui::Separator(); + // if (ImGui::MenuItem("Item 2", "item 2")) { + // } + // ImGui::EndMenu(); + // } - ImGui::Separator(); + // ImGui::Separator(); - if (ImGui::BeginMenu("Third Menu", true)) { - if (ImGui::MenuItem("Item 3", "item 3")) { - } - ImGui::Separator(); - if (ImGui::MenuItem("Item 4", "item 4")) { - } - ImGui::EndMenu(); - } + // if (ImGui::BeginMenu("Third Menu", true)) { + // if (ImGui::MenuItem("Item 3", "item 3")) { + // } + // ImGui::Separator(); + // if (ImGui::MenuItem("Item 4", "item 4")) { + // } + // ImGui::EndMenu(); + // } ImGui::EndMainMenuBar(); - // 1. Show the big demo window (Most of the sample code is in - // ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear - // ImGui!). - // if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window); - - // 2. Show a simple window that we create ourselves.We use a Begin / - // End pair to create a named window. - // { - // static float f = 0.0f; - // static int counter = 0; - - // ImGui::Begin("Hello, world!", NULL, - // ImGuiWindowFlags_NoCollapse); // Create a window called - // // "Hello, world!" - // // and append into it. - - // ImGui::SetCursorPos(ImVec2(0, 0)); - // ImGui::SetWindowSize(ImVec2(500, 500)); - - // ImGui::Text("This is some useful text."); // Display some text (you - // can - // // use a format strings too) - // ImGui::Checkbox( - // "Demo Window", - // &show_demo_window); // Edit bools storing our window open/close - // state - // ImGui::Checkbox("Another Window", &show_another_window); - - // ImGui::SliderFloat( - // "float", &f, 0.0f, - // 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f - // ImGui::ColorEdit3( - // "clear color", - // (float *)&clear_color); // Edit 3 floats representing a color - - // if (ImGui::Button( - // "Button")) // Buttons return true when clicked (most widgets - // // return true when edited/activated) - // counter++; - // ImGui::SameLine(); - // ImGui::Text("counter = %d", counter); - - // ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", - // 1000.0f / io.Framerate, io.Framerate); - // ImGui::End(); - // } - - // 3. Show another simple window. - // if (show_another_window) { - // ImGui::Begin( - // "Another Window", - // &show_another_window); // Pass a pointer to our bool variable (the - // // window will have a closing button that - // will - // // clear the bool when clicked) - // ImGui::Text("Hello from another window!"); - // if (ImGui::Button("Close Me")) show_another_window = false; - // ImGui::End(); - // } - // Rendering ImGui::Render(); SDL_RenderSetScale(sdlRenderer, io.DisplayFramebufferScale.x, @@ -500,7 +423,13 @@ int main() { SDL_UpdateTexture(sdlTexture, NULL, dst_buffer, pixel_w); SDL_RenderClear(sdlRenderer); - SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect); + if (!joined || !received_frame) { + SDL_SetRenderDrawColor( + sdlRenderer, (Uint8)(clear_color.x * 0), (Uint8)(clear_color.y * 0), + (Uint8)(clear_color.z * 0), (Uint8)(clear_color.w * 0)); + } else { + SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect); + } ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData()); SDL_RenderPresent(sdlRenderer); diff --git a/application/remote_desk/remote_desk_server/remote_desk_server.cpp b/application/remote_desk/remote_desk_server/remote_desk_server.cpp index ba24314..053174d 100644 --- a/application/remote_desk/remote_desk_server/remote_desk_server.cpp +++ b/application/remote_desk/remote_desk_server/remote_desk_server.cpp @@ -119,9 +119,9 @@ void RemoteDeskServer::ReceiveDataBuffer(const char *data, size_t size, ip.mi.time = 0; // Set cursor pos - // SetCursorPos(ip.mi.dx, ip.mi.dy); + SetCursorPos(ip.mi.dx, ip.mi.dy); // Send the press - // SendInput(1, &ip, sizeof(INPUT)); + SendInput(1, &ip, sizeof(INPUT)); std::cout << "Receive data from [" << user << "], " << ip.type << " " << ip.mi.dwFlags << " " << ip.mi.dx << " " << ip.mi.dy diff --git a/src/interface/x.h b/src/interface/x.h index 1adab52..3dafda0 100644 --- a/src/interface/x.h +++ b/src/interface/x.h @@ -33,6 +33,8 @@ int CreateConnection(PeerPtr* peer_ptr, const char* transmission_id, int JoinConnection(PeerPtr* peer_ptr, const char* transmission_id, const char* user_id); +int LeaveConnection(PeerPtr* peer_ptr); + int SendData(PeerPtr* peer_ptr, DATA_TYPE data_type, const char* data, size_t size); diff --git a/src/pc/peer_connection.cpp b/src/pc/peer_connection.cpp index e4bef7e..e0c0bfe 100644 --- a/src/pc/peer_connection.cpp +++ b/src/pc/peer_connection.cpp @@ -180,7 +180,7 @@ int PeerConnection::Create(PeerConnectionParams params, const std::string &user_id) { int ret = 0; - ret = Init(params, transmission_id, user_id); + // ret = Init(params, transmission_id, user_id); json message = {{"type", "create_transmission"}, {"user_id", user_id}, @@ -198,13 +198,23 @@ int PeerConnection::Join(PeerConnectionParams params, const std::string &user_id) { int ret = 0; - ret = Init(params, transmission_id, user_id); + // ret = Init(params, transmission_id, user_id); transmission_id_ = transmission_id; ret = RequestTransmissionMemberList(transmission_id_); return ret; } +int PeerConnection::Leave() { + for (auto &ice_transmission : ice_transmission_list_) { + ice_transmission.second->DestroyIceTransmission(); + } + + ice_transmission_list_.erase(ice_transmission_list_.begin(), + ice_transmission_list_.end()); + return 0; +} + void PeerConnection::ProcessSignal(const std::string &signal) { auto j = json::parse(signal); std::string type = j["type"]; diff --git a/src/pc/peer_connection.h b/src/pc/peer_connection.h index 3f3c2b3..d4ceafe 100644 --- a/src/pc/peer_connection.h +++ b/src/pc/peer_connection.h @@ -31,6 +31,9 @@ class PeerConnection { ~PeerConnection(); public: + int Init(PeerConnectionParams params, const std::string &transmission_id, + const std::string &user_id); + int Create(PeerConnectionParams params, const std::string &transmission_id = "", const std::string &user_id = ""); @@ -38,6 +41,8 @@ class PeerConnection { int Join(PeerConnectionParams params, const std::string &transmission_id, const std::string &user_id = ""); + int Leave(); + int Destroy(); SignalStatus GetSignalStatus(); @@ -47,9 +52,6 @@ class PeerConnection { int SendUserData(const char *data, size_t size); private: - int Init(PeerConnectionParams params, const std::string &transmission_id, - const std::string &user_id); - int CreateVideoCodec(bool hardware_acceleration); void ProcessSignal(const std::string &signal); diff --git a/src/ringbuffer/ringbuffer.h b/src/ringbuffer/ringbuffer.h index 312328c..c8dd8a1 100644 --- a/src/ringbuffer/ringbuffer.h +++ b/src/ringbuffer/ringbuffer.h @@ -62,6 +62,9 @@ class RingBuffer { if (isFull()) { return false; } + if (!m_data) { + return false; + } m_data[m_rear] = value; m_rear = (m_rear + 1) % m_size; return true; @@ -71,6 +74,9 @@ class RingBuffer { if (isFull()) { return false; } + if (!m_data) { + return false; + } m_data[m_rear] = *value; m_rear = (m_rear + 1) % m_size; return true; diff --git a/src/rtc/x_inner.cpp b/src/rtc/x_inner.cpp index bd53d2a..f7740e5 100644 --- a/src/rtc/x_inner.cpp +++ b/src/rtc/x_inner.cpp @@ -31,6 +31,9 @@ PeerPtr *CreatePeer(const Params *params) { int CreateConnection(PeerPtr *peer_ptr, const char *transmission_id, const char *user_id) { + peer_ptr->peer_connection->Init(peer_ptr->pc_params, transmission_id, + user_id); + peer_ptr->peer_connection->Create(peer_ptr->pc_params, transmission_id, user_id); return 0; @@ -38,11 +41,19 @@ int CreateConnection(PeerPtr *peer_ptr, const char *transmission_id, int JoinConnection(PeerPtr *peer_ptr, const char *transmission_id, const char *user_id) { + peer_ptr->peer_connection->Init(peer_ptr->pc_params, transmission_id, + user_id); + peer_ptr->peer_connection->Join(peer_ptr->pc_params, transmission_id, user_id); return 0; } +int LeaveConnection(PeerPtr *peer_ptr) { + peer_ptr->peer_connection->Leave(); + return 0; +} + int SendData(PeerPtr *peer_ptr, DATA_TYPE data_type, const char *data, size_t size) { if (DATA_TYPE::VIDEO == data_type) { diff --git a/src/ws/ws_core.cpp b/src/ws/ws_core.cpp index 793acf6..6c21dcd 100644 --- a/src/ws/ws_core.cpp +++ b/src/ws/ws_core.cpp @@ -32,7 +32,9 @@ WsCore::~WsCore() { LOG_INFO("> Error closing connection {}", ec.message()); } - m_thread_->join(); + if (m_thread_->joinable()) { + m_thread_->join(); + } } int WsCore::Connect(std::string const &uri) {