Improve exception message.

We use the `createForCodec` method that does not take a `MediaFormat` during
transformation, the error message always includes "no configured MediaFormat",
which is false.

PiperOrigin-RevId: 429553573
This commit is contained in:
claincly 2022-02-18 14:46:32 +00:00 committed by Ian Baker
parent d622598285
commit c680517527
3 changed files with 29 additions and 28 deletions

View File

@ -43,6 +43,9 @@ public final class DefaultCodec implements Codec {
private static final int MEDIA_CODEC_PCM_ENCODING = C.ENCODING_PCM_16BIT; private static final int MEDIA_CODEC_PCM_ENCODING = C.ENCODING_PCM_16BIT;
private final BufferInfo outputBufferInfo; private final BufferInfo outputBufferInfo;
/** The {@link MediaFormat} used to configure the underlying {@link MediaCodec}. */
private final MediaFormat configurationMediaFormat;
private final Format configurationFormat; private final Format configurationFormat;
private final MediaCodec mediaCodec; private final MediaCodec mediaCodec;
@Nullable private final Surface inputSurface; @Nullable private final Surface inputSurface;
@ -61,7 +64,8 @@ public final class DefaultCodec implements Codec {
* @param configurationFormat The {@link Format} to configure the {@code DefaultCodec}. See {@link * @param configurationFormat The {@link Format} to configure the {@code DefaultCodec}. See {@link
* #getConfigurationFormat()}. The {@link Format#sampleMimeType sampleMimeType} must not be * #getConfigurationFormat()}. The {@link Format#sampleMimeType sampleMimeType} must not be
* {@code null}. * {@code null}.
* @param mediaFormat The {@link MediaFormat} to configure the underlying {@link MediaCodec}. * @param configurationMediaFormat The {@link MediaFormat} to configure the underlying {@link
* MediaCodec}.
* @param mediaCodecName The name of a specific {@link MediaCodec} to instantiate. If {@code * @param mediaCodecName The name of a specific {@link MediaCodec} to instantiate. If {@code
* null}, {@code DefaultCodec} uses {@link Format#sampleMimeType * null}, {@code DefaultCodec} uses {@link Format#sampleMimeType
* configurationFormat.sampleMimeType} to create the underlying {@link MediaCodec codec}. * configurationFormat.sampleMimeType} to create the underlying {@link MediaCodec codec}.
@ -70,12 +74,13 @@ public final class DefaultCodec implements Codec {
*/ */
public DefaultCodec( public DefaultCodec(
Format configurationFormat, Format configurationFormat,
MediaFormat mediaFormat, MediaFormat configurationMediaFormat,
@Nullable String mediaCodecName, @Nullable String mediaCodecName,
boolean isDecoder, boolean isDecoder,
@Nullable Surface outputSurface) @Nullable Surface outputSurface)
throws TransformationException { throws TransformationException {
this.configurationFormat = configurationFormat; this.configurationFormat = configurationFormat;
this.configurationMediaFormat = configurationMediaFormat;
outputBufferInfo = new BufferInfo(); outputBufferInfo = new BufferInfo();
inputBufferIndex = C.INDEX_UNSET; inputBufferIndex = C.INDEX_UNSET;
outputBufferIndex = C.INDEX_UNSET; outputBufferIndex = C.INDEX_UNSET;
@ -91,7 +96,7 @@ public final class DefaultCodec implements Codec {
: isDecoder : isDecoder
? MediaCodec.createDecoderByType(sampleMimeType) ? MediaCodec.createDecoderByType(sampleMimeType)
: MediaCodec.createEncoderByType(sampleMimeType); : MediaCodec.createEncoderByType(sampleMimeType);
configureCodec(mediaCodec, mediaFormat, isDecoder, outputSurface); configureCodec(mediaCodec, configurationMediaFormat, isDecoder, outputSurface);
if (isVideo && !isDecoder) { if (isVideo && !isDecoder) {
inputSurface = mediaCodec.createInputSurface(); inputSurface = mediaCodec.createInputSurface();
} }
@ -106,7 +111,7 @@ public final class DefaultCodec implements Codec {
} }
throw createInitializationTransformationException( throw createInitializationTransformationException(
e, mediaFormat, configurationFormat, isVideo, isDecoder, mediaCodecName); e, configurationMediaFormat, isVideo, isDecoder, mediaCodecName);
} }
this.mediaCodec = mediaCodec; this.mediaCodec = mediaCodec;
this.inputSurface = inputSurface; this.inputSurface = inputSurface;
@ -292,9 +297,9 @@ public final class DefaultCodec implements Codec {
boolean isVideo = MimeTypes.isVideo(configurationFormat.sampleMimeType); boolean isVideo = MimeTypes.isVideo(configurationFormat.sampleMimeType);
return TransformationException.createForCodec( return TransformationException.createForCodec(
cause, cause,
configurationFormat,
isVideo, isVideo,
isDecoder, isDecoder,
configurationMediaFormat,
mediaCodec.getName(), mediaCodec.getName(),
isDecoder isDecoder
? TransformationException.ERROR_CODE_DECODING_FAILED ? TransformationException.ERROR_CODE_DECODING_FAILED
@ -304,17 +309,15 @@ public final class DefaultCodec implements Codec {
private static TransformationException createInitializationTransformationException( private static TransformationException createInitializationTransformationException(
Exception cause, Exception cause,
MediaFormat mediaFormat, MediaFormat mediaFormat,
Format format,
boolean isVideo, boolean isVideo,
boolean isDecoder, boolean isDecoder,
@Nullable String mediaCodecName) { @Nullable String mediaCodecName) {
if (cause instanceof IOException || cause instanceof MediaCodec.CodecException) { if (cause instanceof IOException || cause instanceof MediaCodec.CodecException) {
return TransformationException.createForCodec( return TransformationException.createForCodec(
cause, cause,
mediaFormat,
format,
isVideo, isVideo,
isDecoder, isDecoder,
mediaFormat,
mediaCodecName, mediaCodecName,
isDecoder isDecoder
? TransformationException.ERROR_CODE_DECODER_INIT_FAILED ? TransformationException.ERROR_CODE_DECODER_INIT_FAILED
@ -323,10 +326,9 @@ public final class DefaultCodec implements Codec {
if (cause instanceof IllegalArgumentException) { if (cause instanceof IllegalArgumentException) {
return TransformationException.createForCodec( return TransformationException.createForCodec(
cause, cause,
mediaFormat,
format,
isVideo, isVideo,
isDecoder, isDecoder,
mediaFormat,
mediaCodecName, mediaCodecName,
isDecoder isDecoder
? TransformationException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED ? TransformationException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED

View File

@ -79,9 +79,9 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
} else { } else {
throw TransformationException.createForCodec( throw TransformationException.createForCodec(
new IllegalArgumentException("The requested output format is not supported."), new IllegalArgumentException("The requested output format is not supported."),
format,
/* isVideo= */ false, /* isVideo= */ false,
/* isDecoder= */ false, /* isDecoder= */ false,
format,
/* mediaCodecName= */ null, /* mediaCodecName= */ null,
TransformationException.ERROR_CODE_OUTPUT_FORMAT_UNSUPPORTED); TransformationException.ERROR_CODE_OUTPUT_FORMAT_UNSUPPORTED);
} }
@ -119,9 +119,9 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
if (encoderAndClosestFormatSupport == null) { if (encoderAndClosestFormatSupport == null) {
throw TransformationException.createForCodec( throw TransformationException.createForCodec(
new IllegalArgumentException("The requested output format is not supported."), new IllegalArgumentException("The requested output format is not supported."),
format,
/* isVideo= */ true, /* isVideo= */ true,
/* isDecoder= */ false, /* isDecoder= */ false,
format,
/* mediaCodecName= */ null, /* mediaCodecName= */ null,
TransformationException.ERROR_CODE_OUTPUT_FORMAT_UNSUPPORTED); TransformationException.ERROR_CODE_OUTPUT_FORMAT_UNSUPPORTED);
} }

View File

@ -205,56 +205,55 @@ public final class TransformationException extends Exception {
/** /**
* Creates an instance for a decoder or encoder related exception. * Creates an instance for a decoder or encoder related exception.
* *
* <p>Use this method after the {@link MediaFormat} used to configure the {@link Codec} is known.
*
* @param cause The cause of the failure. * @param cause The cause of the failure.
* @param mediaFormat The {@link MediaFormat} used for configuring the underlying {@link
* MediaCodec}, if known.
* @param format The {@link Format} used for configuring the {@link Codec}.
* @param isVideo Whether the decoder or encoder is configured for video. * @param isVideo Whether the decoder or encoder is configured for video.
* @param isDecoder Whether the exception is created for a decoder. * @param isDecoder Whether the exception is created for a decoder.
* @param mediaFormat The {@link MediaFormat} used for configuring the underlying {@link
* MediaCodec}.
* @param mediaCodecName The name of the {@link MediaCodec} used, if known. * @param mediaCodecName The name of the {@link MediaCodec} used, if known.
* @param errorCode See {@link #errorCode}. * @param errorCode See {@link #errorCode}.
* @return The created instance. * @return The created instance.
*/ */
public static TransformationException createForCodec( public static TransformationException createForCodec(
Throwable cause, Throwable cause,
@Nullable MediaFormat mediaFormat,
Format format,
boolean isVideo, boolean isVideo,
boolean isDecoder, boolean isDecoder,
MediaFormat mediaFormat,
@Nullable String mediaCodecName, @Nullable String mediaCodecName,
int errorCode) { int errorCode) {
String componentName = (isVideo ? "Video" : "Audio") + (isDecoder ? "Decoder" : "Encoder"); String componentName = (isVideo ? "Video" : "Audio") + (isDecoder ? "Decoder" : "Encoder");
String errorMessage = String errorMessage =
componentName componentName + ", mediaFormat=" + mediaFormat + ", mediaCodecName=" + mediaCodecName;
+ " error, format="
+ format
+ ", mediaCodecName="
+ mediaCodecName
+ ", mediaFormat="
+ (mediaFormat == null ? "no configured MediaFormat" : mediaFormat.toString());
return new TransformationException(errorMessage, cause, errorCode); return new TransformationException(errorMessage, cause, errorCode);
} }
/** /**
* Creates an instance for a decoder or encoder related exception. * Creates an instance for a decoder or encoder related exception.
* *
* <p>Use this method before configuring the {@link Codec}, or when the {@link Codec} is not
* configured with a {@link MediaFormat}.
*
* @param cause The cause of the failure. * @param cause The cause of the failure.
* @param format The {@link Format} used for configuring the {@link Codec}.
* @param isVideo Whether the decoder or encoder is configured for video. * @param isVideo Whether the decoder or encoder is configured for video.
* @param isDecoder Whether the exception is created for a decoder. * @param isDecoder Whether the exception is created for a decoder.
* @param format The {@link Format} used for configuring the {@link Codec}.
* @param mediaCodecName The name of the {@link MediaCodec} used, if known. * @param mediaCodecName The name of the {@link MediaCodec} used, if known.
* @param errorCode See {@link #errorCode}. * @param errorCode See {@link #errorCode}.
* @return The created instance. * @return The created instance.
*/ */
public static TransformationException createForCodec( public static TransformationException createForCodec(
Throwable cause, Throwable cause,
Format format,
boolean isVideo, boolean isVideo,
boolean isDecoder, boolean isDecoder,
Format format,
@Nullable String mediaCodecName, @Nullable String mediaCodecName,
int errorCode) { int errorCode) {
return createForCodec( String componentName = (isVideo ? "Video" : "Audio") + (isDecoder ? "Decoder" : "Encoder");
cause, /* mediaFormat= */ null, format, isVideo, isDecoder, mediaCodecName, errorCode); String errorMessage =
componentName + " error, format=" + format + ", mediaCodecName=" + mediaCodecName;
return new TransformationException(errorMessage, cause, errorCode);
} }
/** /**