From 6e19e5360345f8902a368b857dbc0f9216891e98 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Thu, 2 Nov 2023 02:34:14 -0700 Subject: [PATCH] 1.ImGUI display success; 2.To do: fix x264 encode green screen --- README.md | 8 +++++-- .../remote_desk_gui}/linux_capture.cpp | 3 +-- .../remote_desk/remote_desk_gui/main.cpp | 3 +-- .../linux/screen_capture_x11.cpp | 24 +++++++++++++++---- application/remote_desk/xmake.lua | 18 ++++++++++---- xmake.lua | 11 +-------- 6 files changed, 42 insertions(+), 25 deletions(-) rename {tests/peerconnection => application/remote_desk/remote_desk_gui}/linux_capture.cpp (98%) diff --git a/README.md b/README.md index 679a7d3..1f97af4 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,11 @@ vcpkg/buildtrees/versioning_/versions/pcre/69e232f12c4e3eab4115f0672466a6661978b linux -apt-get install nvidia-cuda-toolkit +sudo apt-get install nvidia-cuda-toolkit solve -sudo apt-get install libxcb-randr0-dev libxcb-xtest0-dev libxcb-xinerama0-dev libxcb-shape0-dev libxcb-xkb-dev \ No newline at end of file +sudo apt-get install libxcb-randr0-dev libxcb-xtest0-dev libxcb-xinerama0-dev libxcb-shape0-dev libxcb-xkb-dev libxcb-xfixes0-dev libxv-dev + +sudo apt-get install libavcodec-dev libavformat-dev libavutil-dev libavfilter-dev libavdevice-dev + +sudo apt remove libssl-dev libglib2.0-dev \ No newline at end of file diff --git a/tests/peerconnection/linux_capture.cpp b/application/remote_desk/remote_desk_gui/linux_capture.cpp similarity index 98% rename from tests/peerconnection/linux_capture.cpp rename to application/remote_desk/remote_desk_gui/linux_capture.cpp index ae30c8d..e7f3511 100644 --- a/tests/peerconnection/linux_capture.cpp +++ b/application/remote_desk/remote_desk_gui/linux_capture.cpp @@ -121,7 +121,7 @@ int main(int argc, char *argv[]) { // grabbing frame rate av_dict_set(&options, "framerate", "30", 0); // Make the grabbed area follow the mouse - av_dict_set(&options, "follow_mouse", "centered", 0); + // av_dict_set(&options, "follow_mouse", "centered", 0); // Video frame size. The default is to capture the full screen av_dict_set(&options, "video_size", "1280x720", 0); AVInputFormat *ifmt = (AVInputFormat *)av_find_input_format("x11grab"); @@ -156,7 +156,6 @@ int main(int argc, char *argv[]) { pCodecCtx = avcodec_alloc_context3(NULL); avcodec_parameters_to_context(pCodecCtx, pCodecParam); - // pCodec = const_cast(avcodec_find_decoder(AV_CODEC_ID_H264)); pCodec = const_cast(avcodec_find_decoder(pCodecCtx->codec_id)); if (pCodec == NULL) { printf("Codec not found.\n"); diff --git a/application/remote_desk/remote_desk_gui/main.cpp b/application/remote_desk/remote_desk_gui/main.cpp index 090da93..90cbf03 100644 --- a/application/remote_desk/remote_desk_gui/main.cpp +++ b/application/remote_desk/remote_desk_gui/main.cpp @@ -592,11 +592,10 @@ int main() { if (tc >= 0) { SendData(peer_server, DATA_TYPE::VIDEO, (const char *)nv12_buffer, NV12_BUFFER_SIZE); - // std::cout << "Send" << std::endl; + std::cout << "Send" << std::endl; last_frame_time_ = now_time; } }); - screen_capture->Start(); #endif } else { diff --git a/application/remote_desk/screen_capture/linux/screen_capture_x11.cpp b/application/remote_desk/screen_capture/linux/screen_capture_x11.cpp index b004d0c..def4c75 100644 --- a/application/remote_desk/screen_capture/linux/screen_capture_x11.cpp +++ b/application/remote_desk/screen_capture/linux/screen_capture_x11.cpp @@ -5,9 +5,20 @@ #define NV12_BUFFER_SIZE 1280 * 720 * 3 / 2 unsigned char nv12_buffer_[NV12_BUFFER_SIZE]; -ScreenCaptureX11::ScreenCaptureX11() {} +FILE *file = nullptr; -ScreenCaptureX11::~ScreenCaptureX11() {} +ScreenCaptureX11::ScreenCaptureX11() { + file = fopen("nv12.yuv", "w+b"); + if (!file) { + printf("Fail to open nv12.yuv\n"); + } +} + +ScreenCaptureX11::~ScreenCaptureX11() { + if (capture_thread_->joinable()) { + capture_thread_->join(); + } +} int ScreenCaptureX11::Init(const RECORD_DESKTOP_RECT &rect, const int fps, cb_desktop_data cb) { @@ -22,9 +33,9 @@ int ScreenCaptureX11::Init(const RECORD_DESKTOP_RECT &rect, const int fps, // grabbing frame rate av_dict_set(&options_, "framerate", "30", 0); // Make the grabbed area follow the mouse - av_dict_set(&options_, "follow_mouse", "centered", 0); + // av_dict_set(&options_, "follow_mouse", "centered", 0); // Video frame size. The default is to capture the full screen - // av_dict_set(&options_, "video_size", "1280x720", 0); + av_dict_set(&options_, "video_size", "1280x720", 0); ifmt_ = (AVInputFormat *)av_find_input_format("x11grab"); if (!ifmt_) { printf("Couldn't find_input_format\n"); @@ -57,7 +68,6 @@ int ScreenCaptureX11::Init(const RECORD_DESKTOP_RECT &rect, const int fps, pCodecCtx_ = avcodec_alloc_context3(NULL); avcodec_parameters_to_context(pCodecCtx_, pCodecParam_); - // pCodec_ = const_cast(avcodec_find_decoder(AV_CODEC_ID_H264)); pCodec_ = const_cast(avcodec_find_decoder(pCodecCtx_->codec_id)); if (pCodec_ == NULL) { printf("Codec not found.\n"); @@ -90,6 +100,7 @@ int ScreenCaptureX11::Init(const RECORD_DESKTOP_RECT &rect, const int fps, int ScreenCaptureX11::Start() { capture_thread_.reset(new std::thread([this]() { while (1) { + printf("00000\n"); if (av_read_frame(pFormatCtx_, packet_) >= 0) { printf("111111444444\n"); if (packet_->stream_index == videoindex_) { @@ -108,6 +119,9 @@ int ScreenCaptureX11::Start() { pFrame_->height, pFrameNV12_->data, pFrameNV12_->linesize); + fwrite(nv12_buffer_, 1, pFrame_->width * pFrame_->height * 3 / 2, + file); + _on_data((unsigned char *)nv12_buffer_, pFrame_->width * pFrame_->height * 3 / 2, pFrame_->width, pFrame_->height); diff --git a/application/remote_desk/xmake.lua b/application/remote_desk/xmake.lua index c3f5f96..91ed743 100644 --- a/application/remote_desk/xmake.lua +++ b/application/remote_desk/xmake.lua @@ -6,7 +6,7 @@ add_rules("mode.release", "mode.debug") set_languages("c++17") add_requires("spdlog 1.11.0", {system = false}) -add_requires("imgui 1.89.9", {system = false}, {configs = {sdl2 = true}}) +add_requires("imgui 1.89.9", {configs = {sdl2 = true, sdl2_renderer = true, glfw = true}}) add_defines("UNICODE") add_requires("sdl2", {system = false}) @@ -52,8 +52,8 @@ target("screen_capture") target("remote_desk") set_kind("binary") add_deps("projectx", "screen_capture") - add_packages("log", "imgui", "sdl2", "ffmpeg") - add_files("remote_desk_gui/*.cpp") + add_packages("log", "sdl2", "imgui", "ffmpeg") + add_files("remote_desk_gui/main.cpp") add_includedirs("../../src/interface") if is_os("windows") then add_links("SDL2-static", "SDL2main", "gdi32", "winmm", @@ -66,4 +66,14 @@ target("remote_desk") "-lswscale", "-lavutil", "-lswresample", "-lpostproc", "-lasound", "-lxcb-shape", "-lxcb-xfixes", "-lsndio", "-lxcb", "-lxcb-shm", "-lXext", "-lX11", "-lXv", "-ldl", "-lpthread", {force = true}) - end \ No newline at end of file + end + +target("linux_capture") + set_kind("binary") + add_packages("sdl2", "imgui", "ffmpeg", "glfw", "glad") + add_files("remote_desk_gui/linux_capture.cpp") + add_links("SDL2") + add_ldflags("-lavformat", "-lavdevice", "-lavfilter", "-lavcodec", + "-lswscale", "-lavutil", "-lswresample", "-lpostproc", + "-lasound", "-lxcb-shape", "-lxcb-xfixes", "-lsndio", "-lxcb", + "-lxcb-shm", "-lXext", "-lX11", "-lXv", "-lpthread", "-lx264", "-ldl", "-lSDL2" ,{force = true}) \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index 843c03c..e5fa2ec 100644 --- a/xmake.lua +++ b/xmake.lua @@ -226,13 +226,4 @@ target("nicetest") add_links("nice", "glib-2.0", "gio-2.0", "gmodule-2.0", "gobject-2.0", "gthread-2.0", "pcre2-8", "pcre2-16", "pcre2-32", "pcre2-posix", "zlib", "ffi", "libcrypto", "libssl", "intl", "iconv", "charset", "bz2", - "Shell32", "Advapi32", "Dnsapi", "Shlwapi", "Iphlpapi") - -target("linux_capture") - set_kind("binary") - add_packages("ffmpeg", "sdl2") - add_files("tests/peerconnection/linux_capture.cpp") - add_ldflags("-lavformat", "-lavdevice", "-lavfilter", "-lavcodec", - "-lswscale", "-lavutil", "-lswresample", "-lpostproc", - "-lasound", "-lxcb-shape", "-lxcb-xfixes", "-lsndio", "-lxcb", - "-lxcb-shm", "-lXext", "-lX11", "-lXv", "-lpthread", "-lx264", "-ldl", {force = true}) + "Shell32", "Advapi32", "Dnsapi", "Shlwapi", "Iphlpapi") \ No newline at end of file