Implement kms vaapi capture
This commit is contained in:
parent
10d7bf93e8
commit
75ed160122
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,6 +10,7 @@ tests/compile_commands.json
|
||||
|
||||
*.o
|
||||
gpu-screen-recorder
|
||||
gsr-kms-server
|
||||
|
||||
*.mp4
|
||||
*.flv
|
||||
|
@ -6,7 +6,7 @@ This screen recorder can be used for recording your desktop offline, for live st
|
||||
where only the last few seconds are saved.
|
||||
|
||||
## Note
|
||||
This software works only on x11 and with an nvidia gpu.\
|
||||
This software works only on x11.\
|
||||
If you are using a variable refresh rate monitor then choose to record "screen-direct". This will allow variable refresh rate to work when recording fullscreen applications. Note that some applications such as mpv will not work in fullscreen mode. A fix is being developed for this.\
|
||||
For screen capture to work with PRIME (laptops with a nvidia gpu), you must set the primary GPU to use your dedicated nvidia graphics card. You can do this by selecting "NVIDIA (Performance Mode) in nvidia settings:\
|
||||
![](https://dec05eba.com/images/nvidia-settings-prime.png)\
|
||||
@ -35,11 +35,11 @@ You can also install gpu screen recorder ([the gtk gui version](https://git.dec0
|
||||
|
||||
# Dependencies
|
||||
## AMD
|
||||
`libglvnd (which provides libgl and libegl), mesa, ffmpeg (libavcodec, libavformat, libavutil, libswresample, libavfilter), libx11, libxcomposite, libxrandr, libpulse, libva, libva-mesa-driver`.
|
||||
`libglvnd (which provides libgl and libegl), mesa, ffmpeg (libavcodec, libavformat, libavutil, libswresample, libavfilter), libx11, libxcomposite, libxrandr, libpulse, libva, libva-mesa-driver, libdrm, libcap`.
|
||||
## Intel
|
||||
`libglvnd (which provides libgl and libegl), mesa, ffmpeg (libavcodec, libavformat, libavutil, libswresample, libavfilter), libx11, libxcomposite, libxrandr, libpulse, libva, libva-intel-driver`.
|
||||
`libglvnd (which provides libgl and libegl), mesa, ffmpeg (libavcodec, libavformat, libavutil, libswresample, libavfilter), libx11, libxcomposite, libxrandr, libpulse, libva, libva-intel-driver, libdrm, libcap`.
|
||||
## NVIDIA
|
||||
`libglvnd (which provides libgl and libegl), ffmpeg (libavcodec, libavformat, libavutil, libswresample, libavfilter), libx11, libxcomposite, libxrandr, libpulse, cuda (libnvidia-compute), nvenc (libnvidia-encode), libva`. Additionally, you need to have `nvfbc (libnvidia-fbc1)` installed when using nvfbc and `xnvctrl (libxnvctrl0)` when using the `-oc` option.
|
||||
`libglvnd (which provides libgl and libegl), ffmpeg (libavcodec, libavformat, libavutil, libswresample, libavfilter), libx11, libxcomposite, libxrandr, libpulse, cuda (libnvidia-compute), nvenc (libnvidia-encode), libva, libdrm, libcap`. Additionally, you need to have `nvfbc (libnvidia-fbc1)` installed when using nvfbc and `xnvctrl (libxnvctrl0)` when using the `-oc` option.
|
||||
|
||||
# How to use
|
||||
Run `scripts/interactive.sh` or run gpu-screen-recorder directly, for example: `gpu-screen-recorder -w $(xdotool selectwindow) -c mp4 -f 60 -a "$(pactl get-default-sink).monitor" -o test_video.mp4` then stop the screen recorder with Ctrl+C, which will also save the recording. You can change -w to -w screen if you want to record all monitors or if you want to record a specific monitor then you can use -w monitor-name, for example -w HDMI-0 (use xrandr command to find the name of your monitor. The name can also be found in your desktop environments display settings).\
|
||||
|
24
build.sh
24
build.sh
@ -1,7 +1,16 @@
|
||||
#!/bin/sh -e
|
||||
|
||||
#libdrm
|
||||
dependencies="libavcodec libavformat libavutil x11 xcomposite xrandr libpulse libswresample libavfilter libva"
|
||||
build_gsr_kms_server() {
|
||||
dependencies="libdrm"
|
||||
includes="$(pkg-config --cflags $dependencies)"
|
||||
libs="$(pkg-config --libs $dependencies) -ldl"
|
||||
opts="-O2 -g0 -DNDEBUG"
|
||||
gcc -c src/kms/kms_server.c $opts $includes
|
||||
gcc -o gsr-kms-server -O2 kms_server.o -s $libs
|
||||
}
|
||||
|
||||
build_gsr() {
|
||||
dependencies="libavcodec libavformat libavutil x11 xcomposite xrandr libpulse libswresample libavfilter libva libcap"
|
||||
includes="$(pkg-config --cflags $dependencies)"
|
||||
libs="$(pkg-config --libs $dependencies) -ldl -pthread -lm"
|
||||
opts="-O2 -g0 -DNDEBUG"
|
||||
@ -9,13 +18,20 @@ gcc -c src/capture/capture.c $opts $includes
|
||||
gcc -c src/capture/nvfbc.c $opts $includes
|
||||
gcc -c src/capture/xcomposite_cuda.c $opts $includes
|
||||
gcc -c src/capture/xcomposite_vaapi.c $opts $includes
|
||||
gcc -c src/capture/kms_vaapi.c $opts $includes
|
||||
gcc -c src/kms/kms_client.c $opts $includes
|
||||
gcc -c src/egl.c $opts $includes
|
||||
gcc -c src/cuda.c $opts $includes
|
||||
gcc -c src/xnvctrl.c $opts $includes
|
||||
gcc -c src/overclock.c $opts $includes
|
||||
gcc -c src/window_texture.c $opts $includes
|
||||
gcc -c src/time.c $opts $includes
|
||||
gcc -c src/utils.c $opts $includes
|
||||
gcc -c src/library_loader.c $opts $includes
|
||||
g++ -c src/sound.cpp $opts $includes
|
||||
g++ -c src/main.cpp $opts $includes
|
||||
g++ -o gpu-screen-recorder -O2 capture.o nvfbc.o egl.o cuda.o xnvctrl.o overclock.o window_texture.o time.o xcomposite_cuda.o xcomposite_vaapi.o sound.o main.o -s $libs
|
||||
g++ -o gpu-screen-recorder -O2 capture.o nvfbc.o kms_client.o egl.o cuda.o xnvctrl.o overclock.o window_texture.o utils.o library_loader.o xcomposite_cuda.o xcomposite_vaapi.o kms_vaapi.o sound.o main.o -s $libs
|
||||
}
|
||||
|
||||
build_gsr_kms_server
|
||||
build_gsr
|
||||
echo "Successfully built gpu-screen-recorder"
|
18
include/capture/kms_vaapi.h
Normal file
18
include/capture/kms_vaapi.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef GSR_CAPTURE_KMS_VAAPI_H
|
||||
#define GSR_CAPTURE_KMS_VAAPI_H
|
||||
|
||||
#include "capture.h"
|
||||
#include "../vec2.h"
|
||||
#include <X11/X.h>
|
||||
|
||||
typedef struct _XDisplay Display;
|
||||
|
||||
typedef struct {
|
||||
Display *dpy;
|
||||
const char *display_to_capture; /* if this is "screen", then the entire x11 screen is captured (all displays). A copy is made of this */
|
||||
const char *program_dir; /* ref */
|
||||
} gsr_capture_kms_vaapi_params;
|
||||
|
||||
gsr_capture* gsr_capture_kms_vaapi_create(const gsr_capture_kms_vaapi_params *params);
|
||||
|
||||
#endif /* GSR_CAPTURE_KMS_VAAPI_H */
|
21
include/kms/kms_client.h
Normal file
21
include/kms/kms_client.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef GSR_KMS_CLIENT_H
|
||||
#define GSR_KMS_CLIENT_H
|
||||
|
||||
#include "kms_shared.h"
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef struct {
|
||||
pid_t kms_server_pid;
|
||||
int socket_fd;
|
||||
int client_fd;
|
||||
char socket_path[27];
|
||||
char *card_path;
|
||||
} gsr_kms_client;
|
||||
|
||||
/* |card_path| should be a path to card, for example /dev/dri/card0 */
|
||||
int gsr_kms_client_init(gsr_kms_client *self, const char *card_path, const char *program_dir);
|
||||
void gsr_kms_client_deinit(gsr_kms_client *self);
|
||||
|
||||
int gsr_kms_client_get_kms(gsr_kms_client *self, gsr_kms_response *response);
|
||||
|
||||
#endif /* #define GSR_KMS_CLIENT_H */
|
45
include/kms/kms_shared.h
Normal file
45
include/kms/kms_shared.h
Normal file
@ -0,0 +1,45 @@
|
||||
#ifndef GSR_KMS_SHARED_H
|
||||
#define GSR_KMS_SHARED_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum {
|
||||
KMS_REQUEST_TYPE_GET_KMS
|
||||
} gsr_kms_request_type;
|
||||
|
||||
typedef enum {
|
||||
KMS_RESULT_OK,
|
||||
KMS_RESULT_INVALID_REQUEST,
|
||||
KMS_RESULT_FAILED_TO_OPEN_CARD,
|
||||
KMS_RESULT_INSUFFICIENT_PERMISSIONS,
|
||||
KMS_RESULT_FAILED_TO_GET_KMS,
|
||||
KMS_RESULT_NO_KMS_AVAILABLE,
|
||||
KMS_RESULT_FAILED_TO_SEND
|
||||
} gsr_kms_result;
|
||||
|
||||
typedef struct {
|
||||
int type; /* gsr_kms_request_type */
|
||||
union {
|
||||
char card_path[255];
|
||||
} data;
|
||||
} gsr_kms_request;
|
||||
|
||||
typedef struct {
|
||||
int fd;
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint32_t pitch;
|
||||
uint32_t offset;
|
||||
uint32_t pixel_format;
|
||||
uint64_t modifier;
|
||||
} gsr_kms_response_fd;
|
||||
|
||||
typedef struct {
|
||||
int result; /* gsr_kms_result */
|
||||
union {
|
||||
char err_msg[128];
|
||||
gsr_kms_response_fd fd;
|
||||
} data;
|
||||
} gsr_kms_response;
|
||||
|
||||
#endif /* #define GSR_KMS_SHARED_H */
|
@ -1,42 +1,17 @@
|
||||
#ifndef GSR_LIBRARY_LOADER_H
|
||||
#define GSR_LIBRARY_LOADER_H
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
void **func;
|
||||
const char *name;
|
||||
} dlsym_assign;
|
||||
|
||||
static 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;
|
||||
}
|
||||
|
||||
void* dlsym_print_fail(void *handle, const char *name, bool required);
|
||||
/* |dlsyms| should be null terminated */
|
||||
static 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;
|
||||
}
|
||||
|
||||
bool dlsym_load_list(void *handle, const dlsym_assign *dlsyms);
|
||||
/* |dlsyms| should be null terminated */
|
||||
static 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);
|
||||
}
|
||||
}
|
||||
void dlsym_load_list_optional(void *handle, const dlsym_assign *dlsyms);
|
||||
|
||||
#endif /* GSR_LIBRARY_LOADER_H */
|
||||
|
@ -1,6 +0,0 @@
|
||||
#ifndef GSR_TIME_H
|
||||
#define GSR_TIME_H
|
||||
|
||||
double clock_get_monotonic_seconds();
|
||||
|
||||
#endif /* GSR_TIME_H */
|
26
include/utils.h
Normal file
26
include/utils.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef GSR_UTILS_H
|
||||
#define GSR_UTILS_H
|
||||
|
||||
#include "vec2.h"
|
||||
#include <stdbool.h>
|
||||
#include <X11/extensions/Xrandr.h>
|
||||
|
||||
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;
|
||||
|
||||
double clock_get_monotonic_seconds(void);
|
||||
|
||||
typedef void (*active_monitor_callback)(const XRROutputInfo *output_info, const XRRCrtcInfo *crt_info, const XRRModeInfo *mode_info, void *userdata);
|
||||
void for_each_active_monitor_output(Display *display, active_monitor_callback callback, void *userdata);
|
||||
bool get_monitor_by_name(Display *display, const char *name, gsr_monitor *monitor);
|
||||
|
||||
#endif /* GSR_UTILS_H */
|
@ -6,7 +6,8 @@ cd "$script_dir"
|
||||
[ $(id -u) -ne 0 ] && echo "You need root privileges to run the install script" && exit 1
|
||||
|
||||
./build.sh
|
||||
install -Dm755 "gpu-screen-recorder" "/usr/local/bin/gpu-screen-recorder"
|
||||
rm -f "/usr/local/bin/gpu-screen-recorder"
|
||||
install -Dm755 "gsr-kms-server" "/usr/bin/gsr-kms-server"
|
||||
install -Dm755 "gpu-screen-recorder" "/usr/bin/gpu-screen-recorder"
|
||||
|
||||
[ -f "/proc/driver/nvidia/version" ] && ./install_coolbits.sh
|
||||
|
@ -9,6 +9,6 @@ set -e
|
||||
apt-get -y install build-essential\
|
||||
libswresample-dev libavformat-dev libavcodec-dev libavutil-dev libavfilter-dev\
|
||||
libglvnd-dev libx11-dev libxcomposite-dev libxrandr-dev\
|
||||
libpulse-dev libva2 libegl-mesa0 mesa-va-drivers
|
||||
libpulse-dev libva2 libegl-mesa0 mesa-va-drivers libdrm-dev libcap-dev
|
||||
|
||||
./install.sh
|
||||
|
@ -9,6 +9,6 @@ set -e
|
||||
apt-get -y install build-essential\
|
||||
libswresample-dev libavformat-dev libavcodec-dev libavutil-dev libavfilter-dev\
|
||||
libglvnd-dev libx11-dev libxcomposite-dev libxrandr-dev\
|
||||
libpulse-dev libva2 libegl-mesa0 intel-media-va-driver
|
||||
libpulse-dev libva2 libegl-mesa0 intel-media-va-driver libdrm-dev libcap-dev
|
||||
|
||||
./install.sh
|
||||
|
@ -9,6 +9,7 @@ set -e
|
||||
apt-get -y install build-essential\
|
||||
libswresample-dev libavformat-dev libavcodec-dev libavutil-dev libavfilter-dev\
|
||||
libglvnd-dev libx11-dev libxcomposite-dev libxrandr-dev\
|
||||
libpulse-dev libva2 libxnvctrl0 libnvidia-compute libnvidia-encode libnvidia-fbc1
|
||||
libpulse-dev libva2 libxnvctrl0 libnvidia-compute libnvidia-encode libnvidia-fbc1\
|
||||
libdrm-dev libcap-dev
|
||||
|
||||
./install.sh
|
||||
|
@ -4,6 +4,9 @@ type = "executable"
|
||||
version = "1.3.0"
|
||||
platforms = ["posix"]
|
||||
|
||||
[config]
|
||||
ignore_dirs = ["src/kms"]
|
||||
|
||||
[dependencies]
|
||||
libavcodec = ">=58"
|
||||
libavformat = ">=58"
|
||||
|
470
src/capture/kms_vaapi.c
Normal file
470
src/capture/kms_vaapi.c
Normal file
@ -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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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>
|
||||
|
@ -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.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;
|
||||
|
234
src/kms/kms_client.c
Normal file
234
src/kms/kms_client.c
Normal file
@ -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;
|
||||
}
|
242
src/kms/kms_server.c
Normal file
242
src/kms/kms_server.c
Normal file
@ -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;
|
||||
}
|
34
src/library_loader.c
Normal file
34
src/library_loader.c
Normal file
@ -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);
|
||||
}
|
||||
}
|
125
src/main.cpp
125
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,6 +1362,7 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -1450,6 +1388,20 @@ int main(int argc, char **argv) {
|
||||
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,6 +1517,18 @@ int main(int argc, char **argv) {
|
||||
const double target_fps = 1.0 / (double)fps;
|
||||
|
||||
if(strcmp(video_codec_to_use, "auto") == 0) {
|
||||
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 {
|
||||
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.
|
||||
@ -1584,6 +1548,7 @@ int main(int argc, char **argv) {
|
||||
video_codec = VideoCodec::H265;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//bool use_hevc = strcmp(window_str, "screen") == 0 || strcmp(window_str, "screen-direct") == 0;
|
||||
if(video_codec != VideoCodec::H264 && strcmp(file_extension.c_str(), "flv") == 0) {
|
||||
@ -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;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "../include/sound.hpp"
|
||||
extern "C" {
|
||||
#include "../include/time.h"
|
||||
#include "../include/utils.h"
|
||||
}
|
||||
|
||||
#include <stdlib.h>
|
||||
|
10
src/time.c
10
src/time.c
@ -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;
|
||||
}
|
62
src/utils.c
Normal file
62
src/utils.c
Normal file
@ -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;
|
||||
}
|
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user