diff --git a/extensions/av1/src/main/java/com/google/android/exoplayer2/ext/av1/Libgav1VideoRenderer.java b/extensions/av1/src/main/java/com/google/android/exoplayer2/ext/av1/Libgav1VideoRenderer.java index fab937abc9..c07a590c68 100644 --- a/extensions/av1/src/main/java/com/google/android/exoplayer2/ext/av1/Libgav1VideoRenderer.java +++ b/extensions/av1/src/main/java/com/google/android/exoplayer2/ext/av1/Libgav1VideoRenderer.java @@ -162,4 +162,9 @@ public class Libgav1VideoRenderer extends DecoderVideoRenderer { decoder.setOutputMode(outputMode); } } + + @Override + protected boolean canKeepCodec(Format oldFormat, Format newFormat) { + return true; + } } diff --git a/extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegLibrary.java b/extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegLibrary.java index e3752aad5c..cc2a78ae86 100644 --- a/extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegLibrary.java +++ b/extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegLibrary.java @@ -56,7 +56,8 @@ public final class FfmpegLibrary { } /** Returns the version of the underlying library if available, or null otherwise. */ - public static @Nullable String getVersion() { + @Nullable + public static String getVersion() { return isAvailable() ? ffmpegGetVersion() : null; } @@ -69,7 +70,7 @@ public final class FfmpegLibrary { if (!isAvailable()) { return false; } - String codecName = getCodecName(mimeType); + @Nullable String codecName = getCodecName(mimeType); if (codecName == null) { return false; } @@ -84,7 +85,8 @@ public final class FfmpegLibrary { * Returns the name of the FFmpeg decoder that could be used to decode the format, or {@code null} * if it's unsupported. */ - /* package */ static @Nullable String getCodecName(String mimeType) { + @Nullable + /* package */ static String getCodecName(String mimeType) { switch (mimeType) { case MimeTypes.AUDIO_AAC: return "aac"; diff --git a/extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegVideoRenderer.java b/extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegVideoRenderer.java index de6635852a..6f3b8b1fc7 100644 --- a/extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegVideoRenderer.java +++ b/extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegVideoRenderer.java @@ -24,6 +24,7 @@ import com.google.android.exoplayer2.RendererCapabilities; import com.google.android.exoplayer2.decoder.Decoder; import com.google.android.exoplayer2.drm.ExoMediaCrypto; import com.google.android.exoplayer2.util.TraceUtil; +import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.video.DecoderVideoRenderer; import com.google.android.exoplayer2.video.VideoDecoderInputBuffer; import com.google.android.exoplayer2.video.VideoDecoderOutputBuffer; @@ -113,4 +114,9 @@ public final class FfmpegVideoRenderer extends DecoderVideoRenderer { } */ } + + @Override + protected boolean canKeepCodec(Format oldFormat, Format newFormat) { + return Util.areEqual(oldFormat.sampleMimeType, newFormat.sampleMimeType); + } } diff --git a/extensions/vp9/src/main/java/com/google/android/exoplayer2/ext/vp9/LibvpxVideoRenderer.java b/extensions/vp9/src/main/java/com/google/android/exoplayer2/ext/vp9/LibvpxVideoRenderer.java index d5167806f9..8f95024423 100644 --- a/extensions/vp9/src/main/java/com/google/android/exoplayer2/ext/vp9/LibvpxVideoRenderer.java +++ b/extensions/vp9/src/main/java/com/google/android/exoplayer2/ext/vp9/LibvpxVideoRenderer.java @@ -168,4 +168,9 @@ public class LibvpxVideoRenderer extends DecoderVideoRenderer { decoder.setOutputMode(outputMode); } } + + @Override + protected boolean canKeepCodec(Format oldFormat, Format newFormat) { + return true; + } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/audio/DecoderAudioRenderer.java b/library/core/src/main/java/com/google/android/exoplayer2/audio/DecoderAudioRenderer.java index cc69d5cfb3..05b70c9315 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/audio/DecoderAudioRenderer.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/audio/DecoderAudioRenderer.java @@ -323,7 +323,7 @@ public abstract class DecoderAudioRenderer extends BaseRenderer implements Media * * @param oldFormat The previous format. * @param newFormat The new format. - * @return True if the existing decoder can be kept. + * @return Whether the existing decoder can be kept. */ protected boolean canKeepCodec(Format oldFormat, Format newFormat) { return false; @@ -643,7 +643,9 @@ public abstract class DecoderAudioRenderer extends BaseRenderer implements Media Format oldFormat = inputFormat; inputFormat = newFormat; - if (!canKeepCodec(oldFormat, inputFormat)) { + if (decoder == null) { + maybeInitDecoder(); + } else if (sourceDrmSession != decoderDrmSession || !canKeepCodec(oldFormat, inputFormat)) { if (decoderReceivedBuffers) { // Signal end of stream and wait for any final output buffers before re-initialization. decoderReinitializationState = REINITIALIZATION_STATE_SIGNAL_END_OF_STREAM; @@ -657,7 +659,6 @@ public abstract class DecoderAudioRenderer extends BaseRenderer implements Media encoderDelay = inputFormat.encoderDelay; encoderPadding = inputFormat.encoderPadding; - eventDispatcher.inputFormatChanged(inputFormat); } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/video/DecoderVideoRenderer.java b/library/core/src/main/java/com/google/android/exoplayer2/video/DecoderVideoRenderer.java index 72c4ac2956..23cae05a03 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/video/DecoderVideoRenderer.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/video/DecoderVideoRenderer.java @@ -379,9 +379,12 @@ public abstract class DecoderVideoRenderer extends BaseRenderer { waitingForFirstSampleInFormat = true; Format newFormat = Assertions.checkNotNull(formatHolder.format); setSourceDrmSession(formatHolder.drmSession); + Format oldFormat = inputFormat; inputFormat = newFormat; - if (sourceDrmSession != decoderDrmSession) { + if (decoder == null) { + maybeInitDecoder(); + } else if (sourceDrmSession != decoderDrmSession || !canKeepCodec(oldFormat, inputFormat)) { if (decoderReceivedBuffers) { // Signal end of stream and wait for any final output buffers before re-initialization. decoderReinitializationState = REINITIALIZATION_STATE_SIGNAL_END_OF_STREAM; @@ -640,6 +643,17 @@ public abstract class DecoderVideoRenderer extends BaseRenderer { */ protected abstract void setDecoderOutputMode(@C.VideoOutputMode int outputMode); + /** + * Returns whether the existing decoder can be kept for a new format. + * + * @param oldFormat The previous format. + * @param newFormat The new format. + * @return Whether the existing decoder can be kept. + */ + protected boolean canKeepCodec(Format oldFormat, Format newFormat) { + return false; + } + // Internal methods. private void setSourceDrmSession(@Nullable DrmSession session) {