From 52c7099dbe89a4cd5fcac11d58c1148e1e700351 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Thu, 13 Jun 2024 15:49:26 +0800 Subject: [PATCH] Fix crash when connecting to local desk --- README.md | 2 +- src/main_window/main_window.cpp | 45 +++++++++++++++++++++++++-------- src/main_window/main_window.h | 3 +++ thirdparty/projectx | 2 +- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 1161c6b..1edad77 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Continuous Desk is a lightweight cross-platform remote desktop. It allows multiple users to remotely control the same computer at the same time. In addition to desktop image transmission, it also supports end-to-end voice transmission, providing collaboration capabilities on the basis of remote desktop. -Continuous Desk is an experimental application of [Projectx](https://github.com/dijunkun/projectx) real-time communications library. Projectx is a lightweight cross-platform real-time communications library. It has basic capabilities such as network traversal ([RFC5245](https://datatracker.ietf.org/doc/html/rfc5245)), video softwar/hardware encoding/decoding (H264), audio encoding/decoding ([Opus](https://github.com/xiph/opus)), signaling interaction, and network congestion control ([TCP over UDP](https://libnice.freedesktop.org/)). +Continuous Desk is an experimental application of [Projectx](https://github.com/dijunkun/projectx) real-time communications library. Projectx is a lightweight cross-platform real-time communications library. It has basic capabilities such as network traversal ([RFC5245](https://datatracker.ietf.org/doc/html/rfc5245)), video software/hardware encoding/decoding (H264), audio encoding/decoding ([Opus](https://github.com/xiph/opus)), signaling interaction, and network congestion control ([TCP over UDP](https://libnice.freedesktop.org/)). ## Usage diff --git a/src/main_window/main_window.cpp b/src/main_window/main_window.cpp index 2c13a2a..2056777 100644 --- a/src/main_window/main_window.cpp +++ b/src/main_window/main_window.cpp @@ -185,8 +185,8 @@ int MainWindow::CreateConnectionPeer() { peer_ = CreatePeer(¶ms_); if (peer_) { LOG_INFO("Create peer instance successful"); - std::string user_id = mac_addr_str_; - Init(peer_, user_id.c_str()); + local_id_ = mac_addr_str_; + Init(peer_, local_id_.c_str()); LOG_INFO("Peer init finish"); } else { LOG_INFO("Create peer instance failed"); @@ -464,32 +464,51 @@ int MainWindow::Run() { ImGui::Separator(); ImGui::Spacing(); - if (ImGui::Button(connect_button_label_.c_str())) { + if (ImGui::Button(connect_button_label_.c_str()) || rejoin_) { int ret = -1; if ("SignalConnected" == signal_status_str_) { if (connect_button_label_ == localization::connect[localization_language_index_] && !connection_established_) { - ret = JoinConnection(peer_, remote_id_, client_password_); + if (remote_id_ == local_id_ && !peer_reserved_) { + peer_reserved_ = CreatePeer(¶ms_); + if (peer_reserved_) { + LOG_INFO("Create peer[reserved] instance successful"); + std::string local_id = "C-" + mac_addr_str_; + Init(peer_reserved_, local_id.c_str()); + LOG_INFO("Peer[reserved] init finish"); + } else { + LOG_INFO("Create peer[reserved] instance failed"); + } + } + ret = JoinConnection(peer_reserved_ ? peer_reserved_ : peer_, + remote_id_, client_password_); if (0 == ret) { - is_client_mode_ = true; + if (!peer_reserved_) { + is_client_mode_ = true; + } + rejoin_ = false; + } else { + rejoin_ = true; } } else if (connect_button_label_ == localization::disconnect [localization_language_index_] && connection_established_) { - ret = LeaveConnection(peer_); - memset(audio_buffer_, 0, 960); + ret = LeaveConnection(peer_reserved_ ? peer_reserved_ : peer_); - connection_established_ = false; - received_frame_ = false; - is_client_mode_ = false; + if (0 == ret) { + rejoin_ = false; + memset(audio_buffer_, 0, 960); + connection_established_ = false; + received_frame_ = false; + is_client_mode_ = false; + } } if (0 == ret) { connect_button_pressed_ = !connect_button_pressed_; - connect_button_label_ = connect_button_pressed_ ? localization::disconnect[localization_language_index_] @@ -823,6 +842,10 @@ int MainWindow::Run() { DestroyPeer(peer_); } + if (peer_reserved_) { + DestroyPeer(peer_reserved_); + } + SDL_CloseAudioDevice(output_dev_); SDL_CloseAudioDevice(input_dev_); diff --git a/src/main_window/main_window.h b/src/main_window/main_window.h index ddd8d7d..a470674 100644 --- a/src/main_window/main_window.h +++ b/src/main_window/main_window.h @@ -96,6 +96,7 @@ class MainWindow { std::string settings_button_label_ = "Setting"; char input_password_tmp_[7] = ""; char input_password_[7] = ""; + std::string local_id_ = ""; char remote_id_[20] = ""; char client_password_[20] = ""; bool is_client_mode_ = false; @@ -126,6 +127,7 @@ class MainWindow { bool received_frame_ = false; bool is_create_connection_ = false; bool audio_buffer_fresh_ = false; + bool rejoin_ = false; int fps_ = 0; uint32_t start_time_; @@ -141,6 +143,7 @@ class MainWindow { private: PeerPtr *peer_ = nullptr; + PeerPtr *peer_reserved_ = nullptr; Params params_; private: diff --git a/thirdparty/projectx b/thirdparty/projectx index eee3b2a..c575a91 160000 --- a/thirdparty/projectx +++ b/thirdparty/projectx @@ -1 +1 @@ -Subproject commit eee3b2a95e1c26ce3214adc2c633405abe9d918e +Subproject commit c575a9170c0f9ed99a431cd132becad0f199c694