Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
Support Android scoped storage (#440)
Browse files Browse the repository at this point in the history
* introducing saf_wrapper - a transparent API to use the file descriptor via custom AVIO for saf: Url

* demonstrate how use to use SAF with some demo tabs

SAF is enabled for API 19 or higher
SAF pickers are used for FFprobe on CommandTabFragment and for video output on PipeTabFragment

* Revert "demonstrate how use to use SAF with some demo tabs"

This reverts commit 97b2581

* add SAF tab with two buttons

* change the getSafParameter handles filenames with spaces

see alexcohn#1 (comment)

* take advantage of Cursor being Closeable

* provide NBSP in hexa, to make sure that git and editor don't destroy it

* Avoid "rw" because it may not be supported (e.g. Google Drive).

Also, some code cleanup in SafTabFragment.

* don't run FFprobe after video playback

* ACTION_GET_CONTENT is better than ACTION_OPEN_DOCUMENT: it gives us more options to choose.

* add closeParcelFileDescriptor to fix working with Google Drive

keep the temp files open, we must keep the ParcelFileDecriptor until the fd is closed
  • Loading branch information
alexcohn authored Sep 21, 2020
1 parent ef39b78 commit b898574
Show file tree
Hide file tree
Showing 12 changed files with 764 additions and 2 deletions.
1 change: 1 addition & 0 deletions android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
native <methods>;
void log(long, int, byte[]);
void statistics(long, int, float, float, long , int, double, double);
void closeParcelFileDescriptor(int);
}

-keep class com.arthenica.mobileffmpeg.AbiDetect {
Expand Down
2 changes: 2 additions & 0 deletions android/app/src/main/cpp/fftools_cmdutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"

#include "saf_wrapper.h"

#ifdef _WIN32
#undef main /* We don't want SDL to override our main() */
#endif
Expand Down
20 changes: 19 additions & 1 deletion android/app/src/main/cpp/mobileffmpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ static jmethodID logMethod;
/** Global reference of statistics redirection method in Java */
static jmethodID statisticsMethod;

/** Global reference of closeParcelFileDescriptor method in Java */
static jmethodID closeParcelFileDescriptorMethod;

/** Global reference of String class in Java */
static jclass stringClass;

Expand Down Expand Up @@ -655,6 +658,12 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_FALSE;
}

closeParcelFileDescriptorMethod = (*env)->GetStaticMethodID(env, localConfigClass, "closeParcelFileDescriptor", "(I)V");
if (logMethod == NULL) {
LOGE("OnLoad thread failed to GetStaticMethodID for %s.\n", "closeParcelFileDescriptor");
return JNI_FALSE;
}

stringConstructor = (*env)->GetMethodID(env, localStringClass, "<init>", "([BLjava/lang/String;)V");
if (stringConstructor == NULL) {
LOGE("OnLoad thread failed to GetMethodID for %s.\n", "<init>");
Expand Down Expand Up @@ -690,7 +699,7 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) {
* @param object reference to the class on which this method is invoked
* @param level log level
*/
JNIEXPORT void JNICALL Java_com_arthenica_mobileffmpeg_Config_setNativeLogLevel(JNIEnv *env, jclass object, jint level) {
JNIEXPORT void JNICALL Java_com_arthenica_mobileffmpeg_Config_setNativeLogLevel(JNIEnv *env, jclass object, jint level) {
configuredLogLevel = level;
}

Expand Down Expand Up @@ -943,3 +952,12 @@ JNIEXPORT void JNICALL Java_com_arthenica_mobileffmpeg_Config_ignoreNativeSignal
handleSIGPIPE = 0;
}
}

/**
* used by saf_wrapper; is expected to be called from a Java thread, therefore we don't need attach/detach
*/
void closeParcelFileDescriptor(int fd) {
JNIEnv *env = NULL;
(*globalVm)->GetEnv(globalVm, (void**) &env, JNI_VERSION_1_6);
(*env)->CallStaticVoidMethod(env, configClass, closeParcelFileDescriptorMethod, fd);
}
135 changes: 135 additions & 0 deletions android/app/src/main/cpp/saf_wrapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright (c) 2020 Taner Sener
*
* This file is part of MobileFFmpeg.
*
* MobileFFmpeg is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MobileFFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with MobileFFmpeg. If not, see <http://www.gnu.org/licenses/>.
*/

#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>

