From: Matthieu Bouron <matthieu.bou...@stupeflix.com> Handles uri starting with content://. --- configure | 5 ++ libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/file.c | 155 +++++++++++++++++++++++++++++++++++++++++++++++ libavutil/jni.c | 2 +- 5 files changed, 163 insertions(+), 1 deletion(-)
diff --git a/configure b/configure index de5b905..17bd721 100755 --- a/configure +++ b/configure @@ -194,6 +194,7 @@ Individual component options: --disable-filters disable all filters External library support: + --enable-android-content enable Android content resolver --enable-avisynth enable reading of AviSynth script files [no] --disable-bzlib disable bzlib [autodetect] --enable-chromaprint enable audio fingerprinting with chromaprint [no] @@ -1367,6 +1368,7 @@ EXAMPLE_LIST=" " EXTERNAL_LIBRARY_LIST=" + android_content avisynth bzlib chromaprint @@ -2702,6 +2704,7 @@ x11grab_xcb_indev_deps="libxcb" # protocols async_protocol_deps="threads" +android_content_protocol_deps="file_protocol android_content" bluray_protocol_deps="libbluray" ffrtmpcrypt_protocol_deps="!librtmp_protocol" ffrtmpcrypt_protocol_deps_any="gcrypt gmp openssl" @@ -5255,6 +5258,7 @@ for func in $MATH_FUNCS; do done # these are off by default, so fail if requested and not available +enabled android_content && { check_header jni.h && [ $target_os = android ]; } enabled avfoundation_indev && { check_header_oc AVFoundation/AVFoundation.h || disable avfoundation_indev; } enabled avfoundation_indev && { check_lib2 CoreGraphics/CoreGraphics.h CGGetActiveDisplayList -framework CoreGraphics || check_lib2 ApplicationServices/ApplicationServices.h CGGetActiveDisplayList -framework ApplicationServices; } @@ -6004,6 +6008,7 @@ echo "safe bitstream reader ${safe_bitstream_reader-no}" echo "SDL support ${sdl-no}" echo "opencl enabled ${opencl-no}" echo "JNI support ${jni-no}" +echo "Android content support ${jni-no}" echo "texi2html enabled ${texi2html-no}" echo "perl enabled ${perl-no}" echo "pod2man enabled ${pod2man-no}" diff --git a/libavformat/Makefile b/libavformat/Makefile index 2971912..7ed8c9e 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -499,6 +499,7 @@ OBJS-$(CONFIG_LIBSMBCLIENT_PROTOCOL) += libsmbclient.o # protocols I/O OBJS-$(CONFIG_ASYNC_PROTOCOL) += async.o +OBJS-$(CONFIG_ANDROID_CONTENT_PROTOCOL) += file.o OBJS-$(CONFIG_APPLEHTTP_PROTOCOL) += hlsproto.o OBJS-$(CONFIG_BLURAY_PROTOCOL) += bluray.o OBJS-$(CONFIG_CACHE_PROTOCOL) += cache.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 0ccde9d..7a62af2 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -353,6 +353,7 @@ void av_register_all(void) /* protocols */ REGISTER_PROTOCOL(ASYNC, async); + REGISTER_PROTOCOL(ANDROID_CONTENT, android_content); REGISTER_PROTOCOL(BLURAY, bluray); REGISTER_PROTOCOL(CACHE, cache); REGISTER_PROTOCOL(CONCAT, concat); diff --git a/libavformat/file.c b/libavformat/file.c index d59aa42..8058794 100644 --- a/libavformat/file.c +++ b/libavformat/file.c @@ -37,6 +37,12 @@ #include <stdlib.h> #include "os_support.h" #include "url.h" +#if CONFIG_ANDROID_CONTENT_PROTOCOL +#include "libavutil/jni.h" +#include "libavutil/jni_internal.h" +#include <jni.h> +#endif + /* Some systems may not have S_ISFIFO */ #ifndef S_ISFIFO @@ -102,6 +108,13 @@ static const AVClass pipe_class = { .version = LIBAVUTIL_VERSION_INT, }; +static const AVClass android_content_class = { + .class_name = "android_content", + .item_name = av_default_item_name, + .option = file_options, + .version = LIBAVUTIL_VERSION_INT, +}; + static int file_read(URLContext *h, unsigned char *buf, int size) { FileContext *c = h->priv_data; @@ -390,3 +403,145 @@ URLProtocol ff_pipe_protocol = { }; #endif /* CONFIG_PIPE_PROTOCOL */ + +#if CONFIG_ANDROID_CONTENT_PROTOCOL + +struct JFields { + + jclass uri_class; + jclass parse_id; + + jclass context_class; + jmethodID get_content_resolver_id; + + jclass content_resolver_class; + jmethodID open_file_descriptor_id; + + jclass parcel_file_descriptor_class; + jmethodID detach_fd_id; + +} JFields; + +static const struct FFJniField jfields_mapping[] = { + + { "android/net/Uri", NULL, NULL, FF_JNI_CLASS, offsetof(struct JFields, uri_class), 1 }, + { "android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;", FF_JNI_STATIC_METHOD, offsetof(struct JFields, parse_id), 1 }, + + { "android/content/Context", NULL, NULL, FF_JNI_CLASS, offsetof(struct JFields, context_class), 1 }, + { "android/content/Context", "getContentResolver", "()Landroid/content/ContentResolver;", FF_JNI_METHOD, offsetof(struct JFields, get_content_resolver_id), 1 }, + + { "android/content/ContentResolver", NULL, NULL, FF_JNI_CLASS, offsetof(struct JFields, content_resolver_class), 1 }, + { "android/content/ContentResolver", "openFileDescriptor", "(Landroid/net/Uri;Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;", FF_JNI_METHOD, offsetof(struct JFields, open_file_descriptor_id), 1 }, + + { "android/os/ParcelFileDescriptor", NULL, NULL, FF_JNI_CLASS, offsetof(struct JFields, parcel_file_descriptor_class), 1 }, + { "android/os/ParcelFileDescriptor", "detachFd", "()I", FF_JNI_METHOD, offsetof(struct JFields, detach_fd_id), 1 }, + + { NULL } +}; + +static int android_content_open(URLContext *h, const char *filename, int flags) +{ + FileContext *c = h->priv_data; + int fd, ret = 0; + JNIEnv *env; + struct JFields jfields = { 0 }; + + jobject application_context = NULL; + jobject url = NULL; + jobject mode = NULL; + jobject uri = NULL; + jobject content_resolver = NULL; + jobject parcel_file_descriptor = NULL; + + env = avpriv_jni_attach_env(c); + if (!env) { + return AVERROR(EINVAL); + } + + if (avpriv_jni_init_jfields(env, &jfields, jfields_mapping, 0, c) < 0) { + goto done; + } + + application_context = av_jni_get_application_context(); + if (!application_context) { + av_log(c, AV_LOG_ERROR, "application context is not set\n"); + ret = AVERROR_EXTERNAL; + goto done; + } + + url = avpriv_jni_utf_chars_to_jstring(env, filename, c); + if (!url) { + ret = AVERROR_EXTERNAL; + goto done; + } + + mode = avpriv_jni_utf_chars_to_jstring(env, "r", c); + if (!mode) { + ret = AVERROR_EXTERNAL; + goto done; + } + + uri = (*env)->CallStaticObjectMethod(env, jfields.uri_class, jfields.parse_id, url); + if ((ret = avpriv_jni_exception_check(env, 1, c)) < 0) { + goto done; + } + + content_resolver = (*env)->CallObjectMethod(env, application_context, jfields.get_content_resolver_id); + if ((ret = avpriv_jni_exception_check(env, 1, c)) < 0) { + goto done; + } + + parcel_file_descriptor = (*env)->CallObjectMethod(env, content_resolver, jfields.open_file_descriptor_id, uri, mode); + if ((ret = avpriv_jni_exception_check(env, 1, c)) < 0) { + goto done; + } + + fd = (*env)->CallIntMethod(env, parcel_file_descriptor, jfields.detach_fd_id); + if ((ret = avpriv_jni_exception_check(env, 1, c)) < 0) { + goto done; + } + +#if HAVE_SETMODE + setmode(fd, O_BINARY); +#endif + c->fd = fd; + h->is_streamed = 0; + +done: + if (url) { + (*env)->DeleteLocalRef(env, url); + } + + if (mode) { + (*env)->DeleteLocalRef(env, mode); + } + + if (uri) { + (*env)->DeleteLocalRef(env, uri); + } + + if (content_resolver) { + (*env)->DeleteLocalRef(env, content_resolver); + } + + if (parcel_file_descriptor) { + (*env)->DeleteLocalRef(env, parcel_file_descriptor); + } + + avpriv_jni_detach_env(c); + + return ret; +} + +URLProtocol ff_android_content_protocol = { + .name = "content", + .url_open = android_content_open, + .url_read = file_read, + .url_write = file_write, + .url_get_file_handle = file_get_handle, + .url_check = NULL, + .priv_data_size = sizeof(FileContext), + .priv_data_class = &android_content_class, +}; + +#endif /* CONFIG_ANDROID_CONTENT_PROTOCOL */ diff --git a/libavutil/jni.c b/libavutil/jni.c index 0c4c8ca..3c6852b 100644 --- a/libavutil/jni.c +++ b/libavutil/jni.c @@ -115,7 +115,7 @@ done: } } - return 0; + return ret; } jobject av_jni_get_application_context(void) -- 2.6.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel