Search in sources :

Example 66 with DataSpec

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");
}
Also used : ArrayList(java.util.ArrayList) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) HlsMediaPlaylist(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist) Test(org.junit.Test)

Example 67 with DataSpec

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");
}
Also used : ArrayList(java.util.ArrayList) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) HlsMediaPlaylist(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist) Test(org.junit.Test)

Example 68 with DataSpec

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;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) Uri(android.net.Uri) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) HlsMediaPlaylist(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist) HlsMultivariantPlaylist(com.google.android.exoplayer2.source.hls.playlist.HlsMultivariantPlaylist) Nullable(androidx.annotation.Nullable) HashSet(java.util.HashSet)

Example 69 with DataSpec

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));
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Uri(android.net.Uri)

Example 70 with 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);
}
Also used : CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) IOException(java.io.IOException) CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) DataSource(com.google.android.exoplayer2.upstream.DataSource) DummyDataSource(com.google.android.exoplayer2.upstream.DummyDataSource)

Aggregations

DataSpec (com.google.android.exoplayer2.upstream.DataSpec)118 Test (org.junit.Test)79 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)28 DataSource (com.google.android.exoplayer2.upstream.DataSource)22 Uri (android.net.Uri)17 Nullable (androidx.annotation.Nullable)17 IOException (java.io.IOException)17 DefaultExtractorInput (com.google.android.exoplayer2.extractor.DefaultExtractorInput)9 FakeDataSet (com.google.android.exoplayer2.testutil.FakeDataSet)9 HlsMediaPlaylist (com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist)8 ArrayList (java.util.ArrayList)7 ExtractorInput (com.google.android.exoplayer2.extractor.ExtractorInput)6 InterruptedIOException (java.io.InterruptedIOException)5 List (java.util.List)5 ContainerMediaChunk (com.google.android.exoplayer2.source.chunk.ContainerMediaChunk)4 Representation (com.google.android.exoplayer2.source.dash.manifest.Representation)4 DataSourceException (com.google.android.exoplayer2.upstream.DataSourceException)4 DataSourceInputStream (com.google.android.exoplayer2.upstream.DataSourceInputStream)4 HttpDataSource (com.google.android.exoplayer2.upstream.HttpDataSource)4 ByteBuffer (java.nio.ByteBuffer)4