Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Android Q Support #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android-ffmpeg/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ ext {
}

android {
compileSdkVersion 28
compileSdkVersion 29

defaultConfig {
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode 17
versionName "1.1.7"
}
Expand Down
5 changes: 0 additions & 5 deletions android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/CpuArch.java

This file was deleted.

25 changes: 0 additions & 25 deletions android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/CpuArchHelper.java

This file was deleted.

83 changes: 5 additions & 78 deletions android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FFmpeg.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import java.util.Map;

public class FFmpeg implements FFbinaryInterface {
private static final int VERSION = 17; // up this version when you add a new ffmpeg build
private static final String KEY_PREF_VERSION = "ffmpeg_version";

private final FFbinaryContextProvider context;

private static final long MINIMUM_TIMEOUT = 10 * 1000;
Expand All @@ -40,79 +37,21 @@ public Context provide() {

@Override
public boolean isSupported() {
// check if arch is supported
CpuArch cpuArch = CpuArchHelper.getCpuArch();
if (cpuArch == CpuArch.NONE) {
Log.e("arch not supported");
return false;
}

// get ffmpeg file
File ffmpeg = FileUtils.getFFmpeg(context.provide());

SharedPreferences settings = context.provide().getSharedPreferences("ffmpeg_prefs", Context.MODE_PRIVATE);
int version = settings.getInt(KEY_PREF_VERSION, 0);

// check if ffmpeg file exists
if (!ffmpeg.exists() || version < VERSION) {
String prefix = "arm/";
if (cpuArch == CpuArch.x86) {
prefix = "x86/";
}
Log.d("file does not exist, creating it...");

try {
InputStream inputStream = context.provide().getAssets().open(prefix + "ffmpeg");
if (!FileUtils.inputStreamToFile(inputStream, ffmpeg)) {
return false;
}

Log.d("successfully wrote ffmpeg file!");

settings.edit().putInt(KEY_PREF_VERSION, VERSION).apply();
} catch (IOException e) {
Log.e("error while opening assets", e);
return false;
}
}

// check if ffmpeg can be executed
if (!ffmpeg.canExecute()) {
// try to make executable
try {
try {
Runtime.getRuntime().exec("chmod -R 777 " + ffmpeg.getAbsolutePath()).waitFor();
} catch (InterruptedException e) {
Log.e("interrupted exception", e);
return false;
} catch (IOException e) {
Log.e("io exception", e);
return false;
}

if (!ffmpeg.canExecute()) {
// our last hope!
if (!ffmpeg.setExecutable(true)) {
Log.e("unable to make executable");
return false;
}
}
} catch (SecurityException e) {
Log.e("security exception", e);
return false;
}
Log.e("ffmpeg cannot execute");
return false;
}

Log.d("ffmpeg is ready!");

return true;
}

@Override
public FFtask execute(Map<String, String> environvenmentVars, String[] cmd, FFcommandExecuteResponseHandler ffmpegExecuteResponseHandler) {
if (cmd.length != 0) {
String[] ffmpegBinary = new String[]{FileUtils.getFFmpeg(context.provide()).getAbsolutePath()};
String[] command = concatenate(ffmpegBinary, cmd);
final String[] command = new String[cmd.length + 1];
command[0] = FileUtils.getFFmpeg(context.provide()).getAbsolutePath();
System.arraycopy(cmd, 0, command, 1, cmd.length);
FFcommandExecuteAsyncTask task = new FFcommandExecuteAsyncTask(command, environvenmentVars, timeout, ffmpegExecuteResponseHandler);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return task;
Expand All @@ -121,18 +60,6 @@ public FFtask execute(Map<String, String> environvenmentVars, String[] cmd, FFco
}
}

private static <T> T[] concatenate(T[] a, T[] b) {
int aLen = a.length;
int bLen = b.length;

@SuppressWarnings("unchecked")
T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen + bLen);
System.arraycopy(a, 0, c, 0, aLen);
System.arraycopy(b, 0, c, aLen, bLen);

return c;
}

@Override
public FFtask execute(String[] cmd, FFcommandExecuteResponseHandler ffmpegExecuteResponseHandler) {
return execute(null, cmd, ffmpegExecuteResponseHandler);
Expand Down
84 changes: 6 additions & 78 deletions android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FFprobe.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import java.util.Map;

public class FFprobe implements FFbinaryInterface {
private static final int VERSION = 17; // up this version when you add a new ffprobe build
private static final String KEY_PREF_VERSION = "ffprobe_version";

private final FFbinaryContextProvider context;

Expand Down Expand Up @@ -40,79 +38,21 @@ public Context provide() {

@Override
public boolean isSupported() {
// check if arch is supported
CpuArch cpuArch = CpuArchHelper.getCpuArch();
if (cpuArch == CpuArch.NONE) {
Log.e("arch not supported");
File fFprobe = FileUtils.getFFprobe(context.provide());
if (!fFprobe.canExecute()) {
Log.e("ffprobe cannot execute");
return false;
}

// get ffprobe file
File ffprobe = FileUtils.getFFprobe(context.provide());

SharedPreferences settings = context.provide().getSharedPreferences("ffmpeg_prefs", Context.MODE_PRIVATE);
int version = settings.getInt(KEY_PREF_VERSION, 0);

// check if ffprobe file exists
if (!ffprobe.exists() || version < VERSION) {
String prefix = "arm/";
if (cpuArch == CpuArch.x86) {
prefix = "x86/";
}
Log.d("file does not exist, creating it...");

try {
InputStream inputStream = context.provide().getAssets().open(prefix + "ffprobe");
if (!FileUtils.inputStreamToFile(inputStream, ffprobe)) {
return false;
}

Log.d("successfully wrote ffprobe file!");

settings.edit().putInt(KEY_PREF_VERSION, VERSION).apply();
} catch (IOException e) {
Log.e("error while opening assets", e);
return false;
}
}

// check if ffprobe can be executed
if (!ffprobe.canExecute()) {
// try to make executable
try {
try {
Runtime.getRuntime().exec("chmod -R 777 " + ffprobe.getAbsolutePath()).waitFor();
} catch (InterruptedException e) {
Log.e("interrupted exception", e);
return false;
} catch (IOException e) {
Log.e("io exception", e);
return false;
}

if (!ffprobe.canExecute()) {
// our last hope!
if (!ffprobe.setExecutable(true)) {
Log.e("unable to make executable");
return false;
}
}
} catch (SecurityException e) {
Log.e("security exception", e);
return false;
}
}

Log.d("ffprobe is ready!");

return true;
}

@Override
public FFtask execute(Map<String, String> environvenmentVars, String[] cmd, FFcommandExecuteResponseHandler ffcommandExecuteResponseHandler) {
if (cmd.length != 0) {
String[] ffprobeBinary = new String[]{FileUtils.getFFprobe(context.provide()).getAbsolutePath()};
String[] command = concatenate(ffprobeBinary, cmd);
final String[] command = new String[cmd.length + 1];
command[0] = FileUtils.getFFmpeg(context.provide()).getAbsolutePath();
System.arraycopy(cmd, 0, command, 1, cmd.length);
FFcommandExecuteAsyncTask task = new FFcommandExecuteAsyncTask(command, environvenmentVars, timeout, ffcommandExecuteResponseHandler);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return task;
Expand All @@ -121,18 +61,6 @@ public FFtask execute(Map<String, String> environvenmentVars, String[] cmd, FFco
}
}

private static <T> T[] concatenate(T[] a, T[] b) {
int aLen = a.length;
int bLen = b.length;

@SuppressWarnings("unchecked")
T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen + bLen);
System.arraycopy(a, 0, c, 0, aLen);
System.arraycopy(b, 0, c, aLen, bLen);

return c;
}

@Override
public FFtask execute(String[] cmd, FFcommandExecuteResponseHandler ffcommandExecuteResponseHandler) {
return execute(null, cmd, ffcommandExecuteResponseHandler);
Expand Down
23 changes: 2 additions & 21 deletions android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,12 @@ class FileUtils {
private static final String FFPROBE_FILE_NAME = "ffprobe";

static File getFFmpeg(Context context) {
File folder = context.getFilesDir();
File folder = new File(context.getApplicationInfo().nativeLibraryDir);
return new File(folder, FFMPEG_FILE_NAME);
}

static File getFFprobe(Context context) {
File folder = context.getFilesDir();
File folder = new File(context.getApplicationInfo().nativeLibraryDir);
return new File(folder, FFPROBE_FILE_NAME);
}

static boolean inputStreamToFile(InputStream stream, File file) {
try {
InputStream input = new BufferedInputStream(stream);
OutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
output.flush();
output.close();
input.close();
return true;
} catch (IOException e) {
Log.e("error while writing ff binary file", e);
}
return false;
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added android-ffmpeg/src/main/resources/lib/x86/ffmpeg
Binary file not shown.
Binary file added android-ffmpeg/src/main/resources/lib/x86/ffprobe
Binary file not shown.
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
compileSdkVersion 29

defaultConfig {
applicationId "nl.bravobit.ffmpeg.example"
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0.1"
}
Expand Down