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 981CD4D14B for ; Mon, 10 Nov 2025 14:19:41 +0000 (UTC) Authentication-Results: ffbox; dkim=fail (body hash mismatch (got b'ep5FQSEUrpL14/+1rMXfE3tPYzq4tV3sT6JAsYIehU4=', expected b'aM1XeaY2EANKhkxkB5sERAG7kNe4ErWkgpv1f6BwKUM=')) header.d=ffmpeg.org header.i=@ffmpeg.org header.a=rsa-sha256 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ffmpeg.org; i=@ffmpeg.org; q=dns/txt; s=mail; t=1762784371; h=mime-version : to : date : message-id : 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=ep5FQSEUrpL14/+1rMXfE3tPYzq4tV3sT6JAsYIehU4=; b=a2NECuEvmUDPgZEGxz5nsE5h+9fBaACpsx2VLSHsyjTaxBEMfjmYdYEfghRD0pyoAOcM6 fQ1UUdB7yA1cHX0aiLR7egXbZpb/dTIl/HTInRZKP5b/bJCXBP16kbuWlkFFaRPgX8A7RYU 8WVt1da4kF7vVyrTpWUwXho2BrB3l0dJlhQfPYOjNLgVe7D2ZF5gr15bCO1L8jj2GowYC3m jTFxigL+9SQGpPXxZJlZ/OzDxsZfqdD3rwywjmSD4Y7YDuXrA5FxKNpnNOvIcVkfYyqUuos ZX8qTsE8HI3Qvyp1fi8fm62bsL/ttkZ6cg663rWyIbOswNwUO+oDe9GbqxKQ== Received: from [172.19.0.2] (unknown [172.19.0.2]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 4704F68FB6A; Mon, 10 Nov 2025 16:19:31 +0200 (EET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=ffmpeg.org; s=arc; t=1762784352; b=OHqHeRsI2cLVyUPENA4+1/O5GRh5BuXkVOwGv9TwaX1PkGr/dMpecDxOGUa6CTWKrknZg Q4fZ/tDH09s9pR1zEhK68yAp41YB6t1Kk2HK+9qNP/aZyM3ZbPLlEP1gzsix2raYpRGHo3F +pJ1qJ654jbYBEkhCI35+WI4Fnjl2WcaRhYqflqq3q84OW4AG3jRAhGM2YxD3VI/zOXcO24 94WQxiYLxO97bI/wWp8Abbrpe2/3C9OXq0P/QIg6X+bC5f02q+X1ROZcCGS9S4pycYRbLsE CZKM+FMNP1X5+SxyORg+FFVD44LY2weXvAFAVMDwmB05G3LeaGwQmR4oiUbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=ffmpeg.org; s=arc; t=1762784352; 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=Pzq6eOcRgNI7XaJRAX2nMx3SOHYQdLY9Y8GZZxEwkN8=; b=tlAuMTxbXbqshTRvNfNZDSeWBZGd3tjIIOL6lGgMRY/eP2BgRapiBGBqlBCQsxCOh0UQJ EbH0DMK1LLen0xbGUZWkL7Yu6JvwPtCg9/9H5eqcgvmPy2oT5p5bnBcCfDOCAsAmR2Z55Ah uwA/OyvKh+UWVnT74guoQPWfS+LuhbvxFaNcLCQ06rmt/B6oKe4gQSlsJpLRCsuwSM3EdKS S9rNeSIf0YRMGxGTxMvXS5n4aK73KKq9KPLkeFHuO+W2xFPMRYPutjRWFtc50nW7KkVCAqf q+nTp3f6v/ygz97IoKumu/lp4862bjX/ZcMKs0w5xYy89z7NqdcMH091GsHQ== ARC-Authentication-Results: i=1; ffmpeg.org; dkim=pass header.d=ffmpeg.org header.i=@ffmpeg.org; arc=none; dmarc=pass header.from=ffmpeg.org policy.dmarc=quarantine Authentication-Results: ffmpeg.org; dkim=pass header.d=ffmpeg.org header.i=@ffmpeg.org; arc=none (Message is not ARC signed); dmarc=pass (Used From Domain Record) header.from=ffmpeg.org policy.dmarc=quarantine DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ffmpeg.org; i=@ffmpeg.org; q=dns/txt; s=mail; t=1762784342; h=content-type : mime-version : content-transfer-encoding : from : to : reply-to : subject : date : from; bh=aM1XeaY2EANKhkxkB5sERAG7kNe4ErWkgpv1f6BwKUM=; b=yjtix8aIutj6VIAhaORttb+Vmh0lvX47uVwb5z6PL75FYwz+ukvvfy41v60IXRgWhM5Lx 7daSV165tBiTojnUmmfrwwoyqvBT8tH0sXgqGCMneXL87D8+Dh6QCg+jyfPhv03+2DGSDQ4 51psC4A/VBRn8CRGxGle1y7saH7tfykBQzYdXY3WzX4TjYDaUEg/6YvHqZkvThe+VGKtuJi IX0Ur+c5YunrNgRCmkEZEhI+Ky8/9DcLuMFFyG4ZbP65s9SoXt5Ua8mu21hO6oP3QKm2lwQ ASkPZiS4V74RFW5YeJEJPM3ITAppu8k+s6H0nDqI2ZJ5t/dQY6xYREzEgSww== Received: from 188d6d40ca7a (code.ffmpeg.org [188.245.149.3]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id CEBD068FB3F for ; Mon, 10 Nov 2025 16:19:02 +0200 (EET) MIME-Version: 1.0 To: ffmpeg-devel@ffmpeg.org Date: Mon, 10 Nov 2025 14:19:02 -0000 Message-ID: <176278434300.25.12121622785453222412@2cb04c0e5124> Message-ID-Hash: 5TMXWG3WP5RB7EQY3T2HDX3GGJ54PPRZ X-Message-ID-Hash: 5TMXWG3WP5RB7EQY3T2HDX3GGJ54PPRZ X-MailFrom: code@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 X-Mailman-Version: 3.3.10 Precedence: list Reply-To: FFmpeg development discussions and patches Subject: [FFmpeg-devel] [PATCH] fftools: Fix MediaCodec on Android15+ (PR #20886) 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: Dmitrii Okunev via ffmpeg-devel Cc: Dmitrii Okunev Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Archived-At: List-Archive: List-Post: PR #20886 opened by Dmitrii Okunev (xaionaro) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20886 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20886.patch On Android15+ MediaCodec HAL backend was switched from HIDL to AIDL. As a result, MediaCodec operations started to hang, see: https://trac.ffmpeg.org/ticket/11363 https://github.com/termux/termux-packages/issues/21264 https://issuetracker.google.com/issues/382831999 To fix that it is necessary to initialize binder thread pool. >>From 26975f983969fd639283a0b09c8a8d4e5428de5f Mon Sep 17 00:00:00 2001 From: Dmitrii Okunev Date: Sun, 9 Nov 2025 17:40:55 +0000 Subject: [PATCH] fftools: Fix MediaCodec on Android15+ On Android15+ MediaCodec HAL backend was switched from HIDL to AIDL. As a result, MediaCodec operations started to hang, see: https://trac.ffmpeg.org/ticket/11363 https://github.com/termux/termux-packages/issues/21264 https://issuetracker.google.com/issues/382831999 To fix that it is necessary to initialize binder thread pool. Signed-off-by: Dmitrii Okunev --- fftools/Makefile | 3 + fftools/android_binder.c | 130 +++++++++++++++++++++++++++++++++++++++ fftools/android_binder.h | 31 ++++++++++ fftools/ffmpeg.c | 4 ++ fftools/ffmpeg.h | 1 + fftools/ffplay.c | 5 ++ 6 files changed, 174 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..af942ceb6c --- /dev/null +++ b/fftools/android_binder.c @@ -0,0 +1,130 @@ +/* + * 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) +{ + /* + * libbinder_ndk.so often does not contain the functions we need, so making + * this dependency optional, thus using dlopen/dlsym instead of linking. + * + * 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.49.1 _______________________________________________ ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org