Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@ public final DefaultRenderersFactory experimentalSetMediaCodecAsyncCryptoFlagEna
}

/**
* Sets whether to enable fallback to lower-priority decoders if decoder initialization fails.
* This may result in using a decoder that is less efficient or slower than the primary decoder.
* Sets whether to enable fallback to lower-priority decoders if decoder initialization fails,
* or if a decoder that already initialized successfully fails while decoding and another
* decoder is available for the format. This may result in using a decoder that is less
* efficient or slower than the primary decoder.
*
* @param enableDecoderFallback Whether to enable fallback to lower-priority decoders if decoder
* initialization fails.
* initialization fails, or if a decoder fails while decoding and another is available.
* @return This factory, for convenience.
*/
@CanIgnoreReturnValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ private static String buildCustomDiagnosticInfo(int errorCode) {
private float codecOperatingRate;
private @Nullable ArrayDeque<MediaCodecInfo> availableCodecInfos;
private @Nullable DecoderInitializationException preferredDecoderInitializationException;
private final Set<String> decoderNamesThatFailedDuringDecoding = new HashSet<>();
private @Nullable MediaCodecInfo codecInfo;
private boolean codecNeedsAdaptationWorkaround;
private boolean codecNeedsSosFlushWorkaround;
Expand Down Expand Up @@ -405,8 +406,9 @@ private static String buildCustomDiagnosticInfo(int errorCode) {
* @param codecAdapterFactory A factory for {@link MediaCodecAdapter} instances.
* @param mediaCodecSelector A decoder selector.
* @param enableDecoderFallback Whether to enable fallback to lower-priority decoders if decoder
* initialization fails. This may result in using a decoder that is less efficient or slower
* than the primary decoder.
* initialization fails, or if a decoder that already initialized successfully fails while
* decoding and another decoder is available for the format. This may result in using a
* decoder that is less efficient or slower than the primary decoder.
* @param assumedMinimumCodecOperatingRate A codec operating rate that all codecs instantiated by
* this renderer are assumed to meet implicitly (i.e. without the operating rate being set
* explicitly using {@link MediaFormat#KEY_OPERATING_RATE}).
Expand Down Expand Up @@ -809,6 +811,7 @@ protected void onDisabled() {
inputFormat = null;
setOutputStreamInfo(OutputStreamInfo.UNSET);
pendingOutputStreamChanges.clear();
decoderNamesThatFailedDuringDecoding.clear();
if (bypassEnabled) {
disableBypass();
} else {
Expand Down Expand Up @@ -960,10 +963,32 @@ && shouldContinueRendering(renderStartTimeMs)) {}
onCodecError(e);
boolean isRecoverable =
(e instanceof CodecException) && ((CodecException) e).isRecoverable();
MediaCodecInfo failedCodecInfo = getCodecInfo();
if (isRecoverable) {
releaseCodec();
}
MediaCodecDecoderException exception = createDecoderException(e, getCodecInfo());
if (enableDecoderFallback
&& failedCodecInfo != null
&& decoderNamesThatFailedDuringDecoding.add(failedCodecInfo.name)) {
// A decoder that already initialized successfully has failed while decoding. Unlike an
// initialization failure, nothing else guarantees this decoder won't be selected again,
// so record it as failed and force a fresh, filtered codec selection on the next
// initialization attempt. If no other decoder is available, that attempt will itself
// fail with a DecoderInitializationException, which is surfaced through the usual path.
if (!isRecoverable) {
try {
releaseCodec();
} catch (IllegalStateException releaseException) {
// The codec already failed in a way its own error reporting classified as
// unrecoverable, so failing again while being torn down is expected. releaseCodec()
// clears the codec state in a finally block before this propagates, so the renderer
// is left in a valid state to select a different decoder.
}
}
availableCodecInfos = null;
return;
}
MediaCodecDecoderException exception = createDecoderException(e, failedCodecInfo);
@PlaybackException.ErrorCode
int errorCode =
exception.errorCode == CodecException.ERROR_RECLAIMED
Expand Down Expand Up @@ -1259,6 +1284,16 @@ private void maybeInitCodecWithFallback(
try {
List<MediaCodecInfo> allAvailableCodecInfos =
getAvailableCodecInfos(mediaCryptoRequiresSecureDecoder);
if (!decoderNamesThatFailedDuringDecoding.isEmpty()) {
List<MediaCodecInfo> codecInfosExcludingRuntimeFailures = new ArrayList<>();
for (int i = 0; i < allAvailableCodecInfos.size(); i++) {
MediaCodecInfo candidate = allAvailableCodecInfos.get(i);
if (!decoderNamesThatFailedDuringDecoding.contains(candidate.name)) {
codecInfosExcludingRuntimeFailures.add(candidate);
}
}
allAvailableCodecInfos = codecInfosExcludingRuntimeFailures;
}
availableCodecInfos = new ArrayDeque<>();
if (enableDecoderFallback) {
availableCodecInfos.addAll(allAvailableCodecInfos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.testing.junit.testparameterinjector.TestParameter;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.ArrayList;
Expand All @@ -134,6 +135,7 @@
import java.util.PriorityQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import org.jspecify.annotations.Nullable;
Expand Down Expand Up @@ -1987,6 +1989,133 @@ public void render_withLateAV1BufferWithoutDependencies_dropsInputBuffers() thro
assertThat(decoderCounters.droppedToKeyframeCount).isEqualTo(0);
}

/**
* Constructs a real {@link MediaCodec.CodecException} via reflection, since its constructor is
* not public. This matches what a real device raises when a decoder fails at runtime; a plain
* {@link IllegalStateException} thrown from application code is not recognized the same way.
*/
private static MediaCodec.CodecException createCodecException() {
try {
Constructor<MediaCodec.CodecException> constructor =
MediaCodec.CodecException.class.getDeclaredConstructor(
Integer.TYPE, Integer.TYPE, String.class);
constructor.setAccessible(true);
return constructor.newInstance(
/* errorCode= */ 0, /* actionCode= */ 0, /* detailMessage= */ "Simulated decoder failure");
} catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}

@Test
public void render_decoderFailsAfterInitializingWithFallbackEnabled_recoversWithOtherDecoder()
throws Exception {
String failingDecoderName = "test.decoder.fails-after-initializing";
String otherDecoderName = "test.decoder.other";
AtomicInteger failingDecoderProcessCallCount = new AtomicInteger();
ShadowMediaCodec.addDecoder(
failingDecoderName,
new ShadowMediaCodec.CodecConfig(
/* inputBufferSize= */ 2_000_000,
/* outputBufferSize= */ 2_000_000,
/* codec= */ (in, out) -> {
if (failingDecoderProcessCallCount.incrementAndGet() == 2) {
throw createCodecException();
}
}));
AtomicInteger otherDecoderProcessCallCount = new AtomicInteger();
ShadowMediaCodec.addDecoder(
otherDecoderName,
new ShadowMediaCodec.CodecConfig(
/* inputBufferSize= */ 2_000_000,
/* outputBufferSize= */ 2_000_000,
/* codec= */ (in, out) -> otherDecoderProcessCallCount.incrementAndGet()));
MediaCodecSelector twoDecoderSelector =
(mimeType, requiresSecureDecoder, requiresTunnelingDecoder) ->
ImmutableList.of(
MediaCodecInfo.newInstance(
/* name= */ failingDecoderName,
/* mimeType= */ mimeType,
/* codecMimeType= */ mimeType,
/* capabilities= */ null,
/* hardwareAccelerated= */ true,
/* softwareOnly= */ false,
/* vendor= */ true,
/* forceDisableAdaptive= */ false,
/* forceSecure= */ requiresSecureDecoder),
MediaCodecInfo.newInstance(
/* name= */ otherDecoderName,
/* mimeType= */ mimeType,
/* codecMimeType= */ mimeType,
/* capabilities= */ null,
/* hardwareAccelerated= */ false,
/* softwareOnly= */ true,
/* vendor= */ false,
/* forceDisableAdaptive= */ false,
/* forceSecure= */ requiresSecureDecoder));
List<FakeSampleStream.FakeSampleStreamItem> samples = new ArrayList<>();
for (int i = 0; i < 10; i++) {
samples.add(oneByteSample(/* timeUs= */ i * 33_333L, C.BUFFER_FLAG_KEY_FRAME));
}
samples.add(END_OF_STREAM_ITEM);
FakeSampleStream fakeSampleStream =
new FakeSampleStream(
new DefaultAllocator(/* trimOnReset= */ true, /* individualAllocationSize= */ 1024),
/* mediaSourceEventDispatcher= */ null,
DrmSessionManager.DRM_UNSUPPORTED,
new DrmSessionEventListener.EventDispatcher(),
/* initialFormat= */ VIDEO_H264,
samples);
fakeSampleStream.writeData(/* startPositionUs= */ 0);
mediaCodecVideoRenderer =
new MediaCodecVideoRenderer(
new MediaCodecVideoRenderer.Builder(ApplicationProvider.getApplicationContext())
.setCodecAdapterFactory(codecAdapterFactory)
.setMediaCodecSelector(twoDecoderSelector)
.setAllowedJoiningTimeMs(0)
.setEnableDecoderFallback(true)
.setEventHandler(new Handler(testMainLooper))
.setEventListener(eventListener)
.setMaxDroppedFramesToNotify(1)) {
@Override
protected @Capabilities int supportsFormat(
MediaCodecSelector mediaCodecSelector, Format format) {
return RendererCapabilities.create(C.FORMAT_HANDLED);
}
};
mediaCodecVideoRenderer.init(/* index= */ 0, PlayerId.UNSET, Clock.DEFAULT);
mediaCodecVideoRenderer.handleMessage(Renderer.MSG_SET_VIDEO_OUTPUT, surface);
mediaCodecVideoRenderer.enable(
RendererConfiguration.DEFAULT,
new Format[] {VIDEO_H264},
fakeSampleStream,
/* positionUs= */ 0,
/* joining= */ false,
/* mayRenderStartOfStream= */ true,
/* startPositionUs= */ 0,
/* offsetUs= */ 0,
new MediaSource.MediaPeriodId(new Object()));
mediaCodecVideoRenderer.start();

// Render enough times for the failing decoder to be selected, hit the simulated failure on
// its second sample, and for the renderer to select and start feeding the other decoder.
// Reaching end-of-stream is not needed to verify the recovery, so this stops well short of it.
long positionUs = 0;
for (int i = 0; i < 20; i++) {
mediaCodecVideoRenderer.render(positionUs, SystemClock.elapsedRealtime() * 1000);
codecAdapterFactory.idleQueueingAndCallbackThreads();
ShadowLooper.idleMainLooper();
positionUs += 10_000;
}

// Confirms the failing decoder was actually used and hit the simulated failure, and that the
// renderer recovered by selecting the other decoder and continuing to feed it, instead of the
// failure propagating as a fatal ExoPlaybackException (which would have caused this test to
// fail with an uncaught exception from render()).
assertThat(failingDecoderProcessCallCount.get()).isAtLeast(2);
assertThat(otherDecoderProcessCallCount.get()).isAtLeast(1);
}

// TODO: b/390604981 - Run the test on older SDK levels to ensure it uses a MediaCodec shadow
// with more than one buffer slot.
@Config(minSdk = 30)
Expand Down