27
27
import java .io .FileDescriptor ;
28
28
import java .io .FileInputStream ;
29
29
import java .io .IOException ;
30
+ import java .util .concurrent .Callable ;
30
31
import java .util .concurrent .Future ;
31
32
import java .util .concurrent .LinkedBlockingQueue ;
32
33
import java .util .concurrent .ThreadFactory ;
33
34
import java .util .concurrent .ThreadPoolExecutor ;
34
35
import java .util .concurrent .TimeUnit ;
36
+ import java .util .concurrent .atomic .AtomicReference ;
35
37
36
38
public class MediaTranscoder {
37
39
private static final String TAG = "MediaTranscoder" ;
38
40
private static final int MAXIMUM_THREAD = 1 ; // TODO
39
41
private static volatile MediaTranscoder sMediaTranscoder ;
40
- private Future mFuture ;
41
42
private ThreadPoolExecutor mExecutor ;
42
43
43
44
private MediaTranscoder () {
44
- mFuture = null ;
45
45
mExecutor = new ThreadPoolExecutor (
46
46
0 , MAXIMUM_THREAD , 60 , TimeUnit .SECONDS ,
47
47
new LinkedBlockingQueue <Runnable >(),
@@ -74,8 +74,8 @@ public static MediaTranscoder getInstance() {
74
74
* @deprecated Use {@link #transcodeVideo(FileDescriptor, String, MediaFormatStrategy, MediaTranscoder.Listener)} which accepts output video format.
75
75
*/
76
76
@ Deprecated
77
- public void transcodeVideo (final FileDescriptor inFileDescriptor , final String outPath , final Listener listener ) {
78
- transcodeVideo (inFileDescriptor , outPath , new MediaFormatStrategy () {
77
+ public Future transcodeVideo (final FileDescriptor inFileDescriptor , final String outPath , final Listener listener ) {
78
+ return transcodeVideo (inFileDescriptor , outPath , new MediaFormatStrategy () {
79
79
@ Override
80
80
public MediaFormat createVideoOutputFormat (MediaFormat inputFormat ) {
81
81
return MediaFormatPresets .getExportPreset960x540 ();
@@ -98,7 +98,7 @@ public MediaFormat createAudioOutputFormat(MediaFormat inputFormat) {
98
98
* @param listener Listener instance for callback.
99
99
* @throws IOException if input file could not be read.
100
100
*/
101
- public void transcodeVideo (final String inPath , final String outPath , final MediaFormatStrategy outFormatStrategy , final Listener listener ) throws IOException {
101
+ public Future transcodeVideo (final String inPath , final String outPath , final MediaFormatStrategy outFormatStrategy , final Listener listener ) throws IOException {
102
102
FileInputStream fileInputStream = null ;
103
103
FileDescriptor inFileDescriptor ;
104
104
try {
@@ -115,7 +115,7 @@ public void transcodeVideo(final String inPath, final String outPath, final Medi
115
115
throw e ;
116
116
}
117
117
final FileInputStream finalFileInputStream = fileInputStream ;
118
- transcodeVideo (inFileDescriptor , outPath , outFormatStrategy , new Listener () {
118
+ return transcodeVideo (inFileDescriptor , outPath , outFormatStrategy , new Listener () {
119
119
@ Override
120
120
public void onTranscodeProgress (double progress ) {
121
121
listener .onTranscodeProgress (progress );
@@ -158,13 +158,14 @@ private void closeStream() {
158
158
* @param outFormatStrategy Strategy for output video format.
159
159
* @param listener Listener instance for callback.
160
160
*/
161
- public void transcodeVideo (final FileDescriptor inFileDescriptor , final String outPath , final MediaFormatStrategy outFormatStrategy , final Listener listener ) {
161
+ public Future transcodeVideo (final FileDescriptor inFileDescriptor , final String outPath , final MediaFormatStrategy outFormatStrategy , final Listener listener ) {
162
162
Looper looper = Looper .myLooper ();
163
163
if (looper == null ) looper = Looper .getMainLooper ();
164
164
final Handler handler = new Handler (looper );
165
- mFuture = mExecutor .submit (new Runnable () {
165
+ final AtomicReference <Future > futureReference = new AtomicReference <>();
166
+ final Future <Void > createdFuture = mExecutor .submit (new Callable <Void >() {
166
167
@ Override
167
- public void run () {
168
+ public Void call () throws Exception {
168
169
Exception caughtException = null ;
169
170
try {
170
171
MediaTranscoderEngine engine = new MediaTranscoderEngine ();
@@ -200,23 +201,22 @@ public void run() {
200
201
if (exception == null ) {
201
202
listener .onTranscodeCompleted ();
202
203
} else {
203
- if (exception instanceof InterruptedException ) {
204
+ Future future = futureReference .get ();
205
+ if (future != null && future .isCancelled ()) {
204
206
listener .onTranscodeCanceled ();
205
207
} else {
206
208
listener .onTranscodeFailed (exception );
207
209
}
208
210
}
209
211
}
210
212
});
213
+
214
+ if (exception != null ) throw exception ;
215
+ return null ;
211
216
}
212
217
});
213
- }
214
-
215
- /**
216
- * Cancel transcode video file
217
- */
218
- public boolean cancel () {
219
- return mFuture != null ? mFuture .cancel (true ) : false ;
218
+ futureReference .set (createdFuture );
219
+ return createdFuture ;
220
220
}
221
221
222
222
public interface Listener {
0 commit comments