private long getLoadedPlaylistStartTimeUs(HlsMediaPlaylist oldPlaylist, HlsMediaPlaylist loadedPlaylist) { if (loadedPlaylist.hasProgramDateTime) { return loadedPlaylist.startTimeUs; } long primarySnapshotStartTimeUs = primaryUrlSnapshot != null ? primaryUrlSnapshot.startTimeUs : 0; if (oldPlaylist == null) { return primarySnapshotStartTimeUs; } int oldPlaylistSize = oldPlaylist.segments.size(); Segment firstOldOverlappingSegment = getFirstOldOverlappingSegment(oldPlaylist, loadedPlaylist); if (firstOldOverlappingSegment != null) { return oldPlaylist.startTimeUs + firstOldOverlappingSegment.relativeStartTimeUs; } else if (oldPlaylistSize == loadedPlaylist.mediaSequence - oldPlaylist.mediaSequence) { return oldPlaylist.getEndTimeUs(); } else { // No segments overlap, we assume the new playlist start coincides with the primary playlist. return primarySnapshotStartTimeUs; } }
private int getLoadedPlaylistDiscontinuitySequence(HlsMediaPlaylist oldPlaylist, HlsMediaPlaylist loadedPlaylist) { if (loadedPlaylist.hasDiscontinuitySequence) { return loadedPlaylist.discontinuitySequence; } // TODO: Improve cross-playlist discontinuity adjustment. int primaryUrlDiscontinuitySequence = primaryUrlSnapshot != null ? primaryUrlSnapshot.discontinuitySequence : 0; if (oldPlaylist == null) { return primaryUrlDiscontinuitySequence; } Segment firstOldOverlappingSegment = getFirstOldOverlappingSegment(oldPlaylist, loadedPlaylist); if (firstOldOverlappingSegment != null) { return oldPlaylist.discontinuitySequence + firstOldOverlappingSegment.relativeDiscontinuitySequence - loadedPlaylist.segments.get(0).relativeDiscontinuitySequence; } return primaryUrlDiscontinuitySequence; }
private HlsInitializationChunk buildInitializationChunk(HlsMediaPlaylist mediaPlaylist, Extractor extractor, Format format) { Segment initSegment = mediaPlaylist.initializationSegment; // The initialization segment is required before the actual media chunk. Uri initSegmentUri = UriUtil.resolveToUri(mediaPlaylist.baseUri, initSegment.url); DataSpec initDataSpec = new DataSpec(initSegmentUri, initSegment.byterangeOffset, initSegment.byterangeLength, null); return new HlsInitializationChunk(dataSource, initDataSpec, trackSelection.getSelectionReason(), trackSelection.getSelectionData(), extractor, format); }
private static Segment getFirstOldOverlappingSegment(HlsMediaPlaylist oldPlaylist, HlsMediaPlaylist loadedPlaylist) { int mediaSequenceOffset = loadedPlaylist.mediaSequence - oldPlaylist.mediaSequence; List<Segment> oldSegments = oldPlaylist.segments; return mediaSequenceOffset < oldSegments.size() ? oldSegments.get(mediaSequenceOffset) : null; }