Use one thread to process render and mouse/key events

This commit is contained in:
dijunkun
2023-09-02 00:20:55 +08:00
parent 2088bf190c
commit d3e9df260a
3 changed files with 83 additions and 116 deletions

View File

@@ -33,26 +33,7 @@ std::string window_title = "Remote Desk Client";
int thread_exit = 0;
int refresh_video(void *opaque) {
SDL_Event event;
while (thread_exit == 0) {
event.type = REFRESH_EVENT;
SDL_PushEvent(&event);
SDL_Delay(10);
}
event.type = QUIT_EVENT;
SDL_PushEvent(&event);
return 0;
}
int GetVideoEvent(void *opaque) {
SDL_Event ev;
int quit = 0;
while (!quit) {
while (SDL_PollEvent(&ev)) {
if (ev.type == REFRESH_EVENT) {
inline void FreshVideo() {
sdlRect.x = 0;
sdlRect.y = 0;
sdlRect.w = screen_w;
@@ -62,22 +43,20 @@ int GetVideoEvent(void *opaque) {
SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect);
SDL_RenderPresent(sdlRenderer);
} else if (ev.type == SDL_WINDOWEVENT) {
// If Resize
SDL_GetWindowSize(screen, &screen_w, &screen_h);
printf("Resize windows: %dx%d\n", screen_w, screen_h);
}
}
}
return 0;
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) + "]";
SDL_SetWindowTitle(screen, window_title.data());
start_time = end_time;
}
}
int getMouseKeyEven(void *opaque) {
SDL_Event ev;
int quit = 0;
while (!quit) {
while (SDL_PollEvent(&ev)) {
inline int ProcessMouseKeyEven(SDL_Event &ev) {
if (SDL_KEYDOWN == ev.type) // SDL_KEYUP
{
if (SDLK_DOWN == ev.key.keysym.sym) {
@@ -109,11 +88,12 @@ int getMouseKeyEven(void *opaque) {
printf("SDL_MOUSEMOTION x, y %d %d ...............\n", px, py);
} else if (SDL_QUIT == ev.type) {
printf("SDL_QUIT ...............\n");
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
printf("SDL_QUIT\n");
return 0;
}
}
}
return 0;
}
@@ -148,7 +128,6 @@ std::string GetMac() {
}
int main() {
std::cout << "Mac: " << GetMac() << std::endl;
Params params;
params.cfg_path = "config.ini";
params.on_receive_buffer = GuestReceiveBuffer;
@@ -182,43 +161,22 @@ int main() {
sdlTexture = SDL_CreateTexture(sdlRenderer, pixformat,
SDL_TEXTUREACCESS_STREAMING, pixel_w, pixel_h);
SDL_Thread *refresh_thread = SDL_CreateThread(refresh_video, NULL, NULL);
// SDL_Thread *mouse_thread = SDL_CreateThread(getMouseKeyEven, NULL, NULL);
SDL_Event event;
start_time = SDL_GetTicks();
while (1) {
// Wait
SDL_WaitEvent(&event);
// SDL_WaitEvent(&event);
while (SDL_PollEvent(&event)) {
if (event.type == REFRESH_EVENT) {
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) + "]";
SDL_SetWindowTitle(screen, window_title.data());
start_time = end_time;
}
FreshVideo();
} else if (event.type == SDL_WINDOWEVENT) {
// If Resize
SDL_GetWindowSize(screen, &screen_w, &screen_h);
printf("Resize windows: %dx%d\n", screen_w, screen_h);
} else if (event.type == SDL_QUIT) {
break;
return 0;
} else {
ProcessMouseKeyEven(event);
}
}
}

View File

@@ -103,14 +103,22 @@ int RemoteDeskServer::Init() {
rect.right = GetSystemMetrics(SM_CXSCREEN);
rect.bottom = GetSystemMetrics(SM_CYSCREEN);
last_frame_time_ = std::chrono::high_resolution_clock::now();
screen_capture->Init(
rect, 60,
[this](unsigned char *data, int size, int width, int height) -> void {
// std::cout << "Send" << std::endl;
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) {
BGRAToNV12FFmpeg(data, width, height, (unsigned char *)nv12_buffer_);
SendData(peer, DATA_TYPE::VIDEO, (const char *)nv12_buffer_,
NV12_BUFFER_SIZE);
// std::this_thread::sleep_for(std::chrono::milliseconds(30));
last_frame_time_ = now_time;
}
});
screen_capture->Start();

View File

@@ -20,6 +20,7 @@ class RemoteDeskServer {
ScreenCaptureWgc* screen_capture = nullptr;
char* nv12_buffer_ = nullptr;
std::chrono::steady_clock::time_point last_frame_time_;
};
#endif