Commit 15a2f47f by andrewlewis Committed by Oliver Woodman

Add support for float output in DefaultAudioSink

Also switch from using MIME types to C.ENCODING_* encodings in DefaultAudioSink.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=175936872
parent 8537376a
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
position, for fast backward seeking. The back-buffer can be configured by position, for fast backward seeking. The back-buffer can be configured by
custom `LoadControl` implementations. custom `LoadControl` implementations.
* New Cast extension: Simplifies toggling between local and Cast playbacks. * New Cast extension: Simplifies toggling between local and Cast playbacks.
* Support 32-bit PCM float output from `DefaultAudioSink`.
### 2.6.0 ### ### 2.6.0 ###
......
...@@ -127,8 +127,8 @@ public final class C { ...@@ -127,8 +127,8 @@ public final class C {
*/ */
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
@IntDef({Format.NO_VALUE, ENCODING_INVALID, ENCODING_PCM_8BIT, ENCODING_PCM_16BIT, @IntDef({Format.NO_VALUE, ENCODING_INVALID, ENCODING_PCM_8BIT, ENCODING_PCM_16BIT,
ENCODING_PCM_24BIT, ENCODING_PCM_32BIT, ENCODING_AC3, ENCODING_E_AC3, ENCODING_DTS, ENCODING_PCM_24BIT, ENCODING_PCM_32BIT, ENCODING_PCM_FLOAT, ENCODING_AC3, ENCODING_E_AC3,
ENCODING_DTS_HD}) ENCODING_DTS, ENCODING_DTS_HD})
public @interface Encoding {} public @interface Encoding {}
/** /**
...@@ -136,7 +136,7 @@ public final class C { ...@@ -136,7 +136,7 @@ public final class C {
*/ */
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
@IntDef({Format.NO_VALUE, ENCODING_INVALID, ENCODING_PCM_8BIT, ENCODING_PCM_16BIT, @IntDef({Format.NO_VALUE, ENCODING_INVALID, ENCODING_PCM_8BIT, ENCODING_PCM_16BIT,
ENCODING_PCM_24BIT, ENCODING_PCM_32BIT}) ENCODING_PCM_24BIT, ENCODING_PCM_32BIT, ENCODING_PCM_FLOAT})
public @interface PcmEncoding {} public @interface PcmEncoding {}
/** /**
* @see AudioFormat#ENCODING_INVALID * @see AudioFormat#ENCODING_INVALID
...@@ -159,6 +159,10 @@ public final class C { ...@@ -159,6 +159,10 @@ public final class C {
*/ */
public static final int ENCODING_PCM_32BIT = 0x40000000; public static final int ENCODING_PCM_32BIT = 0x40000000;
/** /**
* @see AudioFormat#ENCODING_PCM_FLOAT
*/
public static final int ENCODING_PCM_FLOAT = AudioFormat.ENCODING_PCM_FLOAT;
/**
* @see AudioFormat#ENCODING_AC3 * @see AudioFormat#ENCODING_AC3
*/ */
public static final int ENCODING_AC3 = AudioFormat.ENCODING_AC3; public static final int ENCODING_AC3 = AudioFormat.ENCODING_AC3;
......
...@@ -25,14 +25,13 @@ import java.nio.ByteBuffer; ...@@ -25,14 +25,13 @@ import java.nio.ByteBuffer;
* A sink that consumes audio data. * A sink that consumes audio data.
* <p> * <p>
* Before starting playback, specify the input audio format by calling * Before starting playback, specify the input audio format by calling
* {@link #configure(String, int, int, int, int, int[], int, int)}. * {@link #configure(int, int, int, int, int[], int, int)}.
* <p> * <p>
* Call {@link #handleBuffer(ByteBuffer, long)} to write data, and {@link #handleDiscontinuity()} * Call {@link #handleBuffer(ByteBuffer, long)} to write data, and {@link #handleDiscontinuity()}
* when the data being fed is discontinuous. Call {@link #play()} to start playing the written data. * when the data being fed is discontinuous. Call {@link #play()} to start playing the written data.
* <p> * <p>
* Call {@link #configure(String, int, int, int, int, int[], int, int)} whenever the input format * Call {@link #configure(int, int, int, int, int[], int, int)} whenever the input format changes.
* changes. The sink will be reinitialized on the next call to * The sink will be reinitialized on the next call to {@link #handleBuffer(ByteBuffer, long)}.
* {@link #handleBuffer(ByteBuffer, long)}.
* <p> * <p>
* Call {@link #reset()} to prepare the sink to receive audio data from a new playback position. * Call {@link #reset()} to prepare the sink to receive audio data from a new playback position.
* <p> * <p>
...@@ -166,13 +165,12 @@ public interface AudioSink { ...@@ -166,13 +165,12 @@ public interface AudioSink {
void setListener(Listener listener); void setListener(Listener listener);
/** /**
* Returns whether it's possible to play audio in the specified format using encoded audio * Returns whether it's possible to play audio in the specified encoding using passthrough.
* passthrough.
* *
* @param mimeType The format mime type. * @param encoding The audio encoding.
* @return Whether it's possible to play audio in the format using encoded audio passthrough. * @return Whether it's possible to play audio in the specified encoding using passthrough.
*/ */
boolean isPassthroughSupported(String mimeType); boolean isPassthroughSupported(@C.Encoding int encoding);
/** /**
* Returns the playback position in the stream starting at zero, in microseconds, or * Returns the playback position in the stream starting at zero, in microseconds, or
...@@ -186,12 +184,9 @@ public interface AudioSink { ...@@ -186,12 +184,9 @@ public interface AudioSink {
/** /**
* Configures (or reconfigures) the sink. * Configures (or reconfigures) the sink.
* *
* @param inputMimeType The MIME type of audio data provided in the input buffers. * @param inputEncoding The encoding of audio data provided in the input buffers.
* @param inputChannelCount The number of channels. * @param inputChannelCount The number of channels.
* @param inputSampleRate The sample rate in Hz. * @param inputSampleRate The sample rate in Hz.
* @param inputPcmEncoding For PCM formats, the encoding used. One of
* {@link C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_16BIT}, {@link C#ENCODING_PCM_24BIT}
* and {@link C#ENCODING_PCM_32BIT}.
* @param specifiedBufferSize A specific size for the playback buffer in bytes, or 0 to infer a * @param specifiedBufferSize A specific size for the playback buffer in bytes, or 0 to infer a
* suitable buffer size. * suitable buffer size.
* @param outputChannels A mapping from input to output channels that is applied to this sink's * @param outputChannels A mapping from input to output channels that is applied to this sink's
...@@ -205,9 +200,9 @@ public interface AudioSink { ...@@ -205,9 +200,9 @@ public interface AudioSink {
* immediately preceding the next call to {@link #reset()} or this method. * immediately preceding the next call to {@link #reset()} or this method.
* @throws ConfigurationException If an error occurs configuring the sink. * @throws ConfigurationException If an error occurs configuring the sink.
*/ */
void configure(String inputMimeType, int inputChannelCount, int inputSampleRate, void configure(@C.Encoding int inputEncoding, int inputChannelCount, int inputSampleRate,
@C.PcmEncoding int inputPcmEncoding, int specifiedBufferSize, @Nullable int[] outputChannels, int specifiedBufferSize, @Nullable int[] outputChannels, int trimStartSamples,
int trimStartSamples, int trimEndSamples) throws ConfigurationException; int trimEndSamples) throws ConfigurationException;
/** /**
* Starts or resumes consuming audio if initialized. * Starts or resumes consuming audio if initialized.
...@@ -228,8 +223,7 @@ public interface AudioSink { ...@@ -228,8 +223,7 @@ public interface AudioSink {
* Returns whether the data was handled in full. If the data was not handled in full then the same * Returns whether the data was handled in full. If the data was not handled in full then the same
* {@link ByteBuffer} must be provided to subsequent calls until it has been fully consumed, * {@link ByteBuffer} must be provided to subsequent calls until it has been fully consumed,
* except in the case of an intervening call to {@link #reset()} (or to * except in the case of an intervening call to {@link #reset()} (or to
* {@link #configure(String, int, int, int, int, int[], int, int)} that causes the sink to be * {@link #configure(int, int, int, int, int[], int, int)} that causes the sink to be reset).
* reset).
* *
* @param buffer The buffer containing audio data. * @param buffer The buffer containing audio data.
* @param presentationTimeUs The presentation timestamp of the buffer in microseconds. * @param presentationTimeUs The presentation timestamp of the buffer in microseconds.
......
...@@ -51,6 +51,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media ...@@ -51,6 +51,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
private boolean passthroughEnabled; private boolean passthroughEnabled;
private boolean codecNeedsDiscardChannelsWorkaround; private boolean codecNeedsDiscardChannelsWorkaround;
private android.media.MediaFormat passthroughMediaFormat; private android.media.MediaFormat passthroughMediaFormat;
@C.Encoding
private int pcmEncoding; private int pcmEncoding;
private int channelCount; private int channelCount;
private int encoderDelay; private int encoderDelay;
...@@ -226,7 +227,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media ...@@ -226,7 +227,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
* @return Whether passthrough playback is supported. * @return Whether passthrough playback is supported.
*/ */
protected boolean allowPassthrough(String mimeType) { protected boolean allowPassthrough(String mimeType) {
return audioSink.isPassthroughSupported(mimeType); return audioSink.isPassthroughSupported(MimeTypes.getEncoding(mimeType));
} }
@Override @Override
...@@ -272,10 +273,15 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media ...@@ -272,10 +273,15 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
@Override @Override
protected void onOutputFormatChanged(MediaCodec codec, MediaFormat outputFormat) protected void onOutputFormatChanged(MediaCodec codec, MediaFormat outputFormat)
throws ExoPlaybackException { throws ExoPlaybackException {
boolean passthrough = passthroughMediaFormat != null; @C.Encoding int encoding;
String mimeType = passthrough ? passthroughMediaFormat.getString(MediaFormat.KEY_MIME) MediaFormat format;
: MimeTypes.AUDIO_RAW; if (passthroughMediaFormat != null) {
MediaFormat format = passthrough ? passthroughMediaFormat : outputFormat; encoding = MimeTypes.getEncoding(passthroughMediaFormat.getString(MediaFormat.KEY_MIME));
format = passthroughMediaFormat;
} else {
encoding = pcmEncoding;
format = outputFormat;
}
int channelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT); int channelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE); int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
int[] channelMap; int[] channelMap;
...@@ -289,8 +295,8 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media ...@@ -289,8 +295,8 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
} }
try { try {
audioSink.configure(mimeType, channelCount, sampleRate, pcmEncoding, 0, channelMap, audioSink.configure(encoding, channelCount, sampleRate, 0, channelMap, encoderDelay,
encoderDelay, encoderPadding); encoderPadding);
} catch (AudioSink.ConfigurationException e) { } catch (AudioSink.ConfigurationException e) {
throw ExoPlaybackException.createForRenderer(e, getIndex()); throw ExoPlaybackException.createForRenderer(e, getIndex());
} }
......
...@@ -329,8 +329,8 @@ public abstract class SimpleDecoderAudioRenderer extends BaseRenderer implements ...@@ -329,8 +329,8 @@ public abstract class SimpleDecoderAudioRenderer extends BaseRenderer implements
if (audioTrackNeedsConfigure) { if (audioTrackNeedsConfigure) {
Format outputFormat = getOutputFormat(); Format outputFormat = getOutputFormat();
audioSink.configure(outputFormat.sampleMimeType, outputFormat.channelCount, audioSink.configure(outputFormat.pcmEncoding, outputFormat.channelCount,
outputFormat.sampleRate, outputFormat.pcmEncoding, 0, null, encoderDelay, encoderPadding); outputFormat.sampleRate, 0, null, encoderDelay, encoderPadding);
audioTrackNeedsConfigure = false; audioTrackNeedsConfigure = false;
} }
......
...@@ -208,12 +208,12 @@ public final class MimeTypes { ...@@ -208,12 +208,12 @@ public final class MimeTypes {
} }
/** /**
* Returns the {@link C}{@code .TRACK_TYPE_*} constant that corresponds to a specified mime type. * Returns the {@link C}{@code .TRACK_TYPE_*} constant that corresponds to a specified MIME type.
* {@link C#TRACK_TYPE_UNKNOWN} if the mime type is not known or the mapping cannot be * {@link C#TRACK_TYPE_UNKNOWN} if the MIME type is not known or the mapping cannot be
* established. * established.
* *
* @param mimeType The mimeType. * @param mimeType The MIME type.
* @return The {@link C}{@code .TRACK_TYPE_*} constant that corresponds to a specified mime type. * @return The {@link C}{@code .TRACK_TYPE_*} constant that corresponds to a specified MIME type.
*/ */
public static int getTrackType(String mimeType) { public static int getTrackType(String mimeType) {
if (TextUtils.isEmpty(mimeType)) { if (TextUtils.isEmpty(mimeType)) {
...@@ -240,6 +240,28 @@ public final class MimeTypes { ...@@ -240,6 +240,28 @@ public final class MimeTypes {
} }
/** /**
* Returns the {@link C}{@code .ENCODING_*} constant that corresponds to a specified MIME type, or
* {@link C#ENCODING_INVALID} if the mapping cannot be established.
*
* @param mimeType The MIME type.
* @return The {@link C}{@code .ENCODING_*} constant that corresponds to a specified MIME type.
*/
public static @C.Encoding int getEncoding(String mimeType) {
switch (mimeType) {
case MimeTypes.AUDIO_AC3:
return C.ENCODING_AC3;
case MimeTypes.AUDIO_E_AC3:
return C.ENCODING_E_AC3;
case MimeTypes.AUDIO_DTS:
return C.ENCODING_DTS;
case MimeTypes.AUDIO_DTS_HD:
return C.ENCODING_DTS_HD;
default:
return C.ENCODING_INVALID;
}
}
/**
* Equivalent to {@code getTrackType(getMediaMimeType(codec))}. * Equivalent to {@code getTrackType(getMediaMimeType(codec))}.
* *
* @param codec The codec. * @param codec The codec.
......
...@@ -826,6 +826,7 @@ public final class Util { ...@@ -826,6 +826,7 @@ public final class Util {
case C.ENCODING_PCM_24BIT: case C.ENCODING_PCM_24BIT:
return channelCount * 3; return channelCount * 3;
case C.ENCODING_PCM_32BIT: case C.ENCODING_PCM_32BIT:
case C.ENCODING_PCM_FLOAT:
return channelCount * 4; return channelCount * 4;
default: default:
throw new IllegalArgumentException(); throw new IllegalArgumentException();
......
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