Skip to content

Commit 41d68e9

Browse files
author
MaiKambayashi
committed
return Future and add error handling
1 parent 6ec16c2 commit 41d68e9

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

example/src/main/java/net/ypresto/androidtranscoder/example/TranscoderActivity.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
import java.io.FileDescriptor;
2222
import java.io.FileNotFoundException;
2323
import java.io.IOException;
24+
import java.util.concurrent.Future;
2425

2526

2627
public class TranscoderActivity extends Activity {
2728
private static final String TAG = "TranscoderActivity";
2829
private static final int REQUEST_CODE_PICK = 1;
2930
private static final int PROGRESS_BAR_MAX = 1000;
31+
private Future mFuture;
3032

3133
@Override
3234
protected void onCreate(Bundle savedInstanceState) {
@@ -41,7 +43,7 @@ public void onClick(View v) {
4143
findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
4244
@Override
4345
public void onClick(View view) {
44-
MediaTranscoder.getInstance().cancel();
46+
mFuture.cancel(true);
4547
}
4648
});
4749
}
@@ -86,22 +88,22 @@ public void onTranscodeProgress(double progress) {
8688
@Override
8789
public void onTranscodeCompleted() {
8890
Log.d(TAG, "transcoding took " + (SystemClock.uptimeMillis() - startTime) + "ms");
89-
transcodeFinishEvent(progressBar, PROGRESS_BAR_MAX, "transcoded file placed on " + file, parcelFileDescriptor);
91+
onTranscodeFinished(true, "transcoded file placed on " + file, parcelFileDescriptor);
9092
startActivity(new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(file), "video/mp4"));
9193
}
9294

9395
@Override
9496
public void onTranscodeCanceled() {
95-
transcodeFinishEvent(progressBar, 0, "Transcoder canceled.", parcelFileDescriptor);
97+
onTranscodeFinished(false, "Transcoder canceled.", parcelFileDescriptor);
9698
}
9799

98100
@Override
99101
public void onTranscodeFailed(Exception exception) {
100-
transcodeFinishEvent(progressBar, 0, "Transcoder error occurred.", parcelFileDescriptor);
102+
onTranscodeFinished(false, "Transcoder error occurred.", parcelFileDescriptor);
101103
}
102104
};
103105
Log.d(TAG, "transcoding into " + file);
104-
MediaTranscoder.getInstance().transcodeVideo(fileDescriptor, file.getAbsolutePath(),
106+
mFuture = MediaTranscoder.getInstance().transcodeVideo(fileDescriptor, file.getAbsolutePath(),
105107
MediaFormatStrategyPresets.createAndroid720pStrategy(), listener);
106108
switchButtonEnabled(true);
107109
}
@@ -131,9 +133,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
131133
return super.onOptionsItemSelected(item);
132134
}
133135

134-
private void transcodeFinishEvent(ProgressBar progressBar, int progress, String toastMessage, ParcelFileDescriptor parcelFileDescriptor) {
136+
private void onTranscodeFinished(boolean isSuccess, String toastMessage, ParcelFileDescriptor parcelFileDescriptor) {
137+
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
135138
progressBar.setIndeterminate(false);
136-
progressBar.setProgress(progress);
139+
progressBar.setProgress(isSuccess ? PROGRESS_BAR_MAX : 0);
137140
switchButtonEnabled(false);
138141
Toast.makeText(TranscoderActivity.this, toastMessage, Toast.LENGTH_LONG).show();
139142
try {

lib/src/main/java/net/ypresto/androidtranscoder/MediaTranscoder.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@
2727
import java.io.FileDescriptor;
2828
import java.io.FileInputStream;
2929
import java.io.IOException;
30+
import java.util.concurrent.Callable;
3031
import java.util.concurrent.Future;
3132
import java.util.concurrent.LinkedBlockingQueue;
3233
import java.util.concurrent.ThreadFactory;
3334
import java.util.concurrent.ThreadPoolExecutor;
3435
import java.util.concurrent.TimeUnit;
36+
import java.util.concurrent.atomic.AtomicReference;
3537

3638
public 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 {

lib/src/main/java/net/ypresto/androidtranscoder/engine/MediaTranscoderEngine.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public double getProgress() {
7878
* @param formatStrategy Output format strategy.
7979
* @throws IOException when input or output file could not be opened.
8080
* @throws InvalidOutputFormatException when output format is not supported.
81+
* @throws InterruptedException when cancel to transcode.
8182
*/
8283
public void transcodeVideo(String outputPath, MediaFormatStrategy formatStrategy) throws IOException, InterruptedException {
8384
if (outputPath == null) {
@@ -95,8 +96,6 @@ public void transcodeVideo(String outputPath, MediaFormatStrategy formatStrategy
9596
setupTrackTranscoders(formatStrategy);
9697
runPipelines();
9798
mMuxer.stop();
98-
} catch (InterruptedException e) {
99-
throw e;
10099
} finally {
101100
try {
102101
if (mVideoTrackTranscoder != null) {
@@ -181,7 +180,7 @@ public void onDetermineOutputFormat() {
181180
mExtractor.selectTrack(trackResult.mAudioTrackIndex);
182181
}
183182

184-
private void runPipelines() throws InterruptedException {
183+
private void runPipelines() {
185184
long loopCount = 0;
186185
if (mDurationUs <= 0) {
187186
double progress = PROGRESS_UNKNOWN;
@@ -203,10 +202,9 @@ private void runPipelines() throws InterruptedException {
203202
try {
204203
Thread.sleep(SLEEP_TO_WAIT_TRACK_TRANSCODERS);
205204
} catch (InterruptedException e) {
206-
throw e;
205+
// nothing to do
207206
}
208207
}
209-
if (Thread.interrupted()) throw new InterruptedException();
210208
}
211209
}
212210

0 commit comments

Comments
 (0)