Search in sources :

Example 1 with DefaultBandwidthMeter

use of androidx.media3.exoplayer.upstream.DefaultBandwidthMeter in project media by androidx.

the class DefaultBandwidthMeterTest method simulateTransfers.

private static long[] simulateTransfers(DefaultBandwidthMeter bandwidthMeter, FakeClock clock) {
    long[] bitrateEstimates = new long[SIMULATED_TRANSFER_COUNT];
    Random random = new Random(/* seed= */
    0);
    DataSource dataSource = new FakeDataSource();
    DataSpec dataSpec = new DataSpec(Uri.parse("https://test.com"));
    for (int i = 0; i < SIMULATED_TRANSFER_COUNT; i++) {
        bandwidthMeter.onTransferStart(dataSource, dataSpec, /* isNetwork= */
        true);
        clock.advanceTime(random.nextInt(/* bound= */
        5000));
        bandwidthMeter.onBytesTransferred(dataSource, dataSpec, /* isNetwork= */
        true, /* bytes= */
        random.nextInt(5 * 1024 * 1024));
        bandwidthMeter.onTransferEnd(dataSource, dataSpec, /* isNetwork= */
        true);
        bitrateEstimates[i] = bandwidthMeter.getBitrateEstimate();
    }
    return bitrateEstimates;
}
Also used : Random(java.util.Random) FakeDataSource(androidx.media3.test.utils.FakeDataSource) DataSpec(androidx.media3.datasource.DataSpec) FakeDataSource(androidx.media3.test.utils.FakeDataSource) DataSource(androidx.media3.datasource.DataSource)

Example 2 with DefaultBandwidthMeter

use of androidx.media3.exoplayer.upstream.DefaultBandwidthMeter in project media by androidx.

the class DefaultBandwidthMeterTest method networkTypeOverride_doesFullReset.

@Test
public void networkTypeOverride_doesFullReset() {
    // Simulate transfers for an ethernet connection.
    setActiveNetworkInfo(networkInfoEthernet);
    FakeClock clock = new FakeClock(/* initialTimeMs= */
    0);
    DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(ApplicationProvider.getApplicationContext()).setClock(clock).build();
    long[] bitrateEstimatesWithNewInstance = simulateTransfers(bandwidthMeter, clock);
    // Create a new instance and seed with some transfers.
    setActiveNetworkInfo(networkInfo2g);
    bandwidthMeter = new DefaultBandwidthMeter.Builder(ApplicationProvider.getApplicationContext()).setClock(clock).build();
    simulateTransfers(bandwidthMeter, clock);
    // Override the network type to ethernet and simulate transfers again.
    bandwidthMeter.setNetworkTypeOverride(C.NETWORK_TYPE_ETHERNET);
    long[] bitrateEstimatesAfterReset = simulateTransfers(bandwidthMeter, clock);
    // If overriding the network type fully reset the bandwidth meter, we expect the bitrate
    // estimates generated during simulation to be the same.
    assertThat(bitrateEstimatesAfterReset).isEqualTo(bitrateEstimatesWithNewInstance);
}
Also used : FakeClock(androidx.media3.test.utils.FakeClock) Test(org.junit.Test)

Aggregations

DataSource (androidx.media3.datasource.DataSource)1 DataSpec (androidx.media3.datasource.DataSpec)1 FakeClock (androidx.media3.test.utils.FakeClock)1 FakeDataSource (androidx.media3.test.utils.FakeDataSource)1 Random (java.util.Random)1 Test (org.junit.Test)1