Only throw the codec exception on the last configuration attempt

This commit is contained in:
Cameron Gutman
2022-09-18 18:47:01 -05:00
parent 5bfce88fc5
commit 9762f4c412

View File

@@ -385,10 +385,35 @@ public class MediaCodecDecoderRenderer extends VideoDecoderRenderer implements C
} }
} }
private void tryConfigureDecoder(MediaCodecInfo selectedDecoderInfo, MediaFormat format) throws IOException { private boolean tryConfigureDecoder(MediaCodecInfo selectedDecoderInfo, MediaFormat format, boolean throwOnCodecError) {
videoDecoder = MediaCodec.createByCodecName(selectedDecoderInfo.getName()); boolean configured = false;
configureAndStartDecoder(format); try {
LimeLog.info("Using codec " + selectedDecoderInfo.getName() + " for hardware decoding " + format.getString(MediaFormat.KEY_MIME)); videoDecoder = MediaCodec.createByCodecName(selectedDecoderInfo.getName());
configureAndStartDecoder(format);
LimeLog.info("Using codec " + selectedDecoderInfo.getName() + " for hardware decoding " + format.getString(MediaFormat.KEY_MIME));
configured = true;
} catch (IllegalArgumentException e) {
e.printStackTrace();
if (throwOnCodecError) {
throw e;
}
} catch (IllegalStateException e) {
e.printStackTrace();
if (throwOnCodecError) {
throw e;
}
} catch (IOException e) {
e.printStackTrace();
if (throwOnCodecError) {
throw new RuntimeException(e);
}
} finally {
if (!configured && videoDecoder != null) {
videoDecoder.release();
videoDecoder = null;
}
}
return configured;
} }
public int initializeDecoder(boolean throwOnCodecError) { public int initializeDecoder(boolean throwOnCodecError) {
@@ -449,8 +474,7 @@ public class MediaCodecDecoderRenderer extends VideoDecoderRenderer implements C
adaptivePlayback = MediaCodecHelper.decoderSupportsAdaptivePlayback(selectedDecoderInfo, mimeType); adaptivePlayback = MediaCodecHelper.decoderSupportsAdaptivePlayback(selectedDecoderInfo, mimeType);
fusedIdrFrame = MediaCodecHelper.decoderSupportsFusedIdrFrame(selectedDecoderInfo, mimeType); fusedIdrFrame = MediaCodecHelper.decoderSupportsFusedIdrFrame(selectedDecoderInfo, mimeType);
boolean configured = false; for (int tryNumber = 0;; tryNumber++) {
for (int tryNumber = 0; !configured; tryNumber++) {
LimeLog.info("Decoder configuration try: "+tryNumber); LimeLog.info("Decoder configuration try: "+tryNumber);
MediaFormat mediaFormat = createBaseMediaFormat(mimeType); MediaFormat mediaFormat = createBaseMediaFormat(mimeType);
@@ -458,32 +482,13 @@ public class MediaCodecDecoderRenderer extends VideoDecoderRenderer implements C
// This will try low latency options until we find one that works (or we give up). // This will try low latency options until we find one that works (or we give up).
boolean newFormat = MediaCodecHelper.setDecoderLowLatencyOptions(mediaFormat, selectedDecoderInfo, tryNumber); boolean newFormat = MediaCodecHelper.setDecoderLowLatencyOptions(mediaFormat, selectedDecoderInfo, tryNumber);
try { // Throw the underlying codec exception on the last attempt if the caller requested it
tryConfigureDecoder(selectedDecoderInfo, mediaFormat); if (tryConfigureDecoder(selectedDecoderInfo, mediaFormat, !newFormat && throwOnCodecError)) {
configured = true; // Success!
} catch (IllegalArgumentException e) { break;
e.printStackTrace();
if (throwOnCodecError) {
throw e;
}
} catch (IllegalStateException e) {
e.printStackTrace();
if (throwOnCodecError) {
throw e;
}
} catch (IOException e) {
e.printStackTrace();
if (throwOnCodecError) {
throw new RuntimeException(e);
}
} finally {
if (!configured && videoDecoder != null) {
videoDecoder.release();
videoDecoder = null;
}
} }
if (!configured && !newFormat) { if (!newFormat) {
// We couldn't even configure a decoder without any low latency options // We couldn't even configure a decoder without any low latency options
return -5; return -5;
} }