mirror of
https://github.com/androidx/media.git
synced 2025-05-03 21:57:46 +08:00
Clean up AudioTagPayloadReader
This commit is contained in:
parent
6e6f912ed1
commit
7a18738dfa
@ -15,8 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.google.android.exoplayer2.extractor.flv;
|
package com.google.android.exoplayer2.extractor.flv;
|
||||||
|
|
||||||
import android.media.AudioFormat;
|
|
||||||
import android.media.AudioTrack;
|
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
import com.google.android.exoplayer2.C;
|
import com.google.android.exoplayer2.C;
|
||||||
import com.google.android.exoplayer2.Format;
|
import com.google.android.exoplayer2.Format;
|
||||||
@ -31,20 +29,13 @@ import java.util.Collections;
|
|||||||
*/
|
*/
|
||||||
/* package */ final class AudioTagPayloadReader extends TagPayloadReader {
|
/* package */ final class AudioTagPayloadReader extends TagPayloadReader {
|
||||||
|
|
||||||
// Audio format
|
|
||||||
private static final int AUDIO_FORMAT_ALAW = 7;
|
private static final int AUDIO_FORMAT_ALAW = 7;
|
||||||
private static final int AUDIO_FORMAT_ULAW = 8;
|
private static final int AUDIO_FORMAT_ULAW = 8;
|
||||||
private static final int AUDIO_FORMAT_AAC = 10;
|
private static final int AUDIO_FORMAT_AAC = 10;
|
||||||
|
|
||||||
// AAC PACKET TYPE
|
|
||||||
private static final int AAC_PACKET_TYPE_SEQUENCE_HEADER = 0;
|
private static final int AAC_PACKET_TYPE_SEQUENCE_HEADER = 0;
|
||||||
private static final int AAC_PACKET_TYPE_AAC_RAW = 1;
|
private static final int AAC_PACKET_TYPE_AAC_RAW = 1;
|
||||||
|
|
||||||
// SAMPLING RATES
|
|
||||||
private static final int[] AUDIO_SAMPLING_RATE_TABLE = new int[] {
|
|
||||||
5500, 11000, 22000, 44000
|
|
||||||
};
|
|
||||||
|
|
||||||
// State variables
|
// State variables
|
||||||
private boolean hasParsedAudioDataHeader;
|
private boolean hasParsedAudioDataHeader;
|
||||||
private boolean hasOutputFormat;
|
private boolean hasOutputFormat;
|
||||||
@ -64,25 +55,18 @@ import java.util.Collections;
|
|||||||
if (!hasParsedAudioDataHeader) {
|
if (!hasParsedAudioDataHeader) {
|
||||||
int header = data.readUnsignedByte();
|
int header = data.readUnsignedByte();
|
||||||
audioFormat = (header >> 4) & 0x0F;
|
audioFormat = (header >> 4) & 0x0F;
|
||||||
int sampleRateIndex = (header >> 2) & 0x03;
|
|
||||||
int encodingSize = header & 0x01;
|
|
||||||
if (sampleRateIndex < 0 || sampleRateIndex >= AUDIO_SAMPLING_RATE_TABLE.length) {
|
|
||||||
throw new UnsupportedFormatException("Invalid sample rate index: " + sampleRateIndex);
|
|
||||||
}
|
|
||||||
// TODO: Add support for MP3.
|
// TODO: Add support for MP3.
|
||||||
if (audioFormat == AUDIO_FORMAT_ALAW || audioFormat == AUDIO_FORMAT_ULAW) {
|
if (audioFormat == AUDIO_FORMAT_ALAW || audioFormat == AUDIO_FORMAT_ULAW) {
|
||||||
|
String type = audioFormat == AUDIO_FORMAT_ALAW ? MimeTypes.AUDIO_ALAW
|
||||||
String type = (audioFormat == AUDIO_FORMAT_ALAW) ? MimeTypes.AUDIO_ALAW : MimeTypes.AUDIO_ULAW;
|
: MimeTypes.AUDIO_ULAW;
|
||||||
int encoding = (encodingSize == 1) ? C.ENCODING_PCM_16BIT : C.ENCODING_PCM_8BIT;
|
int pcmEncoding = (header & 0x01) == 1 ? C.ENCODING_PCM_16BIT : C.ENCODING_PCM_8BIT;
|
||||||
Format format = Format.createAudioSampleFormat(null, type, null, Format.NO_VALUE, Format.NO_VALUE,
|
Format format = Format.createAudioSampleFormat(null, type, null, Format.NO_VALUE,
|
||||||
1, 8000, encoding, null, null, 0, null);
|
Format.NO_VALUE, 1, 8000, pcmEncoding, null, null, 0, null);
|
||||||
output.format(format);
|
output.format(format);
|
||||||
|
hasOutputFormat = true;
|
||||||
hasOutputFormat = true;
|
} else if (audioFormat != AUDIO_FORMAT_AAC) {
|
||||||
} else if (audioFormat != AUDIO_FORMAT_AAC ) {
|
throw new UnsupportedFormatException("Audio format not supported: " + audioFormat);
|
||||||
throw new UnsupportedFormatException("Audio format not supported: " + audioFormat);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hasParsedAudioDataHeader = true;
|
hasParsedAudioDataHeader = true;
|
||||||
} else {
|
} else {
|
||||||
// Skip header if it was parsed previously.
|
// Skip header if it was parsed previously.
|
||||||
@ -94,22 +78,22 @@ import java.util.Collections;
|
|||||||
@Override
|
@Override
|
||||||
protected void parsePayload(ParsableByteArray data, long timeUs) {
|
protected void parsePayload(ParsableByteArray data, long timeUs) {
|
||||||
int packetType = data.readUnsignedByte();
|
int packetType = data.readUnsignedByte();
|
||||||
|
|
||||||
if (packetType == AAC_PACKET_TYPE_SEQUENCE_HEADER && !hasOutputFormat) {
|
if (packetType == AAC_PACKET_TYPE_SEQUENCE_HEADER && !hasOutputFormat) {
|
||||||
// Parse sequence header just in case it was not done before.
|
// Parse the sequence header.
|
||||||
byte[] audioSpecifiConfig = new byte[data.bytesLeft()];
|
byte[] audioSpecificConfig = new byte[data.bytesLeft()];
|
||||||
data.readBytes(audioSpecifiConfig, 0, audioSpecifiConfig.length);
|
data.readBytes(audioSpecificConfig, 0, audioSpecificConfig.length);
|
||||||
Pair<Integer, Integer> audioParams = CodecSpecificDataUtil.parseAacAudioSpecificConfig(
|
Pair<Integer, Integer> audioParams = CodecSpecificDataUtil.parseAacAudioSpecificConfig(
|
||||||
audioSpecifiConfig);
|
audioSpecificConfig);
|
||||||
Format format = Format.createAudioSampleFormat(null, MimeTypes.AUDIO_AAC, null,
|
Format format = Format.createAudioSampleFormat(null, MimeTypes.AUDIO_AAC, null,
|
||||||
Format.NO_VALUE, Format.NO_VALUE, audioParams.second, audioParams.first,
|
Format.NO_VALUE, Format.NO_VALUE, audioParams.second, audioParams.first,
|
||||||
Collections.singletonList(audioSpecifiConfig), null, 0, null);
|
Collections.singletonList(audioSpecificConfig), null, 0, null);
|
||||||
output.format(format);
|
output.format(format);
|
||||||
hasOutputFormat = true;
|
hasOutputFormat = true;
|
||||||
} else if (audioFormat != AUDIO_FORMAT_AAC || packetType == AAC_PACKET_TYPE_AAC_RAW) {
|
} else if (audioFormat != AUDIO_FORMAT_AAC || packetType == AAC_PACKET_TYPE_AAC_RAW) {
|
||||||
int bytes = data.bytesLeft();
|
int sampleSize = data.bytesLeft();
|
||||||
output.sampleData(data, bytes);
|
output.sampleData(data, sampleSize);
|
||||||
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, bytes, 0, null);
|
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user