use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class HlsMediaPlaylistSegmentIteratorTest method next_conventionalLiveStartIteratorAtSecondSegment_correctElements.
@Test
public void next_conventionalLiveStartIteratorAtSecondSegment_correctElements() {
HlsMediaPlaylist mediaPlaylist = getHlsMediaPlaylist(SEGMENTS_ONLY);
HlsChunkSource.HlsMediaPlaylistSegmentIterator hlsMediaPlaylistSegmentIterator = new HlsChunkSource.HlsMediaPlaylistSegmentIterator(mediaPlaylist.baseUri, /* startOfPlaylistInPeriodUs= */
0, HlsChunkSource.getSegmentBaseList(mediaPlaylist, /* mediaSequence= */
11, /* partIndex= */
C.INDEX_UNSET));
List<DataSpec> datasSpecs = new ArrayList<>();
while (hlsMediaPlaylistSegmentIterator.next()) {
datasSpecs.add(hlsMediaPlaylistSegmentIterator.getDataSpec());
}
assertThat(datasSpecs).hasSize(5);
assertThat(datasSpecs.get(0).uri.toString()).isEqualTo("fileSequence11.ts");
assertThat(Iterables.getLast(datasSpecs).uri.toString()).isEqualTo("fileSequence15.ts");
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class HlsMediaPlaylistSegmentIteratorTest method next_startIteratorAtFirstPartInaSegment_usesFullSegment.
@Test
public void next_startIteratorAtFirstPartInaSegment_usesFullSegment() {
HlsMediaPlaylist mediaPlaylist = getHlsMediaPlaylist(LOW_LATENCY_SEGMENTS_AND_PARTS);
HlsChunkSource.HlsMediaPlaylistSegmentIterator hlsMediaPlaylistSegmentIterator = new HlsChunkSource.HlsMediaPlaylistSegmentIterator(mediaPlaylist.baseUri, /* startOfPlaylistInPeriodUs= */
0, HlsChunkSource.getSegmentBaseList(mediaPlaylist, /* mediaSequence= */
14, /* partIndex= */
0));
List<DataSpec> datasSpecs = new ArrayList<>();
while (hlsMediaPlaylistSegmentIterator.next()) {
datasSpecs.add(hlsMediaPlaylistSegmentIterator.getDataSpec());
}
assertThat(datasSpecs).hasSize(5);
// The iterator starts with 6 segments.
assertThat(datasSpecs.get(0).uri.toString()).isEqualTo("fileSequence14.ts");
assertThat(datasSpecs.get(1).uri.toString()).isEqualTo("fileSequence15.ts");
// Followed by trailing parts.
assertThat(datasSpecs.get(2).uri.toString()).isEqualTo("fileSequence16.0.ts");
assertThat(datasSpecs.get(3).uri.toString()).isEqualTo("fileSequence16.1.ts");
// The preload part is the last.
assertThat(Iterables.getLast(datasSpecs).uri.toString()).isEqualTo("fileSequence16.2.ts");
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class HlsDownloader method getSegments.
@Override
protected List<Segment> getSegments(DataSource dataSource, HlsPlaylist playlist, boolean removing) throws IOException, InterruptedException {
ArrayList<DataSpec> mediaPlaylistDataSpecs = new ArrayList<>();
if (playlist instanceof HlsMultivariantPlaylist) {
HlsMultivariantPlaylist multivariantPlaylist = (HlsMultivariantPlaylist) playlist;
addMediaPlaylistDataSpecs(multivariantPlaylist.mediaPlaylistUrls, mediaPlaylistDataSpecs);
} else {
mediaPlaylistDataSpecs.add(SegmentDownloader.getCompressibleDataSpec(Uri.parse(playlist.baseUri)));
}
ArrayList<Segment> segments = new ArrayList<>();
HashSet<Uri> seenEncryptionKeyUris = new HashSet<>();
for (DataSpec mediaPlaylistDataSpec : mediaPlaylistDataSpecs) {
segments.add(new Segment(/* startTimeUs= */
0, mediaPlaylistDataSpec));
HlsMediaPlaylist mediaPlaylist;
try {
mediaPlaylist = (HlsMediaPlaylist) getManifest(dataSource, mediaPlaylistDataSpec, removing);
} catch (IOException e) {
if (!removing) {
throw e;
}
// Generating an incomplete segment list is allowed. Advance to the next media playlist.
continue;
}
@Nullable HlsMediaPlaylist.Segment lastInitSegment = null;
List<HlsMediaPlaylist.Segment> hlsSegments = mediaPlaylist.segments;
for (int i = 0; i < hlsSegments.size(); i++) {
HlsMediaPlaylist.Segment segment = hlsSegments.get(i);
HlsMediaPlaylist.Segment initSegment = segment.initializationSegment;
if (initSegment != null && initSegment != lastInitSegment) {
lastInitSegment = initSegment;
addSegment(mediaPlaylist, initSegment, seenEncryptionKeyUris, segments);
}
addSegment(mediaPlaylist, segment, seenEncryptionKeyUris, segments);
}
}
return segments;
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class HlsDownloader method addSegment.
private void addSegment(HlsMediaPlaylist mediaPlaylist, HlsMediaPlaylist.Segment segment, HashSet<Uri> seenEncryptionKeyUris, ArrayList<Segment> out) {
String baseUri = mediaPlaylist.baseUri;
long startTimeUs = mediaPlaylist.startTimeUs + segment.relativeStartTimeUs;
if (segment.fullSegmentEncryptionKeyUri != null) {
Uri keyUri = UriUtil.resolveToUri(baseUri, segment.fullSegmentEncryptionKeyUri);
if (seenEncryptionKeyUris.add(keyUri)) {
out.add(new Segment(startTimeUs, SegmentDownloader.getCompressibleDataSpec(keyUri)));
}
}
Uri segmentUri = UriUtil.resolveToUri(baseUri, segment.url);
DataSpec dataSpec = new DataSpec(segmentUri, segment.byteRangeOffset, segment.byteRangeLength);
out.add(new Segment(startTimeUs, dataSpec));
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class CacheAsserts method assertDataCached.
/**
* Asserts that the cache contains the given data for {@code dataSpec}.
*
* @throws IOException If an error occurred reading from the Cache.
*/
public static void assertDataCached(Cache cache, DataSpec dataSpec, byte[] expected) throws IOException {
DataSource dataSource = new CacheDataSource(cache, DummyDataSource.INSTANCE, 0);
byte[] bytes;
try {
dataSource.open(dataSpec);
bytes = DataSourceUtil.readToEnd(dataSource);
} catch (IOException e) {
throw new IOException("Opening/reading cache failed: " + dataSpec, e);
} finally {
dataSource.close();
}
assertWithMessage("Cached data doesn't match expected for '" + dataSpec.uri + "',").that(bytes).isEqualTo(expected);
}
Aggregations