mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 20:25:34 +08:00
Fix kcp transmission error
This commit is contained in:
105
remote_desk_client/remote_desk_client.cpp
Normal file
105
remote_desk_client/remote_desk_client.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "x.h"
|
||||
|
||||
#define SDL_MAIN_HANDLED
|
||||
#include "SDL2/SDL.h"
|
||||
|
||||
int screen_w = 1280, screen_h = 720;
|
||||
const int pixel_w = 1280, pixel_h = 720;
|
||||
|
||||
unsigned char dst_buffer[pixel_w * pixel_h * 3 / 2];
|
||||
SDL_Texture *sdlTexture = nullptr;
|
||||
SDL_Renderer *sdlRenderer = nullptr;
|
||||
SDL_Rect sdlRect;
|
||||
|
||||
// Refresh Event
|
||||
#define REFRESH_EVENT (SDL_USEREVENT + 1)
|
||||
#define QUIT_EVENT (SDL_USEREVENT + 2)
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void GuestReceiveBuffer(const char *data, size_t size, const char *user_id,
|
||||
size_t user_id_size) {
|
||||
std::cout << "Receive size: " << size << std::endl;
|
||||
memcpy(dst_buffer, data, size);
|
||||
}
|
||||
|
||||
int main() {
|
||||
Params params;
|
||||
params.cfg_path = "config.ini";
|
||||
params.on_receive_buffer = GuestReceiveBuffer;
|
||||
|
||||
std::string transmission_id = "000000";
|
||||
std::string user_id = "Client";
|
||||
|
||||
PeerPtr *peer = CreatePeer(¶ms);
|
||||
JoinConnection(peer, transmission_id.c_str(), user_id.c_str());
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO)) {
|
||||
printf("Could not initialize SDL - %s\n", SDL_GetError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_Window *screen;
|
||||
screen = SDL_CreateWindow("RTS Receiver", SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED, screen_w, screen_h,
|
||||
SDL_WINDOW_RESIZABLE);
|
||||
if (!screen) {
|
||||
printf("SDL: could not create window - exiting:%s\n", SDL_GetError());
|
||||
return -1;
|
||||
}
|
||||
sdlRenderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED);
|
||||
|
||||
Uint32 pixformat = 0;
|
||||
pixformat = SDL_PIXELFORMAT_NV12;
|
||||
|
||||
sdlTexture = SDL_CreateTexture(sdlRenderer, pixformat,
|
||||
SDL_TEXTUREACCESS_STREAMING, pixel_w, pixel_h);
|
||||
|
||||
SDL_Thread *refresh_thread = SDL_CreateThread(refresh_video, NULL, NULL);
|
||||
SDL_Event event;
|
||||
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);
|
||||
} 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;
|
||||
}
|
||||
@@ -8,7 +8,7 @@ extern "C" {
|
||||
#include <libswscale/swscale.h>
|
||||
};
|
||||
|
||||
#define NV12_BUFFER_SIZE 2560 * 1440 * 3 / 2
|
||||
#define NV12_BUFFER_SIZE 1280 * 720 * 3 / 2
|
||||
|
||||
RemoteDeskServer ::RemoteDeskServer() {}
|
||||
|
||||
@@ -23,14 +23,14 @@ int BGRAToNV12FFmpeg(unsigned char *src_buffer, int width, int height,
|
||||
unsigned char *dst_buffer) {
|
||||
AVFrame *Input_pFrame = av_frame_alloc();
|
||||
AVFrame *Output_pFrame = av_frame_alloc();
|
||||
struct SwsContext *img_convert_ctx = sws_getContext(
|
||||
width, height, AV_PIX_FMT_BGRA, width, height, AV_PIX_FMT_NV12,
|
||||
struct SwsContext *img_convert_ctx =
|
||||
sws_getContext(width, height, AV_PIX_FMT_BGRA, 1280, 720, AV_PIX_FMT_NV12,
|
||||
SWS_FAST_BILINEAR, nullptr, nullptr, nullptr);
|
||||
|
||||
av_image_fill_arrays(Input_pFrame->data, Input_pFrame->linesize, src_buffer,
|
||||
AV_PIX_FMT_BGRA, width, height, 1);
|
||||
av_image_fill_arrays(Output_pFrame->data, Output_pFrame->linesize, dst_buffer,
|
||||
AV_PIX_FMT_NV12, width, height, 1);
|
||||
AV_PIX_FMT_NV12, 1280, 720, 1);
|
||||
|
||||
sws_scale(img_convert_ctx, (uint8_t const **)Input_pFrame->data,
|
||||
Input_pFrame->linesize, 0, height, Output_pFrame->data,
|
||||
@@ -85,7 +85,7 @@ int RemoteDeskServer::Init() {
|
||||
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(100));
|
||||
// std::this_thread::sleep_for(std::chrono::milliseconds(30));
|
||||
});
|
||||
|
||||
screen_capture->Start();
|
||||
|
||||
11
xmake.lua
11
xmake.lua
@@ -43,6 +43,17 @@ target("remote_desk_server")
|
||||
add_includedirs("../../src/interface")
|
||||
-- add_links("avformat", "swscale")
|
||||
|
||||
target("remote_desk_client")
|
||||
set_kind("binary")
|
||||
add_deps("projectx")
|
||||
add_packages("log")
|
||||
add_packages("ffmpeg")
|
||||
add_packages("vcpkg::sdl2")
|
||||
add_files("remote_desk_client/*.cpp")
|
||||
add_includedirs("../../src/interface")
|
||||
add_links("SDL2-static", "SDL2main", "Shell32", "gdi32", "winmm",
|
||||
"setupapi", "version", "WindowsApp", "Imm32", "avutil")
|
||||
|
||||
-- target("remote_desk")
|
||||
-- set_kind("binary")
|
||||
-- add_deps("projectx")
|
||||
|
||||
Reference in New Issue
Block a user