Refactor FlacStreamInfo to FlacStreamMetadata

This commit is contained in:
Venkatarama NG. Avadhani 2019-07-10 20:41:18 +05:30
parent fb1f91b2a1
commit 4b776ffe42
10 changed files with 82 additions and 72 deletions

View file

@ -9,6 +9,6 @@
-keep class com.google.android.exoplayer2.ext.flac.FlacDecoderJni {
*;
}
-keep class com.google.android.exoplayer2.util.FlacStreamInfo {
-keep class com.google.android.exoplayer2.util.FlacStreamMetadata {
*;
}

View file

@ -52,7 +52,7 @@ public final class FlacBinarySearchSeekerTest {
FlacBinarySearchSeeker seeker =
new FlacBinarySearchSeeker(
decoderJni.decodeStreamInfo(), /* firstFramePosition= */ 0, data.length, decoderJni);
decoderJni.decodeStreamMetadata(), /* firstFramePosition= */ 0, data.length, decoderJni);
SeekMap seekMap = seeker.getSeekMap();
assertThat(seekMap).isNotNull();
@ -70,7 +70,7 @@ public final class FlacBinarySearchSeekerTest {
decoderJni.setData(input);
FlacBinarySearchSeeker seeker =
new FlacBinarySearchSeeker(
decoderJni.decodeStreamInfo(), /* firstFramePosition= */ 0, data.length, decoderJni);
decoderJni.decodeStreamMetadata(), /* firstFramePosition= */ 0, data.length, decoderJni);
seeker.setSeekTargetUs(/* timeUs= */ 1000);
assertThat(seeker.isSeeking()).isTrue();

View file

@ -19,7 +19,7 @@ import com.google.android.exoplayer2.extractor.BinarySearchSeeker;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.FlacStreamInfo;
import com.google.android.exoplayer2.util.FlacStreamMetadata;
import java.io.IOException;
import java.nio.ByteBuffer;
@ -34,20 +34,20 @@ import java.nio.ByteBuffer;
private final FlacDecoderJni decoderJni;
public FlacBinarySearchSeeker(
FlacStreamInfo streamInfo,
FlacStreamMetadata streamMetadata,
long firstFramePosition,
long inputLength,
FlacDecoderJni decoderJni) {
super(
new FlacSeekTimestampConverter(streamInfo),
new FlacSeekTimestampConverter(streamMetadata),
new FlacTimestampSeeker(decoderJni),
streamInfo.durationUs(),
streamMetadata.durationUs(),
/* floorTimePosition= */ 0,
/* ceilingTimePosition= */ streamInfo.totalSamples,
/* ceilingTimePosition= */ streamMetadata.totalSamples,
/* floorBytePosition= */ firstFramePosition,
/* ceilingBytePosition= */ inputLength,
/* approxBytesPerFrame= */ streamInfo.getApproxBytesPerFrame(),
/* minimumSearchRange= */ Math.max(1, streamInfo.minFrameSize));
/* approxBytesPerFrame= */ streamMetadata.getApproxBytesPerFrame(),
/* minimumSearchRange= */ Math.max(1, streamMetadata.minFrameSize));
this.decoderJni = Assertions.checkNotNull(decoderJni);
}
@ -112,15 +112,15 @@ import java.nio.ByteBuffer;
* the timestamp for a stream seek time position.
*/
private static final class FlacSeekTimestampConverter implements SeekTimestampConverter {
private final FlacStreamInfo streamInfo;
private final FlacStreamMetadata streamMetadata;
public FlacSeekTimestampConverter(FlacStreamInfo streamInfo) {
this.streamInfo = streamInfo;
public FlacSeekTimestampConverter(FlacStreamMetadata streamMetadata) {
this.streamMetadata = streamMetadata;
}
@Override
public long timeUsToTargetTime(long timeUs) {
return Assertions.checkNotNull(streamInfo).getSampleIndex(timeUs);
return Assertions.checkNotNull(streamMetadata).getSampleIndex(timeUs);
}
}
}

View file

