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,33 +138,39 @@ import java.nio.ByteBuffer;
} }
@Override @Override
public void release() { public void release(boolean forCancellation) {
if (isStarted) { if (!isStarted) {
isStarted = false; mediaMuxer.release();
try { return;
mediaMuxer.stop(); }
} catch (IllegalStateException e) {
if (SDK_INT < 30) { isStarted = false;
// Set the muxer state to stopped even if mediaMuxer.stop() failed so that try {
// mediaMuxer.release() doesn't attempt to stop the muxer and therefore doesn't throw the mediaMuxer.stop();
// same exception without releasing its resources. This is already implemented in } catch (IllegalStateException e) {
// MediaMuxer if (SDK_INT < 30) {
// from API level 30. // Set the muxer state to stopped even if mediaMuxer.stop() failed so that
try { // mediaMuxer.release() doesn't attempt to stop the muxer and therefore doesn't throw the
Field muxerStoppedStateField = MediaMuxer.class.getDeclaredField("MUXER_STATE_STOPPED"); // same exception without releasing its resources. This is already implemented in MediaMuxer
muxerStoppedStateField.setAccessible(true); // from API level 30.
int muxerStoppedState = castNonNull((Integer) muxerStoppedStateField.get(mediaMuxer)); try {
Field muxerStateField = MediaMuxer.class.getDeclaredField("mState"); Field muxerStoppedStateField = MediaMuxer.class.getDeclaredField("MUXER_STATE_STOPPED");
muxerStateField.setAccessible(true); muxerStoppedStateField.setAccessible(true);
muxerStateField.set(mediaMuxer, muxerStoppedState); int muxerStoppedState = castNonNull((Integer) muxerStoppedStateField.get(mediaMuxer));
} catch (Exception reflectionException) { Field muxerStateField = MediaMuxer.class.getDeclaredField("mState");
// Do nothing. muxerStateField.setAccessible(true);
} muxerStateField.set(mediaMuxer, muxerStoppedState);
} catch (Exception reflectionException) {
// 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();
} }
/** /**

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,36 +525,27 @@ 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(); muxerWrapper = null;
} catch (IllegalStateException e) {
if (!swallowStopMuxerException) {
throw e;
}
} finally {
muxerWrapper.release();
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.