diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/DefaultCodec.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/DefaultCodec.java index 8412329090..f210250118 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/DefaultCodec.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/DefaultCodec.java @@ -43,6 +43,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; @@ -61,7 +64,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}. @@ -70,12 +74,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; @@ -91,7 +96,7 @@ public final class DefaultCodec implements Codec { : isDecoder ? MediaCodec.createDecoderByType(sampleMimeType) : MediaCodec.createEncoderByType(sampleMimeType); - configureCodec(mediaCodec, mediaFormat, isDecoder, outputSurface); + configureCodec(mediaCodec, configurationMediaFormat, isDecoder, outputSurface); if (isVideo && !isDecoder) { inputSurface = mediaCodec.createInputSurface(); } @@ -106,7 +111,7 @@ public final class DefaultCodec implements Codec { } throw createInitializationTransformationException( - e, mediaFormat, configurationFormat, isVideo, isDecoder, mediaCodecName); + e, configurationMediaFormat, isVideo, isDecoder, mediaCodecName); } this.mediaCodec = mediaCodec; this.inputSurface = inputSurface; @@ -292,9 +297,9 @@ public final class DefaultCodec implements Codec { boolean isVideo = MimeTypes.isVideo(configurationFormat.sampleMimeType); return TransformationException.createForCodec( cause, - configurationFormat, isVideo, isDecoder, + configurationMediaFormat, mediaCodec.getName(), isDecoder ? TransformationException.ERROR_CODE_DECODING_FAILED @@ -304,17 +309,15 @@ public final class DefaultCodec implements Codec { 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 @@ -323,10 +326,9 @@ public final class DefaultCodec implements Codec { if (cause instanceof IllegalArgumentException) { return TransformationException.createForCodec( cause, - mediaFormat, - format, isVideo, isDecoder, + mediaFormat, mediaCodecName, isDecoder ? TransformationException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/DefaultEncoderFactory.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/DefaultEncoderFactory.java index 08cffdbdad..78bc3b9fad 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/DefaultEncoderFactory.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/DefaultEncoderFactory.java @@ -79,9 +79,9 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory { } 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); } @@ -119,9 +119,9 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory { 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); } diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/TransformationException.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/TransformationException.java index 474c7124b2..ee05d1cfe8 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/TransformationException.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/TransformationException.java @@ -205,56 +205,55 @@ public final class TransformationException extends Exception { /** * Creates an instance for a decoder or encoder related exception. * + *
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. * + *
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); } /**