Skip to content

Commit

Permalink
Improve exception message.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
claincly authored and icbaker committed Feb 18, 2022
1 parent 9238dc7 commit 99074f7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public final class DefaultCodec implements Codec {
private static final int MEDIA_CODEC_PCM_ENCODING = C.ENCODING_PCM_16BIT;

private final BufferInfo outputBufferInfo;
/** The {@link MediaFormat} used to configure the underlying {@link MediaCodec}. */
private final MediaFormat configurationMediaFormat;

private final Format configurationFormat;
private final MediaCodec mediaCodec;
@Nullable private final Surface inputSurface;
Expand All @@ -63,7 +66,8 @@ public final class DefaultCodec implements Codec {
* @param configurationFormat The {@link Format} to configure the {@code DefaultCodec}. See {@link
* #getConfigurationFormat()}. The {@link Format#sampleMimeType sampleMimeType} must not be
* {@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
* null}, {@code DefaultCodec} uses {@link Format#sampleMimeType
* configurationFormat.sampleMimeType} to create the underlying {@link MediaCodec codec}.
Expand All @@ -72,12 +76,13 @@ public final class DefaultCodec implements Codec {
*/
public DefaultCodec(
Format configurationFormat,
MediaFormat mediaFormat,
MediaFormat configurationMediaFormat,
@Nullable String mediaCodecName,
boolean isDecoder,
@Nullable Surface outputSurface)
throws TransformationException {
this.configurationFormat = configurationFormat;
this.configurationMediaFormat = configurationMediaFormat;
outputBufferInfo = new BufferInfo();
inputBufferIndex = C.INDEX_UNSET;
outputBufferIndex = C.INDEX_UNSET;
Expand All @@ -93,7 +98,7 @@ public DefaultCodec(
: isDecoder
? MediaCodec.createDecoderByType(sampleMimeType)
: MediaCodec.createEncoderByType(sampleMimeType);
configureCodec(mediaCodec, mediaFormat, isDecoder, outputSurface);
configureCodec(mediaCodec, configurationMediaFormat, isDecoder, outputSurface);
if (isVideo && !isDecoder) {
inputSurface = mediaCodec.createInputSurface();
}
Expand All @@ -108,7 +113,7 @@ public DefaultCodec(
}

throw createInitializationTransformationException(
e, mediaFormat, configurationFormat, isVideo, isDecoder, mediaCodecName);
e, configurationMediaFormat, isVideo, isDecoder, mediaCodecName);
}
this.mediaCodec = mediaCodec;
this.inputSurface = inputSurface;
Expand Down Expand Up @@ -294,9 +299,9 @@ private TransformationException createTransformationException(Exception cause) {
boolean isVideo = MimeTypes.isVideo(configurationFormat.sampleMimeType);
return TransformationException.createForCodec(
cause,
configurationFormat,
isVideo,
isDecoder,
configurationMediaFormat,
mediaCodec.getName(),
isDecoder
? TransformationException.ERROR_CODE_DECODING_FAILED
Expand All @@ -306,17 +311,15 @@ private TransformationException createTransformationException(Exception cause) {
private static TransformationException createInitializationTransformationException(
Exception cause,
MediaFormat mediaFormat,
Format format,
boolean isVideo,
boolean isDecoder,
@Nullable String mediaCodecName) {
if (cause instanceof IOException || cause instanceof MediaCodec.CodecException) {
return TransformationException.createForCodec(
cause,
mediaFormat,
format,
isVideo,
isDecoder,
mediaFormat,
mediaCodecName,
isDecoder
? TransformationException.ERROR_CODE_DECODER_INIT_FAILED
Expand All @@ -325,10 +328,9 @@ private static TransformationException createInitializationTransformationExcepti
if (cause instanceof IllegalArgumentException) {
return TransformationException.createForCodec(
cause,
mediaFormat,
format,
isVideo,
isDecoder,
mediaFormat,
mediaCodecName,
isDecoder
? TransformationException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public Codec createForAudioEncoding(Format format, List<String> allowedMimeTypes
} else {
throw TransformationException.createForCodec(
new IllegalArgumentException("The requested output format is not supported."),
format,
/* isVideo= */ false,
/* isDecoder= */ false,
format,
/* mediaCodecName= */ null,
TransformationException.ERROR_CODE_OUTPUT_FORMAT_UNSUPPORTED);
}
Expand Down Expand Up @@ -121,9 +121,9 @@ public Codec createForVideoEncoding(Format format, List<String> allowedMimeTypes
if (encoderAndClosestFormatSupport == null) {
throw TransformationException.createForCodec(
new IllegalArgumentException("The requested output format is not supported."),
format,
/* isVideo= */ true,
/* isDecoder= */ false,
format,
/* mediaCodecName= */ null,
TransformationException.ERROR_CODE_OUTPUT_FORMAT_UNSUPPORTED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,56 +207,55 @@ public final String getErrorCodeName() {
/**
* 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 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 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 errorCode See {@link #errorCode}.
* @return The created instance.
*/
public static TransformationException createForCodec(
Throwable cause,
@Nullable MediaFormat mediaFormat,
Format format,
boolean isVideo,
boolean isDecoder,
MediaFormat mediaFormat,
@Nullable String mediaCodecName,
int errorCode) {
String componentName = (isVideo ? "Video" : "Audio") + (isDecoder ? "Decoder" : "Encoder");
String errorMessage =
componentName
+ " error, format="
+ format
+ ", mediaCodecName="
+ mediaCodecName
+ ", mediaFormat="
+ (mediaFormat == null ? "no configured MediaFormat" : mediaFormat.toString());
componentName + ", mediaFormat=" + mediaFormat + ", mediaCodecName=" + mediaCodecName;
return new TransformationException(errorMessage, cause, errorCode);
}

/**
* 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 format The {@link Format} used for configuring the {@link Codec}.
* @param isVideo Whether the decoder or encoder is configured for video.
* @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 errorCode See {@link #errorCode}.
* @return The created instance.
*/
public static TransformationException createForCodec(
Throwable cause,
Format format,
boolean isVideo,
boolean isDecoder,
Format format,
@Nullable String mediaCodecName,
int errorCode) {
return createForCodec(
cause, /* mediaFormat= */ null, format, isVideo, isDecoder, mediaCodecName, errorCode);
String componentName = (isVideo ? "Video" : "Audio") + (isDecoder ? "Decoder" : "Encoder");
String errorMessage =
componentName + " error, format=" + format + ", mediaCodecName=" + mediaCodecName;
return new TransformationException(errorMessage, cause, errorCode);
}

/**
Expand Down

0 comments on commit 99074f7

Please sign in to comment.