diff --git a/src/single_window/control_bar.cpp b/src/single_window/control_bar.cpp index 31d9376..b37a753 100644 --- a/src/single_window/control_bar.cpp +++ b/src/single_window/control_bar.cpp @@ -57,8 +57,8 @@ int Render::ControlBar(std::shared_ptr& properties) { : ICON_FA_COMPUTER_MOUSE; if (ImGui::Button(mouse.c_str(), ImVec2(25, 25))) { if (properties->connection_established_) { - control_mouse_ = !control_mouse_; start_keyboard_capturer_ = !start_keyboard_capturer_; + properties->control_mouse_ = !properties->control_mouse_; properties->mouse_control_button_pressed_ = !properties->mouse_control_button_pressed_; properties->mouse_control_button_label_ = diff --git a/src/single_window/remote_peer_window.cpp b/src/single_window/remote_peer_window.cpp index 7007c8f..8813db8 100644 --- a/src/single_window/remote_peer_window.cpp +++ b/src/single_window/remote_peer_window.cpp @@ -78,9 +78,8 @@ int Render::RemoteWindow() { ConnectTo(remote_id_, remote_password_, false); } - std::string client_id = "C-" + remote_id_; - if (client_properties_.find(client_id) != client_properties_.end()) { - auto props = client_properties_[client_id]; + if (client_properties_.find(remote_id_) != client_properties_.end()) { + auto props = client_properties_[remote_id_]; if (props->rejoin_) { ConnectTo(remote_id_, remote_password_, client_properties_[remote_id_]->remember_password_); @@ -107,41 +106,37 @@ static int InputTextCallback(ImGuiInputTextCallbackData *data) { return 0; } -int Render::ConnectTo(const std::string &host_name, const char *password, +int Render::ConnectTo(const std::string &remote_id, const char *password, bool remember_password) { - LOG_INFO("Connect to [{}]", host_name); + LOG_INFO("Connect to [{}]", remote_id); - std::string client_id = "C-" + host_name; - if (client_properties_.find(client_id) == client_properties_.end()) { - client_properties_[client_id] = + if (client_properties_.find(remote_id) == client_properties_.end()) { + client_properties_[remote_id] = std::make_shared(); - } + auto props = client_properties_[remote_id]; + props->local_id_ = "C-" + remote_id; + props->remote_id_ = remote_id; + memcpy(&props->params_, ¶ms_, sizeof(Params)); + props->params_.user_id = props->local_id_.c_str(); + props->peer_ = CreatePeer(&props->params_); - auto props = client_properties_[client_id]; - props->connection_status_ = ConnectionStatus::Connecting; - props->remember_password_ = remember_password; - memcpy(props->remote_password_, password, 6); - - int ret = -1; - - if (!props->connection_established_) { - if (peer_map_.find(client_id) == peer_map_.end()) { - memcpy(&props->params_, ¶ms_, sizeof(Params)); - props->params_.user_id = client_id.c_str(); - peer_map_[client_id] = CreatePeer(&props->params_); - } - - if (peer_map_[client_id]) { - LOG_INFO("[{}] Create peer instance successful", client_id); - Init(peer_map_[client_id]); - LOG_INFO("[{}] Peer init finish", client_id); + if (props->peer_) { + LOG_INFO("[{}] Create peer instance successful", props->local_id_); + Init(props->peer_); + LOG_INFO("[{}] Peer init finish", props->local_id_); } else { - LOG_INFO("Create peer [{}] instance failed", client_id); + LOG_INFO("Create peer [{}] instance failed", props->local_id_); } - ret = JoinConnection(peer_map_[client_id], host_name.c_str(), password); + props->connection_status_ = ConnectionStatus::Connecting; + props->remember_password_ = remember_password; + memcpy(props->remote_password_, password, 6); + } + int ret = -1; + auto props = client_properties_[remote_id]; + if (!props->connection_established_) { + ret = JoinConnection(props->peer_, remote_id.c_str(), password); if (0 == ret) { - is_client_mode_ = true; props->rejoin_ = false; } else { props->rejoin_ = true; diff --git a/src/single_window/render.cpp b/src/single_window/render.cpp index a081b83..2736da4 100644 --- a/src/single_window/render.cpp +++ b/src/single_window/render.cpp @@ -431,10 +431,12 @@ int Render::CreateRtcConnection() { mouse_controller_is_started_ = false; } - if (start_keyboard_capturer_ && !keyboard_capturer_is_started_) { - StartKeyboardCapturer(); - keyboard_capturer_is_started_ = true; - } else if (!start_keyboard_capturer_ && keyboard_capturer_is_started_) { + if (start_keyboard_capturer_ && foucs_on_stream_window_) { + if (!keyboard_capturer_is_started_) { + StartKeyboardCapturer(); + keyboard_capturer_is_started_ = true; + } + } else if (keyboard_capturer_is_started_) { StopKeyboardCapturer(); keyboard_capturer_is_started_ = false; } @@ -858,8 +860,7 @@ int Render::Run() { } std::string host_name = it.first; - PeerPtr* peer_client = peer_map_[host_name]; - if (peer_client) { + if (props->peer_) { std::string client_id; if (host_name == client_id_) { client_id = "C-" + std::string(client_id_); @@ -867,9 +868,9 @@ int Render::Run() { client_id = client_id_; } LOG_INFO("[{}] Leave connection [{}]", client_id, host_name); - LeaveConnection(peer_client, host_name.c_str()); + LeaveConnection(props->peer_, host_name.c_str()); LOG_INFO("Destroy peer [{}]", client_id); - DestroyPeer(&peer_client); + DestroyPeer(&props->peer_); } props->streaming_ = false; @@ -1000,8 +1001,49 @@ int Render::Run() { SDL_UpdateTexture(props->stream_texture_, NULL, props->dst_buffer_, props->texture_width_); + } else if (event.type == SDL_MOUSEMOTION || + event.type == SDL_MOUSEBUTTONDOWN || + event.type == SDL_MOUSEBUTTONUP || + event.type == SDL_MOUSEWHEEL) { + if (!foucs_on_stream_window_) { + continue; + } + + for (auto& it : client_properties_) { + auto props = it.second; + if (props->control_mouse_) { + ProcessMouseEvent(event); + } + } + } else if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) { + if (stream_window_) { + if (SDL_GetWindowID(stream_window_) == event.window.windowID) { + foucs_on_stream_window_ = true; + LOG_INFO("Focus on stream window"); + } + } + + if (main_window_) { + if (SDL_GetWindowID(main_window_) == event.window.windowID) { + foucs_on_main_window_ = true; + LOG_INFO("Focus on main window"); + } + } + } else if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { + if (stream_window_) { + if (SDL_GetWindowID(stream_window_) == event.window.windowID) { + foucs_on_stream_window_ = false; + LOG_INFO("Lost focus on stream window"); + } + } + + if (main_window_) { + if (SDL_GetWindowID(main_window_) == event.window.windowID) { + foucs_on_main_window_ = false; + LOG_INFO("Lost focus on main window"); + } + } } else { - ProcessMouseEvent(event); } } @@ -1103,7 +1145,7 @@ int Render::Run() { } std::string host_name = it.first; - PeerPtr* peer_client = peer_map_[host_name]; + PeerPtr* peer_client = props->peer_; if (peer_client) { std::string client_id; if (host_name == client_id_) { diff --git a/src/single_window/render.h b/src/single_window/render.h index f24f345..4603ca5 100644 --- a/src/single_window/render.h +++ b/src/single_window/render.h @@ -31,6 +31,9 @@ class Render { public: struct SubStreamWindowProperties { Params params_; + PeerPtr *peer_ = nullptr; + std::string local_id_ = ""; + std::string remote_id_ = ""; bool exit_ = false; bool signal_connected_ = false; SignalStatus signal_status_ = SignalStatus::SignalClosed; @@ -40,6 +43,7 @@ class Render { bool mouse_control_button_pressed_ = false; bool mouse_controller_is_started_ = false; bool audio_capture_button_pressed_ = false; + bool control_mouse_ = false; bool streaming_ = false; bool is_control_bar_in_left_ = true; bool control_bar_hovered_ = false; @@ -117,7 +121,7 @@ class Render { private: int CreateRtcConnection(); - int ConnectTo(const std::string &host_name, const char *password, + int ConnectTo(const std::string &remote_id, const char *password, bool remember_password); int CreateMainWindow(); int DestroyMainWindow(); @@ -243,6 +247,8 @@ class Render { bool screen_capturer_is_started_ = false; bool start_keyboard_capturer_ = false; bool keyboard_capturer_is_started_ = false; + bool foucs_on_main_window_ = false; + bool foucs_on_stream_window_ = false; bool audio_capture_ = false; int main_window_width_real_ = 720; int main_window_height_real_ = 540; @@ -334,7 +340,6 @@ class Render { double regenerate_password_start_time_ = 0; SignalStatus signal_status_ = SignalStatus::SignalClosed; std::string signal_status_str_ = ""; - std::string connection_status_str_ = ""; bool signal_connected_ = false; PeerPtr *peer_ = nullptr; PeerPtr *peer_reserved_ = nullptr; @@ -368,7 +373,6 @@ class Render { /* ------ sub stream window property start ------ */ std::unordered_map> client_properties_; - std::unordered_map peer_map_; /* ------ stream window property end ------ */ std::unordered_map> diff --git a/src/single_window/render_callback_func.cpp b/src/single_window/render_callback_func.cpp index 71d24ce..47553c8 100644 --- a/src/single_window/render_callback_func.cpp +++ b/src/single_window/render_callback_func.cpp @@ -43,38 +43,41 @@ int Render::ProcessMouseEvent(SDL_Event &event) { video_height = props->video_height_; render_width = props->stream_render_rect_.w; render_height = props->stream_render_rect_.h; - } - } - float ratio_x = (float)video_width / (float)render_width; - float ratio_y = (float)video_height / (float)render_height; + float ratio_x = (float)video_width / (float)render_width; + float ratio_y = (float)video_height / (float)render_height; - RemoteAction remote_action; - remote_action.m.x = (size_t)(event.button.x * ratio_x); - remote_action.m.y = (size_t)(event.button.y * ratio_y); + RemoteAction remote_action; + remote_action.m.x = (size_t)(event.button.x * ratio_x); + remote_action.m.y = (size_t)(event.button.y * ratio_y); - if (SDL_MOUSEBUTTONDOWN == event.type) { - remote_action.type = ControlType::mouse; - if (SDL_BUTTON_LEFT == event.button.button) { - remote_action.m.flag = MouseFlag::left_down; - } else if (SDL_BUTTON_RIGHT == event.button.button) { - remote_action.m.flag = MouseFlag::right_down; + if (SDL_MOUSEBUTTONDOWN == event.type) { + remote_action.type = ControlType::mouse; + if (SDL_BUTTON_LEFT == event.button.button) { + remote_action.m.flag = MouseFlag::left_down; + } else if (SDL_BUTTON_RIGHT == event.button.button) { + remote_action.m.flag = MouseFlag::right_down; + } + remote_action.m.flag = MouseFlag::move; + SendDataFrame(peer_, (const char *)&remote_action, + sizeof(remote_action)); + } else if (SDL_MOUSEBUTTONUP == event.type) { + remote_action.type = ControlType::mouse; + if (SDL_BUTTON_LEFT == event.button.button) { + remote_action.m.flag = MouseFlag::left_up; + } else if (SDL_BUTTON_RIGHT == event.button.button) { + remote_action.m.flag = MouseFlag::right_up; + } + remote_action.m.flag = MouseFlag::move; + SendDataFrame(peer_, (const char *)&remote_action, + sizeof(remote_action)); + } else if (SDL_MOUSEMOTION == event.type) { + remote_action.type = ControlType::mouse; + remote_action.m.flag = MouseFlag::move; + SendDataFrame(peer_, (const char *)&remote_action, + sizeof(remote_action)); + } } - remote_action.m.flag = MouseFlag::move; - SendDataFrame(peer_, (const char *)&remote_action, sizeof(remote_action)); - } else if (SDL_MOUSEBUTTONUP == event.type) { - remote_action.type = ControlType::mouse; - if (SDL_BUTTON_LEFT == event.button.button) { - remote_action.m.flag = MouseFlag::left_up; - } else if (SDL_BUTTON_RIGHT == event.button.button) { - remote_action.m.flag = MouseFlag::right_up; - } - remote_action.m.flag = MouseFlag::move; - SendDataFrame(peer_, (const char *)&remote_action, sizeof(remote_action)); - } else if (SDL_MOUSEMOTION == event.type) { - remote_action.type = ControlType::mouse; - remote_action.m.flag = MouseFlag::move; - SendDataFrame(peer_, (const char *)&remote_action, sizeof(remote_action)); } return 0; @@ -87,9 +90,13 @@ void Render::SdlCaptureAudioIn(void *userdata, Uint8 *stream, int len) { } if (1) { - if ("Connected" == render->connection_status_str_) { - SendAudioFrame(render->peer_, (const char *)stream, len); + for (auto it : render->client_properties_) { + auto props = it.second; + if (props->connection_status_ == ConnectionStatus::Connected) { + SendAudioFrame(props->peer_, (const char *)stream, len); + } } + } else { memcpy(render->audio_buffer_, stream, len); render->audio_len_ = len; @@ -102,8 +109,11 @@ void Render::SdlCaptureAudioOut([[maybe_unused]] void *userdata, [[maybe_unused]] Uint8 *stream, [[maybe_unused]] int len) { // Render *render = (Render *)userdata; - // if ("Connected" == render->connection_status_str_) { - // SendAudioFrame(render->peer_, (const char *)stream, len); + // for (auto it : render->client_properties_) { + // auto props = it.second; + // if (props->connection_status_ == SignalStatus::SignalConnected) { + // SendAudioFrame(props->peer_, (const char *)stream, len); + // } // } // if (!render->audio_buffer_fresh_) { @@ -223,15 +233,15 @@ void Render::OnSignalStatusCb(SignalStatus status, const char *user_id, if (!render) { return; } - std::string remote_id(user_id, user_id_size); - if (remote_id == render->client_id_) { + std::string client_id(user_id, user_id_size); + if (client_id == render->client_id_) { render->signal_status_ = status; if (SignalStatus::SignalConnecting == status) { render->signal_connected_ = false; } else if (SignalStatus::SignalConnected == status) { render->signal_connected_ = true; - LOG_INFO("[{}] connected to signal server", remote_id); + LOG_INFO("[{}] connected to signal server", client_id); } else if (SignalStatus::SignalFailed == status) { render->signal_connected_ = false; } else if (SignalStatus::SignalClosed == status) { @@ -242,6 +252,11 @@ void Render::OnSignalStatusCb(SignalStatus status, const char *user_id, render->signal_connected_ = false; } } else { + if (client_id.rfind("C-", 0) != 0) { + return; + } + + std::string remote_id(client_id.begin() + 2, client_id.end()); if (render->client_properties_.find(remote_id) == render->client_properties_.end()) { return; @@ -292,11 +307,8 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char *user_id, props->connection_status_ = status; render->show_connection_status_window_ = true; if (ConnectionStatus::Connecting == status) { - render->connection_status_str_ = "Connecting"; } else if (ConnectionStatus::Gathering == status) { - render->connection_status_str_ = "Gathering"; } else if (ConnectionStatus::Connected == status) { - render->connection_status_str_ = "Connected"; if (!render->need_to_create_stream_window_) { render->need_to_create_stream_window_ = true; } @@ -324,18 +336,15 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char *user_id, (int)(render->stream_window_height_ - render->title_bar_height_); } - if (render->peer_reserved_ || !render->is_client_mode_) { + if (!render->is_client_mode_) { render->start_screen_capturer_ = true; render->start_mouse_controller_ = true; } } else if (ConnectionStatus::Disconnected == status) { - render->connection_status_str_ = "Disconnected"; render->password_validating_time_ = 0; } else if (ConnectionStatus::Failed == status) { - render->connection_status_str_ = "Failed"; render->password_validating_time_ = 0; } else if (ConnectionStatus::Closed == status) { - render->connection_status_str_ = "Closed"; render->password_validating_time_ = 0; render->start_screen_capturer_ = false; render->start_mouse_controller_ = false; @@ -357,7 +366,6 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char *user_id, props->texture_width_); } } else if (ConnectionStatus::IncorrectPassword == status) { - render->connection_status_str_ = "Incorrect password"; render->password_validating_ = false; render->password_validating_time_++; if (render->connect_button_pressed_) { @@ -368,7 +376,6 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char *user_id, : localization::connect[render->localization_language_index_]; } } else if (ConnectionStatus::NoSuchTransmissionId == status) { - render->connection_status_str_ = "No such transmission id"; if (render->connect_button_pressed_) { props->connection_established_ = false; render->connect_button_label_ = diff --git a/thirdparty/projectx b/thirdparty/projectx index 9972047..f48d940 160000 --- a/thirdparty/projectx +++ b/thirdparty/projectx @@ -1 +1 @@ -Subproject commit 9972047199857df55e8b3715689b642b301ec593 +Subproject commit f48d940b8059106363b3d63e4ff87bc4a4f12cfe