2727import java .io .FileDescriptor ;
2828import java .io .FileInputStream ;
2929import java .io .IOException ;
30+ import java .util .concurrent .Callable ;
3031import java .util .concurrent .Future ;
3132import java .util .concurrent .LinkedBlockingQueue ;
3233import java .util .concurrent .ThreadFactory ;
3334import java .util .concurrent .ThreadPoolExecutor ;
3435import java .util .concurrent .TimeUnit ;
36+ import java .util .concurrent .atomic .AtomicReference ;
3537
3638public class MediaTranscoder {
3739 private static final String TAG = "MediaTranscoder" ;
3840 private static final int MAXIMUM_THREAD = 1 ; // TODO
3941 private static volatile MediaTranscoder sMediaTranscoder ;
40- private Future mFuture ;
4142 private ThreadPoolExecutor mExecutor ;
4243
4344 private MediaTranscoder () {
44- mFuture = null ;
4545 mExecutor = new ThreadPoolExecutor (
4646 0 , MAXIMUM_THREAD , 60 , TimeUnit .SECONDS ,
4747 new LinkedBlockingQueue <Runnable >(),
@@ -74,8 +74,8 @@ public static MediaTranscoder getInstance() {
7474 * @deprecated Use {@link #transcodeVideo(FileDescriptor, String, MediaFormatStrategy, MediaTranscoder.Listener)} which accepts output video format.
7575 */
7676 @ 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 () {
7979 @ Override
8080 public MediaFormat createVideoOutputFormat (MediaFormat inputFormat ) {
8181 return MediaFormatPresets .getExportPreset960x540 ();
@@ -98,7 +98,7 @@ public MediaFormat createAudioOutputFormat(MediaFormat inputFormat) {
9898 * @param listener Listener instance for callback.
9999 * @throws IOException if input file could not be read.
100100 */
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 {
102102 FileInputStream fileInputStream = null ;
103103 FileDescriptor inFileDescriptor ;
104104 try {
@@ -115,7 +115,7 @@ public void transcodeVideo(final String inPath, final String outPath, final Medi
115115 throw e ;
116116 }
117117 final FileInputStream finalFileInputStream = fileInputStream ;
118- transcodeVideo (inFileDescriptor , outPath , outFormatStrategy , new Listener () {
118+ return transcodeVideo (inFileDescriptor , outPath , outFormatStrategy , new Listener () {
119119 @ Override
120120 public void onTranscodeProgress (double progress ) {
121121 listener .onTranscodeProgress (progress );
@@ -158,13 +158,14 @@ private void closeStream() {
158158 * @param outFormatStrategy Strategy for output video format.
159159 * @param listener Listener instance for callback.
160160 */
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 ) {
162162 Looper looper = Looper .myLooper ();
163163 if (looper == null ) looper = Looper .getMainLooper ();
164164 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 >() {
166167 @ Override
167- public void run () {
168+ public Void call () throws Exception {
168169 Exception caughtException = null ;
169170 try {
170171 MediaTranscoderEngine engine = new MediaTranscoderEngine ();
@@ -200,23 +201,22 @@ public void run() {
200201 if (exception == null ) {
201202 listener .onTranscodeCompleted ();
202203 } else {
203- if (exception instanceof InterruptedException ) {
204+ Future future = futureReference .get ();
205+ if (future != null && future .isCancelled ()) {
204206 listener .onTranscodeCanceled ();
205207 } else {
206208 listener .onTranscodeFailed (exception );
207209 }
208210 }
209211 }
210212 });
213+ 214+ if (exception != null ) throw exception ;
215+ return null ;
211216 }
212217 });
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 ;
220220 }
221221
222222 public interface Listener {
0 commit comments