-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This seeker is only seeking to frames that have already been read by the extractor for playback. Seeking to not-yet-read frames will be implemented in another change. Issue: #6787 PiperOrigin-RevId: 291888899
- Loading branch information
Showing
8 changed files
with
1,241 additions
and
7 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
library/extractor/src/main/java/com/google/android/exoplayer2/extractor/mp3/IndexSeeker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright (C) 2020 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.android.exoplayer2.extractor.mp3; | ||
|
||
import com.google.android.exoplayer2.C; | ||
import com.google.android.exoplayer2.extractor.SeekPoint; | ||
import com.google.android.exoplayer2.util.LongArray; | ||
import com.google.android.exoplayer2.util.Util; | ||
|
||
/** MP3 seeker that builds a time-to-byte mapping as the stream is read. */ | ||
/* package */ final class IndexSeeker implements Seeker { | ||
|
||
private static final long MIN_TIME_BETWEEN_POINTS_US = C.MICROS_PER_SECOND / 10; | ||
|
||
private final long durationUs; | ||
private final long dataEndPosition; | ||
private final LongArray timesUs; | ||
private final LongArray positions; | ||
|
||
public IndexSeeker(long durationUs, long dataStartPosition, long dataEndPosition) { | ||
this.durationUs = durationUs; | ||
this.dataEndPosition = dataEndPosition; | ||
timesUs = new LongArray(); | ||
positions = new LongArray(); | ||
timesUs.add(0L); | ||
positions.add(dataStartPosition); | ||
} | ||
|
||
@Override | ||
public long getTimeUs(long position) { | ||
int targetIndex = | ||
Util.binarySearchFloor( | ||
positions, position, /* inclusive= */ true, /* stayInBounds= */ true); | ||
return timesUs.get(targetIndex); | ||
} | ||
|
||
@Override | ||
public long getDataEndPosition() { | ||
return dataEndPosition; | ||
} | ||
|
||
@Override | ||
public boolean isSeekable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public long getDurationUs() { | ||
return durationUs; | ||
} | ||
|
||
@Override | ||
public SeekPoints getSeekPoints(long timeUs) { | ||
int targetIndex = | ||
Util.binarySearchFloor(timesUs, timeUs, /* inclusive= */ true, /* stayInBounds= */ true); | ||
SeekPoint seekPoint = new SeekPoint(timesUs.get(targetIndex), positions.get(targetIndex)); | ||
if (seekPoint.timeUs >= timeUs || targetIndex == timesUs.size() - 1) { | ||
return new SeekPoints(seekPoint); | ||
} else { | ||
SeekPoint nextSeekPoint = | ||
new SeekPoint(timesUs.get(targetIndex + 1), positions.get(targetIndex + 1)); | ||
return new SeekPoints(seekPoint, nextSeekPoint); | ||
} | ||
} | ||
|
||
/** | ||
* Adds a seek point to the index if it is sufficiently distant from the other points. | ||
* | ||
* <p>Seek points must be added in order. | ||
* | ||
* @param timeUs The time corresponding to the seek point to add in microseconds. | ||
* @param position The position corresponding to the seek point to add in bytes. | ||
*/ | ||
public void maybeAddSeekPoint(long timeUs, long position) { | ||
long lastTimeUs = timesUs.get(timesUs.size() - 1); | ||
if (timeUs - lastTimeUs < MIN_TIME_BETWEEN_POINTS_US) { | ||
return; | ||
} | ||
timesUs.add(timeUs); | ||
positions.add(position); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.