From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 921F84D13E for ; Mon, 10 Nov 2025 14:12:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ffmpeg.org; i=@ffmpeg.org; q=dns/txt; s=mail; t=1762783923; h=to : date : message-id : mime-version : reply-to : subject : list-id : list-archive : list-archive : list-help : list-owner : list-post : list-subscribe : list-unsubscribe : from : cc : content-type : content-transfer-encoding : from; bh=YdEhvZt8a9sE/BJ6NZJ0SdjuovDW75GzP3KEhysHGJg=; b=txAAfvpIPqOAMQqeSvxDOtbdvqjcpFxY1I+iKkb28pKu+9eWVe5Ia3w2Wn9+E0iikerpT 5tRK4uG/aNzcZ/rw5GRgkSweo+21Ogx57EKiqWicUYfiG15ffi0cbcwf9cfQsHx8ptrwXlX X/lgXkKxyjRt3cI53DduFIJuSVHkE/6Rj+S4AR+DAIzhWd5KanrjJgG9MoVPMCUCCOB5zli stcRlfD8UbKRYSZ1DCL+2SGRkwkTtEsjDNcs5teF0zbauwoOhVUMRb8vukGuEOLBe48G5kK DIw8jIJn9K/aC+CA3iDmcc2kvBVLWiaPzQZFdbYXTjS3ilvNEGUKH35nKFxA== Received: from [172.19.0.2] (unknown [172.19.0.2]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id EF62768FC41; Mon, 10 Nov 2025 16:12:02 +0200 (EET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=ffmpeg.org; s=arc; t=1762783902; b=Enu8nGbvLj2V47Yls+BGHUDM69igo+GwAWWRyPIl3gdfk2UUjdJnRg7RHUc3jAADV1t87 LjVePrkM6H7EZTGsKLatUpvOGiXUpUQ4FIku/lwqbIqmmMhUUQPBKYAzFzRWKLIkJPyuwKM HPLgcatU321OdcmKHwN3xWzWH0QomDKiCxEnankJeikJfyJ/2ayqLSMYLGADNkun8xOBHjU X54Irk1cukzvKAvl5Hta4K1E3AkedNZnTHs7IKOq/kmIk1WwHyFPFc9x8JuBOPC7A+s95t/ V7Ck+sT1YL7XV+acD58THCTtpyrU9auqo4X1X+dhHoTyhb1nuEJ3R8sr17bA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=ffmpeg.org; s=arc; t=1762783902; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=ApZJ3KJCR9Orm9fgs+J99lZ3koNaOrluI3vvYGE1ahA=; b=WMdN3YVWBfAOuF93iyZi+i3sEMn5iewjHSJ5c2+H/wE+gw72sZAhVjsdRZvpTsWRW96Az 0WR4zJxvNmZf+UTjiC7iW4DvjehoObb5W5QYxtISYD50rqv06fdGcHUKtI0bc/cAJp3WTgn Kp95HS/ABLY3AbeBqU+JlfN6oAKXUEOQeIkSQjoR9a3TPlRwkSyrRbwaHTHL9dacOcP5EnY dsJfEHgznIL+T9+it8aWLegvU1EGhRuE/QkLsSCWzdqWiQ9l/+6Z5GtKJ2udwAXje2+ENOB t8ETyAnLrq1jzapIB5L/0vBCZija/fnAZae4tS5ygPwm9aF86O+oASO4tNnw== ARC-Authentication-Results: i=1; ffmpeg.org; dkim=fail; arc=none; dmarc=none Authentication-Results: ffmpeg.org; dkim=fail; arc=none (Message is not ARC signed); dmarc=none Received: from mail.dx.center (unknown [185.197.163.125]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id 407DA68FA7D for ; Sun, 9 Nov 2025 21:15:37 +0200 (EET) Received: from dx-mail (localhost [127.0.0.1]) by mail.dx.center (Postfix) with ESMTP id A1922DDC34B; Sun, 9 Nov 2025 19:15:36 +0000 (UTC) Received: from localhost.dx.center ([86.45.167.45]) by dx-mail with ESMTPSA id oHCDJFjoEGmjTgAALp83/g (envelope-from ); Sun, 09 Nov 2025 19:15:36 +0000 Received: by localhost.dx.center (Postfix, from userid 1004) id 610DA1420007; Sun, 09 Nov 2025 19:15:36 +0000 (GMT) To: ffmpeg-devel@ffmpeg.org Date: Sun, 9 Nov 2025 19:15:35 +0000 Message-ID: <20251109191535.60137-1-xaionaro@dx.center> X-Mailer: git-send-email 2.48.1 MIME-Version: 1.0 X-MailFrom: SRS0=Ouqc=5R=void.dx.center=streaming@ffmpeg.org X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-ffmpeg-devel.ffmpeg.org-0; header-match-ffmpeg-devel.ffmpeg.org-1; header-match-ffmpeg-devel.ffmpeg.org-2; header-match-ffmpeg-devel.ffmpeg.org-3; emergency; member-moderation Message-ID-Hash: S44YDOMZ45QOH7LLRPDP26L53M7P4WD4 X-Message-ID-Hash: S44YDOMZ45QOH7LLRPDP26L53M7P4WD4 X-Mailman-Approved-At: Mon, 10 Nov 2025 14:11:36 +0000 X-Mailman-Version: 3.3.10 Precedence: list Reply-To: FFmpeg development discussions and patches Subject: [FFmpeg-devel] [PATCH] fftools: Fix MediaCodec on Android15+ List-Id: FFmpeg development discussions and patches Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: xaionaro--- via ffmpeg-devel Cc: Dmitrii Okunev Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Archived-At: List-Archive: List-Post: From: Dmitrii Okunev On Android15+ MediaCodec HAL backend was switched from HIDL to AIDL. As a result, MediaCodec operations started to hang, see: https://github.com/termux/termux-packages/issues/21264 To fix that it is necessary to initialize binder thread pool. Signed-off-by: Dmitrii Okunev --- fftools/Makefile | 3 + fftools/android_binder.c | 131 +++++++++++++++++++++++++++++++++++++++ fftools/android_binder.h | 31 +++++++++ fftools/ffmpeg.c | 4 ++ fftools/ffmpeg.h | 1 + fftools/ffplay.c | 5 ++ 6 files changed, 175 insertions(+) create mode 100644 fftools/android_binder.c create mode 100644 fftools/android_binder.h diff --git a/fftools/Makefile b/fftools/Makefile index bdb44fc5ce..00d1c503f3 100644 --- a/fftools/Makefile +++ b/fftools/Makefile @@ -53,6 +53,9 @@ OBJS-ffprobe += \ OBJS-ffplay += fftools/ffplay_renderer.o +OBJS-ffmpeg-$(CONFIG_MEDIACODEC) += fftools/android_binder.o +OBJS-ffplay-$(CONFIG_MEDIACODEC) += fftools/android_binder.o + define DOFFTOOL OBJS-$(1) += fftools/cmdutils.o fftools/opt_common.o fftools/$(1).o $(OBJS-$(1)-yes) ifdef HAVE_GNU_WINDRES diff --git a/fftools/android_binder.c b/fftools/android_binder.c new file mode 100644 index 0000000000..e16c7f4b31 --- /dev/null +++ b/fftools/android_binder.c @@ -0,0 +1,131 @@ +/* + * Android Binder handler + * + * Copyright (c) 2025 Dmitrii Okunev + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +#if defined(__ANDROID__) + +#include +#include +#include +#include + +#include "config.h" +#include "libavutil/log.h" +#include "android_binder.h" + +#define DEFAULT_THREAD_POOL_SIZE 4 +#define MAX_THREAD_POOL_SIZE 256 + +static unsigned get_thread_pool_size(void) +{ + const char *tps_str = getenv("FFMPEG_ANDROID_BINDER_THREAD_POOL_SIZE"); + if (tps_str == NULL || !*tps_str) { + av_log(NULL, AV_LOG_DEBUG, + "android/binder: FFMPEG_ANDROID_BINDER_THREAD_POOL_SIZE not set, using default %u\n", + DEFAULT_THREAD_POOL_SIZE); + return DEFAULT_THREAD_POOL_SIZE; + } + + errno = 0; + unsigned long thread_pool_size = strtoul(tps_str, NULL, 10); + if (errno != 0 || thread_pool_size <= 0 + || thread_pool_size > UINT32_MAX) { + av_log(NULL, AV_LOG_ERROR, + "android/binder: invalid value of FFMPEG_ANDROID_BINDER_THREAD_POOL_SIZE: '%s' (errno: %d), using the default one, instead: %u\n", + tps_str, errno, DEFAULT_THREAD_POOL_SIZE); + return DEFAULT_THREAD_POOL_SIZE; + } + + if (thread_pool_size > MAX_THREAD_POOL_SIZE) { + av_log(NULL, AV_LOG_WARNING, + "android/binder: too large FFMPEG_ANDROID_BINDER_THREAD_POOL_SIZE: '%s', clamping to %d\n", + tps_str, MAX_THREAD_POOL_SIZE); + thread_pool_size = MAX_THREAD_POOL_SIZE; + } + + av_log(NULL, AV_LOG_DEBUG, + "android/binder: thread pool size: %lu\n", thread_pool_size); + return (unsigned) thread_pool_size; +} + +static void *dlopen_libbinder_ndk(void) +{ + /* + * To make ffmpeg builds reusable at different Android versions we intentionally + * avoid including linking with libbinder_ndk.so at the link time. Instead, we + * resolve the symbols at runtime using dlopen()/dlsym(). + * + * See also: https://source.android.com/docs/core/architecture/aidl/aidl-backends + */ + + void *h = dlopen("libbinder_ndk.so", RTLD_NOW | RTLD_LOCAL); + if (h != NULL) + return h; + + av_log(NULL, AV_LOG_VERBOSE, + "android/binder: libbinder_ndk.so not found; skipping binder threadpool init\n"); + return NULL; +} + +void android_binder_threadpool_init(void) +{ + typedef int (*set_thread_pool_max_fn)(uint32_t); + typedef void (*start_thread_pool_fn)(void); + + set_thread_pool_max_fn set_thread_pool_max = NULL; + start_thread_pool_fn start_thread_pool = NULL; + + void *h = dlopen_libbinder_ndk(); + if (h == NULL) { + return; + } + + unsigned thead_pool_size = get_thread_pool_size(); + + set_thread_pool_max = + (set_thread_pool_max_fn) dlsym(h, + "ABinderProcess_setThreadPoolMaxThreadCount"); + start_thread_pool = + (start_thread_pool_fn) dlsym(h, "ABinderProcess_startThreadPool"); + + if (start_thread_pool == NULL) { + av_log(NULL, AV_LOG_VERBOSE, + "android/binder: ABinderProcess_startThreadPool not found; skipping threadpool init\n"); + return; + } + + if (set_thread_pool_max != NULL) { + int ok = set_thread_pool_max(thead_pool_size); + av_log(NULL, AV_LOG_DEBUG, + "android/binder: ABinderProcess_setThreadPoolMaxThreadCount(%u) => %s\n", + thead_pool_size, ok ? "ok" : "fail"); + } else { + av_log(NULL, AV_LOG_DEBUG, + "android/binder: ABinderProcess_setThreadPoolMaxThreadCount is unavailable; using the library default\n"); + } + + start_thread_pool(); + av_log(NULL, AV_LOG_DEBUG, + "android/binder: ABinderProcess_startThreadPool() called\n"); +} + +#endif /* __ANDROID__ */ diff --git a/fftools/android_binder.h b/fftools/android_binder.h new file mode 100644 index 0000000000..56c487fe4c --- /dev/null +++ b/fftools/android_binder.h @@ -0,0 +1,31 @@ +/* + * Android Binder handler + * + * Copyright (c) 2025 Dmitrii Okunev + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef FFTOOLS_ANDROID_BINDER_H +#define FFTOOLS_ANDROID_BINDER_H + +/** + * Initialize Android Binder thread pool. + */ +void android_binder_threadpool_init(void); + +#endif // FFTOOLS_ANDROID_BINDER_H diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 444d027c15..7b250ac5da 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1019,6 +1019,10 @@ int main(int argc, char **argv) goto finish; } +#if defined(__ANDROID__) + android_binder_threadpool_init(); +#endif + current_time = ti = get_benchmark_time_stamps(); ret = transcode(sch); if (ret >= 0 && do_benchmark) { diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index cc2ea1a56e..6c6ef0509a 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -26,6 +26,7 @@ #include #include +#include "android_binder.h" #include "cmdutils.h" #include "ffmpeg_sched.h" #include "sync_queue.h" diff --git a/fftools/ffplay.c b/fftools/ffplay.c index dc2627521e..14fde65f27 100644 --- a/fftools/ffplay.c +++ b/fftools/ffplay.c @@ -54,6 +54,7 @@ #include #include +#include "android_binder.h" #include "cmdutils.h" #include "ffplay_renderer.h" #include "opt_common.h" @@ -3892,6 +3893,10 @@ int main(int argc, char **argv) } } +#if defined(__ANDROID__) + android_binder_threadpool_init(); +#endif + is = stream_open(input_filename, file_iformat); if (!is) { av_log(NULL, AV_LOG_FATAL, "Failed to initialize VideoState!\n"); -- 2.48.1 _______________________________________________ ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org