Skip to content

Commit 950cc70

Browse files
committed
Purely stylistic changes to FLV extractor
1 parent fb75b65 commit 950cc70

5 files changed

Lines changed: 75 additions & 74 deletions

File tree

library/src/main/java/com/google/android/exoplayer/extractor/flv/AudioTagPayloadReader.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
/**
3131
* Parses audio tags of from an FLV stream and extracts AAC frames.
3232
*/
33-
final class AudioTagPayloadReader extends TagPayloadReader {
34-
// Sound format
33+
/* package */ final class AudioTagPayloadReader extends TagPayloadReader {
34+
35+
// Audio format
3536
private static final int AUDIO_FORMAT_AAC = 10;
3637

3738
// AAC PACKET TYPE
@@ -47,48 +48,40 @@ final class AudioTagPayloadReader extends TagPayloadReader {
4748
private boolean hasParsedAudioDataHeader;
4849
private boolean hasOutputFormat;
4950

50-
5151
public AudioTagPayloadReader(TrackOutput output) {
5252
super(output);
5353
}
5454

5555
@Override
5656
public void seek() {
57-
57+
// Do nothing.
5858
}
5959

6060
@Override
61-
protected boolean parseHeader(ParsableByteArray data) throws UnsupportedTrack {
62-
// Parse audio data header, if it was not done, to extract information
63-
// about the audio codec and audio configuration.
61+
protected boolean parseHeader(ParsableByteArray data) throws UnsupportedFormatException {
62+
// Parse audio data header, if it was not done, to extract information about the audio codec
63+
// and audio configuration.
6464
if (!hasParsedAudioDataHeader) {
6565
int header = data.readUnsignedByte();
66-
int soundFormat = (header >> 4) & 0x0F;
66+
int audioFormat = (header >> 4) & 0x0F;
6767
int sampleRateIndex = (header >> 2) & 0x03;
68-
int bitsPerSample = (header & 0x02) == 0x02 ? 16 : 8;
69-
int channels = (header & 0x01) + 1;
70-
7168
if (sampleRateIndex < 0 || sampleRateIndex >= AUDIO_SAMPLING_RATE_TABLE.length) {
72-
throw new UnsupportedTrack("Invalid sample rate for the audio track");
69+
throw new UnsupportedFormatException("Invalid sample rate for the audio track");
7370
}
74-
75-
if (!hasOutputFormat) {
71+
if (audioFormat != AUDIO_FORMAT_AAC) {
7672
// TODO: Adds support for MP3 and PCM
77-
if (soundFormat != AUDIO_FORMAT_AAC) {
78-
throw new UnsupportedTrack("Audio track not supported. Format: " + soundFormat +
79-
", Sample rate: " + sampleRateIndex + ", bps: " + bitsPerSample + ", channels: " +
80-
channels);
73+
if (audioFormat != AUDIO_FORMAT_AAC) {
74+
throw new UnsupportedFormatException("Audio format not supported: " + audioFormat);
8175
}
8276
}
83-
8477
hasParsedAudioDataHeader = true;
8578
} else {
8679
// Skip header if it was parsed previously.
8780
data.skipBytes(1);
8881
}
8982

90-
// In all the cases we will be managing AAC format (otherwise an exception would be
91-
// fired so we can just always return true
83+
// In all the cases we will be managing AAC format (otherwise an exception would be fired so we
84+
// can just always return true.
9285
return true;
9386
}
9487

@@ -110,10 +103,9 @@ protected void parsePayload(ParsableByteArray data, long timeUs) {
110103
audioSpecificConfig);
111104

112105
MediaFormat mediaFormat = MediaFormat.createAudioFormat(MediaFormat.NO_VALUE,
113-
MimeTypes.AUDIO_AAC, MediaFormat.NO_VALUE, MediaFormat.NO_VALUE, durationUs,
106+
MimeTypes.AUDIO_AAC, MediaFormat.NO_VALUE, MediaFormat.NO_VALUE, getDurationUs(),
114107
audioParams.second, audioParams.first, Collections.singletonList(audioSpecificConfig),
115108
null);
116-
117109
output.format(mediaFormat);
118110
hasOutputFormat = true;
119111
} else if (packetType == AAC_PACKET_TYPE_AAC_RAW) {

library/src/main/java/com/google/android/exoplayer/extractor/flv/FlvExtractor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public int read(ExtractorInput input, PositionHolder seekPosition) throws IOExce
129129
return readSample(input);
130130
}
131131
}
132-
} catch (AudioTagPayloadReader.UnsupportedTrack unsupportedTrack) {
132+
} catch (AudioTagPayloadReader.UnsupportedFormatException unsupportedTrack) {
133133
unsupportedTrack.printStackTrace();
134134
return RESULT_END_OF_INPUT;
135135
}
@@ -186,11 +186,11 @@ private boolean readHeader(ExtractorInput input) throws IOException, Interrupted
186186
* @return True if tag header was read successfully. Otherwise, false.
187187
* @throws IOException If an error occurred reading from the source.
188188
* @throws InterruptedException If the thread was interrupted.
189-
* @throws TagPayloadReader.UnsupportedTrack If payload of the tag is using a codec non
189+
* @throws TagPayloadReader.UnsupportedFormatException If payload of the tag is using a codec non
190190
* supported codec.
191191
*/
192192
private boolean readTagHeader(ExtractorInput input) throws IOException, InterruptedException,
193-
TagPayloadReader.UnsupportedTrack {
193+
TagPayloadReader.UnsupportedFormatException {
194194
try {
195195
// skipping previous tag size field
196196
input.skipFully(4);
@@ -235,11 +235,11 @@ private boolean readTagHeader(ExtractorInput input) throws IOException, Interrup
235235
* @return One of {@link Extractor#RESULT_CONTINUE} and {@link Extractor#RESULT_END_OF_INPUT}.
236236
* @throws IOException If an error occurred reading from the source.
237237
* @throws InterruptedException If the thread was interrupted.
238-
* @throws TagPayloadReader.UnsupportedTrack If payload of the tag is using a codec non
238+
* @throws TagPayloadReader.UnsupportedFormatException If payload of the tag is using a codec non
239239
* supported codec.
240240
*/
241241
private int readSample(ExtractorInput input) throws IOException,
242-
InterruptedException, AudioTagPayloadReader.UnsupportedTrack {
242+
InterruptedException, AudioTagPayloadReader.UnsupportedFormatException {
243243
if (tagData != null) {
244244
if (!input.readFully(tagData.data, 0, currentTagHeader.dataSize, true)) {
245245
return RESULT_END_OF_INPUT;

library/src/main/java/com/google/android/exoplayer/extractor/flv/ScriptTagPayloadReader.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/**
2929
* Parses Script Data tags from an FLV stream and extracts metadata information.
3030
*/
31-
final class ScriptTagPayloadReader extends TagPayloadReader {
31+
/* package */ final class ScriptTagPayloadReader extends TagPayloadReader {
3232

3333
// AMF object types
3434
private static final int AMF_TYPE_UNKNOWN = -1;
@@ -50,19 +50,20 @@ public ScriptTagPayloadReader(TrackOutput output) {
5050

5151
@Override
5252
public void seek() {
53-
53+
// Do nothing.
5454
}
5555

5656
@Override
57-
protected boolean parseHeader(ParsableByteArray data) throws UnsupportedTrack {
57+
protected boolean parseHeader(ParsableByteArray data) throws UnsupportedFormatException {
5858
return true;
5959
}
6060

6161
@SuppressWarnings("unchecked")
6262
@Override
6363
protected void parsePayload(ParsableByteArray data, long timeUs) {
64-
// Read message name (don't storing it as we are not going to give it any use)
64+
// Read message name (don't store it because we don't yet have a use for it).
6565
readAMFData(data, AMF_TYPE_UNKNOWN);
66+
// Read message data.
6667
Object obj = readAMFData(data, AMF_TYPE_UNKNOWN);
6768

6869
if (obj instanceof Map) {
@@ -74,7 +75,7 @@ protected void parsePayload(ParsableByteArray data, long timeUs) {
7475

7576
switch (entry.getKey()) {
7677
case "duration":
77-
this.durationUs = (long)(C.MICROS_PER_SECOND * (Double)(entry.getValue()));
78+
setDurationUs((long) (C.MICROS_PER_SECOND * (Double)(entry.getValue())));
7879
break;
7980

8081
default:
@@ -198,4 +199,5 @@ private Date readAMFDate(ParsableByteArray data) {
198199
data.readUnsignedShort();
199200
return date;
200201
}
202+
201203
}

library/src/main/java/com/google/android/exoplayer/extractor/flv/TagPayloadReader.java

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,20 @@
2424
*/
2525
/* package */ abstract class TagPayloadReader {
2626

27+
/**
28+
* Thrown when the format is not supported.
29+
*/
30+
public static final class UnsupportedFormatException extends Exception {
31+
32+
public UnsupportedFormatException(String msg) {
33+
super(msg);
34+
}
35+
36+
}
37+
2738
protected final TrackOutput output;
2839

29-
// Duration of the track
30-
protected long durationUs;
40+
private long durationUs;
3141

3242
/**
3343
* @param output A {@link TrackOutput} to which samples should be written.
@@ -38,61 +48,60 @@ protected TagPayloadReader(TrackOutput output) {
3848
}
3949

4050
/**
41-
* Notifies the reader that a seek has occurred.
42-
* <p>
43-
* Following a call to this method, the data passed to the next invocation of
44-
* {@link #consume(ParsableByteArray, long)} will not be a continuation of the data that
45-
* was previously passed. Hence the reader should reset any internal state.
51+
* Sets duration in microseconds.
52+
*
53+
* @param durationUs duration in microseconds.
4654
*/
47-
public abstract void seek();
55+
public final void setDurationUs(long durationUs) {
56+
this.durationUs = durationUs;
57+
}
4858

4959
/**
50-
* Parses tag header
51-
* @param data Buffer where the tag header is stored
52-
* @return True if header was parsed successfully and then payload should be read;
53-
* Otherwise, false
54-
* @throws UnsupportedTrack
60+
* Gets the duration in microseconds.
61+
*
62+
* @return The duration in microseconds.
5563
*/
56-
protected abstract boolean parseHeader(ParsableByteArray data) throws UnsupportedTrack;
64+
public final long getDurationUs() {
65+
return durationUs;
66+
}
5767

5868
/**
59-
* Parses tag payload
60-
* @param data Buffer where tag payload is stored
61-
* @param timeUs Time position of the frame
69+
* Notifies the reader that a seek has occurred.
70+
* <p>
71+
* Following a call to this method, the data passed to the next invocation of
72+
* {@link #consume(ParsableByteArray, long)} will not be a continuation of the data that
73+
* was previously passed. Hence the reader should reset any internal state.
6274
*/
63-
protected abstract void parsePayload(ParsableByteArray data, long timeUs);
75+
public abstract void seek();
6476

6577
/**
6678
* Consumes payload data.
6779
*
6880
* @param data The payload data to consume.
6981
* @param timeUs The timestamp associated with the payload.
7082
*/
71-
public void consume(ParsableByteArray data, long timeUs) throws UnsupportedTrack {
83+
public final void consume(ParsableByteArray data, long timeUs) throws UnsupportedFormatException {
7284
if (parseHeader(data)) {
7385
parsePayload(data, timeUs);
7486
}
7587
}
7688

7789
/**
78-
* Sets duration in microseconds
79-
* @param durationUs duration in microseconds
90+
* Parses tag header.
91+
*
92+
* @param data Buffer where the tag header is stored.
93+
* @return True if the header was parsed successfully and the payload should be read. False
94+
* otherwise.
95+
* @throws UnsupportedFormatException
8096
*/
81-
public void setDurationUs(long durationUs) {
82-
this.durationUs = durationUs;
83-
}
97+
protected abstract boolean parseHeader(ParsableByteArray data) throws UnsupportedFormatException;
8498

85-
public long getDurationUs() {
86-
return durationUs;
87-
}
8899
/**
89-
* Thrown when format described in the AudioTrack is not supported
100+
* Parses tag payload.
101+
*
102+
* @param data Buffer where tag payload is stored
103+
* @param timeUs Time position of the frame
90104
*/
91-
public static final class UnsupportedTrack extends Exception {
92-
93-
public UnsupportedTrack(String msg) {
94-
super(msg);
95-
}
105+
protected abstract void parsePayload(ParsableByteArray data, long timeUs);
96106

97-
}
98107
}

library/src/main/java/com/google/android/exoplayer/extractor/flv/VideoTagPayloadReader.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
/**
3535
* Parses video tags from an FLV stream and extracts H.264 nal units.
3636
*/
37-
final class VideoTagPayloadReader extends TagPayloadReader {
37+
/* package */ final class VideoTagPayloadReader extends TagPayloadReader {
3838
private static final String TAG = "VideoTagPayloadReader";
3939

4040
// Video codec
@@ -63,25 +63,23 @@ final class VideoTagPayloadReader extends TagPayloadReader {
6363
*/
6464
public VideoTagPayloadReader(TrackOutput output) {
6565
super(output);
66-
6766
nalStartCode = new ParsableByteArray(NalUnitUtil.NAL_START_CODE);
6867
nalLength = new ParsableByteArray(4);
6968
}
7069

7170
@Override
7271
public void seek() {
73-
72+
// Do nothing.
7473
}
7574

7675
@Override
77-
protected boolean parseHeader(ParsableByteArray data) throws UnsupportedTrack {
76+
protected boolean parseHeader(ParsableByteArray data) throws UnsupportedFormatException {
7877
int header = data.readUnsignedByte();
7978
int frameType = (header >> 4) & 0x0F;
8079
int videoCodec = (header & 0x0F);
81-
8280
// Support just H.264 encoded content.
8381
if (videoCodec != VIDEO_CODEC_AVC) {
84-
throw new UnsupportedTrack("Video codec not supported. Codec: " + videoCodec);
82+
throw new UnsupportedFormatException("Video format not supported: " + videoCodec);
8583
}
8684
this.frameType = frameType;
8785
return (frameType != VIDEO_FRAME_VIDEO_INFO);
@@ -113,7 +111,7 @@ protected void parsePayload(ParsableByteArray data, long timeUs) {
113111

114112
// Construct and output the format.
115113
MediaFormat mediaFormat = MediaFormat.createVideoFormat(MediaFormat.NO_VALUE,
116-
MimeTypes.VIDEO_H264, MediaFormat.NO_VALUE, MediaFormat.NO_VALUE, durationUs,
114+
MimeTypes.VIDEO_H264, MediaFormat.NO_VALUE, MediaFormat.NO_VALUE, getDurationUs(),
117115
avcData.width, avcData.height, avcData.initializationData, MediaFormat.NO_VALUE,
118116
avcData.pixelWidthAspectRatio);
119117
output.format(mediaFormat);

0 commit comments

Comments
 (0)