Fix window capture

This commit is contained in:
dec05eba 2023-07-21 19:12:24 +02:00
parent a1d31a5cf0
commit 8b0723a4f2
8 changed files with 63 additions and 96 deletions

View File

@ -9,6 +9,7 @@ typedef struct _XDisplay Display;
typedef struct { typedef struct {
gsr_egl *egl; gsr_egl *egl;
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 *display_to_capture; /* if this is "screen", then the entire x11 screen is captured (all displays). A copy is made of this */
gsr_gpu_info gpu_inf; gsr_gpu_info gpu_inf;
const char *card_path; /* reference */ const char *card_path; /* reference */

View File

@ -10,6 +10,7 @@ typedef struct _XDisplay Display;
typedef struct { typedef struct {
gsr_egl *egl; gsr_egl *egl;
Display *dpy;
Window window; Window window;
bool follow_focused; /* If this is set then |window| is ignored */ bool follow_focused; /* If this is set then |window| is ignored */
vec2i region_size; /* This is currently only used with |follow_focused| */ vec2i region_size; /* This is currently only used with |follow_focused| */

View File

@ -10,6 +10,7 @@ typedef struct _XDisplay Display;
typedef struct { typedef struct {
gsr_egl *egl; gsr_egl *egl;
Display *dpy;
Window window; Window window;
bool follow_focused; /* If this is set then |window| is ignored */ bool follow_focused; /* If this is set then |window| is ignored */
vec2i region_size; /* This is currently only used with |follow_focused| */ vec2i region_size; /* This is currently only used with |follow_focused| */

View File

@ -163,8 +163,8 @@ static int gsr_capture_kms_cuda_start(gsr_capture *cap, AVCodecContext *video_co
cap_kms->capture_pos = monitor.pos; cap_kms->capture_pos = monitor.pos;
cap_kms->capture_size = monitor.size; cap_kms->capture_size = monitor.size;
video_codec_context->width = max_int(2, even_number_ceil(cap_kms->capture_size.x)); video_codec_context->width = max_int(2, cap_kms->capture_size.x & ~1);
video_codec_context->height = max_int(2, even_number_ceil(cap_kms->capture_size.y)); video_codec_context->height = max_int(2, cap_kms->capture_size.y & ~1);
/* Disable vsync */ /* Disable vsync */
cap_kms->params.egl->eglSwapInterval(cap_kms->params.egl->egl_display, 0); cap_kms->params.egl->eglSwapInterval(cap_kms->params.egl->egl_display, 0);

View File

@ -32,7 +32,6 @@ typedef enum {
typedef struct { typedef struct {
gsr_capture_kms_vaapi_params params; gsr_capture_kms_vaapi_params params;
Display *dpy;
XEvent xev; XEvent xev;
bool should_stop; bool should_stop;
@ -160,7 +159,7 @@ static void monitor_callback(const gsr_monitor *monitor, void *userdata) {
monitor_callback_userdata->rotation = monitor->crt_info->rotation; monitor_callback_userdata->rotation = monitor->crt_info->rotation;
for(int i = 0; i < monitor->crt_info->noutput && monitor_callback_userdata->cap_kms->monitor_id.num_connector_ids < MAX_CONNECTOR_IDS; ++i) { for(int i = 0; i < monitor->crt_info->noutput && monitor_callback_userdata->cap_kms->monitor_id.num_connector_ids < MAX_CONNECTOR_IDS; ++i) {
int nprop = 0; int nprop = 0;
Atom *props = XRRListOutputProperties(monitor_callback_userdata->cap_kms->dpy, monitor->crt_info->outputs[i], &nprop); Atom *props = XRRListOutputProperties(monitor_callback_userdata->cap_kms->params.dpy, monitor->crt_info->outputs[i], &nprop);
if(!props) if(!props)
continue; continue;
@ -174,7 +173,7 @@ static void monitor_callback(const gsr_monitor *monitor, void *userdata) {
unsigned long bytes_after = 0; unsigned long bytes_after = 0;
unsigned long nitems = 0; unsigned long nitems = 0;
unsigned char *prop = NULL; unsigned char *prop = NULL;
XRRGetOutputProperty(monitor_callback_userdata->cap_kms->dpy, monitor->crt_info->outputs[i], XRRGetOutputProperty(monitor_callback_userdata->cap_kms->params.dpy, monitor->crt_info->outputs[i],
monitor_callback_userdata->randr_connector_id_atom, monitor_callback_userdata->randr_connector_id_atom,
0, 128, false, false, AnyPropertyType, 0, 128, false, false, AnyPropertyType,
&type, &format, &nitems, &bytes_after, &prop); &type, &format, &nitems, &bytes_after, &prop);
@ -212,7 +211,7 @@ static int gsr_capture_kms_vaapi_start(gsr_capture *cap, AVCodecContext *video_c
return -1; return -1;
} }
void *connection = cap_kms->params.wayland ? (void*)cap_kms->params.card_path : (void*)cap_kms->dpy; void *connection = cap_kms->params.wayland ? (void*)cap_kms->params.card_path : (void*)cap_kms->params.dpy;
const gsr_connection_type connection_type = cap_kms->params.wayland ? GSR_CONNECTION_DRM : GSR_CONNECTION_X11; const gsr_connection_type connection_type = cap_kms->params.wayland ? GSR_CONNECTION_DRM : GSR_CONNECTION_X11;
MonitorCallbackUserdata monitor_callback_userdata = { MonitorCallbackUserdata monitor_callback_userdata = {
@ -229,13 +228,13 @@ static int gsr_capture_kms_vaapi_start(gsr_capture *cap, AVCodecContext *video_c
cap_kms->screen_size.x = 0; cap_kms->screen_size.x = 0;
cap_kms->screen_size.y = 0; cap_kms->screen_size.y = 0;
} else { } else {
const Atom randr_connector_id_atom = XInternAtom(cap_kms->dpy, "CONNECTOR_ID", False); const Atom randr_connector_id_atom = XInternAtom(cap_kms->params.dpy, "CONNECTOR_ID", False);
monitor_callback_userdata.randr_connector_id_atom = randr_connector_id_atom; monitor_callback_userdata.randr_connector_id_atom = randr_connector_id_atom;
monitor_callback_userdata.wayland = false; monitor_callback_userdata.wayland = false;
for_each_active_monitor_output(connection, connection_type, monitor_callback, &monitor_callback_userdata); for_each_active_monitor_output(connection, connection_type, monitor_callback, &monitor_callback_userdata);
cap_kms->screen_size.x = WidthOfScreen(DefaultScreenOfDisplay(cap_kms->dpy)); cap_kms->screen_size.x = WidthOfScreen(DefaultScreenOfDisplay(cap_kms->params.dpy));
cap_kms->screen_size.y = HeightOfScreen(DefaultScreenOfDisplay(cap_kms->dpy)); cap_kms->screen_size.y = HeightOfScreen(DefaultScreenOfDisplay(cap_kms->params.dpy));
} }
gsr_monitor monitor; gsr_monitor monitor;
@ -276,13 +275,13 @@ static int gsr_capture_kms_vaapi_start(gsr_capture *cap, AVCodecContext *video_c
return -1; return -1;
} }
if(cap_kms->dpy && !cap_kms->params.wayland) { if(cap_kms->params.dpy && !cap_kms->params.wayland) {
if(gsr_cursor_init(&cap_kms->cursor, cap_kms->params.egl, cap_kms->dpy) != 0) { if(gsr_cursor_init(&cap_kms->cursor, cap_kms->params.egl, cap_kms->params.dpy) != 0) {
gsr_capture_kms_vaapi_stop(cap, video_codec_context); gsr_capture_kms_vaapi_stop(cap, video_codec_context);
return -1; return -1;
} }
gsr_cursor_change_window_target(&cap_kms->cursor, DefaultRootWindow(cap_kms->dpy)); gsr_cursor_change_window_target(&cap_kms->cursor, DefaultRootWindow(cap_kms->params.dpy));
gsr_cursor_update(&cap_kms->cursor, &cap_kms->xev); gsr_cursor_update(&cap_kms->cursor, &cap_kms->xev);
} }
@ -301,9 +300,9 @@ static void gsr_capture_kms_vaapi_tick(gsr_capture *cap, AVCodecContext *video_c
// TODO: // TODO:
cap_kms->params.egl->glClear(GL_COLOR_BUFFER_BIT); cap_kms->params.egl->glClear(GL_COLOR_BUFFER_BIT);
if(cap_kms->dpy && !cap_kms->params.wayland) { if(cap_kms->params.dpy && !cap_kms->params.wayland) {
while(XPending(cap_kms->dpy)) { while(XPending(cap_kms->params.dpy)) {
XNextEvent(cap_kms->dpy, &cap_kms->xev); XNextEvent(cap_kms->params.dpy, &cap_kms->xev);
gsr_cursor_update(&cap_kms->cursor, &cap_kms->xev); gsr_cursor_update(&cap_kms->cursor, &cap_kms->xev);
} }
} }
@ -610,7 +609,7 @@ static int gsr_capture_kms_vaapi_capture(gsr_capture *cap, AVFrame *frame) {
} }
} }
if(cap_kms->dpy && !cap_kms->params.wayland) { if(cap_kms->params.dpy && !cap_kms->params.wayland) {
gsr_cursor_tick(&cap_kms->cursor); gsr_cursor_tick(&cap_kms->cursor);
} }
@ -627,7 +626,7 @@ static int gsr_capture_kms_vaapi_capture(gsr_capture *cap, AVFrame *frame) {
capture_pos, capture_size, capture_pos, capture_size,
texture_rotation); texture_rotation);
if(cap_kms->dpy && !cap_kms->params.wayland) { if(cap_kms->params.dpy && !cap_kms->params.wayland) {
gsr_color_conversion_draw(&cap_kms->color_conversion, cap_kms->cursor.texture_id, gsr_color_conversion_draw(&cap_kms->color_conversion, cap_kms->cursor.texture_id,
cursor_capture_pos, (vec2i){cap_kms->cursor.size.x, cap_kms->cursor.size.y}, cursor_capture_pos, (vec2i){cap_kms->cursor.size.x, cap_kms->cursor.size.y},
(vec2i){0, 0}, (vec2i){cap_kms->cursor.size.x, cap_kms->cursor.size.y}, (vec2i){0, 0}, (vec2i){cap_kms->cursor.size.x, cap_kms->cursor.size.y},
@ -686,11 +685,6 @@ static void gsr_capture_kms_vaapi_stop(gsr_capture *cap, AVCodecContext *video_c
av_buffer_unref(&video_codec_context->hw_frames_ctx); av_buffer_unref(&video_codec_context->hw_frames_ctx);
gsr_kms_client_deinit(&cap_kms->kms_client); gsr_kms_client_deinit(&cap_kms->kms_client);
if(cap_kms->dpy) {
// TODO: This causes a crash, why? maybe some other library dlclose xlib and that also happened to unload this???
//XCloseDisplay(cap_kms->dpy);
cap_kms->dpy = NULL;
}
} }
static void gsr_capture_kms_vaapi_destroy(gsr_capture *cap, AVCodecContext *video_codec_context) { static void gsr_capture_kms_vaapi_destroy(gsr_capture *cap, AVCodecContext *video_codec_context) {
@ -722,14 +716,6 @@ gsr_capture* gsr_capture_kms_vaapi_create(const gsr_capture_kms_vaapi_params *pa
return NULL; return NULL;
} }
Display *display = XOpenDisplay(NULL);
if(!params->wayland && !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); const char *display_to_capture = strdup(params->display_to_capture);
if(!display_to_capture) { if(!display_to_capture) {
/* TODO XCloseDisplay */ /* TODO XCloseDisplay */
@ -738,7 +724,6 @@ gsr_capture* gsr_capture_kms_vaapi_create(const gsr_capture_kms_vaapi_params *pa
return NULL; return NULL;
} }
cap_kms->dpy = display;
cap_kms->params = *params; cap_kms->params = *params;
cap_kms->params.display_to_capture = display_to_capture; cap_kms->params.display_to_capture = display_to_capture;

View File

@ -9,7 +9,6 @@
typedef struct { typedef struct {
gsr_capture_xcomposite_cuda_params params; gsr_capture_xcomposite_cuda_params params;
Display *dpy;
XEvent xev; XEvent xev;
bool should_stop; bool should_stop;
@ -158,12 +157,12 @@ static int gsr_capture_xcomposite_cuda_start(gsr_capture *cap, AVCodecContext *v
gsr_capture_xcomposite_cuda *cap_xcomp = cap->priv; gsr_capture_xcomposite_cuda *cap_xcomp = cap->priv;
if(cap_xcomp->params.follow_focused) { if(cap_xcomp->params.follow_focused) {
cap_xcomp->net_active_window_atom = XInternAtom(cap_xcomp->dpy, "_NET_ACTIVE_WINDOW", False); cap_xcomp->net_active_window_atom = XInternAtom(cap_xcomp->params.dpy, "_NET_ACTIVE_WINDOW", False);
if(!cap_xcomp->net_active_window_atom) { if(!cap_xcomp->net_active_window_atom) {
fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_start failed: failed to get _NET_ACTIVE_WINDOW atom\n"); fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_start failed: failed to get _NET_ACTIVE_WINDOW atom\n");
return -1; return -1;
} }
cap_xcomp->window = get_focused_window(cap_xcomp->dpy, cap_xcomp->net_active_window_atom); cap_xcomp->window = get_focused_window(cap_xcomp->params.dpy, cap_xcomp->net_active_window_atom);
} else { } else {
cap_xcomp->window = cap_xcomp->params.window; cap_xcomp->window = cap_xcomp->params.window;
} }
@ -173,7 +172,7 @@ static int gsr_capture_xcomposite_cuda_start(gsr_capture *cap, AVCodecContext *v
XWindowAttributes attr; XWindowAttributes attr;
attr.width = 0; attr.width = 0;
attr.height = 0; attr.height = 0;
if(!XGetWindowAttributes(cap_xcomp->dpy, cap_xcomp->window, &attr) && !cap_xcomp->params.follow_focused) { if(!XGetWindowAttributes(cap_xcomp->params.dpy, cap_xcomp->window, &attr) && !cap_xcomp->params.follow_focused) {
fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_start failed: invalid window id: %lu\n", cap_xcomp->window); fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_start failed: invalid window id: %lu\n", cap_xcomp->window);
return -1; return -1;
} }
@ -182,13 +181,13 @@ static int gsr_capture_xcomposite_cuda_start(gsr_capture *cap, AVCodecContext *v
cap_xcomp->window_size.y = max_int(attr.height, 0); cap_xcomp->window_size.y = max_int(attr.height, 0);
if(cap_xcomp->params.follow_focused) if(cap_xcomp->params.follow_focused)
XSelectInput(cap_xcomp->dpy, DefaultRootWindow(cap_xcomp->dpy), PropertyChangeMask); XSelectInput(cap_xcomp->params.dpy, DefaultRootWindow(cap_xcomp->params.dpy), PropertyChangeMask);
XSelectInput(cap_xcomp->dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask); XSelectInput(cap_xcomp->params.dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask);
cap_xcomp->params.egl->eglSwapInterval(cap_xcomp->params.egl->egl_display, 0); cap_xcomp->params.egl->eglSwapInterval(cap_xcomp->params.egl->egl_display, 0);
if(window_texture_init(&cap_xcomp->window_texture, cap_xcomp->dpy, cap_xcomp->window, cap_xcomp->params.egl) != 0 && !cap_xcomp->params.follow_focused) { if(window_texture_init(&cap_xcomp->window_texture, cap_xcomp->params.dpy, cap_xcomp->window, cap_xcomp->params.egl) != 0 && !cap_xcomp->params.follow_focused) {
fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_start: failed get window texture for window %ld\n", cap_xcomp->window); fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_start: failed to get window texture for window %ld\n", cap_xcomp->window);
return -1; return -1;
} }
@ -200,15 +199,15 @@ static int gsr_capture_xcomposite_cuda_start(gsr_capture *cap, AVCodecContext *v
cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y);
cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0); cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0);
cap_xcomp->texture_size.x = max_int(2, even_number_ceil(cap_xcomp->texture_size.x)); cap_xcomp->texture_size.x = max_int(2, cap_xcomp->texture_size.x & ~1);
cap_xcomp->texture_size.y = max_int(2, even_number_ceil(cap_xcomp->texture_size.y)); cap_xcomp->texture_size.y = max_int(2, cap_xcomp->texture_size.y & ~1);
video_codec_context->width = cap_xcomp->texture_size.x; video_codec_context->width = cap_xcomp->texture_size.x;
video_codec_context->height = cap_xcomp->texture_size.y; video_codec_context->height = cap_xcomp->texture_size.y;
if(cap_xcomp->params.region_size.x > 0 && cap_xcomp->params.region_size.y > 0) { if(cap_xcomp->params.region_size.x > 0 && cap_xcomp->params.region_size.y > 0) {
video_codec_context->width = max_int(2, even_number_ceil(cap_xcomp->params.region_size.x)); video_codec_context->width = max_int(2, cap_xcomp->params.region_size.x & ~1);
video_codec_context->height = max_int(2, even_number_ceil(cap_xcomp->params.region_size.y)); video_codec_context->height = max_int(2, cap_xcomp->params.region_size.y & ~1);
} }
cap_xcomp->target_texture_id = gl_create_texture(cap_xcomp, video_codec_context->width, video_codec_context->height); cap_xcomp->target_texture_id = gl_create_texture(cap_xcomp, video_codec_context->width, video_codec_context->height);
@ -218,7 +217,7 @@ static int gsr_capture_xcomposite_cuda_start(gsr_capture *cap, AVCodecContext *v
return -1; return -1;
} }
if(!gsr_cuda_load(&cap_xcomp->cuda, cap_xcomp->dpy, cap_xcomp->params.overclock)) { if(!gsr_cuda_load(&cap_xcomp->cuda, cap_xcomp->params.dpy, cap_xcomp->params.overclock)) {
gsr_capture_xcomposite_cuda_stop(cap, video_codec_context); gsr_capture_xcomposite_cuda_stop(cap, video_codec_context);
return -1; return -1;
} }
@ -266,10 +265,10 @@ static void gsr_capture_xcomposite_cuda_stop(gsr_capture *cap, AVCodecContext *v
gsr_cuda_unload(&cap_xcomp->cuda); gsr_cuda_unload(&cap_xcomp->cuda);
if(cap_xcomp->dpy) { if(cap_xcomp->params.dpy) {
// TODO: This causes a crash, why? maybe some other library dlclose xlib and that also happened to unload this??? // TODO: This causes a crash, why? maybe some other library dlclose xlib and that also happened to unload this???
//XCloseDisplay(cap_xcomp->dpy); //XCloseDisplay(cap_xcomp->dpy);
cap_xcomp->dpy = NULL; cap_xcomp->params.dpy = NULL;
} }
} }
@ -279,8 +278,8 @@ static void gsr_capture_xcomposite_cuda_tick(gsr_capture *cap, AVCodecContext *v
cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT);
bool init_new_window = false; bool init_new_window = false;
while(XPending(cap_xcomp->dpy)) { while(XPending(cap_xcomp->params.dpy)) {
XNextEvent(cap_xcomp->dpy, &cap_xcomp->xev); XNextEvent(cap_xcomp->params.dpy, &cap_xcomp->xev);
switch(cap_xcomp->xev.type) { switch(cap_xcomp->xev.type) {
case DestroyNotify: { case DestroyNotify: {
@ -324,17 +323,17 @@ static void gsr_capture_xcomposite_cuda_tick(gsr_capture *cap, AVCodecContext *v
} }
if(init_new_window) { if(init_new_window) {
Window focused_window = get_focused_window(cap_xcomp->dpy, cap_xcomp->net_active_window_atom); Window focused_window = get_focused_window(cap_xcomp->params.dpy, cap_xcomp->net_active_window_atom);
if(focused_window != cap_xcomp->window || !cap_xcomp->follow_focused_initialized) { if(focused_window != cap_xcomp->window || !cap_xcomp->follow_focused_initialized) {
cap_xcomp->follow_focused_initialized = true; cap_xcomp->follow_focused_initialized = true;
XSelectInput(cap_xcomp->dpy, cap_xcomp->window, 0); XSelectInput(cap_xcomp->params.dpy, cap_xcomp->window, 0);
cap_xcomp->window = focused_window; cap_xcomp->window = focused_window;
XSelectInput(cap_xcomp->dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask); XSelectInput(cap_xcomp->params.dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask);
XWindowAttributes attr; XWindowAttributes attr;
attr.width = 0; attr.width = 0;
attr.height = 0; attr.height = 0;
if(!XGetWindowAttributes(cap_xcomp->dpy, cap_xcomp->window, &attr)) if(!XGetWindowAttributes(cap_xcomp->params.dpy, cap_xcomp->window, &attr))
fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_tick failed: invalid window id: %lu\n", cap_xcomp->window); fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_tick failed: invalid window id: %lu\n", cap_xcomp->window);
cap_xcomp->window_size.x = max_int(attr.width, 0); cap_xcomp->window_size.x = max_int(attr.width, 0);
@ -342,7 +341,7 @@ static void gsr_capture_xcomposite_cuda_tick(gsr_capture *cap, AVCodecContext *v
cap_xcomp->window_resized = true; cap_xcomp->window_resized = true;
window_texture_deinit(&cap_xcomp->window_texture); window_texture_deinit(&cap_xcomp->window_texture);
window_texture_init(&cap_xcomp->window_texture, cap_xcomp->dpy, cap_xcomp->window, cap_xcomp->params.egl); // TODO: Do not do the below window_texture_on_resize after this window_texture_init(&cap_xcomp->window_texture, cap_xcomp->params.dpy, cap_xcomp->window, cap_xcomp->params.egl); // TODO: Do not do the below window_texture_on_resize after this
cap_xcomp->texture_size.x = 0; cap_xcomp->texture_size.x = 0;
cap_xcomp->texture_size.y = 0; cap_xcomp->texture_size.y = 0;
@ -352,8 +351,8 @@ static void gsr_capture_xcomposite_cuda_tick(gsr_capture *cap, AVCodecContext *v
cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y);
cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0); cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0);
cap_xcomp->texture_size.x = min_int(video_codec_context->width, max_int(2, even_number_ceil(cap_xcomp->texture_size.x))); cap_xcomp->texture_size.x = min_int(video_codec_context->width, max_int(2, cap_xcomp->texture_size.x & ~1));
cap_xcomp->texture_size.y = min_int(video_codec_context->height, max_int(2, even_number_ceil(cap_xcomp->texture_size.y))); cap_xcomp->texture_size.y = min_int(video_codec_context->height, max_int(2, cap_xcomp->texture_size.y & ~1));
} }
} }
@ -375,8 +374,8 @@ static void gsr_capture_xcomposite_cuda_tick(gsr_capture *cap, AVCodecContext *v
cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y);
cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0); cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0);
cap_xcomp->texture_size.x = min_int(video_codec_context->width, max_int(2, even_number_ceil(cap_xcomp->texture_size.x))); cap_xcomp->texture_size.x = min_int(video_codec_context->width, max_int(2, cap_xcomp->texture_size.x & ~1));
cap_xcomp->texture_size.y = min_int(video_codec_context->height, max_int(2, even_number_ceil(cap_xcomp->texture_size.y))); cap_xcomp->texture_size.y = min_int(video_codec_context->height, max_int(2, cap_xcomp->texture_size.y & ~1));
if(!cap_xcomp->created_hw_frame) { if(!cap_xcomp->created_hw_frame) {
cap_xcomp->created_hw_frame = true; cap_xcomp->created_hw_frame = true;
@ -500,15 +499,6 @@ gsr_capture* gsr_capture_xcomposite_cuda_create(const gsr_capture_xcomposite_cud
return NULL; return NULL;
} }
Display *display = XOpenDisplay(NULL);
if(!display) {
fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_create failed: XOpenDisplay failed\n");
free(cap);
free(cap_xcomp);
return NULL;
}
cap_xcomp->dpy = display;
cap_xcomp->params = *params; cap_xcomp->params = *params;
*cap = (gsr_capture) { *cap = (gsr_capture) {

View File

@ -13,7 +13,6 @@
typedef struct { typedef struct {
gsr_capture_xcomposite_vaapi_params params; gsr_capture_xcomposite_vaapi_params params;
Display *dpy;
XEvent xev; XEvent xev;
bool should_stop; bool should_stop;
@ -123,12 +122,12 @@ static int gsr_capture_xcomposite_vaapi_start(gsr_capture *cap, AVCodecContext *
gsr_capture_xcomposite_vaapi *cap_xcomp = cap->priv; gsr_capture_xcomposite_vaapi *cap_xcomp = cap->priv;
if(cap_xcomp->params.follow_focused) { if(cap_xcomp->params.follow_focused) {
cap_xcomp->net_active_window_atom = XInternAtom(cap_xcomp->dpy, "_NET_ACTIVE_WINDOW", False); cap_xcomp->net_active_window_atom = XInternAtom(cap_xcomp->params.dpy, "_NET_ACTIVE_WINDOW", False);
if(!cap_xcomp->net_active_window_atom) { if(!cap_xcomp->net_active_window_atom) {
fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start failed: failed to get _NET_ACTIVE_WINDOW atom\n"); fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start failed: failed to get _NET_ACTIVE_WINDOW atom\n");
return -1; return -1;
} }
cap_xcomp->window = get_focused_window(cap_xcomp->dpy, cap_xcomp->net_active_window_atom); cap_xcomp->window = get_focused_window(cap_xcomp->params.dpy, cap_xcomp->net_active_window_atom);
} else { } else {
cap_xcomp->window = cap_xcomp->params.window; cap_xcomp->window = cap_xcomp->params.window;
} }
@ -136,7 +135,7 @@ static int gsr_capture_xcomposite_vaapi_start(gsr_capture *cap, AVCodecContext *
/* TODO: Do these in tick, and allow error if follow_focused */ /* TODO: Do these in tick, and allow error if follow_focused */
XWindowAttributes attr; XWindowAttributes attr;
if(!XGetWindowAttributes(cap_xcomp->dpy, cap_xcomp->params.window, &attr) && !cap_xcomp->params.follow_focused) { if(!XGetWindowAttributes(cap_xcomp->params.dpy, cap_xcomp->params.window, &attr) && !cap_xcomp->params.follow_focused) {
fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start failed: invalid window id: %lu\n", cap_xcomp->params.window); fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start failed: invalid window id: %lu\n", cap_xcomp->params.window);
return -1; return -1;
} }
@ -145,10 +144,10 @@ static int gsr_capture_xcomposite_vaapi_start(gsr_capture *cap, AVCodecContext *
cap_xcomp->window_size.y = max_int(attr.height, 0); cap_xcomp->window_size.y = max_int(attr.height, 0);
if(cap_xcomp->params.follow_focused) if(cap_xcomp->params.follow_focused)
XSelectInput(cap_xcomp->dpy, DefaultRootWindow(cap_xcomp->dpy), PropertyChangeMask); XSelectInput(cap_xcomp->params.dpy, DefaultRootWindow(cap_xcomp->params.dpy), PropertyChangeMask);
// TODO: Get select and add these on top of it and then restore at the end. Also do the same in other xcomposite // TODO: Get select and add these on top of it and then restore at the end. Also do the same in other xcomposite
XSelectInput(cap_xcomp->dpy, cap_xcomp->params.window, StructureNotifyMask | ExposureMask); XSelectInput(cap_xcomp->params.dpy, cap_xcomp->params.window, StructureNotifyMask | ExposureMask);
if(!cap_xcomp->params.egl->eglExportDMABUFImageQueryMESA) { if(!cap_xcomp->params.egl->eglExportDMABUFImageQueryMESA) {
fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start: could not find eglExportDMABUFImageQueryMESA\n"); fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start: could not find eglExportDMABUFImageQueryMESA\n");
@ -162,8 +161,8 @@ static int gsr_capture_xcomposite_vaapi_start(gsr_capture *cap, AVCodecContext *
/* Disable vsync */ /* Disable vsync */
cap_xcomp->params.egl->eglSwapInterval(cap_xcomp->params.egl->egl_display, 0); cap_xcomp->params.egl->eglSwapInterval(cap_xcomp->params.egl->egl_display, 0);
if(window_texture_init(&cap_xcomp->window_texture, cap_xcomp->dpy, cap_xcomp->params.window, cap_xcomp->params.egl) != 0 && !cap_xcomp->params.follow_focused) { if(window_texture_init(&cap_xcomp->window_texture, cap_xcomp->params.dpy, cap_xcomp->params.window, cap_xcomp->params.egl) != 0 && !cap_xcomp->params.follow_focused) {
fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start: failed get window texture for window %ld\n", cap_xcomp->params.window); fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start: failed to get window texture for window %ld\n", cap_xcomp->params.window);
return -1; return -1;
} }
@ -202,8 +201,8 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext *
//cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); //cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT);
bool init_new_window = false; bool init_new_window = false;
while(XPending(cap_xcomp->dpy)) { while(XPending(cap_xcomp->params.dpy)) {
XNextEvent(cap_xcomp->dpy, &cap_xcomp->xev); XNextEvent(cap_xcomp->params.dpy, &cap_xcomp->xev);
switch(cap_xcomp->xev.type) { switch(cap_xcomp->xev.type) {
case DestroyNotify: { case DestroyNotify: {
@ -247,17 +246,17 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext *
} }
if(init_new_window) { if(init_new_window) {
Window focused_window = get_focused_window(cap_xcomp->dpy, cap_xcomp->net_active_window_atom); Window focused_window = get_focused_window(cap_xcomp->params.dpy, cap_xcomp->net_active_window_atom);
if(focused_window != cap_xcomp->window || !cap_xcomp->follow_focused_initialized) { if(focused_window != cap_xcomp->window || !cap_xcomp->follow_focused_initialized) {
cap_xcomp->follow_focused_initialized = true; cap_xcomp->follow_focused_initialized = true;
XSelectInput(cap_xcomp->dpy, cap_xcomp->window, 0); XSelectInput(cap_xcomp->params.dpy, cap_xcomp->window, 0);
cap_xcomp->window = focused_window; cap_xcomp->window = focused_window;
XSelectInput(cap_xcomp->dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask); XSelectInput(cap_xcomp->params.dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask);
XWindowAttributes attr; XWindowAttributes attr;
attr.width = 0; attr.width = 0;
attr.height = 0; attr.height = 0;
if(!XGetWindowAttributes(cap_xcomp->dpy, cap_xcomp->window, &attr)) if(!XGetWindowAttributes(cap_xcomp->params.dpy, cap_xcomp->window, &attr))
fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_tick failed: invalid window id: %lu\n", cap_xcomp->window); fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_tick failed: invalid window id: %lu\n", cap_xcomp->window);
cap_xcomp->window_size.x = max_int(attr.width, 0); cap_xcomp->window_size.x = max_int(attr.width, 0);
@ -265,7 +264,7 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext *
cap_xcomp->window_resized = true; cap_xcomp->window_resized = true;
window_texture_deinit(&cap_xcomp->window_texture); window_texture_deinit(&cap_xcomp->window_texture);
window_texture_init(&cap_xcomp->window_texture, cap_xcomp->dpy, cap_xcomp->window, cap_xcomp->params.egl); // TODO: Do not do the below window_texture_on_resize after this window_texture_init(&cap_xcomp->window_texture, cap_xcomp->params.dpy, cap_xcomp->window, cap_xcomp->params.egl); // TODO: Do not do the below window_texture_on_resize after this
cap_xcomp->texture_size.x = 0; cap_xcomp->texture_size.x = 0;
cap_xcomp->texture_size.y = 0; cap_xcomp->texture_size.y = 0;
@ -590,12 +589,6 @@ static void gsr_capture_xcomposite_vaapi_stop(gsr_capture *cap, AVCodecContext *
av_buffer_unref(&video_codec_context->hw_device_ctx); av_buffer_unref(&video_codec_context->hw_device_ctx);
if(video_codec_context->hw_frames_ctx) if(video_codec_context->hw_frames_ctx)
av_buffer_unref(&video_codec_context->hw_frames_ctx); av_buffer_unref(&video_codec_context->hw_frames_ctx);
if(cap_xcomp->dpy) {
// TODO: This causes a crash, why? maybe some other library dlclose xlib and that also happened to unload this???
//XCloseDisplay(cap_xcomp->dpy);
cap_xcomp->dpy = NULL;
}
} }
static void gsr_capture_xcomposite_vaapi_destroy(gsr_capture *cap, AVCodecContext *video_codec_context) { static void gsr_capture_xcomposite_vaapi_destroy(gsr_capture *cap, AVCodecContext *video_codec_context) {
@ -624,15 +617,6 @@ gsr_capture* gsr_capture_xcomposite_vaapi_create(const gsr_capture_xcomposite_va
return NULL; return NULL;
} }
Display *display = XOpenDisplay(NULL);
if(!display) {
fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_create failed: XOpenDisplay failed\n");
free(cap);
free(cap_xcomp);
return NULL;
}
cap_xcomp->dpy = display;
cap_xcomp->params = *params; cap_xcomp->params = *params;
*cap = (gsr_capture) { *cap = (gsr_capture) {

View File

@ -1450,6 +1450,8 @@ int main(int argc, char **argv) {
capture_target = "screen"; capture_target = "screen";
} }
gsr_egl_unload(&egl);
gsr_capture_nvfbc_params nvfbc_params; gsr_capture_nvfbc_params nvfbc_params;
nvfbc_params.dpy = dpy; nvfbc_params.dpy = dpy;
nvfbc_params.display_to_capture = capture_target; nvfbc_params.display_to_capture = capture_target;
@ -1497,6 +1499,7 @@ int main(int argc, char **argv) {
case GSR_GPU_VENDOR_AMD: { case GSR_GPU_VENDOR_AMD: {
gsr_capture_xcomposite_vaapi_params xcomposite_params; gsr_capture_xcomposite_vaapi_params xcomposite_params;
xcomposite_params.egl = &egl; xcomposite_params.egl = &egl;
xcomposite_params.dpy = dpy;
xcomposite_params.window = src_window_id; xcomposite_params.window = src_window_id;
xcomposite_params.follow_focused = follow_focused; xcomposite_params.follow_focused = follow_focused;
xcomposite_params.region_size = region_size; xcomposite_params.region_size = region_size;
@ -1509,6 +1512,7 @@ int main(int argc, char **argv) {
case GSR_GPU_VENDOR_INTEL: { case GSR_GPU_VENDOR_INTEL: {
gsr_capture_xcomposite_vaapi_params xcomposite_params; gsr_capture_xcomposite_vaapi_params xcomposite_params;
xcomposite_params.egl = &egl; xcomposite_params.egl = &egl;
xcomposite_params.dpy = dpy;
xcomposite_params.window = src_window_id; xcomposite_params.window = src_window_id;
xcomposite_params.follow_focused = follow_focused; xcomposite_params.follow_focused = follow_focused;
xcomposite_params.region_size = region_size; xcomposite_params.region_size = region_size;
@ -1521,6 +1525,7 @@ int main(int argc, char **argv) {
case GSR_GPU_VENDOR_NVIDIA: { case GSR_GPU_VENDOR_NVIDIA: {
gsr_capture_xcomposite_cuda_params xcomposite_params; gsr_capture_xcomposite_cuda_params xcomposite_params;
xcomposite_params.egl = &egl; xcomposite_params.egl = &egl;
xcomposite_params.dpy = dpy;
xcomposite_params.window = src_window_id; xcomposite_params.window = src_window_id;
xcomposite_params.follow_focused = follow_focused; xcomposite_params.follow_focused = follow_focused;
xcomposite_params.region_size = region_size; xcomposite_params.region_size = region_size;