From patchwork Thu Feb 9 14:25:19 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: 40338 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:5494:b0:bf:7b3a:fd32 with SMTP id i20csp505000pzk; Thu, 9 Feb 2023 06:25:46 -0800 (PST) X-Google-Smtp-Source: AK7set8CgmixNOZnHO1x0o4RtQsAA2tWMFuLOwQjnq5ODIJWNepQcwB9e3bo4upmeQdr0K6HOmQj X-Received: by 2002:a17:906:3857:b0:8aa:a7b6:1c4c with SMTP id w23-20020a170906385700b008aaa7b61c4cmr9422544ejc.21.1675952746460; Thu, 09 Feb 2023 06:25:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675952746; cv=none; d=google.com; s=arc-20160816; b=QfjInbwJZFyyZjYen7P6hlaXopBiH/pJg2ns4zwgqfL76PTB1X0cgFuUHUXFyFCCnV AGzXgxg1nb9q/AJH2BibYCaYraMxANKnbnavTGn1t7NmlEYBaaydyJU/NLzXhf70fKoo ZAUe9F9W1N4u5oywY46jeot6Za0xLITST+HCguN9yldRCRoDetsgSICzRkfqUPbafi/x AX+oq6hViIeLfvp+UQjv+l5RfPTwix9VHUL2neYND4/KbtCRPe2JEjrU3e6ITfV9aayb 87PM/ELf4pEzOHWS0y4FEHJ0Tl2AQwx58YoIE0umu44ive7AkyWJPYR84D5W6rWN07lg WvKg== 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:message-id:date:to:from :dkim-signature:dkim-filter:delivered-to; bh=UfjcR+ZmMH6hT0FoM7YchR9WCU1mfRTJbvSBySI5dM8=; b=jVJL2Uo2S/xomFJpHvQn9w2snf5OQSS9Yx982BP6qkzU890jPBYwU3+K4y3zbFlxy/ UZ6GoXBUBlJnJsg9w06WAZiYNZ67j+OTu6SWHz58cMBPYmNkwddVkf/QxtmKUgJSKh2k H29kmj+3A+ejWBzc06qsIVvpAENTpT9aVzPALU1Zv/q1l4JJDzayFwtMiO2AlmcMorvT RPjRA1c9QyD+5RIMe08D5dFMON05CghX2NrgVv1WopbN5b4D+vrdRnYyqapKpDPWw01l bVbkl+xWRXzAtx+xynRIorjzofI9qu1jG6c+utUlCOnOJLqam+da9WyaadI8WVtgsnQp qoNw== 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=jigxw+Cu; 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 uo37-20020a170907cc2500b008aa6e271052si2649463ejc.540.2023.02.09.06.25.40; Thu, 09 Feb 2023 06:25:46 -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=jigxw+Cu; 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 0E96768BBDE; Thu, 9 Feb 2023 16:25:38 +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 C88BA68BBF7 for ; Thu, 9 Feb 2023 16:25:30 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by mail.savoirfairelinux.com (Postfix) with ESMTP id AA5199C1CC7 for ; Thu, 9 Feb 2023 09:25:29 -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 REbnw61IBS0u; Thu, 9 Feb 2023 09:25:27 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mail.savoirfairelinux.com (Postfix) with ESMTP id CA6169C1C2D; Thu, 9 Feb 2023 09:25:27 -0500 (EST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.savoirfairelinux.com CA6169C1C2D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=savoirfairelinux.com; s=DFC430D2-D198-11EC-948E-34200CB392D2; t=1675952727; bh=E0W1Y8bE41gJ3JdkWtrtxOQ9IMRRErhknr1c+3sI5Ew=; h=From:To:Date:Message-Id:MIME-Version; b=jigxw+CuVnwrgzwhegUzaPdxJlfMdjBRDab2+XlMU3ose+QNK73hh8bZ0cayLazc3 uyeezy+7bEKwTy0TxHuLEpdlzxsBGTdurIfOu9D2g8LR9IZWd1vqzm2GdYitdN0f4r KiQnjgK0Nkn9DOnQ55kjTSbxQit53QoWm8La/903ASOYhA5+Qok7b2yWxvpqH3OwQZ /d9fbM1ZJ6Im1XzdEjvy4ElcDLqIlxok7oYFE5/xckasCYNnZgR2aQHQWW75MYccLJ phlngVdh4wRy6MSj15l6Re0OjMkDoGepKJgx1KV8/K3cudgiK7YirkiZTM8SMJNnro ZO86dljW+KFHQ== 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 WPCLtsq-Lnds; Thu, 9 Feb 2023 09:25:27 -0500 (EST) Received: from localhost.localdomain (unknown [181.221.107.93]) by mail.savoirfairelinux.com (Postfix) with ESMTPSA id 232539C1B62; Thu, 9 Feb 2023 09:25:26 -0500 (EST) From: aline.gondimsantos@savoirfairelinux.com To: ffmpeg-devel@ffmpeg.org Date: Thu, 9 Feb 2023 11:25:19 -0300 Message-Id: <20230209142519.38833-1-aline.gondimsantos@savoirfairelinux.com> X-Mailer: git-send-email 2.34.1 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: ta29Fd+NBMt0 From: 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))) {