#include "config.h"
#include "libavformat/avformat.h"
#include "libavutil/avstring.h"

#include "saf_wrapper.h"

/** JNI wrapper in mobileffmpeg.c */
void closeParcelFileDescriptor(int fd);

// in these wrappers, we call the original functions, so we remove the shadow defines
#undef avio_closep
#undef avformat_close_input
#undef avio_open
#undef avio_open2
#undef avformat_open_input

static int fd_read_packet(void* opaque, uint8_t* buf, int buf_size) {
int fd = (int)opaque;
return read(fd, buf, buf_size);
}

static int fd_write_packet(void* opaque, uint8_t* buf, int buf_size) {
int fd = (int)opaque;
return write(fd, buf, buf_size);
}

static int64_t fd_seek(void *opaque, int64_t offset, int whence) {
int fd = (int)opaque;

if (fd < 0) {
return AVERROR(EINVAL);
}

int64_t ret;
if (whence == AVSEEK_SIZE) {
struct stat st;
ret = fstat(fd, &st);
return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
}

ret = lseek(fd, offset, whence);

return ret < 0 ? AVERROR(errno) : ret;
}

/*
* returns NULL if the filename is not of expected format (e.g. 'saf:72/video.md4')
*/
static AVIOContext *create_fd_avio_context(const char *filename, int flags) {
union {int fd; void* opaque;} fdunion;
fdunion.fd = -1;
const char *fd_ptr = NULL;
if (av_strstart(filename, "saf:", &fd_ptr)) {
char *final;
fdunion.fd = strtol(fd_ptr, &final, 10);
if (fd_ptr == final) { /* No digits found */
fdunion.fd = -1;
}
}

if (fdunion.fd >= 0) {
int write_flag = flags & AVIO_FLAG_WRITE ? 1 : 0;
return avio_alloc_context(av_malloc(4096), 4096, write_flag, fdunion.opaque, fd_read_packet, write_flag ? fd_write_packet : NULL, fd_seek);
}
return NULL;
}

static void close_fd_avio_context(AVIOContext *ctx) {
if (fd_seek(ctx->opaque, 0, AVSEEK_SIZE) >= 0) {
int fd = (int)ctx->opaque;
close(fd);
closeParcelFileDescriptor(fd);
}
ctx->opaque = NULL;
}

int android_avformat_open_input(AVFormatContext **ps, const char *filename,
ff_const59 AVInputFormat *fmt, AVDictionary **options) {
if (!(*ps) && !(*ps = avformat_alloc_context()))
return AVERROR(ENOMEM);

(*ps)->pb = create_fd_avio_context(filename, AVIO_FLAG_READ);

return avformat_open_input(ps, filename, fmt, options);
}

int android_avio_open2(AVIOContext **s, const char *filename, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options) {
AVIOContext *fd_context = create_fd_avio_context(filename, flags);

if (fd_context) {
*s = fd_context;
return 0;
}
return avio_open2(s, filename, flags, int_cb, options);
}

int android_avio_open(AVIOContext **s, const char *url, int flags) {
return android_avio_open2(s, url, flags, NULL, NULL);
}

int android_avio_closep(AVIOContext **s) {
close_fd_avio_context(*s);
return avio_closep(s);
}

void android_avformat_close_input(AVFormatContext **ps) {
if (*ps && (*ps)->pb) {
close_fd_avio_context((*ps)->pb);
}
avformat_close_input(ps);
}
46 changes: 46 additions & 0 deletions android/app/src/main/cpp/saf_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2020 Taner Sener
*
* This file is part of MobileFFmpeg.
*
* MobileFFmpeg is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MobileFFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with MobileFFmpeg. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef MOBILE_FFMPEG_SAF_WRAPPER_H
#define MOBILE_FFMPEG_SAF_WRAPPER_H

/*
* These wrappers are intended to be used instead of the ffmpeg apis.
* You don't even need to change the source to call them.
* Instead, we redefine the public api names so that the wrapper be used.
*/

int android_avio_closep(AVIOContext **s);
#define avio_closep android_avio_closep

void android_avformat_close_input(AVFormatContext **s);
#define avformat_close_input android_avformat_close_input

int android_avio_open(AVIOContext **s, const char *url, int flags);
#define avio_open android_avio_open

int android_avio_open2(AVIOContext **s, const char *url, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options);
#define avio_open2 android_avio_open2

