Skip to content

Commit

Permalink
Improve originUs
Browse files Browse the repository at this point in the history
  • Loading branch information
natario1 committed Aug 11, 2024
1 parent 619dbb5 commit 07ecaed
Showing 1 changed file with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,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 @@ -135,7 +147,7 @@ public void releaseTrack(@NonNull TrackType type) {

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

boolean hasVideo = mSelectedTracks.contains(TrackType.VIDEO);
boolean hasAudio = mSelectedTracks.contains(TrackType.AUDIO);
Expand Down Expand Up @@ -189,8 +201,7 @@ public boolean canReadTrack(@NonNull TrackType type) {

@Override
public void readTrack(@NonNull Chunk chunk) {
// Must be called if not to seek.
initializeMOriginUsIfNotYet();
initializeLazyProperties();

int index = mExtractor.getSampleTrackIndex();

Expand Down Expand Up @@ -235,7 +246,7 @@ public void readTrack(@NonNull Chunk chunk) {

@Override
public long getPositionUs() {
if (!isInitializedOriginUs()) 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 Expand Up @@ -288,14 +299,4 @@ public MediaFormat getTrackFormat(@NonNull TrackType type) {
LOG.i("getTrackFormat(" + type + ")");
return mFormat.getOrNull(type);
}

private boolean isInitializedOriginUs() {
return mOriginUs != Long.MIN_VALUE;
}

private void initializeMOriginUsIfNotYet() {
if (!isInitializedOriginUs()) {
mOriginUs = mExtractor.getSampleTime();
}
}
}

0 comments on commit 07ecaed

Please sign in to comment.