From patchwork Sun Mar 17 22:28:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthieu Bouron X-Patchwork-Id: 47147 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:958e:b0:1a3:31a3:7958 with SMTP id iu14csp700398pzb; Sun, 17 Mar 2024 15:31:38 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVTdGkJZDeOU6sK21bcW6DT1MB+3rcX8ulLiIaioAHyPflaXevEQjJ7sp5oMNKC05U/HeIt1tbnwbZq7zLGC1Z8NH+L1qvxINr4Ng== X-Google-Smtp-Source: AGHT+IECLLZoJxvdzHKfI14hm4LWosgR6frh/eJQr99qYZc1NzTPI72vHz/xX7zH9D2MSMCoN0fY X-Received: by 2002:a05:651c:486:b0:2d4:764f:87d9 with SMTP id s6-20020a05651c048600b002d4764f87d9mr5978725ljc.32.1710714698618; Sun, 17 Mar 2024 15:31:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1710714698; cv=none; d=google.com; s=arc-20160816; b=ZxPGT1RAoF9ibQMC34kL1+LYwRUCfPbrMI9q4xl/85XIxrw+5+Ecw3jJ98SbSmaP14 dYlaeqULZY/GfcZdidIQyziNoAkMw704hjDNIO6gzWncV9wN9hqbBqgK+7ZSc5nzTvRh jooGcwIqa+hluFeO07IlGup7UjUjhrXR4ORT2ccqMktZnWPvD7cyrfM5bihFGdLy+BXR wSSJ/mgo3pW8bJA8QnfQfyhSB3KHwV+P7gNIZQGcXPsK6bu6Ls3uCGB3rmgzkk2DqMM1 dGUR+GBcPDC2Lri3t3THgjFU4PsHUbg7WgluSHresaJyJ8PBI/ydsogwjznP6YxYbV/S F+Wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=2mm+xQCIguuHCHRHOGxf8AaWJw3q4oeSFHftiohN5NE=; fh=bsOL+ub9JEnfNazRgCdgjxJAChubFVskZxBzYcxeDLU=; b=Cn15iUfl5vI6BkiUnsMMFCR/MZcElz4VTdpPVMvMl1TY5I+wpKPlXRURLXHliz37eb nQIryVk+kpN1Dpty4QjCgBePE+B71Oh+aVLEPtXNNftO3g1NIeV0VCPRuxF0zIk/Y0W7 cnLCkQpYpB67lYCZ+WOT5BrsYXHJ2dKVw+g+gLKu236BhlV9XrN7mlh+6MMQwYqhYfUy 9F/oScxQwCKui7me1m941Ij8uo6bAEPMSaTZ2pVjDP/kOiv4rZlurZZsYWiOo+mHfsQh xuhFpkEtPumKVSoG+mKuerpCAsYmz8uNVbbBPswieOlOQHq+1dvATMFkkATPYHJmYVFc kUaA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=c7vKcS2o; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z8-20020a2e3508000000b002d4716c9adasi2180230ljz.366.2024.03.17.15.31.38; Sun, 17 Mar 2024 15:31:38 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=c7vKcS2o; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AE47768D210; Mon, 18 Mar 2024 00:31:17 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DE48A68D1A6 for ; Mon, 18 Mar 2024 00:31:08 +0200 (EET) Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-41411421113so1101125e9.1 for ; Sun, 17 Mar 2024 15:31:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710714668; x=1711319468; 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=gZf6z6BHrUpnvV3QsnUsc/zl83hGRjMwleCAjtI45ZU=; b=c7vKcS2oc6pCuHaOaXzj5eBeXdVTnslHu1M5UDRKiGF8oKur3Q3ICH8jM+v1RBPVge nhUNjx6AfwrAj5051jlJ70KCbrI6yqGq0GMxgPILIF2wIlqMSNafHFsM5b4Nnkfxjdl3 WwMIxMen/2+O3wnIz+SRQjwwhFizcm/8PCcAUoAJJu7oreYvOU46KTqUgFqINOq0Cejr Ooy/CV9SCugWH9VfaloYuNVSP4W9eYyYDtrXPyi22Re1ta5VDBK2KBXc6lf133DvQ7n6 Dk3uEE1vR/k6WCX4pbWOeDVob8nigeBxIeRTq6AYPh0qtjVJq+M5/rX41tcbOw0m381g 5Q+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710714668; x=1711319468; 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=gZf6z6BHrUpnvV3QsnUsc/zl83hGRjMwleCAjtI45ZU=; b=D7pJk+KNtZLAd+BqNFkiQ5EhUy/rEnXcWoe09byG6GXnNDBbMsa1z8hrqmBwVUfdOF 1icvViJbyZlk25x6lWIp9vPCU7bUv4RVC9NxwVUf/gv7NU48XmjboSJc6DeoPxRmdmyv pFyN5HeV24H9Fwpv+Ve/vJHXMpok6AhCbYAC9KgrAW4YuiSLKv8dn7HA00Sy2WhLenqg 9Qx6Wvc188jByeEbb63b3TnIF11b3J4GIE/TSRyRxk00VFlHt5gfMy/8RUtsZbFkuszN 9JUfL7ej01qYppsB3Jn8tfygrOVfndqfLPtfmq2riLwOwc+pgYz2DjKXxI/nZuAyDXYM hV9A== X-Gm-Message-State: AOJu0YywOFfzjCnDzNSs8VzKQwbcPx0z2F97XpGVNG++PQvgUhSqP37g ZV6tIxbXXPbC9kQ5kMyUDhTmrWB7nVBpm/qf8/GZ6ZCRv3hidJ3GIi3T1/aJ X-Received: by 2002:a05:600c:4f46:b0:414:b14:5654 with SMTP id m6-20020a05600c4f4600b004140b145654mr2211348wmq.21.1710714668153; Sun, 17 Mar 2024 15:31:08 -0700 (PDT) Received: from localhost.localdomain (2a01cb040b6872000000000000000afa.ipv6.abo.wanadoo.fr. [2a01:cb04:b68:7200::afa]) by smtp.gmail.com with ESMTPSA id g18-20020a05600c4ed200b00413f3b16a26sm12183682wmq.35.2024.03.17.15.31.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Mar 2024 15:31:07 -0700 (PDT) From: Matthieu Bouron To: ffmpeg-devel@ffmpeg.org Date: Sun, 17 Mar 2024 23:28:33 +0100 Message-ID: <20240317223102.49414-3-matthieu.bouron@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240317223102.49414-1-matthieu.bouron@gmail.com> References: <20240227145536.281991-1-matthieu.bouron@gmail.com> <20240317223102.49414-1-matthieu.bouron@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 2/6] 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: jXMhci0oq9di Handles Android content-uri starting with content://. --- configure | 2 + libavcodec/jni.c | 1 + libavformat/Makefile | 1 + libavformat/file.c | 160 ++++++++++++++++++++++++++++++++++++++++ libavformat/protocols.c | 1 + 5 files changed, 165 insertions(+) diff --git a/configure b/configure index 2b4c4ec9a2..9ef432fddf 100755 --- a/configure +++ b/configure @@ -3656,6 +3656,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/libavcodec/jni.c b/libavcodec/jni.c index cfe95bd1ec..1193c608c3 100644 --- a/libavcodec/jni.c +++ b/libavcodec/jni.c @@ -35,6 +35,7 @@ #include "ffjni.h" static void *java_vm; +static void *android_app_ctx; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; int av_jni_set_java_vm(void *vm, void *log_ctx) diff --git a/libavformat/Makefile b/libavformat/Makefile index 94a949f555..44aa485029 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -658,6 +658,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..dd5819c06f 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 "libavcodec/jni.h" +#include "libavcodec/ffjni.c" +#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,142 @@ const URLProtocol ff_fd_protocol = { }; #endif /* CONFIG_FD_PROTOCOL */ + +#if CONFIG_ANDROID_CONTENT_PROTOCOL + +typedef 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; +} JFields; + +#define OFFSET(x) offsetof(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; + struct stat st; + const char *mode_str = "r"; + + JNIEnv *env; + 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 = ff_jni_get_env(c); + if (!env) { + return AVERROR(EINVAL); + } + + ret = ff_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 = ff_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 = ff_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 = ff_jni_exception_check(env, 1, c); + if (ret < 0) + goto done; + + content_resolver = (*env)->CallObjectMethod(env, application_context, jfields.get_content_resolver_id); + ret = ff_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 = ff_jni_exception_check(env, 1, c); + if (ret < 0) + goto done; + + fd = (*env)->CallIntMethod(env, parcel_file_descriptor, jfields.detach_fd_id); + ret = ff_jni_exception_check(env, 1, c); + if (ret < 0) + goto done; + + if (fstat(fd, &st) < 0) { + close(fd); + return AVERROR(errno); + } + + c->fd = fd; + h->is_streamed = !(S_ISREG(st.st_mode) || S_ISBLK(st.st_mode)); + +done: + (*env)->DeleteLocalRef(env, url); + (*env)->DeleteLocalRef(env, mode); + (*env)->DeleteLocalRef(env, uri); + (*env)->DeleteLocalRef(env, content_resolver); + (*env)->DeleteLocalRef(env, parcel_file_descriptor); + ff_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;