Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ public void initialize() {
}
}

// Fetch the start timestamp. Only way to do this is select tracks.
// This is very important to have a timebase e.g. for seeks that happen before any read.
for (int i = 0; i < mExtractor.getTrackCount(); i++) mExtractor.selectTrack(i);
mOriginUs = mExtractor.getSampleTime();
LOG.v("initialize(): found origin=" + mOriginUs);
for (int i = 0; i < mExtractor.getTrackCount(); i++) mExtractor.unselectTrack(i);
mInitialized = true;

// Debugging mOriginUs issues.
Expand All @@ -88,6 +82,18 @@ public void initialize() {
} */
}

/**
* Some properties are better initialized lazily instead of during initialize().
* For example, the origin - very important to have a timebase before reads and seeks -
* requires the extractor tracks to be selected, which is not true in initialize.
* Selecting and unselecting all tracks is not correct either.
*/
private void initializeLazyProperties() {
if (mOriginUs == Long.MIN_VALUE) {
mOriginUs = mExtractor.getSampleTime();
}
}

@Override
public void deinitialize() {
LOG.i("deinitialize(): deinitializing...");
Expand Down Expand Up @@ -141,6 +147,8 @@ public void releaseTrack(@NonNull TrackType type) {

@Override
public long seekTo(long desiredPositionUs) {
initializeLazyProperties();

boolean hasVideo = mSelectedTracks.contains(TrackType.VIDEO);
boolean hasAudio = mSelectedTracks.contains(TrackType.AUDIO);
LOG.i("seekTo(): seeking to " + (mOriginUs + desiredPositionUs)
Expand Down Expand Up @@ -193,6 +201,8 @@ public boolean canReadTrack(@NonNull TrackType type) {

@Override
public void readTrack(@NonNull Chunk chunk) {
initializeLazyProperties();

int index = mExtractor.getSampleTrackIndex();

int position = chunk.buffer.position();
Expand Down Expand Up @@ -236,7 +246,7 @@ public void readTrack(@NonNull Chunk chunk) {

@Override
public long getPositionUs() {
if (!isInitialized()) return 0;
if (mOriginUs == Long.MIN_VALUE) return 0;

// Return the fastest track.
// This ensures linear behavior over time: if a track is behind the other,
Expand Down