mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 12:15:34 +08:00
Use one thread to process render and mouse/key events
This commit is contained in:
@@ -33,86 +33,66 @@ 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;
|
||||
inline void FreshVideo() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
inline int ProcessMouseKeyEven(SDL_Event &ev) {
|
||||
if (SDL_KEYDOWN == ev.type) // SDL_KEYUP
|
||||
{
|
||||
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) {
|
||||
if (SDL_BUTTON_LEFT == ev.button.button) {
|
||||
int px = ev.button.x;
|
||||
int py = ev.button.y;
|
||||
printf("SDL_MOUSEBUTTONDOWN x, y %d %d ...............\n", px, py);
|
||||
|
||||
} else if (SDL_BUTTON_RIGHT == ev.button.button) {
|
||||
int px = ev.button.x;
|
||||
int py = ev.button.y;
|
||||
printf("SDL_BUTTON_RIGHT x, y %d %d ...............\n", px, py);
|
||||
}
|
||||
} else if (SDL_MOUSEMOTION == ev.type) {
|
||||
int px = ev.motion.x;
|
||||
int py = ev.motion.y;
|
||||
|
||||
printf("SDL_MOUSEMOTION x, y %d %d ...............\n", px, py);
|
||||
} else if (SDL_QUIT == ev.type) {
|
||||
SDL_Event event;
|
||||
event.type = SDL_QUIT;
|
||||
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) {
|
||||
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);
|
||||
} 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;
|
||||
}
|
||||
|
||||
int getMouseKeyEven(void *opaque) {
|
||||
SDL_Event ev;
|
||||
int quit = 0;
|
||||
while (!quit) {
|
||||
while (SDL_PollEvent(&ev)) {
|
||||
if (SDL_KEYDOWN == ev.type) // SDL_KEYUP
|
||||
{
|
||||
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) {
|
||||
if (SDL_BUTTON_LEFT == ev.button.button) {
|
||||
int px = ev.button.x;
|
||||
int py = ev.button.y;
|
||||
printf("SDL_MOUSEBUTTONDOWN x, y %d %d ...............\n", px, py);
|
||||
|
||||
} else if (SDL_BUTTON_RIGHT == ev.button.button) {
|
||||
int px = ev.button.x;
|
||||
int py = ev.button.y;
|
||||
printf("SDL_BUTTON_RIGHT x, y %d %d ...............\n", px, py);
|
||||
}
|
||||
} else if (SDL_MOUSEMOTION == ev.type) {
|
||||
int px = ev.motion.x;
|
||||
int py = ev.motion.y;
|
||||
|
||||
printf("SDL_MOUSEMOTION x, y %d %d ...............\n", px, py);
|
||||
} else if (SDL_QUIT == ev.type) {
|
||||
printf("SDL_QUIT ...............\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
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);
|
||||
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;
|
||||
// SDL_WaitEvent(&event);
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == REFRESH_EVENT) {
|
||||
FreshVideo();
|
||||
} else if (event.type == SDL_WINDOWEVENT) {
|
||||
SDL_GetWindowSize(screen, &screen_w, &screen_h);
|
||||
printf("Resize windows: %dx%d\n", screen_w, screen_h);
|
||||
} else if (event.type == SDL_QUIT) {
|
||||
return 0;
|
||||
} else {
|
||||
ProcessMouseKeyEven(event);
|
||||
}
|
||||
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
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));
|
||||
|
||||
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);
|
||||
last_frame_time_ = now_time;
|
||||
}
|
||||
});
|
||||
|
||||
screen_capture->Start();
|
||||
|
||||
@@ -20,6 +20,7 @@ class RemoteDeskServer {
|
||||
ScreenCaptureWgc* screen_capture = nullptr;
|
||||
|
||||
char* nv12_buffer_ = nullptr;
|
||||
std::chrono::steady_clock::time_point last_frame_time_;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user