mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 20:25: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
|
||||
@@ -9,57 +9,7 @@
|
||||
#include "ikcp.h"
|
||||
#include "log.h"
|
||||
|
||||
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
|
||||
#include <windows.h>
|
||||
#elif !defined(__unix)
|
||||
#define __unix
|
||||
#endif
|
||||
|
||||
#ifdef __unix
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
using nlohmann::json;
|
||||
static int count = 1;
|
||||
|
||||
static inline void itimeofday(long *sec, long *usec) {
|
||||
#if defined(__unix)
|
||||
struct timeval time;
|
||||
gettimeofday(&time, NULL);
|
||||
if (sec) *sec = time.tv_sec;
|
||||
if (usec) *usec = time.tv_usec;
|
||||
#else
|
||||
static long mode = 0, addsec = 0;
|
||||
BOOL retval;
|
||||
static IINT64 freq = 1;
|
||||
IINT64 qpc;
|
||||
if (mode == 0) {
|
||||
retval = QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
|
||||
freq = (freq == 0) ? 1 : freq;
|
||||
retval = QueryPerformanceCounter((LARGE_INTEGER *)&qpc);
|
||||
addsec = (long)time(NULL);
|
||||
addsec = addsec - (long)((qpc / freq) & 0x7fffffff);
|
||||
mode = 1;
|
||||
}
|
||||
retval = QueryPerformanceCounter((LARGE_INTEGER *)&qpc);
|
||||
retval = retval * 2;
|
||||
if (sec) *sec = (long)(qpc / freq) + addsec;
|
||||
if (usec) *usec = (long)((qpc % freq) * 1000000 / freq);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline IINT64 iclock64(void) {
|
||||
long s, u;
|
||||
IINT64 value;
|
||||
itimeofday(&s, &u);
|
||||
value = ((IINT64)s) * 1000 + (u / 1000);
|
||||
return value;
|
||||
}
|
||||
|
||||
static inline IUINT32 iclock() { return (IUINT32)(iclock64() & 0xfffffffful); }
|
||||
|
||||
const std::vector<std::string> ice_status = {
|
||||
"JUICE_STATE_DISCONNECTED", "JUICE_STATE_GATHERING",
|
||||
@@ -102,9 +52,9 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) {
|
||||
static_cast<IceTransmission *>(user);
|
||||
return ice_transmission_obj->ice_agent_->Send(buf, len);
|
||||
});
|
||||
ikcp_wndsize(kcp, 1024, 1280);
|
||||
ikcp_wndsize(kcp, 2048, 2048);
|
||||
ikcp_nodelay(kcp, 1, 20, 2, 1);
|
||||
// ikcp_setmtu(kcp_, 4000);
|
||||
// ikcp_setmtu(kcp, 4000);
|
||||
// kcp_->rx_minrto = 10;
|
||||
// kcp_->fastresend = 1;
|
||||
|
||||
@@ -147,7 +97,7 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) {
|
||||
}
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
// std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
|
||||
ikcp_release(kcp);
|
||||
|
||||
Reference in New Issue
Block a user