Search in sources :

Example 1 with FakeData

use of com.google.android.exoplayer2.testutil.FakeDataSet.FakeData in project ExoPlayer by google.

the class CacheDataSourceTest method switchToCacheSourceWithNonBlockingCacheDataSource.

@Test
public void switchToCacheSourceWithNonBlockingCacheDataSource() throws Exception {
    // Create a fake data source with a 1 MB default data.
    FakeDataSource upstream = new FakeDataSource();
    FakeData fakeData = upstream.getDataSet().newDefaultData().appendReadData(1024 * 1024 - 1);
    // Insert an action just before the end of the data to fail the test if reading from upstream
    // reaches end of the data.
    fakeData.appendReadAction(() -> fail("Read from upstream shouldn't reach to the end of the data.")).appendReadData(1);
    // Lock the content on the cache.
    CacheSpan cacheSpan = cache.startReadWriteNonBlocking(defaultCacheKey, 0, C.LENGTH_UNSET);
    assertThat(cacheSpan).isNotNull();
    assertThat(cacheSpan.isHoleSpan()).isTrue();
    // Create non blocking CacheDataSource.
    CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, 0);
    // Open source and read some data from upstream without writing to cache as the data is locked.
    cacheDataSource.open(unboundedDataSpec);
    byte[] buffer = new byte[1024];
    cacheDataSource.read(buffer, 0, buffer.length);
    // Unlock the span.
    cache.releaseHoleSpan(cacheSpan);
    assertCacheEmpty(cache);
    // Cache the data. Although we use another FakeDataSource instance, it shouldn't matter.
    FakeDataSource upstream2 = new FakeDataSource(new FakeDataSource().getDataSet().newDefaultData().appendReadData(1024 * 1024).endData());
    CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, upstream2), unboundedDataSpec, /* temporaryBuffer= */
    null, /* progressListener= */
    null);
    cacheWriter.cache();
    // Read the rest of the data.
    DataSourceUtil.readToEnd(cacheDataSource);
    cacheDataSource.close();
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) FakeData(com.google.android.exoplayer2.testutil.FakeDataSet.FakeData) Test(org.junit.Test)

Example 2 with FakeData

use of com.google.android.exoplayer2.testutil.FakeDataSet.FakeData in project ExoPlayer by google.

the class FakeDataSource method read.

@Override
public final int read(byte[] buffer, int offset, int length) throws IOException {
    Assertions.checkState(sourceOpened);
    while (true) {
        FakeData fakeData = Util.castNonNull(this.fakeData);
        if (currentSegmentIndex == fakeData.getSegments().size() || bytesRemaining == 0) {
            return C.RESULT_END_OF_INPUT;
        }
        Segment current = fakeData.getSegments().get(currentSegmentIndex);
        if (current.isErrorSegment()) {
            if (!current.exceptionCleared) {
                current.exceptionThrown = true;
                throw (IOException) Util.castNonNull(current.exception).fillInStackTrace();
            } else {
                currentSegmentIndex++;
            }
        } else if (current.isActionSegment()) {
            currentSegmentIndex++;
            Util.castNonNull(current.action).run();
        } else {
            // Read at most bytesRemaining.
            length = (int) min(length, bytesRemaining);
            // Do not allow crossing of the segment boundary.
            length = min(length, current.length - current.bytesRead);
            // Perform the read and return.
            Assertions.checkArgument(buffer.length - offset >= length);
            if (current.data != null) {
                System.arraycopy(current.data, current.bytesRead, buffer, offset, length);
            }
            onDataRead(length);
            bytesTransferred(length);
            bytesRemaining -= length;
            current.bytesRead += length;
            if (current.bytesRead == current.length) {
                currentSegmentIndex++;
            }
            return length;
        }
    }
}
Also used : FakeData(com.google.android.exoplayer2.testutil.FakeDataSet.FakeData) IOException(java.io.IOException) Segment(com.google.android.exoplayer2.testutil.FakeDataSet.FakeData.Segment)

Example 3 with FakeData

use of com.google.android.exoplayer2.testutil.FakeDataSet.FakeData in project ExoPlayer by google.

the class FakeDataSource method open.

