Skip to content

Commit a205008

Browse files
committed
Fixed singleton context issue and kill process bug (hopefully)
1 parent 6fd8356 commit a205008

File tree

9 files changed

+35
-20
lines changed

9 files changed

+35
-20
lines changed

FFmpegAndroid/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ android {
2828
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2929
}
3030
}
31+
buildToolsVersion '25.0.0'
3132
}
3233

3334
dependencies {

FFmpegAndroid/src/main/java/com/github/hiteshsondhi88/libffmpeg/FFmpeg.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@SuppressWarnings("unused")
1313
public class FFmpeg implements FFmpegInterface {
1414

15-
private final Context context;
15+
private final FFmpegContextProvider context;
1616
private FFmpegExecuteAsyncTask ffmpegExecuteAsyncTask;
1717
private FFmpegLoadLibraryAsyncTask ffmpegLoadLibraryAsyncTask;
1818

@@ -21,14 +21,14 @@ public class FFmpeg implements FFmpegInterface {
2121

2222
private static FFmpeg instance = null;
2323

24-
private FFmpeg(Context context) {
25-
this.context = context.getApplicationContext();
26-
Log.setDEBUG(Util.isDebug(this.context));
24+
private FFmpeg(FFmpegContextProvider contextProvider) {
25+
this.context = contextProvider;
26+
Log.setDEBUG(Util.isDebug(this.context.provide()));
2727
}
2828

29-
public static FFmpeg getInstance(Context context) {
29+
public static FFmpeg getInstance(FFmpegContextProvider contextProvider) {
3030
if (instance == null) {
31-
instance = new FFmpeg(context);
31+
instance = new FFmpeg(contextProvider);
3232
}
3333
return instance;
3434
}
@@ -63,7 +63,7 @@ public void execute(Map<String, String> environvenmentVars, String[] cmd, FFmpeg
6363
throw new FFmpegCommandAlreadyRunningException("FFmpeg command is already running, you are only allowed to run single command at a time");
6464
}
6565
if (cmd.length != 0) {
66-
String[] ffmpegBinary = new String[] { FileUtils.getFFmpeg(context, environvenmentVars) };
66+
String[] ffmpegBinary = new String[] { FileUtils.getFFmpeg(context.provide(), environvenmentVars) };
6767
String[] command = concatenate(ffmpegBinary, cmd);
6868
ffmpegExecuteAsyncTask = new FFmpegExecuteAsyncTask(command , timeout, ffmpegExecuteResponseHandler);
6969
ffmpegExecuteAsyncTask.execute();
@@ -92,7 +92,7 @@ public void execute(String[] cmd, FFmpegExecuteResponseHandler ffmpegExecuteResp
9292
@Override
9393
public String getDeviceFFmpegVersion() throws FFmpegCommandAlreadyRunningException {
9494
ShellCommand shellCommand = new ShellCommand();
95-
CommandResult commandResult = shellCommand.runWaitFor(new String[] { FileUtils.getFFmpeg(context), "-version" });
95+
CommandResult commandResult = shellCommand.runWaitFor(new String[] { FileUtils.getFFmpeg(context.provide()), "-version" });
9696
if (commandResult.success) {
9797
return commandResult.output.split(" ")[2];
9898
}
@@ -102,7 +102,7 @@ public String getDeviceFFmpegVersion() throws FFmpegCommandAlreadyRunningExcepti
102102

103103
@Override
104104
public String getLibraryFFmpegVersion() {
105-
return context.getString(R.string.shipped_ffmpeg_version);
105+
return context.provide().getString(R.string.shipped_ffmpeg_version);
106106
}
107107

108108
@Override
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.hiteshsondhi88.libffmpeg;
2+
3+
import android.content.Context;
4+
5+
interface FFmpegContextProvider {
6+
Context provide();
7+
}

FFmpegAndroid/src/main/java/com/github/hiteshsondhi88/libffmpeg/FFmpegExecuteAsyncTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ private void checkAndUpdateProcess() throws TimeoutException, InterruptedExcepti
9090
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
9191
while ((line = reader.readLine()) != null) {
9292
if (isCancelled()) {
93+
process.destroy();
9394
return;
9495
}
9596

FFmpegAndroid/src/main/java/com/github/hiteshsondhi88/libffmpeg/FFmpegLoadLibraryAsyncTask.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ class FFmpegLoadLibraryAsyncTask extends AsyncTask<Void, Void, Boolean> {
99

1010
private final String cpuArchNameFromAssets;
1111
private final FFmpegLoadBinaryResponseHandler ffmpegLoadBinaryResponseHandler;
12-
private final Context context;
12+
private final FFmpegContextProvider context;
1313

14-
FFmpegLoadLibraryAsyncTask(Context context, String cpuArchNameFromAssets, FFmpegLoadBinaryResponseHandler ffmpegLoadBinaryResponseHandler) {
14+
FFmpegLoadLibraryAsyncTask(FFmpegContextProvider context, String cpuArchNameFromAssets, FFmpegLoadBinaryResponseHandler ffmpegLoadBinaryResponseHandler) {
1515
this.context = context;
1616
this.cpuArchNameFromAssets = cpuArchNameFromAssets;
1717
this.ffmpegLoadBinaryResponseHandler = ffmpegLoadBinaryResponseHandler;
1818
}
1919

2020
@Override
2121
protected Boolean doInBackground(Void... params) {
22-
File ffmpegFile = new File(FileUtils.getFFmpeg(context));
22+
File ffmpegFile = new File(FileUtils.getFFmpeg(context.provide()));
2323
if (ffmpegFile.exists() && isDeviceFFmpegVersionOld() && !ffmpegFile.delete()) {
2424
return false;
2525
}
2626
if (!ffmpegFile.exists()) {
27-
boolean isFileCopied = FileUtils.copyBinaryFromAssetsToData(context,
27+
boolean isFileCopied = FileUtils.copyBinaryFromAssetsToData(context.provide(),
2828
cpuArchNameFromAssets + File.separator + FileUtils.ffmpegFileName,
2929
FileUtils.ffmpegFileName);
3030

@@ -58,6 +58,6 @@ protected void onPostExecute(Boolean isSuccess) {
5858
}
5959

6060
private boolean isDeviceFFmpegVersionOld() {
61-
return CpuArch.fromString(FileUtils.SHA1(FileUtils.getFFmpeg(context))).equals(CpuArch.NONE);
61+
return CpuArch.fromString(FileUtils.SHA1(FileUtils.getFFmpeg(context.provide()))).equals(CpuArch.NONE);
6262
}
6363
}

FFmpegAndroid/src/main/java/com/github/hiteshsondhi88/libffmpeg/Util.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ static String convertInputStreamToString(InputStream inputStream) {
5353
}
5454

5555
static void destroyProcess(Process process) {
56-
if (process != null)
57-
process.destroy();
56+
if (process != null) {
57+
try {
58+
process.destroy();
59+
} catch (Exception e) {
60+
Log.e("process destroy error", e);
61+
}
62+
}
5863
}
5964

6065
static boolean killAsync(AsyncTask asyncTask) {

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ android {
2828
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2929
}
3030
}
31+
buildToolsVersion '25.0.0'
3132
}
3233

3334
dependencies {

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.1.0-beta3'
9-
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
8+
classpath 'com.android.tools.build:gradle:3.0.0-alpha7'
9+
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
1010
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1"
1111

1212
// NOTE: Do not place your application dependencies here; they belong
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Apr 20 11:52:28 GMT+05:30 2016
1+
#Wed Jul 19 16:37:50 EET 2017
22
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-2.10-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-milestone-1-all.zip

0 commit comments

Comments
 (0)