diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/capture/kms_vaapi.c | 470 | ||||
-rw-r--r-- | src/capture/nvfbc.c | 1 | ||||
-rw-r--r-- | src/capture/xcomposite_cuda.c | 2 | ||||
-rw-r--r-- | src/capture/xcomposite_vaapi.c | 14 | ||||
-rw-r--r-- | src/cuda.c | 2 | ||||
-rw-r--r-- | src/egl.c | 2 | ||||
-rw-r--r-- | src/kms/kms_client.c | 234 | ||||
-rw-r--r-- | src/kms/kms_server.c | 242 | ||||
-rw-r--r-- | src/library_loader.c | 34 | ||||
-rw-r--r-- | src/main.cpp | 203 | ||||
-rw-r--r-- | src/sound.cpp | 2 | ||||
-rw-r--r-- | src/time.c | 10 | ||||
-rw-r--r-- | src/utils.c | 62 | ||||
-rw-r--r-- | src/xnvctrl.c | 2 |
14 files changed, 1144 insertions, 136 deletions
diff --git a/src/capture/kms_vaapi.c b/src/capture/kms_vaapi.c new file mode 100644 index 0000000..7e42011 --- /dev/null +++ b/src/capture/kms_vaapi.c @@ -0,0 +1,470 @@ +#include "../../include/capture/kms_vaapi.h" +#include "../../include/kms/kms_client.h" +#include "../../include/egl.h" +#include "../../include/utils.h" +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <assert.h> +#include <X11/Xlib.h> +#include <libavutil/hwcontext.h> +#include <libavutil/hwcontext_vaapi.h> +#include <libavutil/frame.h> +#include <libavcodec/avcodec.h> +#include <va/va.h> +#include <va/va_drmcommon.h> + +typedef struct { + gsr_capture_kms_vaapi_params params; + bool should_stop; + bool stop_is_error; + bool created_hw_frame; + + gsr_egl egl; + + gsr_kms_client kms_client; + + uint32_t fourcc; + uint64_t modifiers; + int dmabuf_fd; + uint32_t pitch; + uint32_t offset; + vec2i kms_size; + + vec2i capture_pos; + vec2i capture_size; + bool screen_capture; + + VADisplay va_dpy; + VAConfigID config_id; + VAContextID context_id; + VASurfaceID input_surface; + VABufferID buffer_id; + VARectangle input_region; +} gsr_capture_kms_vaapi; + +static int max_int(int a, int b) { + return a > b ? a : b; +} + +static void gsr_capture_kms_vaapi_stop(gsr_capture *cap, AVCodecContext *video_codec_context); + +static bool drm_create_codec_context(gsr_capture_kms_vaapi *cap_kms, AVCodecContext *video_codec_context) { + AVBufferRef *device_ctx; + if(av_hwdevice_ctx_create(&device_ctx, AV_HWDEVICE_TYPE_VAAPI, "/dev/dri/renderD128", NULL, 0) < 0) { + fprintf(stderr, "Error: Failed to create hardware device context\n"); + return false; + } + + AVBufferRef *frame_context = av_hwframe_ctx_alloc(device_ctx); + if(!frame_context) { + fprintf(stderr, "Error: Failed to create hwframe context\n"); + av_buffer_unref(&device_ctx); + return false; + } + + AVHWFramesContext *hw_frame_context = + (AVHWFramesContext *)frame_context->data; + hw_frame_context->width = video_codec_context->width; + hw_frame_context->height = video_codec_context->height; + hw_frame_context->sw_format = AV_PIX_FMT_NV12;//AV_PIX_FMT_0RGB32;//AV_PIX_FMT_YUV420P;//AV_PIX_FMT_0RGB32;//AV_PIX_FMT_NV12; + hw_frame_context->format = video_codec_context->pix_fmt; + hw_frame_context->device_ref = device_ctx; + hw_frame_context->device_ctx = (AVHWDeviceContext*)device_ctx->data; + + hw_frame_context->initial_pool_size = 1; // TODO: (and in other places) + + AVVAAPIDeviceContext *vactx =((AVHWDeviceContext*)device_ctx->data)->hwctx; + cap_kms->va_dpy = vactx->display; + + if (av_hwframe_ctx_init(frame_context) < 0) { + fprintf(stderr, "Error: Failed to initialize hardware frame context " + "(note: ffmpeg version needs to be > 4.0)\n"); + av_buffer_unref(&device_ctx); + //av_buffer_unref(&frame_context); + return false; + } + + video_codec_context->hw_device_ctx = av_buffer_ref(device_ctx); + video_codec_context->hw_frames_ctx = av_buffer_ref(frame_context); + return true; +} + +#define DRM_FORMAT_MOD_INVALID 72057594037927935 + +// TODO: On monitor reconfiguration, find monitor x, y, width and height again. Do the same for nvfbc. + +static int gsr_capture_kms_vaapi_start(gsr_capture *cap, AVCodecContext *video_codec_context) { + gsr_capture_kms_vaapi *cap_kms = cap->priv; + + // TODO: Allow specifying another card, and in other places (TODO: Use /dev/dri/renderD128?) + if(gsr_kms_client_init(&cap_kms->kms_client, "/dev/dri/card0", cap_kms->params.program_dir) != 0) { + return -1; + } + + gsr_kms_response kms_response; + if(gsr_kms_client_get_kms(&cap_kms->kms_client, &kms_response) != 0) { + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_start: failed to get kms, error: %d (%s)\n", kms_response.result, kms_response.data.err_msg); + gsr_capture_kms_vaapi_stop(cap, video_codec_context); + return -1; + } + + cap_kms->dmabuf_fd = kms_response.data.fd.fd; + cap_kms->pitch = kms_response.data.fd.pitch; + cap_kms->offset = kms_response.data.fd.offset; + cap_kms->fourcc = kms_response.data.fd.pixel_format; + cap_kms->modifiers = kms_response.data.fd.modifier; + + // TODO: Update on monitor reconfiguration, make sure to update on window focus change (maybe for kms update each second?), needs to work on focus change to the witcher 3 on full window, fullscreen firefox, etc + gsr_monitor monitor; + if(strcmp(cap_kms->params.display_to_capture, "screen") == 0) { + monitor.pos.x = 0; + monitor.pos.y = 0; + monitor.size.x = XWidthOfScreen(DefaultScreenOfDisplay(cap_kms->params.dpy)); + monitor.size.y = XHeightOfScreen(DefaultScreenOfDisplay(cap_kms->params.dpy)); + cap_kms->screen_capture = true; + } else if(!get_monitor_by_name(cap_kms->params.dpy, cap_kms->params.display_to_capture, &monitor)) { + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_start: failed to find monitor by name \"%s\"\n", cap_kms->params.display_to_capture); + gsr_capture_kms_vaapi_stop(cap, video_codec_context); + return -1; + } + + cap_kms->capture_pos = monitor.pos; + cap_kms->capture_size = monitor.size; + + cap_kms->kms_size.x = kms_response.data.fd.width; + cap_kms->kms_size.y = kms_response.data.fd.height; + + if(!gsr_egl_load(&cap_kms->egl, cap_kms->params.dpy)) { + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_start: failed to load opengl\n"); + return -1; + } + + /* Disable vsync */ + cap_kms->egl.eglSwapInterval(cap_kms->egl.egl_display, 0); + + video_codec_context->width = max_int(2, cap_kms->capture_size.x & ~1); + video_codec_context->height = max_int(2, cap_kms->capture_size.y & ~1); + + if(!drm_create_codec_context(cap_kms, video_codec_context)) { + gsr_capture_kms_vaapi_stop(cap, video_codec_context); + return -1; + } + + //cap_kms->window_resize_timer = clock_get_monotonic_seconds(); // TODO: + return 0; +} + +static void gsr_capture_kms_vaapi_tick(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame **frame) { + gsr_capture_kms_vaapi *cap_kms = cap->priv; + + // TODO: + //cap_kms->egl.glClear(GL_COLOR_BUFFER_BIT); + + //const double window_resize_timeout = 1.0; // 1 second + if(!cap_kms->created_hw_frame) { + if(cap_kms->buffer_id) { + vaDestroyBuffer(cap_kms->va_dpy, cap_kms->buffer_id); + cap_kms->buffer_id = 0; + } + + if(cap_kms->context_id) { + vaDestroyContext(cap_kms->va_dpy, cap_kms->context_id); + cap_kms->context_id = 0; + } + + if(cap_kms->config_id) { + vaDestroyConfig(cap_kms->va_dpy, cap_kms->config_id); + cap_kms->config_id = 0; + } + + if(cap_kms->input_surface) { + vaDestroySurfaces(cap_kms->va_dpy, &cap_kms->input_surface, 1); + cap_kms->input_surface = 0; + } + + if(!cap_kms->created_hw_frame) { + cap_kms->created_hw_frame = true; + av_frame_free(frame); + *frame = av_frame_alloc(); + if(!frame) { + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: failed to allocate frame\n"); + cap_kms->should_stop = true; + cap_kms->stop_is_error = true; + return; + } + (*frame)->format = video_codec_context->pix_fmt; + (*frame)->width = video_codec_context->width; + (*frame)->height = video_codec_context->height; + (*frame)->color_range = video_codec_context->color_range; + (*frame)->color_primaries = video_codec_context->color_primaries; + (*frame)->color_trc = video_codec_context->color_trc; + (*frame)->colorspace = video_codec_context->colorspace; + (*frame)->chroma_location = video_codec_context->chroma_sample_location; + + int res = av_hwframe_get_buffer(video_codec_context->hw_frames_ctx, *frame, 0); + if(res < 0) { + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: av_hwframe_get_buffer failed: %d\n", res); + cap_kms->should_stop = true; + cap_kms->stop_is_error = true; + return; + } + } + + uintptr_t dmabuf = cap_kms->dmabuf_fd; + + VASurfaceAttribExternalBuffers buf = {0}; + buf.pixel_format = VA_FOURCC_BGRX; // VA_FOURCC_XRGB + buf.width = cap_kms->kms_size.x; + buf.height = cap_kms->kms_size.y; + buf.data_size = cap_kms->kms_size.y * cap_kms->pitch; + buf.num_planes = 1; + buf.pitches[0] = cap_kms->pitch; + buf.offsets[0] = cap_kms->offset; + buf.buffers = &dmabuf; + buf.num_buffers = 1; + buf.flags = 0; + buf.private_data = 0; + + VADRMFormatModifierList modifier_list = {0}; + modifier_list.modifiers = &cap_kms->modifiers; + modifier_list.num_modifiers = 1; + + #define VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME 0x20000000 + + VASurfaceAttrib attribs[3] = {0}; + attribs[0].type = VASurfaceAttribMemoryType; + attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE; + attribs[0].value.type = VAGenericValueTypeInteger; + attribs[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME; // TODO: prime1 instead? + attribs[1].type = VASurfaceAttribExternalBufferDescriptor; + attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE; + attribs[1].value.type = VAGenericValueTypePointer; + attribs[1].value.value.p = &buf; + + int num_attribs = 2; + if(cap_kms->modifiers != DRM_FORMAT_MOD_INVALID) { + attribs[2].type = VASurfaceAttribDRMFormatModifiers; + attribs[2].flags = VA_SURFACE_ATTRIB_SETTABLE; + attribs[2].value.type = VAGenericValueTypePointer; + attribs[2].value.value.p = &modifier_list; + ++num_attribs; + } + + VAStatus va_status = vaCreateSurfaces(cap_kms->va_dpy, VA_RT_FORMAT_RGB32, cap_kms->kms_size.x, cap_kms->kms_size.y, &cap_kms->input_surface, 1, attribs, num_attribs); + if(va_status != VA_STATUS_SUCCESS) { + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: vaCreateSurfaces failed: %d\n", va_status); + cap_kms->should_stop = true; + cap_kms->stop_is_error = true; + return; + } + + //vaBeginPicture(cap_kms->va_dpy, ) + + va_status = vaCreateConfig(cap_kms->va_dpy, VAProfileNone, VAEntrypointVideoProc, NULL, 0, &cap_kms->config_id); + if(va_status != VA_STATUS_SUCCESS) { + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: vaCreateConfig failed: %d\n", va_status); + cap_kms->should_stop = true; + cap_kms->stop_is_error = true; + return; + } + + VASurfaceID target_surface_id = (uintptr_t)(*frame)->data[3]; + va_status = vaCreateContext(cap_kms->va_dpy, cap_kms->config_id, cap_kms->kms_size.x, cap_kms->kms_size.y, VA_PROGRESSIVE, &target_surface_id, 1, &cap_kms->context_id); + if(va_status != VA_STATUS_SUCCESS) { + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: vaCreateContext failed: %d\n", va_status); + cap_kms->should_stop = true; + cap_kms->stop_is_error = true; + return; + } + + cap_kms->input_region = (VARectangle) { + .x = cap_kms->capture_pos.x, + .y = cap_kms->capture_pos.y, + .width = cap_kms->capture_size.x, + .height = cap_kms->capture_size.y + }; + + // Copying a surface to another surface will automatically perform the color conversion. Thanks vaapi! + VAProcPipelineParameterBuffer params = {0}; + params.surface = cap_kms->input_surface; + if(cap_kms->screen_capture) + params.surface_region = NULL; + else + params.surface_region = &cap_kms->input_region; + params.output_region = NULL; + params.output_background_color = 0; + params.filter_flags = VA_FRAME_PICTURE; + // TODO: Colors + params.input_color_properties.color_range = (*frame)->color_range == AVCOL_RANGE_JPEG ? VA_SOURCE_RANGE_FULL : VA_SOURCE_RANGE_REDUCED; + params.output_color_properties.color_range = (*frame)->color_range == AVCOL_RANGE_JPEG ? VA_SOURCE_RANGE_FULL : VA_SOURCE_RANGE_REDUCED; + + va_status = vaCreateBuffer(cap_kms->va_dpy, cap_kms->context_id, VAProcPipelineParameterBufferType, sizeof(params), 1, ¶ms, &cap_kms->buffer_id); + if(va_status != VA_STATUS_SUCCESS) { + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: vaCreateBuffer failed: %d\n", va_status); + cap_kms->should_stop = true; + cap_kms->stop_is_error = true; + return; + } + + // Clear texture with black background because the source texture (window_texture_get_opengl_texture_id(&cap_kms->window_texture)) + // might be smaller than cap_kms->target_texture_id + // TODO: + //cap_kms->egl.glClearTexImage(cap_kms->target_texture_id, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + } +} + +static bool gsr_capture_kms_vaapi_should_stop(gsr_capture *cap, bool *err) { + gsr_capture_kms_vaapi *cap_kms = cap->priv; + if(cap_kms->should_stop) { + if(err) + *err = cap_kms->stop_is_error; + return true; + } + + if(err) + *err = false; + return false; +} + +static int gsr_capture_kms_vaapi_capture(gsr_capture *cap, AVFrame *frame) { + gsr_capture_kms_vaapi *cap_kms = cap->priv; + + VASurfaceID target_surface_id = (uintptr_t)frame->data[3]; + + VAStatus va_status = vaBeginPicture(cap_kms->va_dpy, cap_kms->context_id, target_surface_id); + if(va_status != VA_STATUS_SUCCESS) { + static bool error_printed = false; + if(!error_printed) { + error_printed = true; + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: vaBeginPicture failed: %d\n", va_status); + } + return -1; + } + + va_status = vaRenderPicture(cap_kms->va_dpy, cap_kms->context_id, &cap_kms->buffer_id, 1); + if(va_status != VA_STATUS_SUCCESS) { + vaEndPicture(cap_kms->va_dpy, cap_kms->context_id); + static bool error_printed = false; + if(!error_printed) { + error_printed = true; + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: vaRenderPicture failed: %d\n", va_status); + } + return -1; + } + + va_status = vaEndPicture(cap_kms->va_dpy, cap_kms->context_id); + if(va_status != VA_STATUS_SUCCESS) { + static bool error_printed = false; + if(!error_printed) { + error_printed = true; + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: vaEndPicture failed: %d\n", va_status); + } + return -1; + } + + // TODO: Needed? + //vaSyncSurface(cap_kms->va_dpy, cap_kms->input_surface); + //vaSyncSurface(cap_kms->va_dpy, target_surface_id); + + // TODO: Remove + //cap_kms->egl.eglSwapBuffers(cap_kms->egl.egl_display, cap_kms->egl.egl_surface); + + return 0; +} + +static void gsr_capture_kms_vaapi_stop(gsr_capture *cap, AVCodecContext *video_codec_context) { + gsr_capture_kms_vaapi *cap_kms = cap->priv; + + if(cap_kms->buffer_id) { + vaDestroyBuffer(cap_kms->va_dpy, cap_kms->buffer_id); + cap_kms->buffer_id = 0; + } + + if(cap_kms->context_id) { + vaDestroyContext(cap_kms->va_dpy, cap_kms->context_id); + cap_kms->context_id = 0; + } + + if(cap_kms->config_id) { + vaDestroyConfig(cap_kms->va_dpy, cap_kms->config_id); + cap_kms->config_id = 0; + } + + if(cap_kms->input_surface) { + vaDestroySurfaces(cap_kms->va_dpy, &cap_kms->input_surface, 1); + cap_kms->input_surface = 0; + } + + if(cap_kms->dmabuf_fd > 0) { + close(cap_kms->dmabuf_fd); + cap_kms->dmabuf_fd = 0; + } + + if(video_codec_context->hw_device_ctx) + av_buffer_unref(&video_codec_context->hw_device_ctx); + if(video_codec_context->hw_frames_ctx) + av_buffer_unref(&video_codec_context->hw_frames_ctx); + + gsr_egl_unload(&cap_kms->egl); + gsr_kms_client_deinit(&cap_kms->kms_client); +} + +static void gsr_capture_kms_vaapi_destroy(gsr_capture *cap, AVCodecContext *video_codec_context) { + (void)video_codec_context; + gsr_capture_kms_vaapi *cap_kms = cap->priv; + if(cap->priv) { + gsr_capture_kms_vaapi_stop(cap, video_codec_context); + free((void*)cap_kms->params.display_to_capture); + cap_kms->params.display_to_capture = NULL; + free(cap->priv); + cap->priv = NULL; + } + free(cap); +} + +gsr_capture* gsr_capture_kms_vaapi_create(const gsr_capture_kms_vaapi_params *params) { + if(!params) { + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_create params is NULL\n"); + return NULL; + } + + gsr_capture *cap = calloc(1, sizeof(gsr_capture)); + if(!cap) + return NULL; + + gsr_capture_kms_vaapi *cap_kms = calloc(1, sizeof(gsr_capture_kms_vaapi)); + if(!cap_kms) { + free(cap); + return NULL; + } + + Display *display = XOpenDisplay(NULL); + if(!display) { + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_create failed: XOpenDisplay failed\n"); + free(cap); + free(cap_kms); + return NULL; + } + + const char *display_to_capture = strdup(params->display_to_capture); + if(!display_to_capture) { + free(cap); + free(cap_kms); + return NULL; + } + + cap_kms->params = *params; + cap_kms->params.display_to_capture = display_to_capture; + + *cap = (gsr_capture) { + .start = gsr_capture_kms_vaapi_start, + .tick = gsr_capture_kms_vaapi_tick, + .should_stop = gsr_capture_kms_vaapi_should_stop, + .capture = gsr_capture_kms_vaapi_capture, + .destroy = gsr_capture_kms_vaapi_destroy, + .priv = cap_kms + }; + + return cap; +} diff --git a/src/capture/nvfbc.c b/src/capture/nvfbc.c index a7bb16a..c45712d 100644 --- a/src/capture/nvfbc.c +++ b/src/capture/nvfbc.c @@ -444,6 +444,7 @@ static void gsr_capture_nvfbc_destroy(gsr_capture *cap, AVCodecContext *video_co gsr_cuda_unload(&cap_nvfbc->cuda); dlclose(cap_nvfbc->library); free((void*)cap_nvfbc->params.display_to_capture); + cap_nvfbc->params.display_to_capture = NULL; free(cap->priv); cap->priv = NULL; } diff --git a/src/capture/xcomposite_cuda.c b/src/capture/xcomposite_cuda.c index 2b0d1a1..32b0d83 100644 --- a/src/capture/xcomposite_cuda.c +++ b/src/capture/xcomposite_cuda.c @@ -2,7 +2,7 @@ #include "../../include/egl.h" #include "../../include/cuda.h" #include "../../include/window_texture.h" -#include "../../include/time.h" +#include "../../include/utils.h" #include <libavutil/hwcontext.h> #include <libavutil/hwcontext_cuda.h> #include <libavutil/frame.h> diff --git a/src/capture/xcomposite_vaapi.c b/src/capture/xcomposite_vaapi.c index cfe99dc..f12a978 100644 --- a/src/capture/xcomposite_vaapi.c +++ b/src/capture/xcomposite_vaapi.c @@ -1,13 +1,12 @@ #include "../../include/capture/xcomposite_vaapi.h" #include "../../include/egl.h" #include "../../include/window_texture.h" -#include "../../include/time.h" +#include "../../include/utils.h" #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <assert.h> #include <X11/Xlib.h> -#include <X11/extensions/Xcomposite.h> #include <libavutil/hwcontext.h> #include <libavutil/hwcontext_vaapi.h> #include <libavutil/frame.h> @@ -399,6 +398,8 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext * buf.flags = 0; buf.private_data = 0; + // TODO: Use VASurfaceAttribDRMFormatModifiers to set modifier if modifier is not INVALID + #define VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME 0x20000000 VASurfaceAttrib attribs[2] = {0}; @@ -448,10 +449,13 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext * // Copying a surface to another surface will automatically perform the color conversion. Thanks vaapi! VAProcPipelineParameterBuffer params = {0}; params.surface = cap_xcomp->input_surface; - params.surface_region = NULL; // TODO: Use when using kmsgrab to restrict region to captured monitor - params.output_region = &cap_xcomp->output_region; + params.surface_region = NULL; + if(cap_xcomp->params.follow_focused) + params.output_region = &cap_xcomp->output_region; + else + params.output_region = NULL; params.output_background_color = 0; - //params.filter_flags = VA_FRAME_PICTURE; + params.filter_flags = VA_FRAME_PICTURE; // TODO: Colors params.input_color_properties.color_range = (*frame)->color_range == AVCOL_RANGE_JPEG ? VA_SOURCE_RANGE_FULL : VA_SOURCE_RANGE_REDUCED; params.output_color_properties.color_range = (*frame)->color_range == AVCOL_RANGE_JPEG ? VA_SOURCE_RANGE_FULL : VA_SOURCE_RANGE_REDUCED; @@ -1,6 +1,8 @@ #include "../include/cuda.h" #include "../include/library_loader.h" #include <string.h> +#include <stdio.h> +#include <dlfcn.h> bool gsr_cuda_load(gsr_cuda *self, Display *display, bool do_overclock) { memset(self, 0, sizeof(gsr_cuda)); @@ -1,6 +1,8 @@ #include "../include/egl.h" #include "../include/library_loader.h" #include <string.h> +#include <stdio.h> +#include <dlfcn.h> static bool gsr_egl_create_window(gsr_egl *self) { EGLConfig ecfg; diff --git a/src/kms/kms_client.c b/src/kms/kms_client.c new file mode 100644 index 0000000..ededbe9 --- /dev/null +++ b/src/kms/kms_client.c @@ -0,0 +1,234 @@ +#include "../../include/kms/kms_client.h" +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include <unistd.h> +#include <signal.h> +#include <limits.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <sys/wait.h> +#include <sys/capability.h> + +static int send_msg_to_server(int server_fd, gsr_kms_request *request) { + struct iovec iov; + iov.iov_base = request; + iov.iov_len = sizeof(*request); + + struct msghdr request_message = {0}; + request_message.msg_iov = &iov; + request_message.msg_iovlen = 1; + + return sendmsg(server_fd, &request_message, 0); +} + +static int recv_msg_from_server(int server_fd, gsr_kms_response *response) { + struct iovec iov; + iov.iov_base = response; + iov.iov_len = sizeof(*response); + + struct msghdr response_message = {0}; + response_message.msg_iov = &iov; + response_message.msg_iovlen = 1; + + char cmsgbuf[CMSG_SPACE(sizeof(int))]; + memset(cmsgbuf, 0, sizeof(cmsgbuf)); + response_message.msg_control = cmsgbuf; + response_message.msg_controllen = sizeof(cmsgbuf); + + int res = recvmsg(server_fd, &response_message, MSG_WAITALL); + if(res <= 0) + return res; + + if(response->result == KMS_RESULT_OK) + response->data.fd.fd = 0; + + struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message); + if(cmsg) { + fprintf(stderr, "got cmsg, %d\n", cmsg->cmsg_type); + if(cmsg->cmsg_type == SCM_RIGHTS) { + int kms_fd = 0; + memcpy(&kms_fd, CMSG_DATA(cmsg), sizeof(int)); + fprintf(stderr, "kms fd: %d\n", kms_fd); + response->data.fd.fd = kms_fd; + } + } + + return res; +} + +int gsr_kms_client_init(gsr_kms_client *self, const char *card_path, const char *program_dir) { + self->kms_server_pid = -1; + self->card_path = NULL; + self->socket_fd = -1; + self->client_fd = -1; + self->socket_path[0] = '\0'; + struct sockaddr_un local_addr = {0}; + struct sockaddr_un remote_addr = {0}; + + // TODO: Check if gsr-kms-server is installed + // TODO: Check if pkexec is installed + + char server_filepath[PATH_MAX]; + snprintf(server_filepath, sizeof(server_filepath), "%s/%s", program_dir, "gsr-kms-server"); + + int has_perm = 0; + cap_t kms_server_cap = cap_get_file(server_filepath); + if(kms_server_cap) { + cap_flag_value_t res = 0; + cap_get_flag(kms_server_cap, CAP_SYS_ADMIN, CAP_PERMITTED, &res); + if(res == CAP_SET) { + //fprintf(stderr, "has permission!\n"); + has_perm = 1; + } else { + //fprintf(stderr, "No permission:(\n"); + } + cap_free(kms_server_cap); + } else { + if(errno == ENODATA) + fprintf(stderr, "gsr info: gsr_kms_client_init: gsr-kms-server is missing sys_admin cap and will require root authentication. To bypass this automatically, run: sudo setcap cap_sys_admin+ep '%s'\n", server_filepath); + else + fprintf(stderr, "failed to get cap\n"); + } + + self->card_path = strdup(card_path); + if(!self->card_path) { + fprintf(stderr, "gsr error: gsr_kms_client_init: failed to duplicate card_path\n"); + goto err; + } + + strcpy(self->socket_path, "/tmp/gsr-kms-socket-XXXXXX"); + if(!tmpnam(self->socket_path)) { + fprintf(stderr, "gsr error: gsr_kms_client_init: mkstemp failed, error: %s\n", strerror(errno)); + goto err; + } + + self->socket_fd = socket(AF_UNIX, SOCK_STREAM, 0); + if(self->socket_fd == -1) { + fprintf(stderr, "gsr error: gsr_kms_client_init: socket failed, error: %s\n", strerror(errno)); + goto err; + } + + local_addr.sun_family = AF_UNIX; + strncpy(local_addr.sun_path, self->socket_path, sizeof(local_addr.sun_path)); + if(bind(self->socket_fd, (struct sockaddr*)&local_addr, sizeof(local_addr.sun_family) + strlen(local_addr.sun_path)) == -1) { + fprintf(stderr, "gsr error: gsr_kms_client_init: failed to bind socket, error: %s\n", strerror(errno)); + goto err; + } + + if(listen(self->socket_fd, 1) == -1) { + fprintf(stderr, "gsr error: gsr_kms_client_init: failed to listen on socket, error: %s\n", strerror(errno)); + goto err; + } + + pid_t pid = fork(); + if(pid == -1) { + fprintf(stderr, "gsr error: gsr_kms_client_init: fork failed, error: %s\n", strerror(errno)); + goto err; + } else if(pid == 0) { /* child */ + if(has_perm) { + const char *args[] = { server_filepath, self->socket_path, NULL }; + execvp(args[0], (char *const*)args); + } else { + const char *args[] = { "pkexec", server_filepath, self->socket_path, NULL }; + execvp(args[0], (char *const*)args); + } + perror("execvp"); + _exit(127); + } else { /* parent */ + self->kms_server_pid = pid; + } + + fprintf(stderr, "gsr info: gsr_kms_client_init: waiting for client to connect\n"); + for(;;) { + struct timeval tv; + fd_set rfds; + FD_ZERO(&rfds); + FD_SET(self->socket_fd, &rfds); + + tv.tv_sec = 0; + tv.tv_usec = 100 * 1000; // 100 ms + + int select_res = select(1 + self->socket_fd, &rfds, NULL, NULL, &tv); + if(select_res > 0) { + socklen_t sock_len = 0; + self->client_fd = accept(self->socket_fd, (struct sockaddr*)&remote_addr, &sock_len); + if(self->client_fd == -1) { + fprintf(stderr, "gsr error: gsr_kms_client_init: accept failed on socket, error: %s\n", strerror(errno)); + goto err; + } + break; + } else { + int status; + int wait_result = waitpid(self->kms_server_pid, &status, WNOHANG); + if(wait_result > 0) { + fprintf(stderr, "gsr error: gsr_kms_client_init: waitpid failed on kms server, error: %s\n", strerror(errno)); + goto err; + } else if(wait_result > 0) { + fprintf(stderr, "gsr error: gsr_kms_client_init: kms server died\n"); + goto err; + } + } + } + fprintf(stderr, "gsr info: gsr_kms_client_init: client connected\n"); + + return 0; + + err: + gsr_kms_client_deinit(self); + return -1; +} + +void gsr_kms_client_deinit(gsr_kms_client *self) { + if(self->card_path) { + free(self->card_path); + self->card_path = NULL; + } + + if(self->client_fd != -1) { + close(self->client_fd); + self->client_fd = -1; + } + + if(self->socket_fd != -1) { + close(self->socket_fd); + self->socket_fd = -1; + } + + if(self->kms_server_pid != -1) { + kill(self->kms_server_pid, SIGINT); + int status; + waitpid(self->kms_server_pid, &status, 0); + self->kms_server_pid = -1; + } + + if(self->socket_path[0] != '\0') { + remove(self->socket_path); + self->socket_path[0] = '\0'; + } +} + +int gsr_kms_client_get_kms(gsr_kms_client *self, gsr_kms_response *response) { + response->result = KMS_RESULT_FAILED_TO_SEND; + strcpy(response->data.err_msg, "failed to send"); + + gsr_kms_request request; + request.type = KMS_REQUEST_TYPE_GET_KMS; + strcpy(request.data.card_path, self->card_path); + if(send_msg_to_server(self->client_fd, &request) == -1) { + fprintf(stderr, "gsr error: gsr_kms_client_get_kms: failed to send request message to server\n"); + return -1; + } + + const int recv_res = recv_msg_from_server(self->client_fd, response); + if(recv_res == 0) { + fprintf(stderr, "gsr warning: gsr_kms_client_get_kms: kms server shut down\n"); + return -1; + } else if(recv_res == -1) { + fprintf(stderr, "gsr error: gsr_kms_client_get_kms: failed to receive response\n"); + return -1; + } + + return 0; +} diff --git a/src/kms/kms_server.c b/src/kms/kms_server.c new file mode 100644 index 0000000..f811050 --- /dev/null +++ b/src/kms/kms_server.c @@ -0,0 +1,242 @@ +#include "../../include/kms/kms_shared.h" + +#include <asm-generic/socket.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> + +#include <unistd.h> +#include <fcntl.h> +#include <sys/socket.h> +#include <sys/un.h> + +#include <xf86drm.h> +#include <xf86drmMode.h> + +#define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2 + +static int max_int(int a, int b) { + return a > b ? a : b; +} + +static int send_msg_to_client(int client_fd, gsr_kms_response *response, int *fds, int num_fds) { + struct iovec iov; + iov.iov_base = response; + iov.iov_len = sizeof(*response); + + struct msghdr response_message = {0}; + response_message.msg_iov = &iov; + response_message.msg_iovlen = 1; + + char cmsgbuf[CMSG_SPACE(sizeof(int)) * max_int(1, num_fds)]; + memset(cmsgbuf, 0, sizeof(cmsgbuf)); + + if(num_fds > 0) { + response_message.msg_control = cmsgbuf; + response_message.msg_controllen = sizeof(cmsgbuf); + + int total_msg_len = 0; + struct cmsghdr *cmsg = NULL; + for(int i = 0; i < num_fds; ++i) { + if(i == 0) + cmsg = CMSG_FIRSTHDR(&response_message); + else + cmsg = CMSG_NXTHDR(&response_message, cmsg); + + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); + memcpy(CMSG_DATA(cmsg), &fds[i], sizeof(int)); + total_msg_len += cmsg->cmsg_len; + } + + response_message.msg_controllen = total_msg_len; + } + + return sendmsg(client_fd, &response_message, 0); +} + +static int get_kms(const char *card_path, gsr_kms_response *response) { + response->result = KMS_RESULT_OK; + response->data.fd.fd = 0; + response->data.fd.width = 0; + response->data.fd.height = 0; + + const int drmfd = open(card_path, O_RDONLY); + if (drmfd < 0) { + response->result = KMS_RESULT_FAILED_TO_OPEN_CARD; + snprintf(response->data.err_msg, sizeof(response->data.err_msg), "failed to open %s, error: %s", card_path, strerror(errno)); + return -1; + } + + if (0 != drmSetClientCap(drmfd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1)) { + response->result = KMS_RESULT_INSUFFICIENT_PERMISSIONS; + snprintf(response->data.err_msg, sizeof(response->data.err_msg), "drmSetClientCap failed, error: %s", strerror(errno)); + return -1; + } + + drmModePlaneResPtr planes = drmModeGetPlaneResources(drmfd); + if (!planes) { + response->result = KMS_RESULT_FAILED_TO_GET_KMS; + snprintf(response->data.err_msg, sizeof(response->data.err_msg), "failed to access planes, error: %s", strerror(errno)); + return -1; + } + + fprintf(stderr, "DRM planes %d:\n", planes->count_planes); + for (uint32_t i = 0; i < planes->count_planes; ++i) { + drmModePlanePtr plane = drmModeGetPlane(drmfd, planes->planes[i]); + if (!plane) { + fprintf(stderr, "Cannot get drmModePlanePtr for plane %#x: %s (%d)\n", planes->planes[i], strerror(errno), errno); + continue; + } + + fprintf(stderr, "\t%d: fb_id=%#x\n", i, plane->fb_id); + + if (!plane->fb_id) + goto plane_continue; + + drmModeFB2Ptr drmfb = drmModeGetFB2(drmfd, plane->fb_id); + if (!drmfb) { + fprintf(stderr, "Cannot get drmModeFBPtr for fb %#x: %s (%d)\n", plane->fb_id, strerror(errno), errno); + } else { + if (!drmfb->handles[0]) { + fprintf(stderr, "\t\tFB handle for fb %#x is NULL\n", plane->fb_id); + fprintf(stderr, "\t\tPossible reason: not permitted to get FB handles. Do `sudo setcap cap_sys_admin+ep`\n"); + } else { + int fb_fd = -1; + const int ret = drmPrimeHandleToFD(drmfd, drmfb->handles[0], 0, &fb_fd); + if (ret != 0 || fb_fd == -1) { + fprintf(stderr, "Cannot get fd for fb %#x handle %#x: %s (%d)\n", plane->fb_id, drmfb->handles[0], strerror(errno), errno); + } else if(drmfb->width * drmfb->height > response->data.fd.width * response->data.fd.height) { + if(response->data.fd.fd != 0) { + close(response->data.fd.fd); + response->data.fd.fd = 0; + } + + response->data.fd.fd = fb_fd; + response->data.fd.width = drmfb->width; + response->data.fd.height = drmfb->height; + response->data.fd.pitch = drmfb->pitches[0]; + response->data.fd.offset = drmfb->offsets[0]; + response->data.fd.pixel_format = drmfb->pixel_format; + response->data.fd.modifier = drmfb->modifier; + fprintf(stderr, "kms width: %u, height: %u, pixel format: %u, modifier: %lu\n", response->data.fd.width, response->data.fd.height, response->data.fd.pixel_format, response->data.fd.modifier); + } else { + close(fb_fd); + } + } + drmModeFreeFB2(drmfb); + } + + plane_continue: + drmModeFreePlane(plane); + } + + drmModeFreePlaneResources(planes); + close(drmfd); // TODO? + + if(response->data.fd.fd == 0) { + response->result = KMS_RESULT_NO_KMS_AVAILABLE; + snprintf(response->data.err_msg, sizeof(response->data.err_msg), "no kms found"); + return -1; + } + + return 0; +} + +int main(int argc, char **argv) { + if(argc != 2) { + fprintf(stderr, "usage: kms_server <domain_socket_path>\n"); + return 1; + } + + const char *domain_socket_path = argv[1]; + int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0); + if(socket_fd == -1) { + fprintf(stderr, "kms server error: failed to create socket, error: %s\n", strerror(errno)); + return 2; + } + + fprintf(stderr, "kms server info: connecting to the server\n"); + for(;;) { + struct sockaddr_un remote_addr = {0}; + remote_addr.sun_family = AF_UNIX; + strncpy(remote_addr.sun_path, domain_socket_path, sizeof(remote_addr.sun_path)); + // TODO: Check if parent disconnected + if(connect(socket_fd, (struct sockaddr*)&remote_addr, sizeof(remote_addr.sun_family) + strlen(remote_addr.sun_path)) == -1) { + if(errno == ECONNREFUSED || errno == ENOENT) + continue; // Host not ready yet? TODO: sleep + if(errno == EISCONN) // TODO? + break; + fprintf(stderr, "kms server error: connect failed, error: %s (%d)\n", strerror(errno), errno); + return 2; + } + } + fprintf(stderr, "kms server info: connected to the server\n"); + + int res = 0; + for(;;) { + gsr_kms_request request; + struct iovec iov; + iov.iov_base = &request; + iov.iov_len = sizeof(request); + + struct msghdr request_message = {0}; + request_message.msg_iov = &iov; + request_message.msg_iovlen = 1; + const int recv_res = recvmsg(socket_fd, &request_message, MSG_WAITALL); + if(recv_res == 0) { + fprintf(stderr, "kms server info: kms client shutdown, shutting down the server\n"); + res = 3; + goto done; + } else if(recv_res == -1) { + const int err = errno; + fprintf(stderr, "kms server error: failed to read all data in client request (error: %s), ignoring\n", strerror(err)); + if(err == EBADF) { + fprintf(stderr, "kms server error: invalid client fd, shutting down the server\n"); + res = 3; + goto done; + } + continue; + } + request.data.card_path[254] = '\0'; + + switch(request.type) { + case KMS_REQUEST_TYPE_GET_KMS: { + gsr_kms_response response; + int kms_fd = 0; + if (get_kms(request.data.card_path, &response) == 0) { + kms_fd = response.data.fd.fd; + } + + if(send_msg_to_client(socket_fd, &response, &kms_fd, kms_fd == 0 ? 0 : 1) == -1) { + fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n"); + if(kms_fd != 0) + close(kms_fd); + break; + } + + if(kms_fd != 0) + close(kms_fd); + + break; + } + default: { + gsr_kms_response response; + response.result = KMS_RESULT_INVALID_REQUEST; + snprintf(response.data.err_msg, sizeof(response.data.err_msg), "invalid request type %d, expected %d (%s)", request.type, KMS_REQUEST_TYPE_GET_KMS, "KMS_REQUEST_TYPE_GET_KMS"); + fprintf(stderr, "%s\n", response.data.err_msg); + if(send_msg_to_client(socket_fd, &response, NULL, 0) == -1) { + fprintf(stderr, "kms server error: failed to respond to client request\n"); + break; + } + break; + } + } + } + + done: + close(socket_fd); + return res; +} diff --git a/src/library_loader.c b/src/library_loader.c new file mode 100644 index 0000000..0aeee9b --- /dev/null +++ b/src/library_loader.c @@ -0,0 +1,34 @@ +#include "../include/library_loader.h" + +#include <dlfcn.h> +#include <stdbool.h> +#include <stdio.h> + +void* dlsym_print_fail(void *handle, const char *name, bool required) { + dlerror(); + void *sym = dlsym(handle, name); + char *err_str = dlerror(); + + if(!sym) + fprintf(stderr, "%s: dlsym(handle, \"%s\") failed, error: %s\n", required ? "error" : "warning", name, err_str ? err_str : "(null)"); + + return sym; +} + +/* |dlsyms| should be null terminated */ +bool dlsym_load_list(void *handle, const dlsym_assign *dlsyms) { + bool success = true; + for(int i = 0; dlsyms[i].func; ++i) { + *dlsyms[i].func = dlsym_print_fail(handle, dlsyms[i].name, true); + if(!*dlsyms[i].func) + success = false; + } + return success; +} + +/* |dlsyms| should be null terminated */ +void dlsym_load_list_optional(void *handle, const dlsym_assign *dlsyms) { + for(int i = 0; dlsyms[i].func; ++i) { + *dlsyms[i].func = dlsym_print_fail(handle, dlsyms[i].name, false); + } +} diff --git a/src/main.cpp b/src/main.cpp index 6b794ad..5beabd8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,8 +2,9 @@ extern "C" { #include "../include/capture/nvfbc.h" #include "../include/capture/xcomposite_cuda.h" #include "../include/capture/xcomposite_vaapi.h" +#include "../include/capture/kms_vaapi.h" #include "../include/egl.h" -#include "../include/time.h" +#include "../include/utils.h" } #include <assert.h> @@ -18,13 +19,10 @@ extern "C" { #include <signal.h> #include <sys/stat.h> -#include <unistd.h> -#include <fcntl.h> +#include <libgen.h> #include "../include/sound.hpp" -#include <X11/extensions/Xrandr.h> - extern "C" { #include <libavutil/pixfmt.h> #include <libavcodec/avcodec.h> @@ -55,71 +53,6 @@ static const int VIDEO_STREAM_INDEX = 0; static thread_local char av_error_buffer[AV_ERROR_MAX_STRING_SIZE]; -static const XRRModeInfo* get_mode_info(const XRRScreenResources *sr, RRMode id) { - for(int i = 0; i < sr->nmode; ++i) { - if(sr->modes[i].id == id) - return &sr->modes[i]; - } - return nullptr; -} - -typedef void (*active_monitor_callback)(const XRROutputInfo *output_info, const XRRCrtcInfo *crt_info, const XRRModeInfo *mode_info, void *userdata); - -static void for_each_active_monitor_output(Display *display, active_monitor_callback callback, void *userdata) { - XRRScreenResources *screen_res = XRRGetScreenResources(display, DefaultRootWindow(display)); - if(!screen_res) - return; - - for(int i = 0; i < screen_res->noutput; ++i) { - XRROutputInfo *out_info = XRRGetOutputInfo(display, screen_res, screen_res->outputs[i]); - if(out_info && out_info->crtc && out_info->connection == RR_Connected) { - XRRCrtcInfo *crt_info = XRRGetCrtcInfo(display, screen_res, out_info->crtc); - if(crt_info && crt_info->mode) { - const XRRModeInfo *mode_info = get_mode_info(screen_res, crt_info->mode); - if(mode_info) - callback(out_info, crt_info, mode_info, userdata); - } - if(crt_info) - XRRFreeCrtcInfo(crt_info); - } - if(out_info) - XRRFreeOutputInfo(out_info); - } - - XRRFreeScreenResources(screen_res); -} - -typedef struct { - vec2i pos; - vec2i size; -} gsr_monitor; - -typedef struct { - const char *name; - int name_len; - gsr_monitor *monitor; - bool found_monitor; -} get_monitor_by_name_userdata; - -static void get_monitor_by_name_callback(const XRROutputInfo *output_info, const XRRCrtcInfo *crt_info, const XRRModeInfo *mode_info, void *userdata) { - get_monitor_by_name_userdata *data = (get_monitor_by_name_userdata*)userdata; - if(!data->found_monitor && data->name_len == output_info->nameLen && memcmp(data->name, output_info->name, data->name_len) == 0) { - data->monitor->pos = { crt_info->x, crt_info->y }; - data->monitor->size = { (int)crt_info->width, (int)crt_info->height }; - data->found_monitor = true; - } -} - -static bool get_monitor_by_name(Display *display, const char *name, gsr_monitor *monitor) { - get_monitor_by_name_userdata userdata; - userdata.name = name; - userdata.name_len = strlen(name); - userdata.monitor = monitor; - userdata.found_monitor = false; - for_each_active_monitor_output(display, get_monitor_by_name_callback, &userdata); - return userdata.found_monitor; -} - static void monitor_output_callback_print(const XRROutputInfo *output_info, const XRRCrtcInfo *crt_info, const XRRModeInfo *mode_info, void *userdata) { fprintf(stderr, " \"%.*s\" (%dx%d+%d+%d)\n", output_info->nameLen, output_info->name, (int)crt_info->width, (int)crt_info->height, crt_info->x, crt_info->y); } @@ -375,7 +308,7 @@ static AVCodecContext *create_video_codec_context(AVPixelFormat pix_fmt, } codec_context->max_b_frames = 0; codec_context->pix_fmt = pix_fmt; - //codec_context->color_range = AVCOL_RANGE_JPEG; + codec_context->color_range = AVCOL_RANGE_JPEG; // TODO: Amd/nvidia? //codec_context->color_primaries = AVCOL_PRI_BT709; //codec_context->color_trc = AVCOL_TRC_BT709; //codec_context->colorspace = AVCOL_SPC_BT709; @@ -434,7 +367,8 @@ static AVCodecContext *create_video_codec_context(AVPixelFormat pix_fmt, // TODO: More options, better options //codec_context->bit_rate = codec_context->width * codec_context->height; av_opt_set(codec_context->priv_data, "rc_mode", "CQP", 0); - codec_context->global_quality = 4; + //codec_context->global_quality = 4; + codec_context->compression_level = 2; } //codec_context->rc_max_rate = codec_context->bit_rate; @@ -687,7 +621,7 @@ static void open_video(AVCodecContext *codec_context, VideoQuality video_quality if(codec_context->codec_id == AV_CODEC_ID_H264) { av_dict_set(&options, "profile", "high", 0); - av_dict_set_int(&options, "quality", 14, 0); + av_dict_set_int(&options, "quality", 7, 0); } else { av_dict_set(&options, "profile", "main", 0); } @@ -736,7 +670,7 @@ static void usage() { fprintf(stderr, " and the video will only be saved when the gpu-screen-recorder is closed. This feature is similar to Nvidia's instant replay feature.\n"); fprintf(stderr, " This option has be between 5 and 1200. Note that the replay buffer size will not always be precise, because of keyframes. Optional, disabled by default.\n"); fprintf(stderr, "\n"); - fprintf(stderr, " -k Video codec to use. Should be either 'auto', 'h264' or 'h265'. Defaults to 'auto' which defaults to 'h265' unless recording at fps higher than 60.\n"); + fprintf(stderr, " -k Video codec to use. Should be either 'auto', 'h264' or 'h265'. Defaults to 'auto' which defaults to 'h265' unless recording at fps higher than 60. Defaults to 'h264' on intel.\n"); fprintf(stderr, " Forcefully set to 'h264' if -c is 'flv'.\n"); fprintf(stderr, "\n"); fprintf(stderr, " -ac Audio codec to use. Should be either 'aac', 'opus' or 'flac'. Defaults to 'opus' for .mp4/.mkv files, otherwise defaults to 'aac'.\n"); @@ -1174,6 +1108,14 @@ int main(int argc, char **argv) { signal(SIGINT, int_handler); signal(SIGUSR1, save_replay_handler); + if(argc == 0) + usage(); + + char *program_dir = dirname(argv[0]); + char program_dir_full[PATH_MAX]; + program_dir_full[0] = '\0'; + realpath(program_dir, program_dir_full); + //av_log_set_level(AV_LOG_TRACE); std::map<std::string, Arg> args = { @@ -1408,11 +1350,6 @@ int main(int argc, char **argv) { follow_focused = true; } else if(contains_non_hex_number(window_str)) { - if(gpu_inf.vendor != GPU_VENDOR_NVIDIA) { - fprintf(stderr, "Error: recording a monitor is only supported on NVIDIA right now. Record \"focused\" instead for convenient fullscreen window recording\n"); - return 2; - } - if(strcmp(window_str, "screen") != 0 && strcmp(window_str, "screen-direct") != 0 && strcmp(window_str, "screen-direct-force") != 0) { gsr_monitor gmon; if(!get_monitor_by_name(dpy, window_str, &gmon)) { @@ -1425,31 +1362,46 @@ int main(int argc, char **argv) { } } - const char *capture_target = window_str; - bool direct_capture = strcmp(window_str, "screen-direct") == 0; - if(direct_capture) { - capture_target = "screen"; - // TODO: Temporary disable direct capture because push model causes stuttering when it's direct capturing. This might be a nvfbc bug. This does not happen when using a compositor. - direct_capture = false; - fprintf(stderr, "Warning: screen-direct has temporary been disabled as it causes stuttering. This is likely a NvFBC bug. Falling back to \"screen\".\n"); - } + if(gpu_inf.vendor == GPU_VENDOR_NVIDIA) { + const char *capture_target = window_str; + bool direct_capture = strcmp(window_str, "screen-direct") == 0; + if(direct_capture) { + capture_target = "screen"; + // TODO: Temporary disable direct capture because push model causes stuttering when it's direct capturing. This might be a nvfbc bug. This does not happen when using a compositor. + direct_capture = false; + fprintf(stderr, "Warning: screen-direct has temporary been disabled as it causes stuttering. This is likely a NvFBC bug. Falling back to \"screen\".\n"); + } - if(strcmp(window_str, "screen-direct-force") == 0) { - direct_capture = true; - capture_target = "screen"; - } + if(strcmp(window_str, "screen-direct-force") == 0) { + direct_capture = true; + capture_target = "screen"; + } - gsr_capture_nvfbc_params nvfbc_params; - nvfbc_params.dpy = dpy; - nvfbc_params.display_to_capture = capture_target; - nvfbc_params.fps = fps; - nvfbc_params.pos = { 0, 0 }; - nvfbc_params.size = { 0, 0 }; - nvfbc_params.direct_capture = direct_capture; - nvfbc_params.overclock = overclock; - capture = gsr_capture_nvfbc_create(&nvfbc_params); - if(!capture) - return 1; + gsr_capture_nvfbc_params nvfbc_params; + nvfbc_params.dpy = dpy; + nvfbc_params.display_to_capture = capture_target; + nvfbc_params.fps = fps; + nvfbc_params.pos = { 0, 0 }; + nvfbc_params.size = { 0, 0 }; + nvfbc_params.direct_capture = direct_capture; + nvfbc_params.overclock = overclock; + capture = gsr_capture_nvfbc_create(&nvfbc_params); + if(!capture) + return 1; + } else { + const char *capture_target = window_str; + if(strcmp(window_str, "screen-direct") == 0 || strcmp(window_str, "screen-direct-force") == 0) { + capture_target = "screen"; + } + + gsr_capture_kms_vaapi_params kms_params; + kms_params.dpy = dpy; + kms_params.display_to_capture = capture_target; + kms_params.program_dir = program_dir_full; + capture = gsr_capture_kms_vaapi_create(&kms_params); + if(!capture) + return 1; + } } else { errno = 0; src_window_id = strtol(window_str, nullptr, 0); @@ -1565,23 +1517,36 @@ int main(int argc, char **argv) { const double target_fps = 1.0 / (double)fps; if(strcmp(video_codec_to_use, "auto") == 0) { - const AVCodec *h265_codec = find_h265_encoder(gpu_inf.vendor); - - // h265 generally allows recording at a higher resolution than h264 on nvidia cards. On a gtx 1080 4k is the max resolution for h264 but for h265 it's 8k. - // Another important info is that when recording at a higher fps than.. 60? h265 has very bad performance. For example when recording at 144 fps the fps drops to 1 - // while with h264 the fps doesn't drop. - if(!h265_codec) { - fprintf(stderr, "Info: using h264 encoder because a codec was not specified and your gpu does not support h265\n"); - video_codec_to_use = "h264"; - video_codec = VideoCodec::H264; - } else if(fps > 60) { - fprintf(stderr, "Info: using h264 encoder because a codec was not specified and fps is more than 60\n"); - video_codec_to_use = "h264"; - video_codec = VideoCodec::H264; + if(gpu_inf.vendor == GPU_VENDOR_INTEL) { + const AVCodec *h264_codec = find_h264_encoder(gpu_inf.vendor); + if(!h264_codec) { + fprintf(stderr, "Info: using h265 encoder because a codec was not specified and your gpu does not support h264\n"); + video_codec_to_use = "h265"; + video_codec = VideoCodec::H265; + } else { + fprintf(stderr, "Info: using h264 encoder because a codec was not specified\n"); + video_codec_to_use = "h264"; + video_codec = VideoCodec::H264; + } } else { - fprintf(stderr, "Info: using h265 encoder because a codec was not specified\n"); - video_codec_to_use = "h265"; - video_codec = VideoCodec::H265; + const AVCodec *h265_codec = find_h265_encoder(gpu_inf.vendor); + + // h265 generally allows recording at a higher resolution than h264 on nvidia cards. On a gtx 1080 4k is the max resolution for h264 but for h265 it's 8k. + // Another important info is that when recording at a higher fps than.. 60? h265 has very bad performance. For example when recording at 144 fps the fps drops to 1 + // while with h264 the fps doesn't drop. + if(!h265_codec) { + fprintf(stderr, "Info: using h264 encoder because a codec was not specified and your gpu does not support h265\n"); + video_codec_to_use = "h264"; + video_codec = VideoCodec::H264; + } else if(fps > 60) { + fprintf(stderr, "Info: using h264 encoder because a codec was not specified and fps is more than 60\n"); + video_codec_to_use = "h264"; + video_codec = VideoCodec::H264; + } else { + fprintf(stderr, "Info: using h265 encoder because a codec was not specified\n"); + video_codec_to_use = "h265"; + video_codec = VideoCodec::H265; + } } } @@ -1730,7 +1695,7 @@ int main(int argc, char **argv) { const double start_time_pts = clock_get_monotonic_seconds(); - double start_time = clock_get_monotonic_seconds(); + double start_time = clock_get_monotonic_seconds(); // todo - target_fps to make first frame start immediately? double frame_timer_start = start_time; int fps_counter = 0; diff --git a/src/sound.cpp b/src/sound.cpp index 762d962..f29e474 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -1,6 +1,6 @@ #include "../include/sound.hpp" extern "C" { -#include "../include/time.h" +#include "../include/utils.h" } #include <stdlib.h> diff --git a/src/time.c b/src/time.c deleted file mode 100644 index b2dcdea..0000000 --- a/src/time.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "../include/time.h" -#include <time.h> - -double clock_get_monotonic_seconds() { - struct timespec ts; - ts.tv_sec = 0; - ts.tv_nsec = 0; - clock_gettime(CLOCK_MONOTONIC, &ts); - return (double)ts.tv_sec + (double)ts.tv_nsec * 0.000000001; -} diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..bf0a0c1 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,62 @@ +#include "../include/utils.h" +#include <time.h> +#include <string.h> + +double clock_get_monotonic_seconds(void) { + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = 0; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (double)ts.tv_sec + (double)ts.tv_nsec * 0.000000001; +} + +static const XRRModeInfo* get_mode_info(const XRRScreenResources *sr, RRMode id) { + for(int i = 0; i < sr->nmode; ++i) { + if(sr->modes[i].id == id) + return &sr->modes[i]; + } + return NULL; +} + +void for_each_active_monitor_output(Display *display, active_monitor_callback callback, void *userdata) { + XRRScreenResources *screen_res = XRRGetScreenResources(display, DefaultRootWindow(display)); + if(!screen_res) + return; + + for(int i = 0; i < screen_res->noutput; ++i) { + XRROutputInfo *out_info = XRRGetOutputInfo(display, screen_res, screen_res->outputs[i]); + if(out_info && out_info->crtc && out_info->connection == RR_Connected) { + XRRCrtcInfo *crt_info = XRRGetCrtcInfo(display, screen_res, out_info->crtc); + if(crt_info && crt_info->mode) { + const XRRModeInfo *mode_info = get_mode_info(screen_res, crt_info->mode); + if(mode_info) + callback(out_info, crt_info, mode_info, userdata); + } + if(crt_info) + XRRFreeCrtcInfo(crt_info); + } + if(out_info) + XRRFreeOutputInfo(out_info); + } + + XRRFreeScreenResources(screen_res); +} + +static void get_monitor_by_name_callback(const XRROutputInfo *output_info, const XRRCrtcInfo *crt_info, const XRRModeInfo *mode_info, void *userdata) { + get_monitor_by_name_userdata *data = (get_monitor_by_name_userdata*)userdata; + if(!data->found_monitor && data->name_len == output_info->nameLen && memcmp(data->name, output_info->name, data->name_len) == 0) { + data->monitor->pos = (vec2i){ .x = crt_info->x, .y = crt_info->y }; + data->monitor->size = (vec2i){ .x = (int)crt_info->width, .y = (int)crt_info->height }; + data->found_monitor = true; + } +} + +bool get_monitor_by_name(Display *display, const char *name, gsr_monitor *monitor) { + get_monitor_by_name_userdata userdata; + userdata.name = name; + userdata.name_len = strlen(name); + userdata.monitor = monitor; + userdata.found_monitor = false; + for_each_active_monitor_output(display, get_monitor_by_name_callback, &userdata); + return userdata.found_monitor; +} diff --git a/src/xnvctrl.c b/src/xnvctrl.c index 94d48d2..b738455 100644 --- a/src/xnvctrl.c +++ b/src/xnvctrl.c @@ -1,6 +1,8 @@ #include "../include/xnvctrl.h" #include "../include/library_loader.h" #include <string.h> +#include <stdio.h> +#include <dlfcn.h> bool gsr_xnvctrl_load(gsr_xnvctrl *self, Display *display) { memset(self, 0, sizeof(gsr_xnvctrl)); |