Skip to content

Commit ee14fe7

Browse files
committed
Align Decoder(Audio|Video)Renderer decoder re-use logic
- Fix DecoderAudioRenderer to re-init codec if the DRM session changes. - Add canKeepCodec to DecoderVideoRenderer. Previously it was assumed that the decoder could be re-used, but this will not be true in all cases for FfmpegVideoRenderer. Issue: #7079 PiperOrigin-RevId: 309935278
1 parent 2e81186 commit ee14fe7

File tree

6 files changed

+40
-7
lines changed

6 files changed

+40
-7
lines changed

extensions/av1/src/main/java/com/google/android/exoplayer2/ext/av1/Libgav1VideoRenderer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,9 @@ protected void setDecoderOutputMode(@C.VideoOutputMode int outputMode) {
162162
decoder.setOutputMode(outputMode);
163163
}
164164
}
165+
166+
@Override
167+
protected boolean canKeepCodec(Format oldFormat, Format newFormat) {
168+
return true;
169+
}
165170
}

extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegLibrary.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public static boolean isAvailable() {
5656
}
5757

5858
/** Returns the version of the underlying library if available, or null otherwise. */
59-
public static @Nullable String getVersion() {
59+
@Nullable
60+
public static String getVersion() {
6061
return isAvailable() ? ffmpegGetVersion() : null;
6162
}
6263

@@ -69,7 +70,7 @@ public static boolean supportsFormat(String mimeType) {
6970
if (!isAvailable()) {
7071
return false;
7172
}
72-
String codecName = getCodecName(mimeType);
73+
@Nullable String codecName = getCodecName(mimeType);
7374
if (codecName == null) {
7475
return false;
7576
}
@@ -84,7 +85,8 @@ public static boolean supportsFormat(String mimeType) {
8485
* Returns the name of the FFmpeg decoder that could be used to decode the format, or {@code null}
8586
* if it's unsupported.
8687
*/
87-
/* package */ static @Nullable String getCodecName(String mimeType) {
88+
@Nullable
89+
/* package */ static String getCodecName(String mimeType) {
8890
switch (mimeType) {
8991
case MimeTypes.AUDIO_AAC:
9092
return "aac";

extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegVideoRenderer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.android.exoplayer2.decoder.Decoder;
2525
import com.google.android.exoplayer2.drm.ExoMediaCrypto;
2626
import com.google.android.exoplayer2.util.TraceUtil;
27+
import com.google.android.exoplayer2.util.Util;
2728
import com.google.android.exoplayer2.video.DecoderVideoRenderer;
2829
import com.google.android.exoplayer2.video.VideoDecoderInputBuffer;
2930
import com.google.android.exoplayer2.video.VideoDecoderOutputBuffer;
@@ -113,4 +114,9 @@ protected void setDecoderOutputMode(@C.VideoOutputMode int outputMode) {
113114
}
114115
*/
115116
}
117+
118+
@Override
119+
protected boolean canKeepCodec(Format oldFormat, Format newFormat) {
120+
return Util.areEqual(oldFormat.sampleMimeType, newFormat.sampleMimeType);
121+
}
116122
}

extensions/vp9/src/main/java/com/google/android/exoplayer2/ext/vp9/LibvpxVideoRenderer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,9 @@ protected void setDecoderOutputMode(@C.VideoOutputMode int outputMode) {
168168
decoder.setOutputMode(outputMode);
169169
}
170170
}
171+
172+
@Override
173+
protected boolean canKeepCodec(Format oldFormat, Format newFormat) {
174+
return true;
175+
}
171176
}

library/core/src/main/java/com/google/android/exoplayer2/audio/DecoderAudioRenderer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ protected void onAudioTrackSkipSilenceEnabledChanged(boolean skipSilenceEnabled)
323323
*
324324
* @param oldFormat The previous format.
325325
* @param newFormat The new format.
326-
* @return True if the existing decoder can be kept.
326+
* @return Whether the existing decoder can be kept.
327327
*/
328328
protected boolean canKeepCodec(Format oldFormat, Format newFormat) {
329329
return false;
@@ -643,7 +643,9 @@ private void onInputFormatChanged(FormatHolder formatHolder) throws ExoPlaybackE
643643
Format oldFormat = inputFormat;
644644
inputFormat = newFormat;
645645

646-
if (!canKeepCodec(oldFormat, inputFormat)) {
646+
if (decoder == null) {
647+
maybeInitDecoder();
648+
} else if (sourceDrmSession != decoderDrmSession || !canKeepCodec(oldFormat, inputFormat)) {
647649
if (decoderReceivedBuffers) {
648650
// Signal end of stream and wait for any final output buffers before re-initialization.
649651
decoderReinitializationState = REINITIALIZATION_STATE_SIGNAL_END_OF_STREAM;
@@ -657,7 +659,6 @@ private void onInputFormatChanged(FormatHolder formatHolder) throws ExoPlaybackE
657659

658660
encoderDelay = inputFormat.encoderDelay;
659661
encoderPadding = inputFormat.encoderPadding;
660-
661662
eventDispatcher.inputFormatChanged(inputFormat);
662663
}
663664

library/core/src/main/java/com/google/android/exoplayer2/video/DecoderVideoRenderer.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,12 @@ protected void onInputFormatChanged(FormatHolder formatHolder) throws ExoPlaybac
379379
waitingForFirstSampleInFormat = true;
380380
Format newFormat = Assertions.checkNotNull(formatHolder.format);
381381
setSourceDrmSession(formatHolder.drmSession);
382+
Format oldFormat = inputFormat;
382383
inputFormat = newFormat;
383384

384-
if (sourceDrmSession != decoderDrmSession) {
385+
if (decoder == null) {
386+
maybeInitDecoder();
387+
} else if (sourceDrmSession != decoderDrmSession || !canKeepCodec(oldFormat, inputFormat)) {
385388
if (decoderReceivedBuffers) {
386389
// Signal end of stream and wait for any final output buffers before re-initialization.
387390
decoderReinitializationState = REINITIALIZATION_STATE_SIGNAL_END_OF_STREAM;
@@ -640,6 +643,17 @@ protected final void setOutputBufferRenderer(
640643
*/
641644
protected abstract void setDecoderOutputMode(@C.VideoOutputMode int outputMode);
642645

646+
/**
647+
* Returns whether the existing decoder can be kept for a new format.
648+
*
649+
* @param oldFormat The previous format.
650+
* @param newFormat The new format.
651+
* @return Whether the existing decoder can be kept.
652+
*/
653+
protected boolean canKeepCodec(Format oldFormat, Format newFormat) {
654+
return false;
655+
}
656+
643657
// Internal methods.
644658

645659
private void setSourceDrmSession(@Nullable DrmSession session) {

0 commit comments

Comments
 (0)