int android_avformat_open_input(AVFormatContext **ps, const char *filename,
ff_const59 AVInputFormat *fmt, AVDictionary **options);
#define avformat_open_input android_avformat_open_input

#endif //MOBILE_FFMPEG_SAF_WRAPPER_H
60 changes: 60 additions & 0 deletions android/app/src/main/java/com/arthenica/mobileffmpeg/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
package com.arthenica.mobileffmpeg;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;
import android.util.Log;
import android.util.SparseArray;

import java.io.File;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -80,6 +85,7 @@ public class Config {
private static int lastCreatedPipeIndex;

private static final List<FFmpegExecution> executions;
private static SparseArray<ParcelFileDescriptor> pfdmap = new SparseArray<>();

static {

Expand Down Expand Up @@ -769,4 +775,58 @@ static List<FFmpegExecution> listFFmpegExecutions() {
*/
private native static void ignoreNativeSignal(final int signum);

/**
* <p>Convert Structured Access Framework Uri (<code></code>"content:…"</code>) for MobileFfmpeg.
*
* @return String can be passed to FFmpeg or FFprobe
*/
private static String getSafParameter(Context context, Uri uri, String openMode) {

String displayName = "unknown";
try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_DISPLAY_NAME));
}
} catch (Throwable ex) {
Log.e(TAG, "failed to get column", ex);
}

int fd = -1;
try {
ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, openMode);
fd = parcelFileDescriptor.getFd();
pfdmap.put(fd, parcelFileDescriptor);
} catch (Throwable e) {
Log.e(TAG, "obtaining " + openMode + " ParcelFileDescriptor for " + uri, e);
}

// workaround for https://issuetracker.google.com/issues/162440528: ANDROID_CREATE_DOCUMENT generating file names like "transcode.mp3 (2)"
if (displayName.lastIndexOf('.') > 0 && displayName.lastIndexOf(' ') > displayName.lastIndexOf('.')) {
String extension = displayName.substring(displayName.lastIndexOf('.'), displayName.lastIndexOf(' '));
displayName += extension;
}
// spaces can break argument list parsing, see https://github.com/alexcohn/mobile-ffmpeg/pull/1#issuecomment-688643836
final char NBSP = (char)0xa0;
return "saf:" + fd + "/" + displayName.replace(' ', NBSP);
}

public static String getSafParameterForRead(Context context, Uri uri) {
return getSafParameter(context, uri, "r");
}

public static String getSafParameterForWrite(Context context, Uri uri) {
return getSafParameter(context, uri, "w");
}

private static void closeParcelFileDescriptor(int fd) {
try {
ParcelFileDescriptor pfd = pfdmap.get(fd);
if (pfd != null) {
pfd.close();
pfdmap.delete(fd);
}
} catch (Throwable e) {
Log.e(TAG, "closeParcelFileDescriptor " + fd, e);
}
}
}
1 change: 1 addition & 0 deletions android/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ else ifeq ($(TARGET_PLATFORM),android-17)
else
MY_SRC_FILES := mobileffmpeg.c mobileffprobe.c mobileffmpeg_exception.c fftools_cmdutils.c fftools_ffmpeg.c fftools_ffprobe.c fftools_ffmpeg_opt.c fftools_ffmpeg_hw.c fftools_ffmpeg_filter.c
endif
MY_SRC_FILES += saf_wrapper.c

MY_CFLAGS := -Wall -Werror -Wno-unused-parameter -Wno-switch -Wno-sign-compare
MY_LDLIBS := -llog -lz -landroid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import androidx.fragment.app.FragmentPagerAdapter;

public class PagerAdapter extends FragmentPagerAdapter {
private static final int NUMBER_OF_TABS = 8;
private static final int NUMBER_OF_TABS = 9;

private final Context context;

Expand Down Expand Up @@ -62,6 +62,9 @@ public Fragment getItem(final int position) {
case 7: {
return ConcurrentExecutionTabFragment.newInstance();
}
case 8: {
return SafTabFragment.newInstance();
}
default: {
return null;
}
Expand Down Expand Up @@ -100,6 +103,9 @@ public CharSequence getPageTitle(final int position) {
case 7: {
return context.getString(R.string.concurrent_tab);
}
case 8: {
return context.getString(R.string.saf_tab);
}
default: {
return null;
}
Expand Down
Loading

0 comments on commit b898574

Please sign in to comment.