@@ -82,6 +82,18 @@ public void initialize() {
82
82
} */
83
83
}
84
84
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
+
85
97
@ Override
86
98
public void deinitialize () {
87
99
LOG .i ("deinitialize(): deinitializing..." );
@@ -135,7 +147,7 @@ public void releaseTrack(@NonNull TrackType type) {
135
147
136
148
@ Override
137
149
public long seekTo (long desiredPositionUs ) {
138
- initializeMOriginUsIfNotYet ();
150
+ initializeLazyProperties ();
139
151
140
152
boolean hasVideo = mSelectedTracks .contains (TrackType .VIDEO );
141
153
boolean hasAudio = mSelectedTracks .contains (TrackType .AUDIO );
@@ -189,8 +201,7 @@ public boolean canReadTrack(@NonNull TrackType type) {
189
201
190
202
@ Override
191
203
public void readTrack (@ NonNull Chunk chunk ) {
192
- // Must be called if not to seek.
193
- initializeMOriginUsIfNotYet ();
204
+ initializeLazyProperties ();
194
205
195
206
int index = mExtractor .getSampleTrackIndex ();
196
207
@@ -235,7 +246,7 @@ public void readTrack(@NonNull Chunk chunk) {
235
246
236
247
@ Override
237
248
public long getPositionUs () {
238
- if (! isInitializedOriginUs () ) return 0 ;
249
+ if (mOriginUs == Long . MIN_VALUE ) return 0 ;
239
250
240
251
// Return the fastest track.
241
252
// This ensures linear behavior over time: if a track is behind the other,
@@ -288,14 +299,4 @@ public MediaFormat getTrackFormat(@NonNull TrackType type) {
288
299
LOG .i ("getTrackFormat(" + type + ")" );
289
300
return mFormat .getOrNull (type );
290
301
}
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
- }
301
302
}
0 commit comments