Pass correct Format to AudioSink.configure

Building on the Format that was provided on the input side of the
decoder creates a format that's a mixture of the formats on the
input and output sides of the decoder. This change instead builds
a PCM format from scratch.

PiperOrigin-RevId: 320405656
This commit is contained in:
olly 2020-07-09 16:56:09 +01:00 committed by kim-vde
parent 945471452f
commit 351b54e97f
2 changed files with 35 additions and 10 deletions

View file

@ -398,16 +398,18 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
audioSinkInputFormat = getFormatWithEncodingForPassthrough(outputFormat); audioSinkInputFormat = getFormatWithEncodingForPassthrough(outputFormat);
} else { } else {
MediaFormat mediaFormat = getCodec().getOutputFormat(); MediaFormat mediaFormat = getCodec().getOutputFormat();
@C.Encoding int encoding; @C.PcmEncoding int pcmEncoding;
if (mediaFormat.containsKey(VIVO_BITS_PER_SAMPLE_KEY)) { if (mediaFormat.containsKey(VIVO_BITS_PER_SAMPLE_KEY)) {
encoding = Util.getPcmEncoding(mediaFormat.getInteger(VIVO_BITS_PER_SAMPLE_KEY)); pcmEncoding = Util.getPcmEncoding(mediaFormat.getInteger(VIVO_BITS_PER_SAMPLE_KEY));
} else { } else {
encoding = getPcmEncoding(outputFormat); pcmEncoding = getPcmEncoding(outputFormat);
} }
audioSinkInputFormat = audioSinkInputFormat =
outputFormat new Format.Builder()
.buildUpon() .setSampleMimeType(MimeTypes.AUDIO_RAW)
.setEncoding(encoding) .setEncoding(pcmEncoding)
.setEncoderDelay(outputFormat.encoderDelay)
.setEncoderPadding(outputFormat.encoderPadding)
.setChannelCount(mediaFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT)) .setChannelCount(mediaFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT))
.setSampleRate(mediaFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE)) .setSampleRate(mediaFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE))
.build(); .build();

View file

@ -147,10 +147,16 @@ public class MediaCodecAudioRendererTest {
} while (!mediaCodecAudioRenderer.isEnded()); } while (!mediaCodecAudioRenderer.isEnded());
verify(audioSink) verify(audioSink)
.configure(AUDIO_AAC, /* specifiedBufferSize= */ 0, /* outputChannels= */ null); .configure(
getAudioSinkFormat(AUDIO_AAC),
/* specifiedBufferSize= */ 0,
/* outputChannels= */ null);
verify(audioSink) verify(audioSink)
.configure(changedFormat, /* specifiedBufferSize= */ 0, /* outputChannels= */ null); .configure(
getAudioSinkFormat(changedFormat),
/* specifiedBufferSize= */ 0,
/* outputChannels= */ null);
} }
@Test @Test
@ -195,10 +201,16 @@ public class MediaCodecAudioRendererTest {
} while (!mediaCodecAudioRenderer.isEnded()); } while (!mediaCodecAudioRenderer.isEnded());
verify(audioSink) verify(audioSink)
.configure(AUDIO_AAC, /* specifiedBufferSize= */ 0, /* outputChannels= */ null); .configure(
getAudioSinkFormat(AUDIO_AAC),
/* specifiedBufferSize= */ 0,
/* outputChannels= */ null);
verify(audioSink) verify(audioSink)
.configure(changedFormat, /* specifiedBufferSize= */ 0, /* outputChannels= */ null); .configure(
getAudioSinkFormat(changedFormat),
/* specifiedBufferSize= */ 0,
/* outputChannels= */ null);
} }
@Test @Test
@ -261,4 +273,15 @@ public class MediaCodecAudioRendererTest {
// render. // render.
exceptionThrowingRenderer.render(/* positionUs= */ 750, SystemClock.elapsedRealtime() * 1000); exceptionThrowingRenderer.render(/* positionUs= */ 750, SystemClock.elapsedRealtime() * 1000);
} }
private static Format getAudioSinkFormat(Format inputFormat) {
return new Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_RAW)
.setEncoding(C.ENCODING_PCM_16BIT)
.setChannelCount(inputFormat.channelCount)
.setSampleRate(inputFormat.sampleRate)
.setEncoderDelay(inputFormat.encoderDelay)
.setEncoderPadding(inputFormat.encoderPadding)
.build();
}
} }