4
4
import android .os .AsyncTask ;
5
5
6
6
import java .io .File ;
7
+ import java .io .FileOutputStream ;
8
+ import java .io .IOException ;
9
+ import java .io .InputStream ;
10
+ import java .io .OutputStream ;
11
+ import java .net .HttpURLConnection ;
12
+ import java .net .URL ;
7
13
8
- class FFmpegLoadLibraryAsyncTask extends AsyncTask <Void , Void , Boolean > {
14
+ class FFmpegLoadLibraryAsyncTask extends AsyncTask <Void , Integer , Boolean > {
9
15
16
+ private final String baseUrl ;
10
17
private final String cpuArchNameFromAssets ;
11
18
private final FFmpegLoadBinaryResponseHandler ffmpegLoadBinaryResponseHandler ;
12
19
private final Context context ;
13
20
14
- FFmpegLoadLibraryAsyncTask (Context context , String cpuArchNameFromAssets , FFmpegLoadBinaryResponseHandler ffmpegLoadBinaryResponseHandler ) {
21
+ FFmpegLoadLibraryAsyncTask (Context context , String baseUrl , String cpuArchNameFromAssets , FFmpegLoadBinaryResponseHandler ffmpegLoadBinaryResponseHandler ) {
15
22
this .context = context ;
23
+ this .baseUrl = baseUrl ;
16
24
this .cpuArchNameFromAssets = cpuArchNameFromAssets ;
17
25
this .ffmpegLoadBinaryResponseHandler = ffmpegLoadBinaryResponseHandler ;
18
26
}
19
27
20
28
@ Override
21
29
protected Boolean doInBackground (Void ... params ) {
22
30
File ffmpegFile = new File (FileUtils .getFFmpeg (context ));
23
- if (ffmpegFile .exists () && isDeviceFFmpegVersionOld () && !ffmpegFile .delete ()) {
24
- return false ;
25
- }
31
+
26
32
if (!ffmpegFile .exists ()) {
27
- boolean isFileCopied = FileUtils .copyBinaryFromAssetsToData (context ,
28
- cpuArchNameFromAssets + File .separator + FileUtils .ffmpegFileName ,
29
- FileUtils .ffmpegFileName );
33
+ InputStream input = null ;
34
+ OutputStream output = null ;
35
+ HttpURLConnection connection = null ;
36
+
37
+ try {
38
+ URL url = new URL (baseUrl + "/" + cpuArchNameFromAssets + "/" + FileUtils .ffmpegFileName );
39
+ connection = (HttpURLConnection ) url .openConnection ();
40
+ connection .connect ();
41
+
42
+ if (connection .getResponseCode () != HttpURLConnection .HTTP_OK )
43
+ return false ;
44
+
45
+ int fileLength = connection .getContentLength ();
46
+ input = connection .getInputStream ();
47
+ output = new FileOutputStream (ffmpegFile , false );
48
+
49
+ byte data [] = new byte [8192 ];
50
+ long total = 0 ;
51
+ int count ;
52
+
53
+ while ((count = input .read (data )) != -1 ) {
54
+ total += count ;
55
+
56
+ if (fileLength > 0 )
57
+ publishProgress ((int ) (total * 100 / fileLength ));
58
+
59
+ output .write (data , 0 , count );
60
+ }
30
61
31
- // make file executable
32
- if (isFileCopied ) {
33
62
if (!ffmpegFile .canExecute ()) {
34
- Log .d ("FFmpeg is not executable, trying to make it executable ..." );
35
63
if (ffmpegFile .setExecutable (true )) {
36
64
return true ;
37
65
}
38
- } else {
39
- Log .d ("FFmpeg is executable" );
40
- return true ;
41
66
}
67
+
68
+ return true ;
69
+
70
+ } catch (Exception e ) {
71
+
72
+ return false ;
73
+
74
+ } finally {
75
+ try {
76
+ if (output != null )
77
+ output .close ();
78
+ if (input != null )
79
+ input .close ();
80
+ } catch (IOException e ) {
81
+ }
82
+
83
+ if (connection != null )
84
+ connection .disconnect ();
42
85
}
43
86
}
87
+
44
88
return ffmpegFile .exists () && ffmpegFile .canExecute ();
45
89
}
46
90
@@ -57,7 +101,9 @@ protected void onPostExecute(Boolean isSuccess) {
57
101
}
58
102
}
59
103
60
- private boolean isDeviceFFmpegVersionOld () {
61
- return CpuArch .fromString (FileUtils .SHA1 (FileUtils .getFFmpeg (context ))).equals (CpuArch .NONE );
104
+ @ Override
105
+ protected void onProgressUpdate (Integer ... values ) {
106
+ super .onProgressUpdate (values );
107
+ ffmpegLoadBinaryResponseHandler .onProgress (values [0 ]);
62
108
}
63
109
}
0 commit comments