From patchwork Thu Feb 9 18:19:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aline Gondim Santos Gondim Santos X-Patchwork-Id: 40343 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:5494:b0:bf:7b3a:fd32 with SMTP id i20csp706813pzk; Thu, 9 Feb 2023 10:20:58 -0800 (PST) X-Google-Smtp-Source: AK7set80VAI51RfPWGl05XzQgyZWN/XfgdwClhLcNVvhbjQLo+UwPSaoN2/XyP3AOb3VXw1zuX40 X-Received: by 2002:a17:906:dff7:b0:877:ef84:c7de with SMTP id lc23-20020a170906dff700b00877ef84c7demr12308811ejc.61.1675966858047; Thu, 09 Feb 2023 10:20:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675966858; cv=none; d=google.com; s=arc-20160816; b=uLnTV+NhLOc8r2GZe4HWHiA82RXZW2ivgXzkMh5tHB74EpSOJ5ucLFukeXR4CE18KC 7Yo6uqFxwhfL+4fl2i3jx0lG3+/7VyQBGNYPpqVNWe1ITvD0yMETFkwEprNGQnXv9xAn edOrITq899QTQnczCMhLboqnLZjrMNjcCmR0SDbwzWFNDETGLbpCSBmG0jX7IedHI+// dPEUcdXGKCJzHLU0oLQwrokC/fl+FP3LR8wAt1pPt8cK+kY2v87UnfQGTPgJByNjS/Ei SO1iDKZ1jJ8uV6EEUZftxNJYp9E1O50ziubspb/BTHzofZimuBYEAVoTGeF3P3LKzBkq 4UTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:dkim-filter:delivered-to; bh=XeZhqdVkbv07va0RjgpF5ayJJG1wKHICwR4PzWS5FWc=; b=ChTBIQkWCMrmd5BQrcTdXrMSN6ym/+NIci4/AXhFGNGHKwKTAQGSn8tvsQv3qpJu+V ZnJ2qVEZ2/Jj3hNkSVRTB3EJAPzBtUyMgZicyHrrQ4jdD5b+4L0iIgHg1cKU/HZhDgPS rYK472RWJC/SMBveSCUgF2/iS2k6oOyHiQUOoUUoPG+1Y3+5n4ZSSPQLTHxYy7GKFBvc nkKRPI9pPk5VBzFajJGTxqh8qEmc80KBzbSHNaWdjIpqsTe6UgcUvHVZOirci/G7P7BF x1NJawaG7e1QPGLfnBLAFrLb9Azpb0UyuY9zaB/07MJ86c12DdiLI0RZa92O4JJscsQn YPMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@savoirfairelinux.com header.s=DFC430D2-D198-11EC-948E-34200CB392D2 header.b=qiFVMpot; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id r22-20020aa7da16000000b004a21b1071fbsi3040094eds.213.2023.02.09.10.20.55; Thu, 09 Feb 2023 10:20:58 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@savoirfairelinux.com header.s=DFC430D2-D198-11EC-948E-34200CB392D2 header.b=qiFVMpot; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A363868BE22; Thu, 9 Feb 2023 20:20:53 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.savoirfairelinux.com (mail.savoirfairelinux.com [208.88.110.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 09B3568BE16 for ; Thu, 9 Feb 2023 20:20:51 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by mail.savoirfairelinux.com (Postfix) with ESMTP id 120069C1C93 for ; Thu, 9 Feb 2023 13:20:51 -0500 (EST) Received: from mail.savoirfairelinux.com ([127.0.0.1]) by localhost (mail.savoirfairelinux.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id wYuZ2ub9WXJ2; Thu, 9 Feb 2023 13:20:49 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mail.savoirfairelinux.com (Postfix) with ESMTP id 66A4A9C1CC7; Thu, 9 Feb 2023 13:20:49 -0500 (EST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.savoirfairelinux.com 66A4A9C1CC7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=savoirfairelinux.com; s=DFC430D2-D198-11EC-948E-34200CB392D2; t=1675966849; bh=b3Tl7NqHOVXAAHzgdr9Q1IIHXjcpN9Bx+nqP2oNCeF0=; h=From:To:Date:Message-Id:MIME-Version; b=qiFVMpotPPfWZIqwBpdZzfO0NyrLEzkpzhQQXbuEG5VslovwDoMxoGExgTyg53meh 39pE/Khgl6LAggW23Lgr0035xeEL6y0KnZSYAjFfbl5hJc5c6IOjLWQwW64PfeA9tT MApCRde7oxjB+oEpgambTfQ5BtYRoQgCj9XETjn8mGBSoxC5g+2cyc5sEK026vNEFS vLP83xfT/sqFWhE93YbjAiEL+NLHB/y3843sBXt0sxqdEvOxZoyKbwp1y3hEowAm9H 0c3tsnlB94yBNOQh7SK3BxZc5H+5tVotSewzq1TuDzP8sQqLJidYUfFLBY2LGAmeJe APFw+WFxDqG6w== X-Virus-Scanned: amavisd-new at mail.savoirfairelinux.com Received: from mail.savoirfairelinux.com ([127.0.0.1]) by localhost (mail.savoirfairelinux.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id Ine0tCbxVAh8; Thu, 9 Feb 2023 13:20:49 -0500 (EST) Received: from localhost.localdomain (unknown [181.221.107.93]) by mail.savoirfairelinux.com (Postfix) with ESMTPSA id B52079C1C93; Thu, 9 Feb 2023 13:20:48 -0500 (EST) From: Aline Gondim Santos To: ffmpeg-devel@ffmpeg.org Date: Thu, 9 Feb 2023 15:19:53 -0300 Message-Id: <20230209181953.437020-2-aline.gondimsantos@savoirfairelinux.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209181953.437020-1-aline.gondimsantos@savoirfairelinux.com> References: <20230209142519.38833-1-aline.gondimsantos@savoirfairelinux.com> <20230209181953.437020-1-aline.gondimsantos@savoirfairelinux.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avdevice/xcbgrab: enable window resizing X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Aline Gondim Santos Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: d/VAOnmb/J+Y Signed-off-by: Aline Gondim Santos Signed-off-by: Aline Gondim Santos --- libavdevice/xcbgrab.c | 180 +++++++++--------------------------------- 1 file changed, 39 insertions(+), 141 deletions(-) diff --git a/libavdevice/xcbgrab.c b/libavdevice/xcbgrab.c index 64a68ba497..05282911a9 100644 --- a/libavdevice/xcbgrab.c +++ b/libavdevice/xcbgrab.c @@ -29,11 +29,6 @@ #include #endif -#if CONFIG_LIBXCB_SHM -#include -#include -#endif - #if CONFIG_LIBXCB_SHAPE #include #endif @@ -53,9 +48,6 @@ typedef struct XCBGrabContext { xcb_connection_t *conn; xcb_screen_t *screen; xcb_window_t window; -#if CONFIG_LIBXCB_SHM - AVBufferPool *shm_pool; -#endif int64_t time_frame; AVRational time_base; int64_t frame_duration; @@ -72,10 +64,9 @@ typedef struct XCBGrabContext { int region_border; int centered; int select_region; + int is_area; const char *framerate; - - int has_shm; } XCBGrabContext; #define FOLLOW_CENTER -1 @@ -97,6 +88,7 @@ static const AVOption options[] = { { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D }, { "region_border", "Set the region border thickness.", OFFSET(region_border), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128, D }, { "select_region", "Select the grabbing region graphically using the pointer.", OFFSET(select_region), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D }, + { "is_area", "Define if we are grabing a region of the display/window.", OFFSET(is_area), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D }, { NULL }, }; @@ -216,99 +208,6 @@ static int64_t wait_frame(AVFormatContext *s, AVPacket *pkt) return curtime; } -#if CONFIG_LIBXCB_SHM -static int check_shm(xcb_connection_t *conn) -{ - xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn); - xcb_shm_query_version_reply_t *reply; - - reply = xcb_shm_query_version_reply(conn, cookie, NULL); - if (reply) { - free(reply); - return 1; - } - - return 0; -} - -static void free_shm_buffer(void *opaque, uint8_t *data) -{ - shmdt(data); -} - -static AVBufferRef *allocate_shm_buffer(void *opaque, size_t size) -{ - xcb_connection_t *conn = opaque; - xcb_shm_seg_t segment; - AVBufferRef *ref; - uint8_t *data; - int id; - - id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777); - if (id == -1) - return NULL; - - segment = xcb_generate_id(conn); - xcb_shm_attach(conn, segment, id, 0); - data = shmat(id, NULL, 0); - shmctl(id, IPC_RMID, 0); - if ((intptr_t)data == -1 || !data) - return NULL; - - ref = av_buffer_create(data, size, free_shm_buffer, (void *)(ptrdiff_t)segment, 0); - if (!ref) - shmdt(data); - - return ref; -} - -static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt) -{ - XCBGrabContext *c = s->priv_data; - xcb_shm_get_image_cookie_t iq; - xcb_shm_get_image_reply_t *img; - xcb_drawable_t drawable = c->window_id; - xcb_generic_error_t *e = NULL; - AVBufferRef *buf; - xcb_shm_seg_t segment; - - buf = av_buffer_pool_get(c->shm_pool); - if (!buf) { - av_log(s, AV_LOG_ERROR, "Could not get shared memory buffer.\n"); - return AVERROR(ENOMEM); - } - segment = (xcb_shm_seg_t)(uintptr_t)av_buffer_pool_buffer_get_opaque(buf); - - iq = xcb_shm_get_image(c->conn, drawable, - c->x, c->y, c->width, c->height, ~0, - XCB_IMAGE_FORMAT_Z_PIXMAP, segment, 0); - img = xcb_shm_get_image_reply(c->conn, iq, &e); - - xcb_flush(c->conn); - - if (e) { - av_log(s, AV_LOG_ERROR, - "Cannot get the image data " - "event_error: response_type:%u error_code:%u " - "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n", - e->response_type, e->error_code, - e->sequence, e->resource_id, e->minor_code, e->major_code); - - free(e); - av_buffer_unref(&buf); - return AVERROR(EACCES); - } - - free(img); - - pkt->buf = buf; - pkt->data = buf->data; - pkt->size = c->frame_size; - - return 0; -} -#endif /* CONFIG_LIBXCB_SHM */ - #if CONFIG_LIBXCB_XFIXES static int check_xfixes(xcb_connection_t *conn) { @@ -462,14 +361,7 @@ static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt) if (c->show_region) xcbgrab_update_region(s, win_x, win_y); -#if CONFIG_LIBXCB_SHM - if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0) { - av_log(s, AV_LOG_WARNING, "Continuing without shared memory.\n"); - c->has_shm = 0; - } -#endif - if (!c->has_shm) - ret = xcbgrab_frame(s, pkt); + ret = xcbgrab_frame(s, pkt); pkt->dts = pkt->pts = pts; pkt->duration = c->frame_duration; @@ -488,11 +380,8 @@ static av_cold int xcbgrab_read_close(AVFormatContext *s) { XCBGrabContext *ctx = s->priv_data; -#if CONFIG_LIBXCB_SHM - av_buffer_pool_uninit(&ctx->shm_pool); -#endif - xcb_disconnect(ctx->conn); + ctx->conn = NULL; return 0; } @@ -572,7 +461,15 @@ static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth, static int create_stream(AVFormatContext *s) { XCBGrabContext *c = s->priv_data; - AVStream *st = avformat_new_stream(s, NULL); + + // If we try to open another stream to x11grab, there is no reason + // to keep more than one stream in the context. + AVStream *st; + if (!s->nb_streams) { + st = avformat_new_stream(s, NULL); + } else { + st = s->streams[0]; + } xcb_get_geometry_cookie_t gc; xcb_get_geometry_reply_t *geo; int64_t frame_size_bits; @@ -594,7 +491,16 @@ static int create_stream(AVFormatContext *s) return AVERROR_EXTERNAL; } + // Width and Height are not 0 only when we set a window area to share + // This if may be valid only in the first call to create_stream if (!c->width || !c->height) { + c->is_area = 0; + c->width = geo->width; + c->height = geo->height; + } + // If not a predefined area, then we should follow geometry changes + // This can be valid only on the second call onwards + if (!c->is_area && (c->width != geo->width || c->height != geo->height)) { c->width = geo->width; c->height = geo->height; } @@ -628,13 +534,6 @@ static int create_stream(AVFormatContext *s) } c->frame_size = frame_size_bits / 8; -#if CONFIG_LIBXCB_SHM - c->shm_pool = av_buffer_pool_init2(c->frame_size + AV_INPUT_BUFFER_PADDING_SIZE, - c->conn, allocate_shm_buffer, NULL); - if (!c->shm_pool) - return AVERROR(ENOMEM); -#endif - st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO; st->codecpar->width = c->width; @@ -829,23 +728,26 @@ static av_cold int xcbgrab_read_header(AVFormatContext *s) sscanf(s->url, "+%d,%d", &c->x, &c->y); } - c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num); - av_freep(&display_name); + if (!c->conn || !c->screen) { + xcbgrab_read_close(s); + c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num); + av_freep(&display_name); - if ((ret = xcb_connection_has_error(c->conn))) { - av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n", - s->url[0] ? s->url : "default", ret); - return AVERROR(EIO); - } + if ((ret = xcb_connection_has_error(c->conn))) { + av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n", + s->url[0] ? s->url : "default", ret); + return AVERROR(EIO); + } - setup = xcb_get_setup(c->conn); + setup = xcb_get_setup(c->conn); - c->screen = get_screen(setup, screen_num); - if (!c->screen) { - av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n", - screen_num); - xcbgrab_read_close(s); - return AVERROR(EIO); + c->screen = get_screen(setup, screen_num); + if (!c->screen) { + av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n", + screen_num); + xcbgrab_read_close(s); + return AVERROR(EIO); + } } if (c->window_id == XCB_NONE) @@ -876,10 +778,6 @@ static av_cold int xcbgrab_read_header(AVFormatContext *s) return ret; } -#if CONFIG_LIBXCB_SHM - c->has_shm = check_shm(c->conn); -#endif - #if CONFIG_LIBXCB_XFIXES if (c->draw_mouse) { if (!(c->draw_mouse = check_xfixes(c->conn))) {