@ -21,7 +21,7 @@ import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.decoder.SimpleDecoder;
import com.google.android.exoplayer2.decoder.SimpleOutputBuffer;
import com.google.android.exoplayer2.util.FlacStreamInfo;
import com.google.android.exoplayer2.util.FlacStreamMetadata;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
@ -58,9 +58,9 @@ import java.util.List;
}
decoderJni = new FlacDecoderJni();
decoderJni.setData(ByteBuffer.wrap(initializationData.get(0)));
FlacStreamInfo streamInfo;
FlacStreamMetadata streamMetadata;
try {
streamInfo = decoderJni.decodeStreamInfo();
streamMetadata = decoderJni.decodeStreamMetadata();
} catch (ParserException e) {
throw new FlacDecoderException("Failed to decode StreamInfo", e);
} catch (IOException | InterruptedException e) {
@ -69,9 +69,9 @@ import java.util.List;
}
int initialInputBufferSize =
maxInputBufferSize != Format.NO_VALUE ? maxInputBufferSize : streamInfo.maxFrameSize;
maxInputBufferSize != Format.NO_VALUE ? maxInputBufferSize : streamMetadata.maxFrameSize;
setInitialInputBufferSize(initialInputBufferSize);
maxOutputBufferSize = streamInfo.maxDecodedFrameSize();
maxOutputBufferSize = streamMetadata.maxDecodedFrameSize();
}
@Override

View file

