Commit 8c07f1e1 by samrobinson Committed by Ian Baker

Align MediaCodec and Decoder AudioRenderer onDisabled logic.

PiperOrigin-RevId: 443156130
parent e996d48b
...@@ -137,7 +137,7 @@ public abstract class DecoderAudioRenderer< ...@@ -137,7 +137,7 @@ public abstract class DecoderAudioRenderer<
private @ReinitializationState int decoderReinitializationState; private @ReinitializationState int decoderReinitializationState;
private boolean decoderReceivedBuffers; private boolean decoderReceivedBuffers;
private boolean audioTrackNeedsConfigure; private boolean audioSinkNeedsConfigure;
private long currentPositionUs; private long currentPositionUs;
private boolean allowFirstBufferPositionDiscontinuity; private boolean allowFirstBufferPositionDiscontinuity;
...@@ -202,7 +202,7 @@ public abstract class DecoderAudioRenderer< ...@@ -202,7 +202,7 @@ public abstract class DecoderAudioRenderer<
audioSink.setListener(new AudioSinkListener()); audioSink.setListener(new AudioSinkListener());
flagsOnlyBuffer = DecoderInputBuffer.newNoDataInstance(); flagsOnlyBuffer = DecoderInputBuffer.newNoDataInstance();
decoderReinitializationState = REINITIALIZATION_STATE_NONE; decoderReinitializationState = REINITIALIZATION_STATE_NONE;
audioTrackNeedsConfigure = true; audioSinkNeedsConfigure = true;
} }
/** /**
...@@ -397,7 +397,7 @@ public abstract class DecoderAudioRenderer< ...@@ -397,7 +397,7 @@ public abstract class DecoderAudioRenderer<
releaseDecoder(); releaseDecoder();
maybeInitDecoder(); maybeInitDecoder();
// The audio track may need to be recreated once the new output format is known. // The audio track may need to be recreated once the new output format is known.
audioTrackNeedsConfigure = true; audioSinkNeedsConfigure = true;
} else { } else {
outputBuffer.release(); outputBuffer.release();
outputBuffer = null; outputBuffer = null;
...@@ -411,7 +411,7 @@ public abstract class DecoderAudioRenderer< ...@@ -411,7 +411,7 @@ public abstract class DecoderAudioRenderer<
return false; return false;
} }
if (audioTrackNeedsConfigure) { if (audioSinkNeedsConfigure) {
Format outputFormat = Format outputFormat =
getOutputFormat(decoder) getOutputFormat(decoder)
.buildUpon() .buildUpon()
...@@ -419,7 +419,7 @@ public abstract class DecoderAudioRenderer< ...@@ -419,7 +419,7 @@ public abstract class DecoderAudioRenderer<
.setEncoderPadding(encoderPadding) .setEncoderPadding(encoderPadding)
.build(); .build();
audioSink.configure(outputFormat, /* specifiedBufferSize= */ 0, /* outputChannels= */ null); audioSink.configure(outputFormat, /* specifiedBufferSize= */ 0, /* outputChannels= */ null);
audioTrackNeedsConfigure = false; audioSinkNeedsConfigure = false;
} }
if (audioSink.handleBuffer( if (audioSink.handleBuffer(
...@@ -581,7 +581,7 @@ public abstract class DecoderAudioRenderer< ...@@ -581,7 +581,7 @@ public abstract class DecoderAudioRenderer<
@Override @Override
protected void onDisabled() { protected void onDisabled() {
inputFormat = null; inputFormat = null;
audioTrackNeedsConfigure = true; audioSinkNeedsConfigure = true;
try { try {
setSourceDrmSession(null); setSourceDrmSession(null);
releaseDecoder(); releaseDecoder();
...@@ -734,7 +734,7 @@ public abstract class DecoderAudioRenderer< ...@@ -734,7 +734,7 @@ public abstract class DecoderAudioRenderer<
// There aren't any final output buffers, so release the decoder immediately. // There aren't any final output buffers, so release the decoder immediately.
releaseDecoder(); releaseDecoder();
maybeInitDecoder(); maybeInitDecoder();
audioTrackNeedsConfigure = true; audioSinkNeedsConfigure = true;
} }
} }
eventDispatcher.inputFormatChanged(inputFormat, evaluation); eventDispatcher.inputFormatChanged(inputFormat, evaluation);
......
...@@ -103,7 +103,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media ...@@ -103,7 +103,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
private long currentPositionUs; private long currentPositionUs;
private boolean allowFirstBufferPositionDiscontinuity; private boolean allowFirstBufferPositionDiscontinuity;
private boolean allowPositionDiscontinuity; private boolean allowPositionDiscontinuity;
private boolean audioSinkNeedsReset; private boolean audioSinkNeedsConfigure;
private boolean experimentalKeepAudioTrackOnSeek; private boolean experimentalKeepAudioTrackOnSeek;
...@@ -254,6 +254,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media ...@@ -254,6 +254,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
this.audioSink = audioSink; this.audioSink = audioSink;
eventDispatcher = new EventDispatcher(eventHandler, eventListener); eventDispatcher = new EventDispatcher(eventHandler, eventListener);
audioSink.setListener(new AudioSinkListener()); audioSink.setListener(new AudioSinkListener());
audioSinkNeedsConfigure = true;
} }
@Override @Override
...@@ -500,50 +501,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media ...@@ -500,50 +501,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
@Override @Override
protected void onOutputFormatChanged(Format format, @Nullable MediaFormat mediaFormat) protected void onOutputFormatChanged(Format format, @Nullable MediaFormat mediaFormat)
throws ExoPlaybackException { throws ExoPlaybackException {
Format audioSinkInputFormat; audioSinkNeedsConfigure = true;
@Nullable int[] channelMap = null;
if (decryptOnlyCodecFormat != null) { // Direct playback with a codec for decryption.
audioSinkInputFormat = decryptOnlyCodecFormat;
} else if (getCodec() == null) { // Direct playback with codec bypass.
audioSinkInputFormat = format;
} else {
@C.PcmEncoding int pcmEncoding;
if (MimeTypes.AUDIO_RAW.equals(format.sampleMimeType)) {
// For PCM streams, the encoder passes through int samples despite set to float mode.
pcmEncoding = format.pcmEncoding;
} else if (Util.SDK_INT >= 24 && mediaFormat.containsKey(MediaFormat.KEY_PCM_ENCODING)) {
pcmEncoding = mediaFormat.getInteger(MediaFormat.KEY_PCM_ENCODING);
} else if (mediaFormat.containsKey(VIVO_BITS_PER_SAMPLE_KEY)) {
pcmEncoding = Util.getPcmEncoding(mediaFormat.getInteger(VIVO_BITS_PER_SAMPLE_KEY));
} else {
// If the format is anything other than PCM then we assume that the audio decoder will
// output 16-bit PCM.
pcmEncoding = C.ENCODING_PCM_16BIT;
}
audioSinkInputFormat =
new Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_RAW)
.setPcmEncoding(pcmEncoding)
.setEncoderDelay(format.encoderDelay)
.setEncoderPadding(format.encoderPadding)
.setChannelCount(mediaFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT))
.setSampleRate(mediaFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE))
.build();
if (codecNeedsDiscardChannelsWorkaround
&& audioSinkInputFormat.channelCount == 6
&& format.channelCount < 6) {
channelMap = new int[format.channelCount];
for (int i = 0; i < format.channelCount; i++) {
channelMap[i] = i;
}
}
}
try {
audioSink.configure(audioSinkInputFormat, /* specifiedBufferSize= */ 0, channelMap);
} catch (AudioSink.ConfigurationException e) {
throw createRendererException(
e, e.format, PlaybackException.ERROR_CODE_AUDIO_TRACK_INIT_FAILED);
}
} }
/** See {@link AudioSink.Listener#onPositionDiscontinuity()}. */ /** See {@link AudioSink.Listener#onPositionDiscontinuity()}. */
...@@ -595,9 +553,9 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media ...@@ -595,9 +553,9 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
@Override @Override
protected void onDisabled() { protected void onDisabled() {
audioSinkNeedsReset = true; audioSinkNeedsConfigure = true;
try { try {
audioSink.flush(); audioSink.reset();
} finally { } finally {
try { try {
super.onDisabled(); super.onDisabled();
...@@ -608,18 +566,6 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media ...@@ -608,18 +566,6 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
} }
@Override @Override
protected void onReset() {
try {
super.onReset();
} finally {
if (audioSinkNeedsReset) {
audioSinkNeedsReset = false;
audioSink.reset();
}
}
}
@Override
public boolean isEnded() { public boolean isEnded() {
return super.isEnded() && audioSink.isEnded(); return super.isEnded() && audioSink.isEnded();
} }
...@@ -689,6 +635,8 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media ...@@ -689,6 +635,8 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
return true; return true;
} }
maybeConfigureAudioSink(format, getCodecOutputMediaFormat());
if (isDecodeOnlyBuffer) { if (isDecodeOnlyBuffer) {
if (codec != null) { if (codec != null) {
codec.releaseOutputBuffer(bufferIndex, false); codec.releaseOutputBuffer(bufferIndex, false);
...@@ -871,6 +819,58 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media ...@@ -871,6 +819,58 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
} }
} }
private void maybeConfigureAudioSink(Format format, @Nullable MediaFormat mediaFormat)
throws ExoPlaybackException {
if (!audioSinkNeedsConfigure) {
return;
}
Format audioSinkInputFormat;
@Nullable int[] channelMap = null;
if (decryptOnlyCodecFormat != null) { // Direct playback with a codec for decryption.
audioSinkInputFormat = decryptOnlyCodecFormat;
} else if (getCodec() == null) { // Direct playback with codec bypass.
audioSinkInputFormat = format;
} else {
@C.PcmEncoding int pcmEncoding;
if (MimeTypes.AUDIO_RAW.equals(format.sampleMimeType)) {
// For PCM streams, the encoder passes through int samples despite set to float mode.
pcmEncoding = format.pcmEncoding;
} else if (Util.SDK_INT >= 24 && mediaFormat.containsKey(MediaFormat.KEY_PCM_ENCODING)) {
pcmEncoding = mediaFormat.getInteger(MediaFormat.KEY_PCM_ENCODING);
} else if (mediaFormat.containsKey(VIVO_BITS_PER_SAMPLE_KEY)) {
pcmEncoding = Util.getPcmEncoding(mediaFormat.getInteger(VIVO_BITS_PER_SAMPLE_KEY));
} else {
// If the format is anything other than PCM then we assume that the audio decoder will
// output 16-bit PCM.
pcmEncoding = C.ENCODING_PCM_16BIT;
}
audioSinkInputFormat =
new Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_RAW)
.setPcmEncoding(pcmEncoding)
.setEncoderDelay(format.encoderDelay)
.setEncoderPadding(format.encoderPadding)
.setChannelCount(mediaFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT))
.setSampleRate(mediaFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE))
.build();
if (codecNeedsDiscardChannelsWorkaround
&& audioSinkInputFormat.channelCount == 6
&& format.channelCount < 6) {
channelMap = new int[format.channelCount];
for (int i = 0; i < format.channelCount; i++) {
channelMap[i] = i;
}
}
}
try {
audioSink.configure(audioSinkInputFormat, /* specifiedBufferSize= */ 0, channelMap);
audioSinkNeedsConfigure = false;
} catch (AudioSink.ConfigurationException e) {
throw createRendererException(
e, e.format, PlaybackException.ERROR_CODE_AUDIO_TRACK_INIT_FAILED);
}
}
/** /**
* Returns whether the device's decoders are known to not support setting the codec operating * Returns whether the device's decoders are known to not support setting the codec operating
* rate. * rate.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment