diff --git a/library/src/main/java/com/google/android/exoplayer2/source/dash/DashChunkSource.java b/library/src/main/java/com/google/android/exoplayer2/source/dash/DashChunkSource.java index b19802d569..01e041a5a9 100644 --- a/library/src/main/java/com/google/android/exoplayer2/source/dash/DashChunkSource.java +++ b/library/src/main/java/com/google/android/exoplayer2/source/dash/DashChunkSource.java @@ -75,7 +75,8 @@ public class DashChunkSource implements ChunkSource { /** * @param manifestLoader The {@link Loader} being used to load manifests. * @param manifest The initial manifest. - * @param adaptationSetIndex The index of the adaptation set in the manifest. + * @param periodIndex The index of the period in the manifest. + * @param adaptationSetIndex The index of the adaptation set in the period. * @param trackGroup The track group corresponding to the adaptation set. * @param tracks The indices of the selected tracks within the adaptation set. * @param dataSource A {@link DataSource} suitable for loading the media data. @@ -85,8 +86,9 @@ public class DashChunkSource implements ChunkSource { * as the server's unix time minus the local elapsed time. If unknown, set to 0. */ public DashChunkSource(Loader manifestLoader, MediaPresentationDescription manifest, - int adaptationSetIndex, TrackGroup trackGroup, int[] tracks, DataSource dataSource, - FormatEvaluator adaptiveFormatEvaluator, long elapsedRealtimeOffsetMs, int index) { + int periodIndex, int adaptationSetIndex, TrackGroup trackGroup, int[] tracks, + DataSource dataSource, FormatEvaluator adaptiveFormatEvaluator, + long elapsedRealtimeOffsetMs) { this.manifestLoader = manifestLoader; this.manifest = manifest; this.adaptationSetIndex = adaptationSetIndex; @@ -96,8 +98,8 @@ public class DashChunkSource implements ChunkSource { this.elapsedRealtimeOffsetUs = elapsedRealtimeOffsetMs * 1000; this.evaluation = new Evaluation(); - long periodDurationUs = getPeriodDurationUs(index); - List representations = getRepresentations(index); + long periodDurationUs = getPeriodDurationUs(periodIndex); + List representations = getRepresentations(periodIndex); representationHolders = new RepresentationHolder[representations.size()]; for (int i = 0; i < representations.size(); i++) { @@ -117,11 +119,11 @@ public class DashChunkSource implements ChunkSource { } } - public void updateManifest(MediaPresentationDescription newManifest, int index) { + public void updateManifest(MediaPresentationDescription newManifest, int periodIndex) { try { manifest = newManifest; - long periodDurationUs = getPeriodDurationUs(index); - List representations = getRepresentations(index); + long periodDurationUs = getPeriodDurationUs(periodIndex); + List representations = getRepresentations(periodIndex); for (int i = 0; i < representationHolders.length; i++) { Representation representation = representations.get(i); representationHolders[i].updateRepresentation(periodDurationUs, representation); @@ -131,8 +133,8 @@ public class DashChunkSource implements ChunkSource { } } - private List getRepresentations(int index) { - return manifest.getPeriod(index).adaptationSets.get(adaptationSetIndex).representations; + private List getRepresentations(int periodIndex) { + return manifest.getPeriod(periodIndex).adaptationSets.get(adaptationSetIndex).representations; } // ChunkSource implementation. @@ -355,8 +357,8 @@ public class DashChunkSource implements ChunkSource { throw new IllegalStateException("Invalid format: " + format); } - private long getPeriodDurationUs(int index) { - long durationMs = manifest.getPeriodDuration(index); + private long getPeriodDurationUs(int periodIndex) { + long durationMs = manifest.getPeriodDuration(periodIndex); if (durationMs == -1) { return C.UNSET_TIME_US; } else { diff --git a/library/src/main/java/com/google/android/exoplayer2/source/dash/DashMediaPeriod.java b/library/src/main/java/com/google/android/exoplayer2/source/dash/DashMediaPeriod.java index 43a7ae018c..90f7e43046 100644 --- a/library/src/main/java/com/google/android/exoplayer2/source/dash/DashMediaPeriod.java +++ b/library/src/main/java/com/google/android/exoplayer2/source/dash/DashMediaPeriod.java @@ -37,6 +37,8 @@ import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSourceFactory; import com.google.android.exoplayer2.upstream.Loader; +import android.util.Pair; + import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -47,22 +49,22 @@ import java.util.List; /* package */ final class DashMediaPeriod implements MediaPeriod, SequenceableLoader.Callback> { - private final Loader loader; private final DataSourceFactory dataSourceFactory; private final BandwidthMeter bandwidthMeter; + private final int minLoadableRetryCount; private final EventDispatcher eventDispatcher; + private final long elapsedRealtimeOffset; + private final Loader loader; + private final long durationUs; + private final TrackGroupArray trackGroups; + private final int[] trackGroupAdaptationSetIndices; private ChunkSampleStream[] sampleStreams; private CompositeSequenceableLoader sequenceableLoader; private Callback callback; private Allocator allocator; - private long durationUs; - private TrackGroupArray trackGroups; - private int[] trackGroupAdaptationSetIndices; private MediaPresentationDescription manifest; private int index; - private int minLoadableRetryCount; - private long elapsedRealtimeOffset; private Period period; public DashMediaPeriod(MediaPresentationDescription manifest, int index, @@ -77,8 +79,11 @@ import java.util.List; this.eventDispatcher = eventDispatcher; this.elapsedRealtimeOffset = elapsedRealtimeOffset; this.loader = loader; - period = manifest.getPeriod(index); durationUs = manifest.dynamic ? C.UNSET_TIME_US : manifest.getPeriodDuration(index) * 1000; + period = manifest.getPeriod(index); + Pair trackGroupsAndAdaptationSetIndices = buildTrackGroups(period); + trackGroups = trackGroupsAndAdaptationSetIndices.first; + trackGroupAdaptationSetIndices = trackGroupsAndAdaptationSetIndices.second; } public void updateManifest(MediaPresentationDescription manifest, int index) { @@ -101,7 +106,6 @@ import java.util.List; this.allocator = allocator; sampleStreams = newSampleStreamArray(0); sequenceableLoader = new CompositeSequenceableLoader(sampleStreams); - buildTrackGroups(); callback.onPeriodPrepared(this); } @@ -198,9 +202,6 @@ import java.util.List; sequenceableLoader = null; callback = null; allocator = null; - durationUs = 0; - trackGroups = null; - trackGroupAdaptationSetIndices = null; } // SequenceableLoader.Callback implementation. @@ -212,9 +213,9 @@ import java.util.List; // Internal methods. - private void buildTrackGroups() { + private static Pair buildTrackGroups(Period period) { int trackGroupCount = 0; - trackGroupAdaptationSetIndices = new int[period.adaptationSets.size()]; + int[] trackGroupAdaptationSetIndices = new int[period.adaptationSets.size()]; TrackGroup[] trackGroupArray = new TrackGroup[period.adaptationSets.size()]; for (int i = 0; i < period.adaptationSets.size(); i++) { AdaptationSet adaptationSet = period.adaptationSets.get(i); @@ -236,7 +237,8 @@ import java.util.List; trackGroupCount); trackGroupArray = Arrays.copyOf(trackGroupArray, trackGroupCount); } - trackGroups = new TrackGroupArray(trackGroupArray); + TrackGroupArray trackGroups = new TrackGroupArray(trackGroupArray); + return Pair.create(trackGroups, trackGroupAdaptationSetIndices); } private ChunkSampleStream buildSampleStream(TrackSelection selection, @@ -245,14 +247,12 @@ import java.util.List; FormatEvaluator adaptiveEvaluator = selectedTracks.length > 1 ? new FormatEvaluator.AdaptiveEvaluator(bandwidthMeter) : null; int adaptationSetIndex = trackGroupAdaptationSetIndices[selection.group]; - AdaptationSet adaptationSet = period.adaptationSets.get( - adaptationSetIndex); + AdaptationSet adaptationSet = period.adaptationSets.get(adaptationSetIndex); int adaptationSetType = adaptationSet.type; - DataSource dataSource = - dataSourceFactory.createDataSource(bandwidthMeter); - DashChunkSource chunkSource = new DashChunkSource(loader, manifest, adaptationSetIndex, + DataSource dataSource = dataSourceFactory.createDataSource(bandwidthMeter); + DashChunkSource chunkSource = new DashChunkSource(loader, manifest, index, adaptationSetIndex, trackGroups.get(selection.group), selectedTracks, dataSource, adaptiveEvaluator, - elapsedRealtimeOffset, index); + elapsedRealtimeOffset); return new ChunkSampleStream<>(adaptationSetType, chunkSource, this, allocator, positionUs, minLoadableRetryCount, eventDispatcher); }