This repository has been archived by the owner on Jan 6, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 811
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Android scoped storage (#440)
* 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
Showing
12 changed files
with
764 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.