From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 6404E495D7 for ; Tue, 13 Feb 2024 23:05:17 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1CED868D1C9; Wed, 14 Feb 2024 01:04:56 +0200 (EET) Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 54FDF68D1AE for ; Wed, 14 Feb 2024 01:04:50 +0200 (EET) Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1d7354ba334so41481245ad.1 for ; Tue, 13 Feb 2024 15:04:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707865488; x=1708470288; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gm+wu9NCYpQqngrxbNNHNjXs3NHbYfHUppJVPG2WMBg=; b=E1yDmIr+xyOtolSod+aiol6MJMZKkmgm0/agUzZuVwj5Ec5Do3skkp3uHQRpum6oK8 o7DTegTJmWcIYzfbmCdY3UrTZw/SupyfDlBqNeohzWb++XcgU0EjmmkxC9k9rxIk4QtY W9RzkrV1X4mG2M+W+0ovLgLvTzyAkXtq4f7S1t5zdaWLzV5DSGpmRtGk9Ecym6fDO9ZR 6kVPl0WYZqKxkDVEpzMxDAhAcpaSiXYlzWpx9/SagYNlL45RIsM7ukBmW1weB+Ayl+sy v8Te6oHShfgYlDtDxXICvw/CcBRCdRGU7RGrefNFdVUXmwyeFsDWW90gFdPOYbKliPA/ 2bxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707865488; x=1708470288; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gm+wu9NCYpQqngrxbNNHNjXs3NHbYfHUppJVPG2WMBg=; b=PLgKXKidIEqJwCFDio4zQKnQKZSKM+mTnEDvv1sJvyQmTPVj1v27JLUa4gmTsR2aQg xWubcbN+Tpj47Cm/KoprdFS9xeNyo7PO14p+qDifoQq1wMQHza8rGwCy6joQ+N2dxIa7 PUK6Kk4xJU9NIJZXXLHFD5SNujwFuyGGsdcFddgKRjb2cKHcv+EXmSzWNtpEK7Kn+DI2 uAIwDYQU8HzoFBBi9KseKNym+ZNRLPalVyWjO0jHiUPq6zTfu9Xr3JshoPx2Skj+xWws DSznoL2THE1G1Adg94zozq4JmIVmGKGqs6fxBYzkv1QZhEQ24mqT2ckwD2dleB4oqmfh MnqA== X-Gm-Message-State: AOJu0Yxknf4Xx2JpZ/FiaPkEie/eWnV9zi++tzzO/tonH297Zs6OXTw5 WUshgfVgmSLUdDezR3L8f0wRHr/ANutP1B1lfdanlacwyWa4HHnJBnc7ld32 X-Google-Smtp-Source: AGHT+IE4c3xXLeFc2KwB1RN9RriIuR+LF0Fvmtl/Y8xLcVQmG8Wfn/7GR4ojVHBYTt4DNanFOCEwkQ== X-Received: by 2002:a17:903:24d:b0:1da:2a0d:feaa with SMTP id j13-20020a170903024d00b001da2a0dfeaamr1088109plh.32.1707865488489; Tue, 13 Feb 2024 15:04:48 -0800 (PST) Received: from localhost.localdomain (2a01cb040b6872000000000000000afa.ipv6.abo.wanadoo.fr. [2a01:cb04:b68:7200::afa]) by smtp.gmail.com with ESMTPSA id l13-20020a170903244d00b001da294ff6d5sm2580098pls.189.2024.02.13.15.04.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 15:04:47 -0800 (PST) From: Matthieu Bouron To: ffmpeg-devel@ffmpeg.org Date: Tue, 13 Feb 2024 23:50:11 +0100 Message-ID: <20240213230418.457056-4-matthieu.bouron@gmail.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240213230418.457056-1-matthieu.bouron@gmail.com> References: <20240213230418.457056-1-matthieu.bouron@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/7] avformat: add Android content resolver protocol support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Matthieu Bouron Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Handles Android content-uri starting with content://. --- configure | 2 + doc/APIchanges | 3 + libavformat/Makefile | 1 + libavformat/file.c | 157 ++++++++++++++++++++++++++++++++++++++++ libavformat/protocols.c | 1 + 5 files changed, 164 insertions(+) diff --git a/configure b/configure index f72533b7d2..a3593dc200 100755 --- a/configure +++ b/configure @@ -3651,6 +3651,8 @@ xcbgrab_indev_suggest="libxcb_shm libxcb_shape libxcb_xfixes" xv_outdev_deps="xlib_xv xlib_x11 xlib_xext" # protocols +android_content_protocol_deps="jni" +android_content_protocol_select="file_protocol" async_protocol_deps="threads" bluray_protocol_deps="libbluray" ffrtmpcrypt_protocol_conflict="librtmp_protocol" diff --git a/doc/APIchanges b/doc/APIchanges index 45611ea7ea..7f6631b3d3 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -2,6 +2,9 @@ The last version increases of all libraries were on 2023-02-09 API changes, most recent first: +2024-02-xx - xxxxxxxxxx - lavu 58.40.100 - jni.h + Add av_jni_set_android_app_ctx() and av_jni_get_android_app_ctx(). + 2024-02-xx - xxxxxxxxxx - lavu 58.39.100 - jni.h Add av_jni_set_jvm() and av_jni_get_jvm(). diff --git a/libavformat/Makefile b/libavformat/Makefile index 05b9b8a115..788d4e4135 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -655,6 +655,7 @@ OBJS-$(CONFIG_LIBOPENMPT_DEMUXER) += libopenmpt.o OBJS-$(CONFIG_VAPOURSYNTH_DEMUXER) += vapoursynth.o # protocols I/O +OBJS-$(CONFIG_ANDROID_CONTENT_PROTOCOL) += file.o OBJS-$(CONFIG_ASYNC_PROTOCOL) += async.o OBJS-$(CONFIG_APPLEHTTP_PROTOCOL) += hlsproto.o OBJS-$(CONFIG_BLURAY_PROTOCOL) += bluray.o diff --git a/libavformat/file.c b/libavformat/file.c index 64df7ff6fb..c7c7a5073f 100644 --- a/libavformat/file.c +++ b/libavformat/file.c @@ -40,6 +40,12 @@ #include #include "os_support.h" #include "url.h" +#if CONFIG_ANDROID_CONTENT_PROTOCOL +#include +#include "libavutil/jni.h" +#include "libavutil/jniutils.h" +#endif + /* Some systems may not have S_ISFIFO */ #ifndef S_ISFIFO @@ -101,6 +107,21 @@ typedef struct FileContext { int64_t initial_pos; } FileContext; + +#if CONFIG_ANDROID_CONTENT_PROTOCOL +static const AVOption android_content_options[] = { + { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, + { NULL } +}; + +static const AVClass android_content_class = { + .class_name = "android_content", + .item_name = av_default_item_name, + .option = android_content_options, + .version = LIBAVUTIL_VERSION_INT, +}; +#endif + static const AVOption file_options[] = { { "truncate", "truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM }, { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, @@ -524,3 +545,139 @@ const URLProtocol ff_fd_protocol = { }; #endif /* CONFIG_FD_PROTOCOL */ + +#if CONFIG_ANDROID_CONTENT_PROTOCOL + +struct JFields { + jclass uri_class; + jmethodID 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; +}; + +#define OFFSET(x) offsetof(struct JFields, x) +static const struct FFJniField jfields_mapping[] = { + { "android/net/Uri", NULL, NULL, FF_JNI_CLASS, OFFSET(uri_class), 1 }, + { "android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;", FF_JNI_STATIC_METHOD, OFFSET(parse_id), 1 }, + + { "android/content/Context", NULL, NULL, FF_JNI_CLASS, OFFSET(context_class), 1 }, + { "android/content/Context", "getContentResolver", "()Landroid/content/ContentResolver;", FF_JNI_METHOD, OFFSET(get_content_resolver_id), 1 }, + + { "android/content/ContentResolver", NULL, NULL, FF_JNI_CLASS, OFFSET(content_resolver_class), 1 }, + { "android/content/ContentResolver", "openFileDescriptor", "(Landroid/net/Uri;Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;", FF_JNI_METHOD, OFFSET(open_file_descriptor_id), 1 }, + + { "android/os/ParcelFileDescriptor", NULL, NULL, FF_JNI_CLASS, OFFSET(parcel_file_descriptor_class), 1 }, + { "android/os/ParcelFileDescriptor", "detachFd", "()I", FF_JNI_METHOD, OFFSET(detach_fd_id), 1 }, + + { NULL } +}; +#undef OFFSET + +static int android_content_open(URLContext *h, const char *filename, int flags) +{ + FileContext *c = h->priv_data; + int fd, ret; + const char *mode_str = "r"; + + 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_get_env(c); + if (!env) { + return AVERROR(EINVAL); + } + + ret = avpriv_jni_init_jfields(env, &jfields, jfields_mapping, 0, c); + if (ret < 0) { + av_log(c, AV_LOG_ERROR, "failed to initialize jni fields\n"); + return ret; + } + + application_context = av_jni_get_android_app_ctx(); + 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; + } + + if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) + mode_str = "rw"; + else if (flags & AVIO_FLAG_WRITE) + mode_str = "w"; + + mode = avpriv_jni_utf_chars_to_jstring(env, mode_str, c); + if (!mode) { + ret = AVERROR_EXTERNAL; + goto done; + } + + uri = (*env)->CallStaticObjectMethod(env, jfields.uri_class, jfields.parse_id, url); + ret = avpriv_jni_exception_check(env, 1, c); + if (ret < 0) + goto done; + + content_resolver = (*env)->CallObjectMethod(env, application_context, jfields.get_content_resolver_id); + ret = avpriv_jni_exception_check(env, 1, c); + if (ret < 0) + goto done; + + parcel_file_descriptor = (*env)->CallObjectMethod(env, content_resolver, jfields.open_file_descriptor_id, uri, mode); + ret = avpriv_jni_exception_check(env, 1, c); + if (ret < 0) + goto done; + + fd = (*env)->CallIntMethod(env, parcel_file_descriptor, jfields.detach_fd_id); + ret = avpriv_jni_exception_check(env, 1, c); + if (ret < 0) + goto done; + +#if HAVE_SETMODE + setmode(fd, O_BINARY); +#endif + c->fd = fd; + h->is_streamed = 0; + +done: + (*env)->DeleteLocalRef(env, url); + (*env)->DeleteLocalRef(env, mode); + (*env)->DeleteLocalRef(env, uri); + (*env)->DeleteLocalRef(env, content_resolver); + (*env)->DeleteLocalRef(env, parcel_file_descriptor); + avpriv_jni_reset_jfields(env, &jfields, jfields_mapping, 0, c); + + return ret; +} + +URLProtocol ff_android_content_protocol = { + .name = "content", + .url_open = android_content_open, + .url_read = file_read, + .url_write = file_write, + .url_seek = file_seek, + .url_close = file_close, + .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/libavformat/protocols.c b/libavformat/protocols.c index 360018b17c..93a6d67261 100644 --- a/libavformat/protocols.c +++ b/libavformat/protocols.c @@ -24,6 +24,7 @@ #include "url.h" +extern const URLProtocol ff_android_content_protocol; extern const URLProtocol ff_async_protocol; extern const URLProtocol ff_bluray_protocol; extern const URLProtocol ff_cache_protocol; -- 2.43.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".