Merge MuxerWrapper stop() and release() methods

#minor-release

PiperOrigin-RevId: 354938190
This commit is contained in:
kimvde 2021-02-01 15:50:43 +00:00 committed by Oliver Woodman
parent 03b9fa303d
commit 15472a510a
5 changed files with 53 additions and 59 deletions

View File

@ -138,8 +138,12 @@ import java.nio.ByteBuffer;
} }
@Override @Override
public void release() { public void release(boolean forCancellation) {
if (isStarted) { if (!isStarted) {
mediaMuxer.release();
return;
}
isStarted = false; isStarted = false;
try { try {
mediaMuxer.stop(); mediaMuxer.stop();
@ -147,8 +151,7 @@ import java.nio.ByteBuffer;
if (SDK_INT < 30) { if (SDK_INT < 30) {
// Set the muxer state to stopped even if mediaMuxer.stop() failed so that // Set the muxer state to stopped even if mediaMuxer.stop() failed so that
// mediaMuxer.release() doesn't attempt to stop the muxer and therefore doesn't throw the // mediaMuxer.release() doesn't attempt to stop the muxer and therefore doesn't throw the
// same exception without releasing its resources. This is already implemented in // same exception without releasing its resources. This is already implemented in MediaMuxer
// MediaMuxer
// from API level 30. // from API level 30.
try { try {
Field muxerStoppedStateField = MediaMuxer.class.getDeclaredField("MUXER_STATE_STOPPED"); Field muxerStoppedStateField = MediaMuxer.class.getDeclaredField("MUXER_STATE_STOPPED");
@ -161,11 +164,14 @@ import java.nio.ByteBuffer;
// Do nothing. // Do nothing.
} }
} }
// It doesn't matter that stopping the muxer throws if the transformation is being cancelled.
if (!forCancellation) {
throw e; throw e;
} }
} } finally {
mediaMuxer.release(); mediaMuxer.release();
} }
}
/** /**
* Converts a {@link MimeTypes MIME type} into a {@link MediaMuxer.OutputFormat MediaMuxer output * Converts a {@link MimeTypes MIME type} into a {@link MediaMuxer.OutputFormat MediaMuxer output

View File

@ -84,6 +84,11 @@ import java.nio.ByteBuffer;
void writeSampleData( void writeSampleData(
int trackIndex, ByteBuffer data, boolean isKeyFrame, long presentationTimeUs); int trackIndex, ByteBuffer data, boolean isKeyFrame, long presentationTimeUs);
/** Releases any resources associated with muxing. */ /**
void release(); * Releases any resources associated with muxing.
*
* @param forCancellation Whether the reason for releasing the resources is the transformation
* cancellation.
*/
void release(boolean forCancellation);
} }

View File

@ -159,24 +159,16 @@ import java.nio.ByteBuffer;
} }
/** /**
* Stops the muxer. * Releases any resources associated with muxing.
* *
* <p>The muxer cannot be used anymore once it is stopped. * <p>The muxer cannot be used anymore once this method has been called.
*/
public void stop() {
if (isReady) {
isReady = false;
}
}
/**
* Releases the muxer.
* *
* <p>The muxer cannot be used anymore once it is released. * @param forCancellation Whether the reason for releasing the resources is the transformation
* cancellation.
*/ */
public void release() { public void release(boolean forCancellation) {
isReady = false; isReady = false;
muxer.release(); muxer.release(forCancellation);
} }
/** Returns the number of {@link #registerTrack() registered} tracks. */ /** Returns the number of {@link #registerTrack() registered} tracks. */

View File

@ -525,37 +525,28 @@ public final class Transformer {
* @throws IllegalStateException If this method is called from the wrong thread. * @throws IllegalStateException If this method is called from the wrong thread.
*/ */
public void cancel() { public void cancel() {
// It doesn't matter that stopping the muxer throws, because the transformation is cancelled releaseResources(/* forCancellation= */ true);
// anyway.
releaseResources(/* swallowStopMuxerException= */ true);
} }
/** /**
* Releases the resources. * Releases the resources.
* *
* @param swallowStopMuxerException Whether to swallow exceptions thrown by stopping the muxer. * @param forCancellation Whether the reason for releasing the resources is the transformation
* cancellation.
* @throws IllegalStateException If this method is called from the wrong thread. * @throws IllegalStateException If this method is called from the wrong thread.
* @throws IllegalStateException If the muxer is in the wrong state when stopping it and {@code * @throws IllegalStateException If the muxer is in the wrong state and {@code forCancellation} is
* swallowStopMuxerException} is false. * false.
*/ */
private void releaseResources(boolean swallowStopMuxerException) { private void releaseResources(boolean forCancellation) {
verifyApplicationThread(); verifyApplicationThread();
if (player != null) { if (player != null) {
player.release(); player.release();
player = null; player = null;
} }
if (muxerWrapper != null) { if (muxerWrapper != null) {
try { muxerWrapper.release(forCancellation);
muxerWrapper.stop();
} catch (IllegalStateException e) {
if (!swallowStopMuxerException) {
throw e;
}
} finally {
muxerWrapper.release();
muxerWrapper = null; muxerWrapper = null;
} }
}
progressState = PROGRESS_STATE_NO_TRANSFORMATION; progressState = PROGRESS_STATE_NO_TRANSFORMATION;
} }
@ -654,7 +645,7 @@ public final class Transformer {
private void handleTransformationEnded(@Nullable Exception exception) { private void handleTransformationEnded(@Nullable Exception exception) {
try { try {
releaseResources(/* swallowStopMuxerException= */ false); releaseResources(/* forCancellation= */ false);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
if (exception == null) { if (exception == null) {
exception = e; exception = e;

View File

@ -62,9 +62,9 @@ public final class TestMuxer implements Muxer, Dumper.Dumpable {
} }
@Override @Override
public void release() { public void release(boolean forCancellation) {
dumpables.add(dumper -> dumper.add("released", true)); dumpables.add(dumper -> dumper.add("released", true));
frameworkMuxer.release(); frameworkMuxer.release(forCancellation);
} }
// Dumper.Dumpable implementation. // Dumper.Dumpable implementation.