Skip to content

Commit 07ecaed

Browse files
committed
Improve originUs
1 parent 619dbb5 commit 07ecaed

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

lib/src/main/java/com/otaliastudios/transcoder/source/DefaultDataSource.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ public void initialize() {
8282
} */
8383
}
8484

85+
/**
86+
* Some properties are better initialized lazily instead of during initialize().
87+
* For example, the origin - very important to have a timebase before reads and seeks -
88+
* requires the extractor tracks to be selected, which is not true in initialize.
89+
* Selecting and unselecting all tracks is not correct either.
90+
*/
91+
private void initializeLazyProperties() {
92+
if (mOriginUs == Long.MIN_VALUE) {
93+
mOriginUs = mExtractor.getSampleTime();
94+
}
95+
}
96+
8597
@Override
8698
public void deinitialize() {
8799
LOG.i("deinitialize(): deinitializing...");
@@ -135,7 +147,7 @@ public void releaseTrack(@NonNull TrackType type) {
135147

136148
@Override
137149
public long seekTo(long desiredPositionUs) {
138-
initializeMOriginUsIfNotYet();
150+
initializeLazyProperties();
139151

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

190202
@Override
191203
public void readTrack(@NonNull Chunk chunk) {
192-
// Must be called if not to seek.
193-
initializeMOriginUsIfNotYet();
204+
initializeLazyProperties();
194205

195206
int index = mExtractor.getSampleTrackIndex();
196207

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

236247
@Override
237248
public long getPositionUs() {
238-
if (!isInitializedOriginUs()) return 0;
249+
if (mOriginUs == Long.MIN_VALUE) return 0;
239250

240251
// Return the fastest track.
241252
// This ensures linear behavior over time: if a track is behind the other,
@@ -288,14 +299,4 @@ public MediaFormat getTrackFormat(@NonNull TrackType type) {
288299
LOG.i("getTrackFormat(" + type + ")");
289300
return mFormat.getOrNull(type);
290301
}
291-
292-
private boolean isInitializedOriginUs() {
293-
return mOriginUs != Long.MIN_VALUE;
294-
}
295-
296-
private void initializeMOriginUsIfNotYet() {
297-
if (!isInitializedOriginUs()) {
298-
mOriginUs = mExtractor.getSampleTime();
299-
}
300-
}
301302
}

0 commit comments

Comments
 (0)