Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: add android content uri support to FFmpeg #236

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ def _gen_android_cross_file(cfg):
url="https://ffmpeg.org/releases/ffmpeg-@[email protected]",
dst_file="ffmpeg-@[email protected]",
sha256="8684f4b00f94b85461884c3719382f1261f0d9eb3d59640a1f4ac0873616f968",
patches=[
"0001-avcodec-add-av_jni_-get-set-_android_app_ctx-helper.patch",
"0002-avformat-add-Android-content-resolver-protocol-suppo.patch",
"0003-avcodec-jni-use-size_t-to-store-structure-offsets.patch",
"0004-avcodec-jni-remove-unnecessary-NULL-checks-before-ca.patch",
"0005-avcodec-mediacodec_wrapper-use-an-OFFSET-macro-where.patch",
"0006-avcodec-mediacodec_wrapper-remove-unnecessary-NULL-c.patch",
],
),
ffmpeg_Windows=dict(
version="6.1.1",
Expand Down Expand Up @@ -317,13 +325,15 @@ def _download_extract(args, dep_item):
assert _file_chk(dst_path, chksum)

# Extract
extracted = False
if tarfile.is_tarfile(dst_path):
with tarfile.open(dst_path) as tar:
dirs = {f.name for f in tar.getmembers() if f.isdir()}
extract_dir = op.join(dst_base, dst_dir or _guess_base_dir(dirs))
if not op.exists(extract_dir):
logging.info("extracting %s", dst_file)
tar.extractall(op.join(dst_base, dst_dir))
extracted = True

elif zipfile.is_zipfile(dst_path):
with zipfile.ZipFile(dst_path) as zip_:
Expand All @@ -332,9 +342,17 @@ def _download_extract(args, dep_item):
if not op.exists(extract_dir):
logging.info("extracting %s", dst_file)
zip_.extractall(op.join(dst_base, dst_dir))
extracted = True
else:
assert False

# Patch
if extracted:
patch_basedir = op.join(_ROOTDIR, "patches", name)
for patch in dep.get("patches", []):
patch_path = op.join(patch_basedir, patch)
run(["patch", "-p1", "-i", patch_path], cwd=extract_dir)

# Remove previous link if needed
target = op.join(dst_base, name)
rel_extract_dir = op.basename(extract_dir)
Expand Down Expand Up @@ -513,6 +531,7 @@ def _ffmpeg_setup(cfg):
"http",
"https",
"pipe",
"android_content",
]
filters = [
"aformat",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
From 15533f90bc98b4cebd84b4ead2d0dd322b4f8ea7 Mon Sep 17 00:00:00 2001
From: Matthieu Bouron <[email protected]>
Date: Mon, 12 Feb 2024 23:13:09 +0100
Subject: [PATCH 1/6] avcodec: add av_jni_{get,set}_android_app_ctx helper

This will allow users to pass the Android ApplicationContext which is mandatory
to retrieve the ContentResolver responsible to resolve/open Android content-uri.
---
libavcodec/jni.c | 40 ++++++++++++++++++++++++++++++++++++++++
libavcodec/jni.h | 17 +++++++++++++++++
2 files changed, 57 insertions(+)

diff --git a/libavcodec/jni.c b/libavcodec/jni.c
index ae6490de9d..7d04d0a268 100644
--- a/libavcodec/jni.c
+++ b/libavcodec/jni.c
@@ -64,6 +64,36 @@ void *av_jni_get_java_vm(void *log_ctx)
return vm;
}

+int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
+{
+ JNIEnv *env = avpriv_jni_get_env(c);
+ if (!env)
+ return AVERROR(EINVAL);
+
+ jobjectRefType type = (*env)->GetObjectRefType(env, app_ctx);
+ if (type != JNIGlobalRefType) {
+ av_log(log_ctx, AV_LOG_ERROR, "Application context must be passed as a global reference");
+ return AVERROR(EINVAL);
+ }
+
+ pthread_mutex_lock(&lock);
+ android_app_ctx = app_ctx;
+ pthread_mutex_unlock(&lock);
+
+ return 0;
+}
+
+void *av_jni_get_android_app_ctx(void)
+{
+ void *ctx;
+
+ pthread_mutex_lock(&lock);
+ ctx = android_app_ctx;
+ pthread_mutex_unlock(&lock);
+
+ return ctx;
+}
+
#else

int av_jni_set_java_vm(void *vm, void *log_ctx)
@@ -76,4 +106,14 @@ void *av_jni_get_java_vm(void *log_ctx)
return NULL;
}

+int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
+{
+ return AVERROR(ENOSYS);
+}
+
+void *av_jni_get_android_app_ctx(void)
+{
+ return NULL;
+}
+
#endif
diff --git a/libavcodec/jni.h b/libavcodec/jni.h
index dd99e92611..da8025f830 100644
--- a/libavcodec/jni.h
+++ b/libavcodec/jni.h
@@ -43,4 +43,21 @@ int av_jni_set_java_vm(void *vm, void *log_ctx);
*/
void *av_jni_get_java_vm(void *log_ctx);

+/*
+ * Set the Android application context which will be used to retrieve the Android
+ * content resolver to resolve content uris.
+ *
+ * @param app_ctx global JNI reference to the Android application context
+ * @return 0 on success, < 0 otherwise
+ */
+int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx);
+
+/*
+ * Get the Android application context that has been set with
+ * av_jni_set_android_app_ctx.
+ *
+ * @return a pointer the the Android application context
+ */
+void *av_jni_get_android_app_ctx(void);
+
#endif /* AVCODEC_JNI_H */
--
2.44.0

Loading
Loading