@ -19,7 +19,7 @@ import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.util.FlacStreamInfo;
import com.google.android.exoplayer2.util.FlacStreamMetadata;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.nio.ByteBuffer;
@ -142,13 +142,13 @@ import java.nio.ByteBuffer;
return byteCount;
}
/** Decodes and consumes the StreamInfo section from the FLAC stream. */
public FlacStreamInfo decodeStreamInfo() throws IOException, InterruptedException {
FlacStreamInfo streamInfo = flacDecodeMetadata(nativeDecoderContext);
if (streamInfo == null) {
/** Decodes and consumes the metadata from the FLAC stream. */
public FlacStreamMetadata decodeStreamMetadata() throws IOException, InterruptedException {
FlacStreamMetadata streamMetadata = flacDecodeMetadata(nativeDecoderContext);
if (streamMetadata == null) {
throw new ParserException("Failed to decode StreamInfo");
}
return streamInfo;
return streamMetadata;
}
/**
@ -266,7 +266,7 @@ import java.nio.ByteBuffer;
private native long flacInit();
private native FlacStreamInfo flacDecodeMetadata(long context)
private native FlacStreamMetadata flacDecodeMetadata(long context)
throws IOException, InterruptedException;
private native int flacDecodeToBuffer(long context, ByteBuffer outputBuffer)

View file

@ -34,7 +34,7 @@ import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.id3.Id3Decoder;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.FlacStreamInfo;
import com.google.android.exoplayer2.util.FlacStreamMetadata;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.ParsableByteArray;
import java.io.IOException;
@ -87,7 +87,7 @@ public final class FlacExtractor implements Extractor {
private @MonotonicNonNull TrackOutput trackOutput;
private boolean streamInfoDecoded;
private @MonotonicNonNull FlacStreamInfo streamInfo;
private @MonotonicNonNull FlacStreamMetadata streamMetadata;
private @MonotonicNonNull OutputFrameHolder outputFrameHolder;
@Nullable private Metadata id3Metadata;
@ -207,16 +207,16 @@ public final class FlacExtractor implements Extractor {
}
@RequiresNonNull({"decoderJni", "extractorOutput", "trackOutput"}) // Requires initialized.
@EnsuresNonNull({"streamInfo", "outputFrameHolder"}) // Ensures StreamInfo decoded.
@EnsuresNonNull({"streamMetadata", "outputFrameHolder"}) // Ensures StreamInfo decoded.
@SuppressWarnings({"contracts.postcondition.not.satisfied"})
private void decodeStreamInfo(ExtractorInput input) throws InterruptedException, IOException {
if (streamInfoDecoded) {
return;
}
FlacStreamInfo streamInfo;
FlacStreamMetadata streamMetadata;
try {
streamInfo = decoderJni.decodeStreamInfo();
streamMetadata = decoderJni.decodeStreamMetadata();
} catch (IOException e) {
decoderJni.reset(/* newPosition= */ 0);
input.setRetryPosition(/* position= */ 0, e);
@ -224,16 +224,16 @@ public final class FlacExtractor implements Extractor {
}
streamInfoDecoded = true;
if (this.streamInfo == null) {
this.streamInfo = streamInfo;
if (this.streamMetadata == null) {
this.streamMetadata = streamMetadata;
binarySearchSeeker =
outputSeekMap(decoderJni, streamInfo, input.getLength(), extractorOutput);
outputSeekMap(decoderJni, streamMetadata, input.getLength(), extractorOutput);
Metadata metadata = id3MetadataDisabled ? null : id3Metadata;
if (streamInfo.vorbisComments != null) {
metadata = streamInfo.vorbisComments.copyWithAppendedEntriesFrom(metadata);
if (streamMetadata.vorbisComments != null) {
metadata = streamMetadata.vorbisComments.copyWithAppendedEntriesFrom(metadata);
}
outputFormat(streamInfo, metadata, trackOutput);
outputBuffer.reset(streamInfo.maxDecodedFrameSize());
outputFormat(streamMetadata, metadata, trackOutput);
outputBuffer.reset(streamMetadata.maxDecodedFrameSize());
outputFrameHolder = new OutputFrameHolder(ByteBuffer.wrap(outputBuffer.data));
}
}
@ -273,38 +273,38 @@ public final class FlacExtractor implements Extractor {
@Nullable
private static FlacBinarySearchSeeker outputSeekMap(
FlacDecoderJni decoderJni,
FlacStreamInfo streamInfo,
FlacStreamMetadata streamMetadata,
long streamLength,
ExtractorOutput output) {
boolean hasSeekTable = decoderJni.getSeekPosition(/* timeUs= */ 0) != -1;
FlacBinarySearchSeeker binarySearchSeeker = null;
SeekMap seekMap;
if (hasSeekTable) {
seekMap = new FlacSeekMap(streamInfo.durationUs(), decoderJni);
seekMap = new FlacSeekMap(streamMetadata.durationUs(), decoderJni);
} else if (streamLength != C.LENGTH_UNSET) {
long firstFramePosition = decoderJni.getDecodePosition();
binarySearchSeeker =
new FlacBinarySearchSeeker(streamInfo, firstFramePosition, streamLength, decoderJni);
new FlacBinarySearchSeeker(streamMetadata, firstFramePosition, streamLength, decoderJni);
seekMap = binarySearchSeeker.getSeekMap();
} else {
seekMap = new SeekMap.Unseekable(streamInfo.durationUs());
seekMap = new SeekMap.Unseekable(streamMetadata.durationUs());
}
output.seekMap(seekMap);
return binarySearchSeeker;
}
private static void outputFormat(
FlacStreamInfo streamInfo, @Nullable Metadata metadata, TrackOutput output) {
FlacStreamMetadata streamMetadata, @Nullable Metadata metadata, TrackOutput output) {
Format mediaFormat =
Format.createAudioSampleFormat(
/* id= */ null,
MimeTypes.AUDIO_RAW,
/* codecs= */ null,
streamInfo.bitRate(),
streamInfo.maxDecodedFrameSize(),
streamInfo.channels,
streamInfo.sampleRate,
getPcmEncoding(streamInfo.bitsPerSample),
streamMetadata.bitRate(),
streamMetadata.maxDecodedFrameSize(),
streamMetadata.channels,
streamMetadata.sampleRate,
getPcmEncoding(streamMetadata.bitsPerSample),
/* encoderDelay= */ 0,
/* encoderPadding= */ 0,
/* initializationData= */ null,

View file

@ -124,7 +124,7 @@ DECODER_FUNC(jobject, flacDecodeMetadata, jlong jContext) {
context->parser->getStreamInfo();
jclass cls = env->FindClass("com/google/android/exoplayer2/util/"
"FlacStreamInfo");
"FlacStreamMetadata");
jmethodID constructor = env->GetMethodID(cls, "<init>",
"(IIIIIIIJLjava/util/ArrayList;)V");

View file

@ -19,7 +19,7 @@ import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.extractor.SeekPoint;
import com.google.android.exoplayer2.util.FlacStreamInfo;
import com.google.android.exoplayer2.util.FlacStreamMetadata;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.ParsableByteArray;
import com.google.android.exoplayer2.util.Util;
@ -38,7 +38,7 @@ import java.util.List;
private static final int FRAME_HEADER_SAMPLE_NUMBER_OFFSET = 4;
private FlacStreamInfo streamInfo;
private FlacStreamMetadata streamMetadata;
private FlacOggSeeker flacOggSeeker;
public static boolean verifyBitstreamType(ParsableByteArray data) {
@ -50,7 +50,7 @@ import java.util.List;
protected void reset(boolean headerData) {
super.reset(headerData);
if (headerData) {
streamInfo = null;
streamMetadata = null;
flacOggSeeker = null;
}
}
@ -71,14 +71,24 @@ import java.util.List;
protected boolean readHeaders(ParsableByteArray packet, long position, SetupData setupData)
throws IOException, InterruptedException {
byte[] data = packet.data;
if (streamInfo == null) {
streamInfo = new FlacStreamInfo(data, 17);
if (streamMetadata == null) {
streamMetadata = new FlacStreamMetadata(data, 17);
byte[] metadata = Arrays.copyOfRange(data, 9, packet.limit());
metadata[4] = (byte) 0x80; // Set the last metadata block flag, ignore the other blocks
List<byte[]> initializationData = Collections.singletonList(metadata);
setupData.format = Format.createAudioSampleFormat(null, MimeTypes.AUDIO_FLAC, null,
Format.NO_VALUE, streamInfo.bitRate(), streamInfo.channels, streamInfo.sampleRate,
initializationData, null, 0, null);
setupData.format =
Format.createAudioSampleFormat(
null,
MimeTypes.AUDIO_FLAC,
null,
Format.NO_VALUE,
streamMetadata.bitRate(),
streamMetadata.channels,
streamMetadata.sampleRate,
initializationData,
null,
0,
null);
} else if ((data[0] & 0x7F) == SEEKTABLE_PACKET_TYPE) {
flacOggSeeker = new FlacOggSeeker();
flacOggSeeker.parseSeekTable(packet);
@ -211,7 +221,7 @@ import java.util.List;
@Override
public long getDurationUs() {
return streamInfo.durationUs();
return streamMetadata.durationUs();
}
}

View file

@ -24,7 +24,7 @@ import java.util.ArrayList;
/**
* Holder for FLAC stream info.
*/
public final class FlacStreamInfo {
public final class FlacStreamMetadata {
public final int minBlockSize;
public final int maxBlockSize;
@ -40,14 +40,14 @@ public final class FlacStreamInfo {
private static final String SEPARATOR="=";
/**
* Constructs a FlacStreamInfo parsing the given binary FLAC stream info metadata structure.
* Constructs a FlacStreamMetadata parsing the given binary FLAC stream info metadata structure.
*
* @param data An array holding FLAC stream info metadata structure
* @param offset Offset of the structure in the array
* @see <a href="https://xiph.org/flac/format.html#metadata_block_streaminfo">FLAC format
* METADATA_BLOCK_STREAMINFO</a>
*/
public FlacStreamInfo(byte[] data, int offset) {
public FlacStreamMetadata(byte[] data, int offset) {
ParsableBitArray scratch = new ParsableBitArray(data);
scratch.setPosition(offset * 8);
this.minBlockSize = scratch.readBits(16);
@ -64,7 +64,7 @@ public final class FlacStreamInfo {
}
/**
* Constructs a FlacStreamInfo given the parameters.
* Constructs a FlacStreamMetadata given the parameters.
*
* @param minBlockSize Minimum block size of the FLAC stream.
* @param maxBlockSize Maximum block size of the FLAC stream.
@ -77,7 +77,7 @@ public final class FlacStreamInfo {
* @see <a href="https://xiph.org/flac/format.html#metadata_block_streaminfo">FLAC format
* METADATA_BLOCK_STREAMINFO</a>
*/
public FlacStreamInfo(
public FlacStreamMetadata(
int minBlockSize,
int maxBlockSize,
int minFrameSize,
@ -98,7 +98,7 @@ public final class FlacStreamInfo {
}
/**
* Constructs a FlacStreamInfo given the parameters.
* Constructs a FlacStreamMetadata given the parameters.
*
* @param minBlockSize Minimum block size of the FLAC stream.
* @param maxBlockSize Maximum block size of the FLAC stream.
@ -109,11 +109,11 @@ public final class FlacStreamInfo {
* @param bitsPerSample Number of bits per sample of the FLAC stream.
* @param totalSamples Total samples of the FLAC stream.
* @param vorbisCommentList An {@link ArrayList<String>} that contains vorbis comments, which will
* be converted and stored as metadata in {@link FlacStreamInfo#vorbisComments}
* be converted and stored as metadata in {@link FlacStreamMetadata#vorbisComments}
* @see <a href="https://xiph.org/flac/format.html#metadata_block_streaminfo">FLAC format
* METADATA_BLOCK_STREAMINFO</a>
*/
public FlacStreamInfo(
public FlacStreamMetadata(
int minBlockSize,
int maxBlockSize,
int minFrameSize,

View file

@ -19,12 +19,12 @@ import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.util.FlacStreamInfo;
import com.google.android.exoplayer2.util.FlacStreamMetadata;
import java.util.ArrayList;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Test for {@link FlacStreamInfo}'s conversion of {@link ArrayList} to {@link Metadata}. */
/** Test for {@link FlacStreamMetadata}'s conversion of {@link ArrayList} to {@link Metadata}. */
@RunWith(AndroidJUnit4.class)
public final class VorbisCommentDecoderTest {
@ -35,7 +35,7 @@ public final class VorbisCommentDecoderTest {
commentsList.add("Title=Song");
commentsList.add("Artist=Singer");
Metadata metadata = new FlacStreamInfo(0, 0, 0, 0, 0, 0, 0, 0, commentsList).vorbisComments;
Metadata metadata = new FlacStreamMetadata(0, 0, 0, 0, 0, 0, 0, 0, commentsList).vorbisComments;
assertThat(metadata.length()).isEqualTo(2);
VorbisCommentFrame commentFrame = (VorbisCommentFrame) metadata.get(0);
@ -50,7 +50,7 @@ public final class VorbisCommentDecoderTest {
public void decodeEmptyList() {
ArrayList<String> commentsList = new ArrayList<>();
Metadata metadata = new FlacStreamInfo(0, 0, 0, 0, 0, 0, 0, 0, commentsList).vorbisComments;
Metadata metadata = new FlacStreamMetadata(0, 0, 0, 0, 0, 0, 0, 0, commentsList).vorbisComments;
assertThat(metadata).isNull();
}
@ -62,7 +62,7 @@ public final class VorbisCommentDecoderTest {
commentsList.add("Title=Song");
commentsList.add("Artist=Sing=er");
Metadata metadata = new FlacStreamInfo(0, 0, 0, 0, 0, 0, 0, 0, commentsList).vorbisComments;
Metadata metadata = new FlacStreamMetadata(0, 0, 0, 0, 0, 0, 0, 0, commentsList).vorbisComments;
assertThat(metadata.length()).isEqualTo(2);
VorbisCommentFrame commentFrame = (VorbisCommentFrame) metadata.get(0);
@ -80,7 +80,7 @@ public final class VorbisCommentDecoderTest {
commentsList.add("TitleSong");
commentsList.add("Artist=Singer");
Metadata metadata = new FlacStreamInfo(0, 0, 0, 0, 0, 0, 0, 0, commentsList).vorbisComments;
Metadata metadata = new FlacStreamMetadata(0, 0, 0, 0, 0, 0, 0, 0, commentsList).vorbisComments;
assertThat(metadata.length()).isEqualTo(1);
VorbisCommentFrame commentFrame = (VorbisCommentFrame) metadata.get(0);