63
63
64
64
/** A {@link MediaPeriod} that loads an HLS stream. */
65
65
@ UnstableApi
66
- public final class HlsMediaPeriod
67
- implements MediaPeriod ,
68
- HlsSampleStreamWrapper .Callback ,
69
- HlsPlaylistTracker .PlaylistEventListener {
66
+ public final class HlsMediaPeriod implements MediaPeriod , HlsPlaylistTracker .PlaylistEventListener {
70
67
71
68
private final HlsExtractorFactory extractorFactory ;
72
69
private final HlsPlaylistTracker playlistTracker ;
@@ -84,8 +81,9 @@ public final class HlsMediaPeriod
84
81
private final @ HlsMediaSource .MetadataType int metadataType ;
85
82
private final boolean useSessionKeys ;
86
83
private final PlayerId playerId ;
84
+ private final HlsSampleStreamWrapper .Callback sampleStreamWrapperCallback ;
87
85
88
- @ Nullable private Callback callback ;
86
+ @ Nullable private MediaPeriod . Callback mediaPeriodCallback ;
89
87
private int pendingPrepareCount ;
90
88
private @ MonotonicNonNull TrackGroupArray trackGroups ;
91
89
private HlsSampleStreamWrapper [] sampleStreamWrappers ;
@@ -143,6 +141,7 @@ public HlsMediaPeriod(
143
141
this .metadataType = metadataType ;
144
142
this .useSessionKeys = useSessionKeys ;
145
143
this .playerId = playerId ;
144
+ sampleStreamWrapperCallback = new SampleStreamWrapperCallback ();
146
145
compositeSequenceableLoader =
147
146
compositeSequenceableLoaderFactory .createCompositeSequenceableLoader ();
148
147
streamWrapperIndices = new IdentityHashMap <>();
@@ -157,12 +156,12 @@ public void release() {
157
156
for (HlsSampleStreamWrapper sampleStreamWrapper : sampleStreamWrappers ) {
158
157
sampleStreamWrapper .release ();
159
158
}
160
- callback = null ;
159
+ mediaPeriodCallback = null ;
161
160
}
162
161
163
162
@ Override
164
163
public void prepare (Callback callback , long positionUs ) {
165
- this .callback = callback ;
164
+ this .mediaPeriodCallback = callback ;
166
165
playlistTracker .addListener (this );
167
166
buildAndPrepareSampleStreamWrappers (positionUs );
168
167
}
@@ -439,46 +438,14 @@ public long getAdjustedSeekPositionUs(long positionUs, SeekParameters seekParame
439
438
440
439
// HlsSampleStreamWrapper.Callback implementation.
441
440
442
- @ Override
443
- public void onPrepared () {
444
- if (--pendingPrepareCount > 0 ) {
445
- return ;
446
- }
447
-
448
- int totalTrackGroupCount = 0 ;
449
- for (HlsSampleStreamWrapper sampleStreamWrapper : sampleStreamWrappers ) {
450
- totalTrackGroupCount += sampleStreamWrapper .getTrackGroups ().length ;
451
- }
452
- TrackGroup [] trackGroupArray = new TrackGroup [totalTrackGroupCount ];
453
- int trackGroupIndex = 0 ;
454
- for (HlsSampleStreamWrapper sampleStreamWrapper : sampleStreamWrappers ) {
455
- int wrapperTrackGroupCount = sampleStreamWrapper .getTrackGroups ().length ;
456
- for (int j = 0 ; j < wrapperTrackGroupCount ; j ++) {
457
- trackGroupArray [trackGroupIndex ++] = sampleStreamWrapper .getTrackGroups ().get (j );
458
- }
459
- }
460
- trackGroups = new TrackGroupArray (trackGroupArray );
461
- callback .onPrepared (this );
462
- }
463
-
464
- @ Override
465
- public void onPlaylistRefreshRequired (Uri url ) {
466
- playlistTracker .refreshPlaylist (url );
467
- }
468
-
469
- @ Override
470
- public void onContinueLoadingRequested (HlsSampleStreamWrapper sampleStreamWrapper ) {
471
- callback .onContinueLoadingRequested (this );
472
- }
473
-
474
441
// PlaylistListener implementation.
475
442
476
443
@ Override
477
444
public void onPlaylistChanged () {
478
445
for (HlsSampleStreamWrapper streamWrapper : sampleStreamWrappers ) {
479
446
streamWrapper .onPlaylistUpdated ();
480
447
}
481
- callback .onContinueLoadingRequested (this );
448
+ mediaPeriodCallback .onContinueLoadingRequested (this );
482
449
}
483
450
484
451
@ Override
@@ -488,7 +455,7 @@ public boolean onPlaylistError(
488
455
for (HlsSampleStreamWrapper streamWrapper : sampleStreamWrappers ) {
489
456
exclusionSucceeded &= streamWrapper .onPlaylistError (url , loadErrorInfo , forceRetry );
490
457
}
491
- callback .onContinueLoadingRequested (this );
458
+ mediaPeriodCallback .onContinueLoadingRequested (this );
492
459
return exclusionSucceeded ;
493
460
}
494
461
@@ -810,7 +777,7 @@ private HlsSampleStreamWrapper buildSampleStreamWrapper(
810
777
return new HlsSampleStreamWrapper (
811
778
uid ,
812
779
trackType ,
813
- /* callback= */ this ,
780
+ /* callback= */ sampleStreamWrapperCallback ,
814
781
defaultChunkSource ,
815
782
overridingDrmInitData ,
816
783
allocator ,
@@ -915,4 +882,38 @@ private static Format deriveAudioFormat(
915
882
.setLanguage (language )
916
883
.build ();
917
884
}
885
+
886
+ private class SampleStreamWrapperCallback implements HlsSampleStreamWrapper .Callback {
887
+ @ Override
888
+ public void onPrepared () {
889
+ if (--pendingPrepareCount > 0 ) {
890
+ return ;
891
+ }
892
+
893
+ int totalTrackGroupCount = 0 ;
894
+ for (HlsSampleStreamWrapper sampleStreamWrapper : sampleStreamWrappers ) {
895
+ totalTrackGroupCount += sampleStreamWrapper .getTrackGroups ().length ;
896
+ }
897
+ TrackGroup [] trackGroupArray = new TrackGroup [totalTrackGroupCount ];
898
+ int trackGroupIndex = 0 ;
899
+ for (HlsSampleStreamWrapper sampleStreamWrapper : sampleStreamWrappers ) {
900
+ int wrapperTrackGroupCount = sampleStreamWrapper .getTrackGroups ().length ;
901
+ for (int j = 0 ; j < wrapperTrackGroupCount ; j ++) {
902
+ trackGroupArray [trackGroupIndex ++] = sampleStreamWrapper .getTrackGroups ().get (j );
903
+ }
904
+ }
905
+ trackGroups = new TrackGroupArray (trackGroupArray );
906
+ mediaPeriodCallback .onPrepared (HlsMediaPeriod .this );
907
+ }
908
+
909
+ @ Override
910
+ public void onPlaylistRefreshRequired (Uri url ) {
911
+ playlistTracker .refreshPlaylist (url );
912
+ }
913
+
914
+ @ Override
915
+ public void onContinueLoadingRequested (HlsSampleStreamWrapper sampleStreamWrapper ) {
916
+ mediaPeriodCallback .onContinueLoadingRequested (HlsMediaPeriod .this );
917
+ }
918
+ }
918
919
}
0 commit comments