mirror of
https://github.com/androidx/media.git
synced 2025-05-11 09:39:52 +08:00
Merge MuxerWrapper stop() and release() methods
#minor-release PiperOrigin-RevId: 354938190
This commit is contained in:
parent
03b9fa303d
commit
15472a510a
@ -138,33 +138,39 @@ import java.nio.ByteBuffer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release() {
|
||||
if (isStarted) {
|
||||
isStarted = false;
|
||||
try {
|
||||
mediaMuxer.stop();
|
||||
} catch (IllegalStateException e) {
|
||||
if (SDK_INT < 30) {
|
||||
// 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
|
||||
// same exception without releasing its resources. This is already implemented in
|
||||
// MediaMuxer
|
||||
// from API level 30.
|
||||
try {
|
||||
Field muxerStoppedStateField = MediaMuxer.class.getDeclaredField("MUXER_STATE_STOPPED");
|
||||
muxerStoppedStateField.setAccessible(true);
|
||||
int muxerStoppedState = castNonNull((Integer) muxerStoppedStateField.get(mediaMuxer));
|
||||
Field muxerStateField = MediaMuxer.class.getDeclaredField("mState");
|
||||
muxerStateField.setAccessible(true);
|
||||
muxerStateField.set(mediaMuxer, muxerStoppedState);
|
||||
} catch (Exception reflectionException) {
|
||||
// Do nothing.
|
||||
}
|
||||
public void release(boolean forCancellation) {
|
||||
if (!isStarted) {
|
||||
mediaMuxer.release();
|
||||
return;
|
||||
}
|
||||
|
||||
isStarted = false;
|
||||
try {
|
||||
mediaMuxer.stop();
|
||||
} catch (IllegalStateException e) {
|
||||
if (SDK_INT < 30) {
|
||||
// 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
|
||||
// same exception without releasing its resources. This is already implemented in MediaMuxer
|
||||
// from API level 30.
|
||||
try {
|
||||
Field muxerStoppedStateField = MediaMuxer.class.getDeclaredField("MUXER_STATE_STOPPED");
|
||||
muxerStoppedStateField.setAccessible(true);
|
||||
int muxerStoppedState = castNonNull((Integer) muxerStoppedStateField.get(mediaMuxer));
|
||||
Field muxerStateField = MediaMuxer.class.getDeclaredField("mState");
|
||||
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;
|
||||
}
|
||||
} finally {
|
||||
mediaMuxer.release();
|
||||
}
|
||||
mediaMuxer.release();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -84,6 +84,11 @@ import java.nio.ByteBuffer;
|
||||
void writeSampleData(
|
||||
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);
|
||||
}
|
||||
|
@ -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.
|
||||
*/
|
||||
public void stop() {
|
||||
if (isReady) {
|
||||
isReady = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases the muxer.
|
||||
* <p>The muxer cannot be used anymore once this method has been called.
|
||||
*
|
||||
* <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;
|
||||
muxer.release();
|
||||
muxer.release(forCancellation);
|
||||
}
|
||||
|
||||
/** Returns the number of {@link #registerTrack() registered} tracks. */
|
||||
|
@ -525,36 +525,27 @@ public final class Transformer {
|
||||
* @throws IllegalStateException If this method is called from the wrong thread.
|
||||
*/
|
||||
public void cancel() {
|
||||
// It doesn't matter that stopping the muxer throws, because the transformation is cancelled
|
||||
// anyway.
|
||||
releaseResources(/* swallowStopMuxerException= */ true);
|
||||
releaseResources(/* forCancellation= */ true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the muxer is in the wrong state when stopping it and {@code
|
||||
* swallowStopMuxerException} is false.
|
||||
* @throws IllegalStateException If the muxer is in the wrong state and {@code forCancellation} is
|
||||
* false.
|
||||
*/
|
||||
private void releaseResources(boolean swallowStopMuxerException) {
|
||||
private void releaseResources(boolean forCancellation) {
|
||||
verifyApplicationThread();
|
||||
if (player != null) {
|
||||
player.release();
|
||||
player = null;
|
||||
}
|
||||
if (muxerWrapper != null) {
|
||||
try {
|
||||
muxerWrapper.stop();
|
||||
} catch (IllegalStateException e) {
|
||||
if (!swallowStopMuxerException) {
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
muxerWrapper.release();
|
||||
muxerWrapper = null;
|
||||
}
|
||||
muxerWrapper.release(forCancellation);
|
||||
muxerWrapper = null;
|
||||
}
|
||||
progressState = PROGRESS_STATE_NO_TRANSFORMATION;
|
||||
}
|
||||
@ -654,7 +645,7 @@ public final class Transformer {
|
||||
|
||||
private void handleTransformationEnded(@Nullable Exception exception) {
|
||||
try {
|
||||
releaseResources(/* swallowStopMuxerException= */ false);
|
||||
releaseResources(/* forCancellation= */ false);
|
||||
} catch (IllegalStateException e) {
|
||||
if (exception == null) {
|
||||
exception = e;
|
||||
|
@ -62,9 +62,9 @@ public final class TestMuxer implements Muxer, Dumper.Dumpable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release() {
|
||||
public void release(boolean forCancellation) {
|
||||
dumpables.add(dumper -> dumper.add("released", true));
|
||||
frameworkMuxer.release();
|
||||
frameworkMuxer.release(forCancellation);
|
||||
}
|
||||
|
||||
// Dumper.Dumpable implementation.
|
||||
|
Loading…
x
Reference in New Issue
Block a user