Use OOP to refactor main function

This commit is contained in:
dijunkun
2024-06-03 15:23:37 +08:00
parent d150c374b5
commit 6f703c8267
9 changed files with 414 additions and 1204 deletions

View File

@@ -4,13 +4,15 @@
#include <iostream>
#include <string>
#include "device_controller_factory.h"
#include "localization.h"
#include "log.h"
#include "platform.h"
#include "screen_capturer_factory.h"
// Refresh Event
#define REFRESH_EVENT (SDL_USEREVENT + 1)
#define QUIT_EVENT (SDL_USEREVENT + 2)
#define NV12_BUFFER_SIZE 1280 * 720 * 3 / 2
MainWindow::MainWindow() {}
@@ -111,6 +113,97 @@ int MainWindow::Run() {
mac_addr_str_ = GetMac();
std::thread rtc_thread(
[this](int screen_width, int screen_height) {
std::string default_cfg_path = "../../../../config/config.ini";
std::ifstream f(default_cfg_path.c_str());
std::string mac_addr_str_ = GetMac();
params_.cfg_path =
f.good() ? "../../../../config/config.ini" : "config.ini";
params_.on_receive_video_buffer = OnReceiveVideoBufferCb;
params_.on_receive_audio_buffer = OnReceiveAudioBufferCb;
params_.on_receive_data_buffer = OnReceiveDataBufferCb;
params_.on_signal_status = OnSignalStatusCb;
params_.on_connection_status = OnConnectionStatusCb;
params_.user_data = this;
std::string transmission_id = "000001";
peer_ = CreatePeer(&params_);
LOG_INFO("Create peer");
std::string server_user_id = "S-" + mac_addr_str_;
Init(peer_, server_user_id.c_str());
LOG_INFO("peer_ init finish");
{
while (SignalStatus::SignalConnected != signal_status_ && !exit_) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
if (exit_) {
return;
}
std::string user_id = "S-" + mac_addr_str_;
is_create_connection_ =
CreateConnection(peer_, mac_addr_str_.c_str(), input_password_)
? false
: true;
nv12_buffer_ = new char[NV12_BUFFER_SIZE];
// Screen capture
screen_capturer_factory_ = new ScreenCapturerFactory();
screen_capturer_ =
(ScreenCapturer *)screen_capturer_factory_->Create();
last_frame_time_ = std::chrono::high_resolution_clock::now();
ScreenCapturer::RECORD_DESKTOP_RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = screen_width_;
rect.bottom = screen_height_;
int screen_capturer_init_ret = screen_capturer_->Init(
rect, 60,
[this](unsigned char *data, int size, int width,
int height) -> void {
auto now_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration =
now_time - last_frame_time_;
auto tc = duration.count() * 1000;
if (tc >= 0) {
SendData(peer_, DATA_TYPE::VIDEO, (const char *)data,
NV12_BUFFER_SIZE);
last_frame_time_ = now_time;
}
});
if (0 == screen_capturer_init_ret) {
screen_capturer_->Start();
} else {
screen_capturer_->Destroy();
screen_capturer_ = nullptr;
}
// Mouse control
device_controller_factory_ = new DeviceControllerFactory();
mouse_controller_ =
(MouseController *)device_controller_factory_->Create(
DeviceControllerFactory::Device::Mouse);
int mouse_controller_init_ret =
mouse_controller_->Init(screen_width_, screen_height_);
if (0 != mouse_controller_init_ret) {
mouse_controller_->Destroy();
mouse_controller_ = nullptr;
}
}
},
screen_width_, screen_height_);
// Main loop
while (!exit_) {
localization_language_ = config_center_.GetLanguage();
@@ -122,9 +215,14 @@ int MainWindow::Run() {
localization_language_index_last_ = localization_language_index_;
}
connect_button_label_ = localization::connect[localization_language_index_];
connect_button_label_ =
connect_button_pressed_
? localization::disconnect[localization_language_index_]
: localization::connect[localization_language_index_];
fullscreen_button_label_ =
localization::fullscreen[localization_language_index_];
fullscreen_button_pressed_
? localization::exit_fullscreen[localization_language_index_]
: localization::fullscreen[localization_language_index_];
// Start the Dear ImGui frame
ImGui_ImplSDLRenderer2_NewFrame();
@@ -257,18 +355,28 @@ int MainWindow::Run() {
ImGui::Spacing();
if (ImGui::Button(connect_button_label_.c_str())) {
LOG_ERROR("Click connect button");
int ret = -1;
if ("ClientSignalConnected" == client_signal_status_str_) {
if ("SignalConnected" == signal_status_str_) {
if (connect_button_label_ ==
localization::connect[localization_language_index_] &&
!connection_established_) {
std::string user_id = "C-" + mac_addr_str_;
ret = JoinConnection(peer_, remote_id_, client_password_);
if (0 == ret) {
}
} else if (connect_button_label_ ==
localization::disconnect
[localization_language_index_] &&
connection_established_) {
ret = LeaveConnection(peer_);
is_create_connection_ = CreateConnection(
peer_, mac_addr_str_.c_str(), client_password_);
memset(audio_buffer_, 0, 960);
if (0 == ret) {
connection_established_ = false;
received_frame_ = false;
}
}
if (0 == ret) {
@@ -277,7 +385,6 @@ int MainWindow::Run() {
connect_button_label_ =
connect_button_pressed_
? localization::disconnect[localization_language_index_]
: localization::connect[localization_language_index_];
}
}
@@ -294,17 +401,22 @@ int MainWindow::Run() {
if (ImGui::Button(fullscreen_button_label_.c_str())) {
if (fullscreen_button_label_ ==
localization::fullscreen[localization_language_index_]) {
main_window_width_before_fullscreen_ = main_window_width_;
main_window_height_before_fullscreen_ = main_window_height_;
SDL_SetWindowFullscreen(main_window_, SDL_WINDOW_FULLSCREEN_DESKTOP);
fullscreen_button_pressed_ = true;
} else {
SDL_SetWindowFullscreen(main_window_, SDL_FALSE);
SDL_SetWindowSize(main_window_, main_window_width_before_fullscreen_,
main_window_height_before_fullscreen_);
}
fullscreen_button_pressed_ = !fullscreen_button_pressed_;
fullscreen_button_label_ =
fullscreen_button_pressed_
? localization::exit_fullscreen[localization_language_index_]
: localization::fullscreen[localization_language_index_];
fullscreen_button_pressed_ = false;
}
ImGui::End();
}
@@ -343,18 +455,21 @@ int MainWindow::Run() {
event.window.windowID == SDL_GetWindowID(main_window_)) {
exit_ = true;
} else if (event.type == REFRESH_EVENT) {
sdlRect.x = 0;
sdlRect.y = 0;
sdlRect.w = main_window_width_;
sdlRect.h = main_window_height_;
sdl_rect_.x = 0;
sdl_rect_.y = 0;
sdl_rect_.w = main_window_width_;
sdl_rect_.h = main_window_height_;
// SDL_UpdateTexture(sdl_texture_, NULL, dst_buffer, pixel_w);
SDL_UpdateTexture(sdl_texture_, NULL, dst_buffer_, 1280);
} else {
if (connection_established_) {
ProcessMouseKeyEven(event);
}
}
}
SDL_RenderClear(sdl_renderer_);
SDL_RenderCopy(sdl_renderer_, sdl_texture_, NULL, &sdlRect);
SDL_RenderCopy(sdl_renderer_, sdl_texture_, NULL, &sdl_rect_);
if (!connection_established_ || !received_frame_) {
SDL_RenderClear(sdl_renderer_);
@@ -373,10 +488,8 @@ int MainWindow::Run() {
fps_ = frame_count_ / (elapsed_time_ / 1000);
frame_count_ = 0;
window_title = "Remote Desk Client FPS [" + std::to_string(fps_) +
"] status [" + server_signal_status_str_ + "|" +
client_signal_status_str_ + "|" +
server_connection_status_str_ + "|" +
client_connection_status_str_ + "]";
"] status [" + connection_status_str_ + "|" +
connection_status_str_ + "]";
// For MacOS, UI frameworks can only be called from the main thread
SDL_SetWindowTitle(main_window_, window_title.c_str());
start_time_ = end_time_;
@@ -384,6 +497,23 @@ int MainWindow::Run() {
}
// Cleanup
if (is_create_connection_) {
LeaveConnection(peer_);
}
rtc_thread.join();
SDL_CloseAudioDevice(output_dev_);
SDL_CloseAudioDevice(input_dev_);
if (screen_capturer_) {
screen_capturer_->Destroy();
}
if (mouse_controller_) {
mouse_controller_->Destroy();
}
ImGui_ImplSDLRenderer2_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();

View File

@@ -10,12 +10,16 @@
#include <SDL.h>
#include <atomic>
#include <chrono>
#include <string>
#include "../../thirdparty/projectx/src/interface/x.h"
#include "config_center.h"
#include "device_controller_factory.h"
#include "imgui.h"
#include "imgui_impl_sdl2.h"
#include "imgui_impl_sdlrenderer2.h"
#include "screen_capturer_factory.h"
class MainWindow {
public:
@@ -25,6 +29,28 @@ class MainWindow {
public:
int Run();
public:
static void OnReceiveVideoBufferCb(const char *data, size_t size,
const char *user_id, size_t user_id_size,
void *user_data);
static void OnReceiveAudioBufferCb(const char *data, size_t size,
const char *user_id, size_t user_id_size,
void *user_data);
static void OnReceiveDataBufferCb(const char *data, size_t size,
const char *user_id, size_t user_id_size,
void *user_data);
static void OnSignalStatusCb(SignalStatus status, void *user_data);
static void OnConnectionStatusCb(ConnectionStatus status, void *user_data);
private:
int ProcessMouseKeyEven(SDL_Event &ev);
void SdlCaptureAudioIn(void *userdata, Uint8 *stream, int len);
void SdlCaptureAudioOut(void *userdata, Uint8 *stream, int len);
private:
typedef struct {
char password[7];
@@ -42,46 +68,41 @@ class MainWindow {
int localization_language_index_ = -1;
int localization_language_index_last_ = -1;
private:
int default_main_window_width_ = 1280;
int default_main_window_height_ = 720;
private:
std::string window_title = "Remote Desk Client";
std::string mac_addr_str_ = "";
std::string connect_button_label_ = "Connect";
std::string fullscreen_button_label_ = "Fullscreen";
char input_password_tmp_[7] = "";
char input_password_[7] = "";
char remote_id_[20] = "";
char client_password_[20] = "";
private:
int screen_width_ = 1280;
int screen_height_ = 720;
int main_window_width_ = 1280;
int main_window_height_ = 720;
int main_window_width_before_fullscreen_ = 1280;
int main_window_height_before_fullscreen_ = 720;
int texture_width_ = 1280;
int texture_height_ = 720;
SDL_Texture *sdl_texture_ = nullptr;
SDL_Renderer *sdl_renderer_ = nullptr;
SDL_Rect sdlRect;
SDL_Rect sdl_rect_;
SDL_Window *main_window_;
uint32_t pixformat_ = 0;
std::string mac_addr_str_ = "";
bool exit_ = false;
std::string connect_button_label_ = "Connect";
std::string fullscreen_button_label_ = "Fullscreen";
bool connection_established_ = false;
bool menu_hovered_ = false;
char input_password_tmp_[7] = "";
char input_password_[7] = "";
char remote_id_[20] = "";
char client_password_[20] = "";
bool connect_button_pressed_ = false;
bool fullscreen_button_pressed_ = false;
bool received_frame_ = false;
bool is_create_connection_ = false;
bool audio_buffer_fresh_ = false;
int fps_ = 0;
uint32_t start_time_;
@@ -89,13 +110,35 @@ class MainWindow {
uint32_t elapsed_time_;
uint32_t frame_count_ = 0;
bool received_frame_ = false;
private:
ConnectionStatus connection_status_ = ConnectionStatus::Closed;
SignalStatus signal_status_ = SignalStatus::SignalClosed;
std::string signal_status_str_ = "";
std::string connection_status_str_ = "";
private:
std::string server_connection_status_str_ = "-";
std::string client_connection_status_str_ = "-";
std::string server_signal_status_str_ = "-";
std::string client_signal_status_str_ = "-";
PeerPtr *peer_ = nullptr;
Params params_;
private:
SDL_AudioDeviceID input_dev_;
SDL_AudioDeviceID output_dev_;
unsigned char audio_buffer_[960];
int audio_len_ = 0;
char *nv12_buffer_ = nullptr;
unsigned char *dst_buffer_ = new unsigned char[1280 * 720 * 3];
private:
ScreenCapturerFactory *screen_capturer_factory_ = nullptr;
ScreenCapturer *screen_capturer_ = nullptr;
DeviceControllerFactory *device_controller_factory_ = nullptr;
MouseController *mouse_controller_ = nullptr;
#ifdef __linux__
std::chrono::_V2::system_clock::time_point last_frame_time_;
#else
std::chrono::steady_clock::time_point last_frame_time_;
#endif
};
#endif

View File

@@ -0,0 +1,187 @@
#include "device_controller.h"
#include "localization.h"
#include "main_window.h"
// Refresh Event
#define REFRESH_EVENT (SDL_USEREVENT + 1)
int MainWindow::ProcessMouseKeyEven(SDL_Event &ev) {
float ratio = (float)(1280.0 / main_window_width_);
RemoteAction remote_action;
remote_action.m.x = (size_t)(ev.button.x * ratio);
remote_action.m.y = (size_t)(ev.button.y * ratio);
if (SDL_KEYDOWN == ev.type) // SDL_KEYUP
{
// printf("SDLK_DOWN: %d\n", SDL_KeyCode(ev.key.keysym.sym));
if (SDLK_DOWN == ev.key.keysym.sym) {
// printf("SDLK_DOWN \n");
} else if (SDLK_UP == ev.key.keysym.sym) {
// printf("SDLK_UP \n");
} else if (SDLK_LEFT == ev.key.keysym.sym) {
// printf("SDLK_LEFT \n");
} else if (SDLK_RIGHT == ev.key.keysym.sym) {
// printf("SDLK_RIGHT \n");
}
} else if (SDL_MOUSEBUTTONDOWN == ev.type) {
remote_action.type = ControlType::mouse;
if (SDL_BUTTON_LEFT == ev.button.button) {
remote_action.m.flag = MouseFlag::left_down;
} else if (SDL_BUTTON_RIGHT == ev.button.button) {
remote_action.m.flag = MouseFlag::right_down;
}
SendData(peer_, DATA_TYPE::DATA, (const char *)&remote_action,
sizeof(remote_action));
} else if (SDL_MOUSEBUTTONUP == ev.type) {
remote_action.type = ControlType::mouse;
if (SDL_BUTTON_LEFT == ev.button.button) {
remote_action.m.flag = MouseFlag::left_up;
} else if (SDL_BUTTON_RIGHT == ev.button.button) {
remote_action.m.flag = MouseFlag::right_up;
}
SendData(peer_, DATA_TYPE::DATA, (const char *)&remote_action,
sizeof(remote_action));
} else if (SDL_MOUSEMOTION == ev.type) {
remote_action.type = ControlType::mouse;
remote_action.m.flag = MouseFlag::move;
SendData(peer_, DATA_TYPE::DATA, (const char *)&remote_action,
sizeof(remote_action));
} else if (SDL_QUIT == ev.type) {
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
printf("SDL_QUIT\n");
return 0;
}
return 0;
}
void MainWindow::SdlCaptureAudioIn(void *userdata, Uint8 *stream, int len) {
if (1) {
if ("Connected" == connection_status_str_) {
SendData(peer_, DATA_TYPE::AUDIO, (const char *)stream, len);
}
} else {
memcpy(audio_buffer_, stream, len);
audio_len_ = len;
SDL_Delay(10);
audio_buffer_fresh_ = true;
}
}
void MainWindow::SdlCaptureAudioOut(void *userdata, Uint8 *stream, int len) {
if (!audio_buffer_fresh_) {
return;
}
SDL_memset(stream, 0, len);
if (audio_len_ == 0) {
return;
} else {
}
len = (len > audio_len_ ? audio_len_ : len);
SDL_MixAudioFormat(stream, audio_buffer_, AUDIO_S16LSB, len,
SDL_MIX_MAXVOLUME);
audio_buffer_fresh_ = false;
}
void MainWindow::OnReceiveVideoBufferCb(const char *data, size_t size,
const char *user_id,
size_t user_id_size, void *user_data) {
MainWindow *main_window = (MainWindow *)user_data;
if (main_window->connection_established_) {
memcpy(main_window->dst_buffer_, data, size);
SDL_Event event;
event.type = REFRESH_EVENT;
SDL_PushEvent(&event);
main_window->received_frame_ = true;
}
}
void MainWindow::OnReceiveAudioBufferCb(const char *data, size_t size,
const char *user_id,
size_t user_id_size, void *user_data) {
MainWindow *main_window = (MainWindow *)user_data;
main_window->audio_buffer_fresh_ = true;
SDL_QueueAudio(main_window->output_dev_, data, (uint32_t)size);
}
void MainWindow::OnReceiveDataBufferCb(const char *data, size_t size,
const char *user_id, size_t user_id_size,
void *user_data) {
MainWindow *main_window = (MainWindow *)user_data;
std::string user(user_id, user_id_size);
RemoteAction remote_action;
memcpy(&remote_action, data, sizeof(remote_action));
#if MOUSE_CONTROL
if (main_window->mouse_controller_) {
main_window->mouse_controller_->SendCommand(remote_action);
}
#endif
}
void MainWindow::OnSignalStatusCb(SignalStatus status, void *user_data) {
MainWindow *main_window = (MainWindow *)user_data;
main_window->signal_status_ = status;
if (SignalStatus::SignalConnecting == status) {
main_window->signal_status_str_ = "SignalConnecting";
} else if (SignalStatus::SignalConnected == status) {
main_window->signal_status_str_ = "SignalConnected";
} else if (SignalStatus::SignalFailed == status) {
main_window->signal_status_str_ = "SignalFailed";
} else if (SignalStatus::SignalClosed == status) {
main_window->signal_status_str_ = "SignalClosed";
} else if (SignalStatus::SignalReconnecting == status) {
main_window->signal_status_str_ = "SignalReconnecting";
}
}
void MainWindow::OnConnectionStatusCb(ConnectionStatus status,
void *user_data) {
MainWindow *main_window = (MainWindow *)user_data;
main_window->connection_status_ = status;
if (ConnectionStatus::Connecting == status) {
main_window->connection_status_str_ = "Connecting";
} else if (ConnectionStatus::Connected == status) {
main_window->connection_status_str_ = "Connected";
main_window->connection_established_ = true;
} else if (ConnectionStatus::Disconnected == status) {
main_window->connection_status_str_ = "Disconnected";
} else if (ConnectionStatus::Failed == status) {
main_window->connection_status_str_ = "Failed";
} else if (ConnectionStatus::Closed == status) {
main_window->connection_status_str_ = "Closed";
} else if (ConnectionStatus::IncorrectPassword == status) {
main_window->connection_status_str_ = "Incorrect password";
if (main_window->connect_button_pressed_) {
main_window->connect_button_pressed_ = false;
main_window->connection_established_ = false;
main_window->connect_button_label_ =
main_window->connect_button_pressed_
? localization::disconnect[main_window
->localization_language_index_]
: localization::connect[main_window
->localization_language_index_];
}
} else if (ConnectionStatus::NoSuchTransmissionId == status) {
main_window->connection_status_str_ = "No such transmission id";
if (main_window->connect_button_pressed_) {
main_window->connect_button_pressed_ = false;
main_window->connection_established_ = false;
main_window->connect_button_label_ =
main_window->connect_button_pressed_
? localization::disconnect[main_window
->localization_language_index_]
: localization::connect[main_window
->localization_language_index_];
}
}
}