FFmpeg
Loading...
Searching...
No Matches
binder.c
Go to the documentation of this file.
1/*
2 * Android Binder handler
3 *
4 * Copyright (c) 2025 Dmitrii Okunev
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23
24#if defined(__ANDROID__)
25
26#include <dlfcn.h>
27#include <stdint.h>
28#include <stdlib.h>
29
30#include "libavutil/log.h"
31#include "binder.h"
32
33#define THREAD_POOL_SIZE 1
34
35static void *dlopen_libbinder_ndk(void)
36{
37 /*
38 * libbinder_ndk.so often does not contain the functions we need, so making
39 * this dependency optional, thus using dlopen/dlsym instead of linking.
40 *
41 * See also: https://source.android.com/docs/core/architecture/aidl/aidl-backends
42 */
43
44 void *h = dlopen("libbinder_ndk.so", RTLD_NOW | RTLD_LOCAL);
45 if (h != NULL)
46 return h;
47
49 "android/binder: unable to load libbinder_ndk.so: '%s'; skipping binder threadpool init (MediaCodec likely won't work)\n",
50 dlerror());
51 return NULL;
52}
53
54static void android_binder_threadpool_init(void)
55{
56 typedef int (*set_thread_pool_max_fn)(uint32_t);
57 typedef void (*start_thread_pool_fn)(void);
58
59 set_thread_pool_max_fn set_thread_pool_max = NULL;
60 start_thread_pool_fn start_thread_pool = NULL;
61
62 void *h = dlopen_libbinder_ndk();
63 if (h == NULL)
64 return;
65
66 unsigned thead_pool_size = THREAD_POOL_SIZE;
67
68 set_thread_pool_max =
69 (set_thread_pool_max_fn) dlsym(h,
70 "ABinderProcess_setThreadPoolMaxThreadCount");
71 start_thread_pool =
72 (start_thread_pool_fn) dlsym(h, "ABinderProcess_startThreadPool");
73
74 if (start_thread_pool == NULL) {
76 "android/binder: ABinderProcess_startThreadPool not found; skipping threadpool init (MediaCodec likely won't work)\n");
77 return;
78 }
79
80 if (set_thread_pool_max != NULL) {
81 int ok = set_thread_pool_max(thead_pool_size);
83 "android/binder: ABinderProcess_setThreadPoolMaxThreadCount(%u) => %s\n",
84 thead_pool_size, ok ? "ok" : "fail");
85 } else {
87 "android/binder: ABinderProcess_setThreadPoolMaxThreadCount is unavailable; using the library default\n");
88 }
89
90 start_thread_pool();
92 "android/binder: ABinderProcess_startThreadPool() called\n");
93}
94
96{
97#if __ANDROID_API__ >= 24
98 if (android_get_device_api_level() < 35) {
99 // the issue with the thread pool was introduced in Android 15 (API 35)
101 "android/binder: API<35, thus no need to initialize a thread pool\n");
102 return;
103 }
104 android_binder_threadpool_init();
105#else
106 // android_get_device_api_level was introduced in API 24, so we cannot use it
107 // to detect the API level in API<24. For simplicity we just assume
108 // libbinder_ndk.so on the system running this code would have API level < 35;
110 "android/binder: is built with API<24, assuming this is not Android 15+\n");
111#endif
112}
113
114#endif /* __ANDROID__ */
void android_binder_threadpool_init_if_required(void)
Initialize Android Binder thread pool.
#define NULL
Definition coverity.c:32
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define av_log(a,...)