@Override
public final long open(DataSpec dataSpec) throws IOException {
    Assertions.checkState(!openCalled);
    openCalled = true;
    // DataSpec requires a matching close call even if open fails.
    uri = dataSpec.uri;
    openedDataSpecs.add(dataSpec);
    transferInitializing(dataSpec);
    FakeData fakeData = fakeDataSet.getData(dataSpec.uri.toString());
    if (fakeData == null) {
        throw new IOException("Data not found: " + dataSpec.uri);
    }
    this.fakeData = fakeData;
    long totalLength = 0;
    for (Segment segment : fakeData.getSegments()) {
        totalLength += segment.length;
    }
    if (totalLength == 0) {
        throw new IOException("Data is empty: " + dataSpec.uri);
    }
    if (dataSpec.position > totalLength) {
        throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE);
    }
    // Scan through the segments, configuring them for the current read.
    boolean findingCurrentSegmentIndex = true;
    currentSegmentIndex = 0;
    int scannedLength = 0;
    for (Segment segment : fakeData.getSegments()) {
        segment.bytesRead = (int) min(max(0, dataSpec.position - scannedLength), segment.length);
        scannedLength += segment.length;
        findingCurrentSegmentIndex &= segment.isErrorSegment() ? segment.exceptionCleared : (!segment.isActionSegment() && segment.bytesRead == segment.length);
        if (findingCurrentSegmentIndex) {
            currentSegmentIndex++;
        }
    }
    sourceOpened = true;
    transferStarted(dataSpec);
    // Configure bytesRemaining, and return.
    if (dataSpec.length == C.LENGTH_UNSET) {
        bytesRemaining = totalLength - dataSpec.position;
        return fakeData.isSimulatingUnknownLength() ? C.LENGTH_UNSET : bytesRemaining;
    } else {
        bytesRemaining = dataSpec.length;
        return bytesRemaining;
    }
}
Also used : DataSourceException(com.google.android.exoplayer2.upstream.DataSourceException) FakeData(com.google.android.exoplayer2.testutil.FakeDataSet.FakeData) IOException(java.io.IOException) Segment(com.google.android.exoplayer2.testutil.FakeDataSet.FakeData.Segment)

Example 4 with FakeData

use of com.google.android.exoplayer2.testutil.FakeDataSet.FakeData in project ExoPlayer by google.

the class FakeDataSource method close.

@Override
public final void close() {
    Assertions.checkState(openCalled);
    openCalled = false;
    uri = null;
    if (fakeData != null && currentSegmentIndex < fakeData.getSegments().size()) {
        Segment current = fakeData.getSegments().get(currentSegmentIndex);
        if (current.isErrorSegment() && current.exceptionThrown) {
            current.exceptionCleared = true;
        }
    }
    if (sourceOpened) {
        sourceOpened = false;
        transferEnded();
    }
    fakeData = null;
    onClosed();
}
Also used : Segment(com.google.android.exoplayer2.testutil.FakeDataSet.FakeData.Segment)

Example 5 with FakeData

use of com.google.android.exoplayer2.testutil.FakeDataSet.FakeData in project ExoPlayer by google.

the class CacheDataSourceTest method switchToCacheSourceWithReadOnlyCacheDataSource.

@Test
public void switchToCacheSourceWithReadOnlyCacheDataSource() throws Exception {
    // Create a fake data source with a 1 MB default data.
    FakeDataSource upstream = new FakeDataSource();
    FakeData fakeData = upstream.getDataSet().newDefaultData().appendReadData(1024 * 1024 - 1);
    // Insert an action just before the end of the data to fail the test if reading from upstream
    // reaches end of the data.
    fakeData.appendReadAction(() -> fail("Read from upstream shouldn't reach to the end of the data.")).appendReadData(1);
    // Create cache read-only CacheDataSource.
    CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, new FileDataSource(), null, 0, null);
    // Open source and read some data from upstream as the data hasn't cached yet.
    cacheDataSource.open(unboundedDataSpec);
    byte[] buffer = new byte[1024];
    cacheDataSource.read(buffer, 0, buffer.length);
    // Cache the data. Although we use another FakeDataSource instance, it shouldn't matter.
    FakeDataSource upstream2 = new FakeDataSource(new FakeDataSource().getDataSet().newDefaultData().appendReadData(1024 * 1024).endData());
    CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, upstream2), unboundedDataSpec, /* temporaryBuffer= */
    null, /* progressListener= */
    null);
    cacheWriter.cache();
    // Read the rest of the data.
    DataSourceUtil.readToEnd(cacheDataSource);
    cacheDataSource.close();
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) FileDataSource(com.google.android.exoplayer2.upstream.FileDataSource) FakeData(com.google.android.exoplayer2.testutil.FakeDataSet.FakeData) Test(org.junit.Test)

Aggregations

FakeData (com.google.android.exoplayer2.testutil.FakeDataSet.FakeData)6 Segment (com.google.android.exoplayer2.testutil.FakeDataSet.FakeData.Segment)3 Test (org.junit.Test)3 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)2 IOException (java.io.IOException)2 DataSourceException (com.google.android.exoplayer2.upstream.DataSourceException)1 FileDataSource (com.google.android.exoplayer2.upstream.FileDataSource)1 Random (java.util.Random)1