mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
Use one thread to process render and mouse/key events
This commit is contained in:
@@ -33,26 +33,7 @@ std::string window_title = "Remote Desk Client";
|
|||||||
|
|
||||||
int thread_exit = 0;
|
int thread_exit = 0;
|
||||||
|
|
||||||
int refresh_video(void *opaque) {
|
inline void FreshVideo() {
|
||||||
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) {
|
|
||||||
sdlRect.x = 0;
|
sdlRect.x = 0;
|
||||||
sdlRect.y = 0;
|
sdlRect.y = 0;
|
||||||
sdlRect.w = screen_w;
|
sdlRect.w = screen_w;
|
||||||
@@ -62,22 +43,20 @@ int GetVideoEvent(void *opaque) {
|
|||||||
SDL_RenderClear(sdlRenderer);
|
SDL_RenderClear(sdlRenderer);
|
||||||
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect);
|
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect);
|
||||||
SDL_RenderPresent(sdlRenderer);
|
SDL_RenderPresent(sdlRenderer);
|
||||||
} else if (ev.type == SDL_WINDOWEVENT) {
|
|
||||||
// If Resize
|
frame_count++;
|
||||||
SDL_GetWindowSize(screen, &screen_w, &screen_h);
|
end_time = SDL_GetTicks();
|
||||||
printf("Resize windows: %dx%d\n", screen_w, screen_h);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
inline int ProcessMouseKeyEven(SDL_Event &ev) {
|
||||||
}
|
|
||||||
|
|
||||||
int getMouseKeyEven(void *opaque) {
|
|
||||||
SDL_Event ev;
|
|
||||||
int quit = 0;
|
|
||||||
while (!quit) {
|
|
||||||
while (SDL_PollEvent(&ev)) {
|
|
||||||
if (SDL_KEYDOWN == ev.type) // SDL_KEYUP
|
if (SDL_KEYDOWN == ev.type) // SDL_KEYUP
|
||||||
{
|
{
|
||||||
if (SDLK_DOWN == ev.key.keysym.sym) {
|
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);
|
printf("SDL_MOUSEMOTION x, y %d %d ...............\n", px, py);
|
||||||
} else if (SDL_QUIT == ev.type) {
|
} 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;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -148,7 +128,6 @@ std::string GetMac() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "Mac: " << GetMac() << std::endl;
|
|
||||||
Params params;
|
Params params;
|
||||||
params.cfg_path = "config.ini";
|
params.cfg_path = "config.ini";
|
||||||
params.on_receive_buffer = GuestReceiveBuffer;
|
params.on_receive_buffer = GuestReceiveBuffer;
|
||||||
@@ -182,43 +161,22 @@ int main() {
|
|||||||
sdlTexture = SDL_CreateTexture(sdlRenderer, pixformat,
|
sdlTexture = SDL_CreateTexture(sdlRenderer, pixformat,
|
||||||
SDL_TEXTUREACCESS_STREAMING, pixel_w, pixel_h);
|
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;
|
SDL_Event event;
|
||||||
start_time = SDL_GetTicks();
|
start_time = SDL_GetTicks();
|
||||||
while (1) {
|
while (1) {
|
||||||
// Wait
|
// Wait
|
||||||
SDL_WaitEvent(&event);
|
// SDL_WaitEvent(&event);
|
||||||
|
while (SDL_PollEvent(&event)) {
|
||||||
if (event.type == REFRESH_EVENT) {
|
if (event.type == REFRESH_EVENT) {
|
||||||
sdlRect.x = 0;
|
FreshVideo();
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (event.type == SDL_WINDOWEVENT) {
|
} else if (event.type == SDL_WINDOWEVENT) {
|
||||||
// If Resize
|
|
||||||
SDL_GetWindowSize(screen, &screen_w, &screen_h);
|
SDL_GetWindowSize(screen, &screen_w, &screen_h);
|
||||||
printf("Resize windows: %dx%d\n", screen_w, screen_h);
|
printf("Resize windows: %dx%d\n", screen_w, screen_h);
|
||||||
} else if (event.type == SDL_QUIT) {
|
} else if (event.type == SDL_QUIT) {
|
||||||
break;
|
return 0;
|
||||||
|
} else {
|
||||||
|
ProcessMouseKeyEven(event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -103,14 +103,22 @@ int RemoteDeskServer::Init() {
|
|||||||
rect.right = GetSystemMetrics(SM_CXSCREEN);
|
rect.right = GetSystemMetrics(SM_CXSCREEN);
|
||||||
rect.bottom = GetSystemMetrics(SM_CYSCREEN);
|
rect.bottom = GetSystemMetrics(SM_CYSCREEN);
|
||||||
|
|
||||||
|
last_frame_time_ = std::chrono::high_resolution_clock::now();
|
||||||
screen_capture->Init(
|
screen_capture->Init(
|
||||||
rect, 60,
|
rect, 60,
|
||||||
[this](unsigned char *data, int size, int width, int height) -> void {
|
[this](unsigned char *data, int size, int width, int height) -> void {
|
||||||
// std::cout << "Send" << std::endl;
|
// 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_);
|
BGRAToNV12FFmpeg(data, width, height, (unsigned char *)nv12_buffer_);
|
||||||
SendData(peer, DATA_TYPE::VIDEO, (const char *)nv12_buffer_,
|
SendData(peer, DATA_TYPE::VIDEO, (const char *)nv12_buffer_,
|
||||||
NV12_BUFFER_SIZE);
|
NV12_BUFFER_SIZE);
|
||||||
// std::this_thread::sleep_for(std::chrono::milliseconds(30));
|
last_frame_time_ = now_time;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
screen_capture->Start();
|
screen_capture->Start();
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ class RemoteDeskServer {
|
|||||||
ScreenCaptureWgc* screen_capture = nullptr;
|
ScreenCaptureWgc* screen_capture = nullptr;
|
||||||
|
|
||||||
char* nv12_buffer_ = nullptr;
|
char* nv12_buffer_ = nullptr;
|
||||||
|
std::chrono::steady_clock::time_point last_frame_time_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -9,57 +9,7 @@
|
|||||||
#include "ikcp.h"
|
#include "ikcp.h"
|
||||||
#include "log.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;
|
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 = {
|
const std::vector<std::string> ice_status = {
|
||||||
"JUICE_STATE_DISCONNECTED", "JUICE_STATE_GATHERING",
|
"JUICE_STATE_DISCONNECTED", "JUICE_STATE_GATHERING",
|
||||||
@@ -102,9 +52,9 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) {
|
|||||||
static_cast<IceTransmission *>(user);
|
static_cast<IceTransmission *>(user);
|
||||||
return ice_transmission_obj->ice_agent_->Send(buf, len);
|
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_nodelay(kcp, 1, 20, 2, 1);
|
||||||
// ikcp_setmtu(kcp_, 4000);
|
// ikcp_setmtu(kcp, 4000);
|
||||||
// kcp_->rx_minrto = 10;
|
// kcp_->rx_minrto = 10;
|
||||||
// kcp_->fastresend = 1;
|
// 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);
|
ikcp_release(kcp);
|
||||||
|
|||||||
Reference in New Issue
Block a user