Commit 6bd0ba88 by olly Committed by Oliver Woodman

Allow more aggressive switching for HLS with independent segments

We currently switch without downloading overlapping segments, but
we do not actually switch more aggressively. This change fixes
this. Note there's an implicit assumption made that if one media
playlist declares independent segments, the others will too. This
is almost certainly true in practice, and if it's not the penalty
isn't too bad (the player may try and switch to a higher quality
variant one segment's worth of buffer too soon).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=167120992
parent 0b78837f
......@@ -92,6 +92,7 @@ import java.util.List;
private byte[] scratchSpace;
private IOException fatalError;
private HlsUrl expectedPlaylistUrl;
private boolean independentSegments;
private Uri encryptionKeyUri;
private byte[] encryptionKey;
......@@ -206,10 +207,11 @@ import java.util.List;
int oldVariantIndex = previous == null ? C.INDEX_UNSET
: trackGroup.indexOf(previous.trackFormat);
expectedPlaylistUrl = null;
// Use start time of the previous chunk rather than its end time because switching format will
// require downloading overlapping segments.
long bufferedDurationUs = previous == null ? 0
: Math.max(0, previous.startTimeUs - playbackPositionUs);
// Unless segments are known to be independent, switching variant will require downloading
// overlapping segments. Hence we use the start time of the previous chunk rather than its end
// time for this case.
long bufferedDurationUs = previous == null ? 0 : Math.max(0,
(independentSegments ? previous.endTimeUs : previous.startTimeUs) - playbackPositionUs);
// Select the variant.
trackSelection.updateSelectedTrack(bufferedDurationUs);
......@@ -224,12 +226,13 @@ import java.util.List;
return;
}
HlsMediaPlaylist mediaPlaylist = playlistTracker.getPlaylistSnapshot(selectedUrl);
independentSegments = mediaPlaylist.hasIndependentSegmentsTag;
// Select the chunk.
int chunkMediaSequence;
if (previous == null || switchingVariant) {
long targetPositionUs = previous == null ? playbackPositionUs
: mediaPlaylist.hasIndependentSegmentsTag ? previous.endTimeUs : previous.startTimeUs;
: independentSegments ? previous.endTimeUs : previous.startTimeUs;
if (!mediaPlaylist.hasEndTag && targetPositionUs >= mediaPlaylist.getEndTimeUs()) {
// If the playlist is too old to contain the chunk, we need to refresh it.
chunkMediaSequence = mediaPlaylist.mediaSequence + mediaPlaylist.segments.size();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment