Skip to content

Commit 75befd4

Browse files
alexcohnIrad Cohen
authored and
Irad Cohen
committed
let it run on Android Q
see bravobit#126
1 parent a28d6f9 commit 75befd4

File tree

11 files changed

+32
-236
lines changed

11 files changed

+32
-236
lines changed

android-ffmpeg/build.gradle

+9-19
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ ext {
2222
}
2323

2424
android {
25-
compileSdkVersion 27
25+
compileSdkVersion 29
2626

2727
defaultConfig {
2828
minSdkVersion 16
29-
targetSdkVersion 27
29+
targetSdkVersion 29
3030
versionCode 15
3131
versionName "1.1.5"
3232
}
@@ -46,9 +46,7 @@ android {
4646
}
4747

4848
dependencies {
49-
implementation fileTree(dir: 'libs', include: ['*.jar'])
50-
51-
implementation 'com.android.support:appcompat-v7:27.1.1'
49+
implementation 'androidx.appcompat:appcompat:1.1.0'
5250
}
5351

5452
apply plugin: 'com.github.dcendents.android-maven'
@@ -96,21 +94,13 @@ apply plugin: 'com.jfrog.bintray'
9694

9795
version = libraryVersion
9896

99-
if (project.hasProperty("android")) { // Android libraries
100-
task sourcesJar(type: Jar) {
101-
classifier = 'sources'
102-
from android.sourceSets.main.java.srcDirs
103-
}
97+
task sourcesJar(type: Jar) {
98+
from android.sourceSets.main.java.srcDirs
99+
}
104100

105-
task javadoc(type: Javadoc) {
106-
source = android.sourceSets.main.java.srcDirs
107-
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
108-
}
109-
} else { // Java libraries
110-
task sourcesJar(type: Jar, dependsOn: classes) {
111-
classifier = 'sources'
112-
from sourceSets.main.allSource
113-
}
101+
task javadoc(type: Javadoc) {
102+
source = android.sourceSets.main.java.srcDirs
103+
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
114104
}
115105

116106
task javadocJar(type: Jar, dependsOn: javadoc) {

android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/CpuArch.java

-5
This file was deleted.

android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/CpuArchHelper.java

-25
This file was deleted.

android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FFmpeg.java

+5-74
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package nl.bravobit.ffmpeg;
22

33
import android.content.Context;
4-
import android.content.SharedPreferences;
54
import android.os.AsyncTask;
65

76
import java.io.File;
8-
import java.io.IOException;
9-
import java.io.InputStream;
10-
import java.lang.reflect.Array;
117
import java.util.Map;
128

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

1711
private final FFbinaryContextProvider context;
1812

@@ -40,67 +34,15 @@ public Context provide() {
4034

4135
@Override
4236
public boolean isSupported() {
43-
// check if arch is supported
44-
CpuArch cpuArch = CpuArchHelper.getCpuArch();
45-
if (cpuArch == CpuArch.NONE) {
46-
Log.e("arch not supported");
47-
return false;
48-
}
4937

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

53-
SharedPreferences settings = context.provide().getSharedPreferences("ffmpeg_prefs", Context.MODE_PRIVATE);
54-
int version = settings.getInt(KEY_PREF_VERSION, 0);
55-
56-
// check if ffmpeg file exists
57-
if (!ffmpeg.exists() || version < VERSION) {
58-
String prefix = "arm/";
59-
if (cpuArch == CpuArch.x86) {
60-
prefix = "x86/";
61-
}
62-
Log.d("file does not exist, creating it...");
63-
64-
try {
65-
InputStream inputStream = context.provide().getAssets().open(prefix + "ffmpeg");
66-
if (!FileUtils.inputStreamToFile(inputStream, ffmpeg)) {
67-
return false;
68-
}
69-
70-
Log.d("successfully wrote ffmpeg file!");
71-
72-
settings.edit().putInt(KEY_PREF_VERSION, VERSION).apply();
73-
} catch (IOException e) {
74-
Log.e("error while opening assets", e);
75-
return false;
76-
}
77-
}
78-
7941
// check if ffmpeg can be executed
8042
if (!ffmpeg.canExecute()) {
8143
// try to make executable
82-
try {
83-
try {
84-
Runtime.getRuntime().exec("chmod -R 777 " + ffmpeg.getAbsolutePath()).waitFor();
85-
} catch (InterruptedException e) {
86-
Log.e("interrupted exception", e);
87-
return false;
88-
} catch (IOException e) {
89-
Log.e("io exception", e);
90-
return false;
91-
}
92-
93-
if (!ffmpeg.canExecute()) {
94-
// our last hope!
95-
if (!ffmpeg.setExecutable(true)) {
96-
Log.e("unable to make executable");
97-
return false;
98-
}
99-
}
100-
} catch (SecurityException e) {
101-
Log.e("security exception", e);
102-
return false;
103-
}
44+
Log.e("ffmpeg cannot execute");
45+
return false;
10446
}
10547

10648
Log.d("ffmpeg is ready!");
@@ -111,8 +53,9 @@ public boolean isSupported() {
11153
@Override
11254
public FFtask execute(Map<String, String> environvenmentVars, String[] cmd, FFcommandExecuteResponseHandler ffmpegExecuteResponseHandler) {
11355
if (cmd.length != 0) {
114-
String[] ffmpegBinary = new String[]{FileUtils.getFFmpeg(context.provide()).getAbsolutePath()};
115-
String[] command = concatenate(ffmpegBinary, cmd);
56+
final String[] command = new String[cmd.length + 1];
57+
command[0] = FileUtils.getFFmpeg(context.provide()).getAbsolutePath();
58+
System.arraycopy(cmd, 0, command, 1, cmd.length);
11659
FFcommandExecuteAsyncTask task = new FFcommandExecuteAsyncTask(command, environvenmentVars, timeout, ffmpegExecuteResponseHandler);
11760
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
11861
return task;
@@ -121,18 +64,6 @@ public FFtask execute(Map<String, String> environvenmentVars, String[] cmd, FFco
12164
}
12265
}
12366

124-
private static <T> T[] concatenate(T[] a, T[] b) {
125-
int aLen = a.length;
126-
int bLen = b.length;
127-
128-
@SuppressWarnings("unchecked")
129-
T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen + bLen);
130-
System.arraycopy(a, 0, c, 0, aLen);
131-
System.arraycopy(b, 0, c, aLen, bLen);
132-
133-
return c;
134-
}
135-
13667
@Override
13768
public FFtask execute(String[] cmd, FFcommandExecuteResponseHandler ffmpegExecuteResponseHandler) {
13869
return execute(null, cmd, ffmpegExecuteResponseHandler);

android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FFprobe.java

+5-76
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package nl.bravobit.ffmpeg;
22

33
import android.content.Context;
4-
import android.content.SharedPreferences;
54
import android.os.AsyncTask;
65

76
import java.io.File;
8-
import java.io.IOException;
9-
import java.io.InputStream;
10-
import java.lang.reflect.Array;
117
import java.util.Map;
128

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

1711
private final FFbinaryContextProvider context;
1812

@@ -40,67 +34,13 @@ public Context provide() {
4034

4135
@Override
4236
public boolean isSupported() {
43-
// check if arch is supported
44-
CpuArch cpuArch = CpuArchHelper.getCpuArch();
45-
if (cpuArch == CpuArch.NONE) {
46-
Log.e("arch not supported");
47-
return false;
48-
}
49-
5037
// get ffprobe file
5138
File ffprobe = FileUtils.getFFprobe(context.provide());
5239

53-
SharedPreferences settings = context.provide().getSharedPreferences("ffmpeg_prefs", Context.MODE_PRIVATE);
54-
int version = settings.getInt(KEY_PREF_VERSION, 0);
55-
56-
// check if ffprobe file exists
57-
if (!ffprobe.exists() || version < VERSION) {
58-
String prefix = "arm/";
59-
if (cpuArch == CpuArch.x86) {
60-
prefix = "x86/";
61-
}
62-
Log.d("file does not exist, creating it...");
63-
64-
try {
65-
InputStream inputStream = context.provide().getAssets().open(prefix + "ffprobe");
66-
if (!FileUtils.inputStreamToFile(inputStream, ffprobe)) {
67-
return false;
68-
}
69-
70-
Log.d("successfully wrote ffprobe file!");
71-
72-
settings.edit().putInt(KEY_PREF_VERSION, VERSION).apply();
73-
} catch (IOException e) {
74-
Log.e("error while opening assets", e);
75-
return false;
76-
}
77-
}
78-
7940
// check if ffprobe can be executed
8041
if (!ffprobe.canExecute()) {
81-
// try to make executable
82-
try {
83-
try {
84-
Runtime.getRuntime().exec("chmod -R 777 " + ffprobe.getAbsolutePath()).waitFor();
85-
} catch (InterruptedException e) {
86-
Log.e("interrupted exception", e);
87-
return false;
88-
} catch (IOException e) {
89-
Log.e("io exception", e);
90-
return false;
91-
}
92-
93-
if (!ffprobe.canExecute()) {
94-
// our last hope!
95-
if (!ffprobe.setExecutable(true)) {
96-
Log.e("unable to make executable");
97-
return false;
98-
}
99-
}
100-
} catch (SecurityException e) {
101-
Log.e("security exception", e);
102-
return false;
103-
}
42+
Log.e("ffprobe cannot execute");
43+
return false;
10444
}
10545

10646
Log.d("ffprobe is ready!");
@@ -111,8 +51,9 @@ public boolean isSupported() {
11151
@Override
11252
public FFtask execute(Map<String, String> environvenmentVars, String[] cmd, FFcommandExecuteResponseHandler ffcommandExecuteResponseHandler) {
11353
if (cmd.length != 0) {
114-
String[] ffprobeBinary = new String[]{FileUtils.getFFprobe(context.provide()).getAbsolutePath()};
115-
String[] command = concatenate(ffprobeBinary, cmd);
54+
final String[] command = new String[cmd.length + 1];
55+
command[0] = FileUtils.getFFprobe(context.provide()).getAbsolutePath();
56+
System.arraycopy(cmd, 0, command, 1, cmd.length);
11657
FFcommandExecuteAsyncTask task = new FFcommandExecuteAsyncTask(command, environvenmentVars, timeout, ffcommandExecuteResponseHandler);
11758
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
11859
return task;
@@ -121,18 +62,6 @@ public FFtask execute(Map<String, String> environvenmentVars, String[] cmd, FFco
12162
}
12263
}
12364

124-
private static <T> T[] concatenate(T[] a, T[] b) {
125-
int aLen = a.length;
126-
int bLen = b.length;
127-
128-
@SuppressWarnings("unchecked")
129-
T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen + bLen);
130-
System.arraycopy(a, 0, c, 0, aLen);
131-
System.arraycopy(b, 0, c, aLen, bLen);
132-
133-
return c;
134-
}
135-
13665
@Override
13766
public FFtask execute(String[] cmd, FFcommandExecuteResponseHandler ffcommandExecuteResponseHandler) {
13867
return execute(null, cmd, ffcommandExecuteResponseHandler);

android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FileUtils.java

+4-28
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,19 @@
22

33
import android.content.Context;
44

5-
import java.io.BufferedInputStream;
65
import java.io.File;
7-
import java.io.FileOutputStream;
8-
import java.io.IOException;
9-
import java.io.InputStream;
10-
import java.io.OutputStream;
116

127
class FileUtils {
13-
private static final String FFMPEG_FILE_NAME = "ffmpeg";
14-
private static final String FFPROBE_FILE_NAME = "ffprobe";
8+
private static final String FFMPEG_FILE_NAME = "lib..ffmpeg..so";
9+
private static final String FFPROBE_FILE_NAME = "lib..ffprobe.so";
1510

1611
static File getFFmpeg(Context context) {
17-
File folder = new File(context.getApplicationInfo().nativeLibraryDir);
12+
File folder = new File(context.getApplicationInfo().nativeLibraryDir);
1813
return new File(folder, FFMPEG_FILE_NAME);
1914
}
2015

2116
static File getFFprobe(Context context) {
22-
File folder = new File(context.getApplicationInfo().nativeLibraryDir);
17+
File folder = new File(context.getApplicationInfo().nativeLibraryDir);
2318
return new File(folder, FFPROBE_FILE_NAME);
2419
}
25-
26-
static boolean inputStreamToFile(InputStream stream, File file) {
27-
try {
28-
InputStream input = new BufferedInputStream(stream);
29-
OutputStream output = new FileOutputStream(file);
30-
byte[] buffer = new byte[1024];
31-
int bytesRead;
32-
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
33-
output.write(buffer, 0, bytesRead);
34-
}
35-
output.flush();
36-
output.close();
37-
input.close();
38-
return true;
39-
} catch (IOException e) {
40-
Log.e("error while writing ff binary file", e);
41-
}
42-
return false;
43-
}
4420
}

build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ buildscript {
66
jcenter()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.1.2'
10-
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
11-
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
9+
classpath 'com.android.tools.build:gradle:3.4.2'
10+
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
11+
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
1212
}
1313
}
1414

gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

0 commit comments

Comments
 (0)