Search in sources :

Example 1 with FakeData

use of androidx.media3.test.utils.FakeDataSet.FakeData in project media by androidx.

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(androidx.media3.test.utils.FakeDataSet.FakeData) IOException(java.io.IOException) Segment(androidx.media3.test.utils.FakeDataSet.FakeData.Segment)

Example 2 with FakeData

use of androidx.media3.test.utils.FakeDataSet.FakeData in project media by androidx.

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(androidx.media3.test.utils.FakeDataSource) FileDataSource(androidx.media3.datasource.FileDataSource) FakeData(androidx.media3.test.utils.FakeDataSet.FakeData) Test(org.junit.Test)

Example 3 with FakeData

use of androidx.media3.test.utils.FakeDataSet.FakeData in project media by androidx.

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(androidx.media3.datasource.DataSourceException) FakeData(androidx.media3.test.utils.FakeDataSet.FakeData) IOException(java.io.IOException) Segment(androidx.media3.test.utils.FakeDataSet.FakeData.Segment)

Example 4 with FakeData

use of androidx.media3.test.utils.FakeDataSet.FakeData in project media by androidx.

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(androidx.media3.test.utils.FakeDataSet.FakeData.Segment)

Example 5 with FakeData

use of androidx.media3.test.utils.FakeDataSet.FakeData in project media by androidx.

the class FakeAdaptiveDataSetTest method testAdaptiveDataSetChunkSizeDistribution.

@Test
public void testAdaptiveDataSetChunkSizeDistribution() {
    double expectedStdDev = 4.0;
    FakeAdaptiveDataSet dataSet = new FakeAdaptiveDataSet(TRACK_GROUP, 100000 * C.MICROS_PER_SECOND, C.MICROS_PER_SECOND, expectedStdDev, new Random(0));
    for (int i = 0; i < TEST_FORMATS.length; i++) {
        FakeData data = dataSet.getData(dataSet.getUri(i));
        double mean = computeSegmentSizeMean(data.getSegments());
        double stddev = computeSegmentSizeStdDev(data.getSegments(), mean);
        double relativePercentStdDev = stddev / mean * 100.0;
        assertThat(relativePercentStdDev).isWithin(0.02).of(expectedStdDev);
        assertThat(mean * 8 / TEST_FORMATS[i].bitrate).isWithin(0.01).of(1.0);
    }
}
Also used : Random(java.util.Random) FakeData(androidx.media3.test.utils.FakeDataSet.FakeData) Test(org.junit.Test)

Aggregations

FakeData (androidx.media3.test.utils.FakeDataSet.FakeData)6 Segment (androidx.media3.test.utils.FakeDataSet.FakeData.Segment)3 Test (org.junit.Test)3 FakeDataSource (androidx.media3.test.utils.FakeDataSource)2 IOException (java.io.IOException)2 DataSourceException (androidx.media3.datasource.DataSourceException)1 FileDataSource (androidx.media3.datasource.FileDataSource)1 Random (java.util.Random)1