Skip to content

Commit

Permalink
Merge pull request #65 from kachi1227/master
Browse files Browse the repository at this point in the history
Fixes Cancel Not Working Issue + Calls Listener's onCanceled() Method
  • Loading branch information
MasayukiSuda authored Jan 23, 2020
2 parents f4b44be + 9e6a0ac commit ecbefb0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class Mp4Composer {
private EGLContext shareContext;

private ExecutorService executorService;
private Mp4ComposerEngine engine;

private Logger logger;

Expand Down Expand Up @@ -200,13 +201,18 @@ private ExecutorService getExecutorService() {


public Mp4Composer start() {
//if we're already composing, calling this should do nothing
if (engine != null) {
return this;
}

getExecutorService().execute(new Runnable() {
@Override
public void run() {
if (logger == null) {
logger = new AndroidLogger();
}
Mp4ComposerEngine engine = new Mp4ComposerEngine(logger);
engine = new Mp4ComposerEngine(logger);

engine.setProgressCallback(new Mp4ComposerEngine.ProgressCallback() {
@Override
Expand Down Expand Up @@ -306,9 +312,14 @@ public void onProgress(final double progress) {
}

if (listener != null) {
listener.onCompleted();
if (engine.isCanceled()) {
listener.onCanceled();
} else {
listener.onCompleted();
}
}
executorService.shutdown();
engine = null;
}
});

Expand All @@ -325,7 +336,9 @@ private void notifyListenerOfFailureAndShutdown(final Exception failure) {
}

public void cancel() {
getExecutorService().shutdownNow();
if (engine != null) {
engine.cancel();
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Mp4ComposerEngine {
private ProgressCallback progressCallback;
private long durationUs;
private MediaMetadataRetriever mediaMetadataRetriever;
private volatile boolean canceled;
private final Logger logger;

Mp4ComposerEngine(@NonNull final Logger logger) {
Expand Down Expand Up @@ -171,8 +172,14 @@ void compose(
logger.error(TAG, "Failed to release mediaMetadataRetriever.", e);
}
}
}

void cancel() {
canceled = true;
}

boolean isCanceled() {
return canceled;
}

@NonNull
Expand Down Expand Up @@ -256,7 +263,7 @@ private void runPipelines() {
progressCallback.onProgress(PROGRESS_UNKNOWN);
}// unknown
}
while (!(videoComposer.isFinished() && audioComposer.isFinished())) {
while (!canceled && !(videoComposer.isFinished() && audioComposer.isFinished())) {
boolean stepped = videoComposer.stepPipeline()
|| audioComposer.stepPipeline();
loopCount++;
Expand Down Expand Up @@ -285,7 +292,7 @@ private void runPipelinesNoAudio() {
progressCallback.onProgress(PROGRESS_UNKNOWN);
} // unknown
}
while (!videoComposer.isFinished()) {
while (!canceled && !videoComposer.isFinished()) {
boolean stepped = videoComposer.stepPipeline();
loopCount++;
if (durationUs > 0 && loopCount % PROGRESS_INTERVAL_STEPS == 0) {
Expand Down

0 comments on commit ecbefb0

Please sign in to comment.