diff --git a/remote_desk_gui/main.cpp b/remote_desk_gui/main.cpp index c5d6ced..90a2383 100644 --- a/remote_desk_gui/main.cpp +++ b/remote_desk_gui/main.cpp @@ -215,7 +215,7 @@ void ReceiveDataBuffer(const char *data, size_t size, const char *user_id, std::cout << "remote_action: " << remote_action.type << " " << remote_action.m.flag << " " << remote_action.m.x << " " << remote_action.m.y << std::endl; - +#ifdef _WIN32 INPUT ip; if (remote_action.type == ControlType::mouse) { @@ -247,6 +247,7 @@ void ReceiveDataBuffer(const char *data, size_t size, const char *user_id, << ip.mi.dwFlags << " " << ip.mi.dx << " " << ip.mi.dy << std::endl; } +#endif } std::string GetMac(char *mac_addr) { @@ -470,7 +471,7 @@ int main() { CreateConnection(peer_server, mac_addr, user_id.c_str()); nv12_buffer_ = new char[NV12_BUFFER_SIZE]; - +#ifdef _WIN32 screen_capture = new ScreenCaptureWgc(); RECORD_DESKTOP_RECT rect; @@ -504,6 +505,7 @@ int main() { }); screen_capture->Start(); +#endif } else { LeaveConnection(peer_server); } diff --git a/screen_capture/linux/screen_capture_wgc.cpp b/screen_capture/linux/screen_capture_wgc.cpp new file mode 100644 index 0000000..077e527 --- /dev/null +++ b/screen_capture/linux/screen_capture_wgc.cpp @@ -0,0 +1,26 @@ +#include "screen_capture_wgc.h" + +#include + +ScreenCaptureWgc::ScreenCaptureWgc() {} + +ScreenCaptureWgc::~ScreenCaptureWgc() {} + +bool ScreenCaptureWgc::IsWgcSupported() { return false; } + +int ScreenCaptureWgc::Init(const RECORD_DESKTOP_RECT &rect, const int fps, + cb_desktop_data cb) { + return 0; +} + +int ScreenCaptureWgc::Start() { return 0; } + +int ScreenCaptureWgc::Pause() { return 0; } + +int ScreenCaptureWgc::Resume() { return 0; } + +int ScreenCaptureWgc::Stop() { return 0; } + +void ScreenCaptureWgc::OnFrame() {} + +void ScreenCaptureWgc::CleanUp() {} diff --git a/screen_capture/linux/screen_capture_wgc.h b/screen_capture/linux/screen_capture_wgc.h new file mode 100644 index 0000000..813bf09 --- /dev/null +++ b/screen_capture/linux/screen_capture_wgc.h @@ -0,0 +1,56 @@ +#ifndef _SCREEN_CAPTURE_WGC_H_ +#define _SCREEN_CAPTURE_WGC_H_ + +#include +#include +#include +#include + +typedef struct { + int left; + int top; + int right; + int bottom; +} RECORD_DESKTOP_RECT; + +typedef std::function cb_desktop_data; +typedef std::function cb_desktop_error; + +class ScreenCaptureWgc { + public: + ScreenCaptureWgc(); + ~ScreenCaptureWgc(); + + public: + bool IsWgcSupported(); + + int Init(const RECORD_DESKTOP_RECT &rect, const int fps, cb_desktop_data cb); + + int Start(); + int Pause(); + int Resume(); + int Stop(); + + void OnFrame(); + + protected: + void CleanUp(); + + private: + std::atomic_bool _running; + std::atomic_bool _paused; + std::atomic_bool _inited; + + std::thread _thread; + + std::string _device_name; + + RECORD_DESKTOP_RECT _rect; + + int _fps; + + cb_desktop_data _on_data; + cb_desktop_error _on_error; +}; + +#endif \ No newline at end of file diff --git a/screen_capture/macosx/screen_capture_wgc.cpp b/screen_capture/macosx/screen_capture_wgc.cpp new file mode 100644 index 0000000..077e527 --- /dev/null +++ b/screen_capture/macosx/screen_capture_wgc.cpp @@ -0,0 +1,26 @@ +#include "screen_capture_wgc.h" + +#include + +ScreenCaptureWgc::ScreenCaptureWgc() {} + +ScreenCaptureWgc::~ScreenCaptureWgc() {} + +bool ScreenCaptureWgc::IsWgcSupported() { return false; } + +int ScreenCaptureWgc::Init(const RECORD_DESKTOP_RECT &rect, const int fps, + cb_desktop_data cb) { + return 0; +} + +int ScreenCaptureWgc::Start() { return 0; } + +int ScreenCaptureWgc::Pause() { return 0; } + +int ScreenCaptureWgc::Resume() { return 0; } + +int ScreenCaptureWgc::Stop() { return 0; } + +void ScreenCaptureWgc::OnFrame() {} + +void ScreenCaptureWgc::CleanUp() {} diff --git a/screen_capture/macosx/screen_capture_wgc.h b/screen_capture/macosx/screen_capture_wgc.h new file mode 100644 index 0000000..813bf09 --- /dev/null +++ b/screen_capture/macosx/screen_capture_wgc.h @@ -0,0 +1,56 @@ +#ifndef _SCREEN_CAPTURE_WGC_H_ +#define _SCREEN_CAPTURE_WGC_H_ + +#include +#include +#include +#include + +typedef struct { + int left; + int top; + int right; + int bottom; +} RECORD_DESKTOP_RECT; + +typedef std::function cb_desktop_data; +typedef std::function cb_desktop_error; + +class ScreenCaptureWgc { + public: + ScreenCaptureWgc(); + ~ScreenCaptureWgc(); + + public: + bool IsWgcSupported(); + + int Init(const RECORD_DESKTOP_RECT &rect, const int fps, cb_desktop_data cb); + + int Start(); + int Pause(); + int Resume(); + int Stop(); + + void OnFrame(); + + protected: + void CleanUp(); + + private: + std::atomic_bool _running; + std::atomic_bool _paused; + std::atomic_bool _inited; + + std::thread _thread; + + std::string _device_name; + + RECORD_DESKTOP_RECT _rect; + + int _fps; + + cb_desktop_data _on_data; + cb_desktop_error _on_error; +}; + +#endif \ No newline at end of file diff --git a/screen_capture/main.cpp.bak b/screen_capture/main.cpp.bak deleted file mode 100644 index 7c3d838..0000000 --- a/screen_capture/main.cpp.bak +++ /dev/null @@ -1,134 +0,0 @@ - - -#include - -#include -#include -#include - -#include "screen_capture_wgc.h" - -extern "C" { -#include -#include -}; - -#define SDL_MAIN_HANDLED -#include "SDL2/SDL.h" - -int screen_w = 2560, screen_h = 1440; -const int pixel_w = 2560, pixel_h = 1440; - -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; -} - -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, - 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); - - sws_scale(img_convert_ctx, (uint8_t const **)Input_pFrame->data, - Input_pFrame->linesize, 0, height, Output_pFrame->data, - Output_pFrame->linesize); - - if (Input_pFrame) av_free(Input_pFrame); - if (Output_pFrame) av_free(Output_pFrame); - if (img_convert_ctx) sws_freeContext(img_convert_ctx); - - return 0; -} - -void OnFrame(unsigned char *data, int size, int width, int height) { - BGRAToNV12FFmpeg(data, width, height, dst_buffer); -} - -int main() { - ScreenCaptureWgc *recorder = new ScreenCaptureWgc(); - - RECORD_DESKTOP_RECT rect; - rect.left = 0; - rect.top = 0; - rect.right = GetSystemMetrics(SM_CXSCREEN); - rect.bottom = GetSystemMetrics(SM_CYSCREEN); - - recorder->Init(rect, 60, OnFrame); - - recorder->Start(); - - 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 / 2, screen_h / 2, - 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; -} diff --git a/screen_capture/screen_capture_wgc.cpp b/screen_capture/windows/screen_capture_wgc.cpp similarity index 99% rename from screen_capture/screen_capture_wgc.cpp rename to screen_capture/windows/screen_capture_wgc.cpp index f27f045..1bc0227 100644 --- a/screen_capture/screen_capture_wgc.cpp +++ b/screen_capture/windows/screen_capture_wgc.cpp @@ -1,5 +1,6 @@ #include "screen_capture_wgc.h" +#include #include #include #include diff --git a/screen_capture/screen_capture_wgc.h b/screen_capture/windows/screen_capture_wgc.h similarity index 86% rename from screen_capture/screen_capture_wgc.h rename to screen_capture/windows/screen_capture_wgc.h index 5358234..9dc58d3 100644 --- a/screen_capture/screen_capture_wgc.h +++ b/screen_capture/windows/screen_capture_wgc.h @@ -1,16 +1,11 @@ #ifndef _SCREEN_CAPTURE_WGC_H_ #define _SCREEN_CAPTURE_WGC_H_ -#include - #include #include #include #include -#include "wgc_session.h" -#include "wgc_session_impl.h" - typedef struct { int left; int top; @@ -21,7 +16,7 @@ typedef struct { typedef std::function cb_desktop_data; typedef std::function cb_desktop_error; -class ScreenCaptureWgc : public WgcSession::wgc_session_observer { +class ScreenCaptureWgc { public: ScreenCaptureWgc(); ~ScreenCaptureWgc(); diff --git a/screen_capture/wgc_session.h b/screen_capture/windows/wgc_session.h similarity index 100% rename from screen_capture/wgc_session.h rename to screen_capture/windows/wgc_session.h diff --git a/screen_capture/wgc_session_impl.cpp b/screen_capture/windows/wgc_session_impl.cpp similarity index 100% rename from screen_capture/wgc_session_impl.cpp rename to screen_capture/windows/wgc_session_impl.cpp diff --git a/screen_capture/wgc_session_impl.h b/screen_capture/windows/wgc_session_impl.h similarity index 100% rename from screen_capture/wgc_session_impl.h rename to screen_capture/windows/wgc_session_impl.h diff --git a/xmake.lua b/xmake.lua index fc5474d..604768d 100644 --- a/xmake.lua +++ b/xmake.lua @@ -35,44 +35,16 @@ target("log") target("screen_capture") set_kind("static") add_packages("log") - add_files("screen_capture/*.cpp") - add_includedirs("screen_capture", {public = true}) - -target("remote_desk_server") - set_kind("binary") - add_packages("log", "vcpkg::ffmpeg", "sdl2") - add_deps("projectx", "screen_capture") - add_files("remote_desk_server/*.cpp") - add_includedirs("../../src/interface") - add_links("swscale", "avutil") - add_defines("WIN32_LEAN_AND_MEAN") if is_os("windows") then - add_links("SDL2-static", "SDL2main", "gdi32", "winmm", - "setupapi", "version", "Imm32", "iphlpapi") - end - - -target("remote_desk_client") - set_kind("binary") - add_deps("projectx") - add_packages("log") - if is_os("windows") then - add_packages("sdl2") - elseif is_os("macosx") or is_os("linux")then - add_packages("sdl2") - add_packages("ffmpeg") - end - add_files("remote_desk_client/*.cpp") - add_includedirs("../../src/interface") - if is_os("windows") then - add_links("SDL2-static", "SDL2main", "gdi32", "winmm", - "setupapi", "version", "Imm32", "iphlpapi") + add_files("screen_capture/windows/*.cpp") + add_includedirs("screen_capture/windows", {public = true}) elseif is_os("macosx") then - add_links("SDL2") + add_files("screen_capture/macosx/*.cpp") + add_includedirs("screen_capture/macosx", {public = true}) elseif is_os("linux") then - add_links("SDL2") + add_files("screen_capture/linux/*.cpp") + add_includedirs("screen_capture/linux", {public = true}) end - target("remote_desk_gui") set_kind("binary")