mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Allow AudioDecoderTrackRenderers to override their output formats.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=122247516
This commit is contained in:
parent
403f164b75
commit
7e58f4cd9d
1 changed files with 23 additions and 9 deletions
|
|
@ -49,7 +49,7 @@ public abstract class AudioDecoderTrackRenderer extends TrackRenderer implements
|
||||||
private final AudioTrackRendererEventListener eventListener;
|
private final AudioTrackRendererEventListener eventListener;
|
||||||
private final FormatHolder formatHolder;
|
private final FormatHolder formatHolder;
|
||||||
|
|
||||||
private Format format;
|
private Format inputFormat;
|
||||||
private SimpleDecoder<DecoderInputBuffer, ? extends SimpleOutputBuffer,
|
private SimpleDecoder<DecoderInputBuffer, ? extends SimpleOutputBuffer,
|
||||||
? extends AudioDecoderException> decoder;
|
? extends AudioDecoderException> decoder;
|
||||||
private DecoderInputBuffer inputBuffer;
|
private DecoderInputBuffer inputBuffer;
|
||||||
|
|
@ -93,7 +93,7 @@ public abstract class AudioDecoderTrackRenderer extends TrackRenderer implements
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try and read a format if we don't have one already.
|
// Try and read a format if we don't have one already.
|
||||||
if (format == null && !readFormat()) {
|
if (inputFormat == null && !readFormat()) {
|
||||||
// We can't make progress without one.
|
// We can't make progress without one.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -101,7 +101,7 @@ public abstract class AudioDecoderTrackRenderer extends TrackRenderer implements
|
||||||
// If we don't have a decoder yet, we need to instantiate one.
|
// If we don't have a decoder yet, we need to instantiate one.
|
||||||
if (decoder == null) {
|
if (decoder == null) {
|
||||||
try {
|
try {
|
||||||
decoder = createDecoder(format);
|
decoder = createDecoder(inputFormat);
|
||||||
} catch (AudioDecoderException e) {
|
} catch (AudioDecoderException e) {
|
||||||
throw ExoPlaybackException.createForRenderer(e, getIndex());
|
throw ExoPlaybackException.createForRenderer(e, getIndex());
|
||||||
}
|
}
|
||||||
|
|
@ -132,6 +132,19 @@ public abstract class AudioDecoderTrackRenderer extends TrackRenderer implements
|
||||||
protected abstract SimpleDecoder<DecoderInputBuffer, ? extends SimpleOutputBuffer,
|
protected abstract SimpleDecoder<DecoderInputBuffer, ? extends SimpleOutputBuffer,
|
||||||
? extends AudioDecoderException> createDecoder(Format format) throws AudioDecoderException;
|
? extends AudioDecoderException> createDecoder(Format format) throws AudioDecoderException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the format of audio buffers output by the decoder. Will not be called until the first
|
||||||
|
* output buffer has been dequeued, so the decoder may use input data to determine the format.
|
||||||
|
* <p>
|
||||||
|
* The default implementation returns a 16-bit PCM format with the same channel count and sample
|
||||||
|
* rate as the input.
|
||||||
|
*/
|
||||||
|
protected Format getOutputFormat() {
|
||||||
|
return Format.createAudioSampleFormat(null, MimeTypes.AUDIO_RAW, Format.NO_VALUE,
|
||||||
|
Format.NO_VALUE, inputFormat.channelCount, inputFormat.sampleRate, C.ENCODING_PCM_16BIT,
|
||||||
|
null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
private void renderBuffer() throws AudioDecoderException, AudioTrack.InitializationException,
|
private void renderBuffer() throws AudioDecoderException, AudioTrack.InitializationException,
|
||||||
AudioTrack.WriteException {
|
AudioTrack.WriteException {
|
||||||
if (outputStreamEnded) {
|
if (outputStreamEnded) {
|
||||||
|
|
@ -154,6 +167,9 @@ public abstract class AudioDecoderTrackRenderer extends TrackRenderer implements
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!audioTrack.isInitialized()) {
|
if (!audioTrack.isInitialized()) {
|
||||||
|
Format outputFormat = getOutputFormat();
|
||||||
|
audioTrack.configure(outputFormat.sampleMimeType, outputFormat.channelCount,
|
||||||
|
outputFormat.sampleRate, outputFormat.pcmEncoding);
|
||||||
if (audioSessionId != AudioTrack.SESSION_ID_NOT_SET) {
|
if (audioSessionId != AudioTrack.SESSION_ID_NOT_SET) {
|
||||||
audioTrack.initialize(audioSessionId);
|
audioTrack.initialize(audioSessionId);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -197,7 +213,7 @@ public abstract class AudioDecoderTrackRenderer extends TrackRenderer implements
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (result == TrackStream.FORMAT_READ) {
|
if (result == TrackStream.FORMAT_READ) {
|
||||||
format = formatHolder.format;
|
inputFormat = formatHolder.format;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (inputBuffer.isEndOfStream()) {
|
if (inputBuffer.isEndOfStream()) {
|
||||||
|
|
@ -227,7 +243,7 @@ public abstract class AudioDecoderTrackRenderer extends TrackRenderer implements
|
||||||
@Override
|
@Override
|
||||||
protected boolean isReady() {
|
protected boolean isReady() {
|
||||||
return audioTrack.hasPendingData()
|
return audioTrack.hasPendingData()
|
||||||
|| (format != null && (isSourceReady() || outputBuffer != null));
|
|| (inputFormat != null && (isSourceReady() || outputBuffer != null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -272,7 +288,7 @@ public abstract class AudioDecoderTrackRenderer extends TrackRenderer implements
|
||||||
protected void onDisabled() {
|
protected void onDisabled() {
|
||||||
inputBuffer = null;
|
inputBuffer = null;
|
||||||
outputBuffer = null;
|
outputBuffer = null;
|
||||||
format = null;
|
inputFormat = null;
|
||||||
audioSessionId = AudioTrack.SESSION_ID_NOT_SET;
|
audioSessionId = AudioTrack.SESSION_ID_NOT_SET;
|
||||||
try {
|
try {
|
||||||
if (decoder != null) {
|
if (decoder != null) {
|
||||||
|
|
@ -289,9 +305,7 @@ public abstract class AudioDecoderTrackRenderer extends TrackRenderer implements
|
||||||
private boolean readFormat() {
|
private boolean readFormat() {
|
||||||
int result = readSource(formatHolder, null);
|
int result = readSource(formatHolder, null);
|
||||||
if (result == TrackStream.FORMAT_READ) {
|
if (result == TrackStream.FORMAT_READ) {
|
||||||
format = formatHolder.format;
|
inputFormat = formatHolder.format;
|
||||||
audioTrack.configure(MimeTypes.AUDIO_RAW, format.channelCount, format.sampleRate,
|
|
||||||
C.ENCODING_PCM_16BIT);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue