From ce433d9b1e08ffd33b8eaff9fcecbfc41a5faf51 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 18 Oct 2022 09:02:24 +0200 Subject: Attempt to reduce stuttering of video --- include/NvFBCLibrary.hpp | 63 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 10 deletions(-) (limited to 'include') diff --git a/include/NvFBCLibrary.hpp b/include/NvFBCLibrary.hpp index 19b9bcc..dc7db1f 100644 --- a/include/NvFBCLibrary.hpp +++ b/include/NvFBCLibrary.hpp @@ -61,8 +61,32 @@ public: if(!library || !display_to_capture || !display_width || !display_height || fbc_handle_created) return false; + this->fps = fps; const bool capture_region = (x > 0 || y > 0 || width > 0 || height > 0); + bool supports_direct_cursor = false; + int driver_major_version = 0; + int driver_minor_version = 0; + if(direct_capture && get_driver_version(&driver_major_version, &driver_minor_version)) { + fprintf(stderr, "Info: detected nvidia version: %d.%d\n", driver_major_version, driver_minor_version); + + if(version_at_least(driver_major_version, driver_minor_version, 515, 57) && version_less_than(driver_major_version, driver_minor_version, 520, 56)) { + direct_capture = false; + fprintf(stderr, "Warning: \"screen-direct\" has temporary been disabled as it causes stuttering with driver versions >= 515.57 and < 520.56. Please update your driver if possible. Capturing \"screen\" instead.\n"); + } + + // TODO: + // Cursor capture disabled because moving the cursor doesn't update capture rate to monitor hz and instead captures at 10-30 hz + /* + if(direct_capture) { + if(version_at_least(driver_major_version, driver_minor_version, 515, 57)) + supports_direct_cursor = true; + else + fprintf(stderr, "Info: capturing \"screen-direct\" but driver version appears to be less than 515.57. Disabling capture of cursor. Please update your driver if you want to capture your cursor or record \"screen\" instead.\n"); + } + */ + } + NVFBCSTATUS status; NVFBC_TRACKING_TYPE tracking_type; bool capture_session_created = false; @@ -129,14 +153,14 @@ public: memset(&create_capture_params, 0, sizeof(create_capture_params)); create_capture_params.dwVersion = NVFBC_CREATE_CAPTURE_SESSION_PARAMS_VER; create_capture_params.eCaptureType = NVFBC_CAPTURE_SHARED_CUDA; - create_capture_params.bWithCursor = (!direct_capture || driver_supports_direct_capture_cursor()) ? NVFBC_TRUE : NVFBC_FALSE; + create_capture_params.bWithCursor = (!direct_capture || supports_direct_cursor) ? NVFBC_TRUE : NVFBC_FALSE; if(capture_region) { create_capture_params.captureBox = { x, y, width, height }; *display_width = width; *display_height = height; } create_capture_params.eTrackingType = tracking_type; - create_capture_params.dwSamplingRateMs = 1000 / fps; + create_capture_params.dwSamplingRateMs = 1000 / (fps + 1); create_capture_params.bAllowDirectCapture = direct_capture ? NVFBC_TRUE : NVFBC_FALSE; create_capture_params.bPushModel = direct_capture ? NVFBC_TRUE : NVFBC_FALSE; if(tracking_type == NVFBC_TRACKING_OUTPUT) @@ -192,13 +216,14 @@ public: NVFBC_TOCUDA_GRAB_FRAME_PARAMS grab_params; memset(&grab_params, 0, sizeof(grab_params)); grab_params.dwVersion = NVFBC_TOCUDA_GRAB_FRAME_PARAMS_VER; - grab_params.dwFlags = NVFBC_TOCUDA_GRAB_FLAGS_NOWAIT | NVFBC_TOCUDA_GRAB_FLAGS_FORCE_REFRESH; + grab_params.dwFlags = NVFBC_TOCUDA_GRAB_FLAGS_NOWAIT;// | NVFBC_TOCUDA_GRAB_FLAGS_FORCE_REFRESH;//NVFBC_TOCUDA_GRAB_FLAGS_NOWAIT_IF_NEW_FRAME_READY; grab_params.pFrameGrabInfo = &frame_info; grab_params.pCUDADeviceBuffer = cu_device_ptr; + grab_params.dwTimeoutMs = 0;//1000 / (fps + 10); status = nv_fbc_function_list.nvFBCToCudaGrabFrame(nv_fbc_handle, &grab_params); if(status != NVFBC_SUCCESS) { - fprintf(stderr, "Error: %s\n", nv_fbc_function_list.nvFBCGetLastErrorStr(nv_fbc_handle)); + fprintf(stderr, "Error: capture: %s\n", nv_fbc_function_list.nvFBCGetLastErrorStr(nv_fbc_handle)); return false; } @@ -246,28 +271,45 @@ private: } // TODO: Test with optimus and open kernel modules - static bool driver_supports_direct_capture_cursor() { + static bool get_driver_version(int *major, int *minor) { + *major = 0; + *minor = 0; + FILE *f = fopen("/proc/driver/nvidia/version", "rb"); - if(!f) + if(!f) { + fprintf(stderr, "Warning: failed to get nvidia driver version (failed to read /proc/driver/nvidia/version)\n"); return false; + } char buffer[2048]; size_t bytes_read = fread(buffer, 1, sizeof(buffer) - 1, f); buffer[bytes_read] = '\0'; - bool supports_cursor = false; + bool success = false; const char *p = strstr(buffer, "Kernel Module"); if(p) { p += 13; int driver_major_version = 0, driver_minor_version = 0; if(sscanf(p, "%d.%d", &driver_major_version, &driver_minor_version) == 2) { - if(driver_major_version > 515 || (driver_major_version == 515 && driver_minor_version >= 57)) - supports_cursor = true; + *major = driver_major_version; + *minor = driver_minor_version; + success = true; } } + if(!success) + fprintf(stderr, "Warning: failed to get nvidia driver version\n"); + fclose(f); - return supports_cursor; + return success; + } + + static bool version_at_least(int major, int minor, int expected_major, int expected_minor) { + return major > expected_major || (major == expected_major && minor >= expected_minor); + } + + static bool version_less_than(int major, int minor, int expected_major, int expected_minor) { + return major < expected_major || (major == expected_major && minor < expected_minor); } private: void *library = nullptr; @@ -275,4 +317,5 @@ private: NVFBC_API_FUNCTION_LIST nv_fbc_function_list; NVFBC_SESSION_HANDLE nv_fbc_handle; bool fbc_handle_created = false; + int fps = 0; }; -- cgit v1.2.3