From ca32ebeefe1c1a79e6c66a0aeb459e9072ab6fff Mon Sep 17 00:00:00 2001 From: dijunkun Date: Mon, 18 Nov 2024 17:33:09 +0800 Subject: [PATCH] [feat] net traffic stats supported --- src/localization/localization.h | 4 + src/single_window/control_bar.cpp | 92 +++++++++++++++++++++- src/single_window/render.h | 14 +++- src/single_window/render_callback_func.cpp | 19 ++++- thirdparty/projectx | 2 +- 5 files changed, 121 insertions(+), 10 deletions(-) diff --git a/src/localization/localization.h b/src/localization/localization.h index 764efb8..8e193d2 100644 --- a/src/localization/localization.h +++ b/src/localization/localization.h @@ -26,6 +26,10 @@ static std::vector recent_connections = {u8"近期连接", "Recent Connections"}; static std::vector disconnect = {u8"断开连接", "Disconnect"}; static std::vector fullscreen = {u8" 全屏", " Fullscreen"}; +static std::vector show_net_traffic_stats = { + u8"显示流量统计", "Show Net Traffic Stats"}; +static std::vector hide_net_traffic_stats = { + u8"隐藏流量统计", "Hide Net Traffic Stats"}; static std::vector exit_fullscreen = {u8" 退出全屏", " Exit fullscreen"}; static std::vector control_mouse = {u8" 控制", " Control"}; diff --git a/src/single_window/control_bar.cpp b/src/single_window/control_bar.cpp index 5821e5e..715ef9c 100644 --- a/src/single_window/control_bar.cpp +++ b/src/single_window/control_bar.cpp @@ -85,6 +85,23 @@ int Render::ControlBar() { 2.0f); } + ImGui::SameLine(); + // net traffic stats button + std::string net_traffic_stats = ICON_FA_SIGNAL; + if (ImGui::Button(net_traffic_stats.c_str(), ImVec2(25, 25))) { + net_traffic_stats_button_pressed_ = !net_traffic_stats_button_pressed_; + net_traffic_stats_button_label_ = + net_traffic_stats_button_pressed_ + ? localization::hide_net_traffic_stats + [localization_language_index_] + : localization::show_net_traffic_stats + [localization_language_index_]; + } + + if (net_traffic_stats_button_pressed_) { + NetTrafficStats(); + } + ImGui::SameLine(); // fullscreen button std::string fullscreen = @@ -140,4 +157,77 @@ int Render::ControlBar() { ImGui::PopStyleVar(); return 0; -} \ No newline at end of file +} + +int Render::NetTrafficStats() { + const ImGuiViewport* viewport = ImGui::GetMainViewport(); + ImGui::SetNextWindowPos(ImVec2((viewport->WorkSize.x - viewport->WorkPos.x - + connection_status_window_width_) / + 2, + (viewport->WorkSize.y - viewport->WorkPos.y - + connection_status_window_height_) / + 2)); + + ImGui::SetNextWindowSize(ImVec2(connection_status_window_width_, + connection_status_window_height_)); + ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f); + ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0, 1.0, 1.0, 1.0)); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f); + ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f); + + ImGui::Begin("NetTrafficStatsWindow", nullptr, + ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | + ImGuiWindowFlags_NoSavedSettings); + ImGui::PopStyleVar(2); + ImGui::PopStyleColor(); + + if (ImGui::BeginTable("split", 3)) { + int row = 0; + ImGui::TableNextColumn(); + ImGui::Text(" "); + ImGui::TableNextColumn(); + ImGui::Text("In"); + ImGui::TableNextColumn(); + ImGui::Text("Out"); + + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::Text("Video"); + ImGui::TableNextColumn(); + ImGui::Text("%d", net_traffic_stats_.video_in); + ImGui::TableNextColumn(); + ImGui::Text("%d", net_traffic_stats_.video_out); + + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::Text("Audio"); + ImGui::TableNextColumn(); + ImGui::Text("%d", net_traffic_stats_.audio_in); + ImGui::TableNextColumn(); + ImGui::Text("%d", net_traffic_stats_.audio_out); + + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::Text("Total"); + ImGui::TableNextColumn(); + ImGui::Text("%d", net_traffic_stats_.total_in); + ImGui::TableNextColumn(); + ImGui::Text("%d", net_traffic_stats_.total_out); + ImGui::EndTable(); + } + + ImGui::SetCursorPosX(connection_status_window_width_ * 6 / 19); + ImGui::SetCursorPosY(connection_status_window_height_ * 2 / 3); + + // ok + ImGui::SetWindowFontScale(0.5f); + if (ImGui::Button(localization::ok[localization_language_index_].c_str()) || + ImGui::IsKeyPressed(ImGuiKey_Enter)) { + net_traffic_stats_button_pressed_ = false; + } + + ImGui::End(); + ImGui::PopStyleVar(); + return 0; +} diff --git a/src/single_window/render.h b/src/single_window/render.h index e582b8f..51a292a 100644 --- a/src/single_window/render.h +++ b/src/single_window/render.h @@ -64,6 +64,7 @@ class Render { int DrawMainWindow(); int DrawStreamWindow(); int ConfirmDeleteConnection(); + int NetTrafficStats(); public: static void OnReceiveVideoBufferCb(const XVideoFrame *video_frame, @@ -84,8 +85,9 @@ class Render { size_t user_id_size, void *user_data); static void NetStatusReport(const char *client_id, size_t client_id_size, - TraversalMode mode, const unsigned short send, - const unsigned short receive, void *user_data); + TraversalMode mode, + const XNetTrafficStats *net_traffic_stats, + void *user_data); static SDL_HitTestResult HitTestCallback(SDL_Window *window, const SDL_Point *area, void *data); @@ -148,6 +150,7 @@ class Render { std::string mac_addr_str_ = ""; std::string connect_button_label_ = "Connect"; std::string fullscreen_button_label_ = "Fullscreen"; + std::string net_traffic_stats_button_label_ = "Show Net Traffic Stats"; std::string mouse_control_button_label_ = "Mouse Control"; std::string audio_capture_button_label_ = "Audio Capture"; std::string settings_button_label_ = "Setting"; @@ -180,8 +183,8 @@ class Render { int main_window_width_before_maximized_ = 640; int main_window_height_before_maximized_ = 480; int control_window_min_width_ = 20; - int control_window_max_width_ = 170; - int control_window_width_ = 170; + int control_window_max_width_ = 200; + int control_window_width_ = 200; int control_window_height_ = 40; int local_window_width_ = 320; int local_window_height_ = 235; @@ -259,6 +262,7 @@ class Render { uint32_t password_validating_time_ = 0; bool control_bar_expand_ = true; bool fullscreen_button_pressed_ = false; + bool net_traffic_stats_button_pressed_ = false; bool mouse_control_button_pressed_ = false; bool audio_capture_button_pressed_ = false; bool show_settings_window_ = false; @@ -312,6 +316,8 @@ class Render { PeerPtr *peer_ = nullptr; PeerPtr *peer_reserved_ = nullptr; Params params_; + TraversalMode traversal_mode_ = TraversalMode::UnknownMode; + XNetTrafficStats net_traffic_stats_; private: SDL_AudioDeviceID input_dev_; diff --git a/src/single_window/render_callback_func.cpp b/src/single_window/render_callback_func.cpp index b62d7aa..61abf13 100644 --- a/src/single_window/render_callback_func.cpp +++ b/src/single_window/render_callback_func.cpp @@ -332,8 +332,9 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char *user_id, } void Render::NetStatusReport(const char *client_id, size_t client_id_size, - TraversalMode mode, const unsigned short send, - const unsigned short receive, void *user_data) { + TraversalMode mode, + const XNetTrafficStats *net_traffic_stats, + void *user_data) { Render *render = (Render *)user_data; if (!render) { return; @@ -345,7 +346,17 @@ void Render::NetStatusReport(const char *client_id, size_t client_id_size, LOG_INFO("Use client id [{}] and save id into cache file", client_id); render->SaveSettingsIntoCacheFile(); } - if (mode != TraversalMode::UnknownMode) { - LOG_INFO("Net mode: [{}]", int(mode)); + if (render->traversal_mode_ != mode) { + render->traversal_mode_ = mode; + LOG_INFO("Net mode: [{}]", int(render->traversal_mode_)); + } + + if (net_traffic_stats) { + render->net_traffic_stats_.video_in = net_traffic_stats->video_in; + render->net_traffic_stats_.video_out = net_traffic_stats->video_out; + render->net_traffic_stats_.audio_in = net_traffic_stats->audio_in; + render->net_traffic_stats_.audio_out = net_traffic_stats->audio_out; + render->net_traffic_stats_.total_in = net_traffic_stats->total_in; + render->net_traffic_stats_.total_out = net_traffic_stats->total_out; } } \ No newline at end of file diff --git a/thirdparty/projectx b/thirdparty/projectx index 9d2e6f0..c5c85f0 160000 --- a/thirdparty/projectx +++ b/thirdparty/projectx @@ -1 +1 @@ -Subproject commit 9d2e6f0c2a3fe20d0a70d0faee778fbb5f5325a2 +Subproject commit c5c85f0785fb1130fed58d3933959fc573f38750