Skip to content

Commit

Permalink
Remove side effects from SeekMap.getPosition implementations
Browse files Browse the repository at this point in the history
Issue: #2167

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=141451444
  • Loading branch information
ojw28 committed Dec 12, 2016
1 parent f457518 commit 1055c52
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

private int state;
private long totalGranules;
private volatile long queriedGranule;
private long positionBeforeSeekToEnd;
private long targetGranule;

Expand Down Expand Up @@ -114,9 +113,9 @@ public long read(ExtractorInput input) throws IOException, InterruptedException
}

@Override
public long startSeek() {
public long startSeek(long timeUs) {
Assertions.checkArgument(state == STATE_IDLE || state == STATE_SEEK);
targetGranule = queriedGranule;
targetGranule = timeUs == 0 ? 0 : streamReader.convertTimeToGranule(timeUs);
state = STATE_SEEK;
resetSeeking();
return targetGranule;
Expand Down Expand Up @@ -222,11 +221,10 @@ public boolean isSeekable() {
@Override
public long getPosition(long timeUs) {
if (timeUs == 0) {
queriedGranule = 0;
return startPosition;
}
queriedGranule = streamReader.convertTimeToGranule(timeUs);
return getEstimatedPosition(startPosition, queriedGranule, DEFAULT_OFFSET);
long granule = streamReader.convertTimeToGranule(timeUs);
return getEstimatedPosition(startPosition, granule, DEFAULT_OFFSET);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,15 @@ private class FlacOggSeeker implements OggSeeker, SeekMap {
private static final int METADATA_LENGTH_OFFSET = 1;
private static final int SEEK_POINT_SIZE = 18;

private long[] sampleNumbers;
private long[] offsets;
private long firstFrameOffset = -1;
private volatile long queriedGranule;
private volatile long seekedGranule;
private long currentGranule = -1;
private long[] seekPointGranules;
private long[] seekPointOffsets;
private long firstFrameOffset;
private long pendingSeekGranule;

public FlacOggSeeker() {
firstFrameOffset = -1;
pendingSeekGranule = -1;
}

public void setFirstFrameOffset(long firstFrameOffset) {
this.firstFrameOffset = firstFrameOffset;
Expand All @@ -141,40 +144,40 @@ public void setFirstFrameOffset(long firstFrameOffset) {
/**
* Parses a FLAC file seek table metadata structure and initializes internal fields.
*
* @param data
* A ParsableByteArray including whole seek table metadata block. Its position should be set
* to the beginning of the block.
* @param data A {@link ParsableByteArray} including whole seek table metadata block. Its
* position should be set to the beginning of the block.
* @see <a href="https://xiph.org/flac/format.html#metadata_block_seektable">FLAC format
* METADATA_BLOCK_SEEKTABLE</a>
* METADATA_BLOCK_SEEKTABLE</a>
*/
public void parseSeekTable(ParsableByteArray data) {
data.skipBytes(METADATA_LENGTH_OFFSET);
int length = data.readUnsignedInt24();
int numberOfSeekPoints = length / SEEK_POINT_SIZE;

sampleNumbers = new long[numberOfSeekPoints];
offsets = new long[numberOfSeekPoints];

seekPointGranules = new long[numberOfSeekPoints];
seekPointOffsets = new long[numberOfSeekPoints];
for (int i = 0; i < numberOfSeekPoints; i++) {
sampleNumbers[i] = data.readLong();
offsets[i] = data.readLong();
seekPointGranules[i] = data.readLong();
seekPointOffsets[i] = data.readLong();
data.skipBytes(2); // Skip "Number of samples in the target frame."
}
}

@Override
public long read(ExtractorInput input) throws IOException, InterruptedException {
if (currentGranule >= 0) {
currentGranule = -currentGranule - 2;
return currentGranule;
if (pendingSeekGranule >= 0) {
long result = -(pendingSeekGranule + 2);
pendingSeekGranule = -1;
return result;
}
return -1;
}

@Override
public synchronized long startSeek() {
currentGranule = seekedGranule;
return queriedGranule;
public long startSeek(long timeUs) {
long granule = convertTimeToGranule(timeUs);
int index = Util.binarySearchFloor(seekPointGranules, granule, true, true);
pendingSeekGranule = seekPointGranules[index];
return granule;
}

@Override
Expand All @@ -188,11 +191,10 @@ public boolean isSeekable() {
}

@Override
public synchronized long getPosition(long timeUs) {
queriedGranule = convertTimeToGranule(timeUs);
int index = Util.binarySearchFloor(sampleNumbers, queriedGranule, true, true);
seekedGranule = sampleNumbers[index];
return firstFrameOffset + offsets[index];
public long getPosition(long timeUs) {
long granule = convertTimeToGranule(timeUs);
int index = Util.binarySearchFloor(seekPointGranules, granule, true, true);
return firstFrameOffset + seekPointOffsets[index];
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void init(ExtractorOutput output) {

@Override
public void seek(long position, long timeUs) {
streamReader.seek(position);
streamReader.seek(position, timeUs);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
/**
* Initializes a seek operation.
*
* @param timeUs The seek position in microseconds.
* @return The granule position targeted by the seek.
*/
long startSeek();
long startSeek(long timeUs);

/**
* Reads data from the {@link ExtractorInput} to build the {@link SeekMap} or to continue a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ protected void reset(boolean headerData) {
}

/**
* @see Extractor#seek(long)
* @see Extractor#seek(long, long)
*/
final void seek(long position) {
final void seek(long position, long timeUs) {
oggPacket.reset();
if (position == 0) {
reset(!seekMapSet);
} else {
if (state != STATE_READ_HEADERS) {
targetGranule = oggSeeker.startSeek();
targetGranule = oggSeeker.startSeek(timeUs);
state = STATE_READ_PAYLOAD;
}
}
Expand Down Expand Up @@ -162,7 +162,7 @@ private int readPayload(ExtractorInput input, PositionHolder seekPosition)
seekPosition.position = position;
return Extractor.RESULT_SEEK;
} else if (position < -1) {
onSeekEnd(-position - 2);
onSeekEnd(-(position + 2));
}
if (!seekMapSet) {
SeekMap seekMap = oggSeeker.createSeekMap();
Expand Down Expand Up @@ -232,7 +232,7 @@ protected abstract boolean readHeaders(ParsableByteArray packet, long position,
/**
* Called on end of seeking.
*
* @param currentGranule Current granule at the current position of input.
* @param currentGranule The granule at the current input position.
*/
protected void onSeekEnd(long currentGranule) {
this.currentGranule = currentGranule;
Expand All @@ -246,7 +246,7 @@ public long read(ExtractorInput input) throws IOException, InterruptedException
}

@Override
public long startSeek() {
public long startSeek(long timeUs) {
return 0;
}

Expand Down

0 comments on commit 1055c52

Please sign in to comment.