From: Dmitrii Okunev via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Dmitrii Okunev <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH] fftools: Fix MediaCodec on Android15+ (PR #20886)
Date: Mon, 10 Nov 2025 14:19:02 -0000
Message-ID: <176278434300.25.12121622785453222412@2cb04c0e5124> (raw)
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 <xaionaro@dx.center>
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 <xaionaro@dx.center>
---
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 <dlfcn.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#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 <stdio.h>
#include <signal.h>
+#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 <SDL.h>
#include <SDL_thread.h>
+#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
reply other threads:[~2025-11-10 14:19 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=176278434300.25.12121622785453222412@2cb04c0e5124 \
--to=ffmpeg-devel@ffmpeg.org \
--cc=code@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git