ExperimentalBandwidthMeter


@UnstableApi
public final class ExperimentalBandwidthMeter implements BandwidthMeter, TransferListener


An experimental BandwidthMeter that estimates bandwidth by listening to data transfers.

The initial estimate is based on the current operator's network country code or the locale of the user, as well as the network connection type. This can be configured in the Builder.

Summary

Nested types

Builder for a bandwidth meter.

Constants

static final long

Default initial bitrate estimate used when the device is offline or the network type cannot be determined, in bits per second.

static final ImmutableList<Long>

Default initial 2G bitrate estimates in bits per second.

static final ImmutableList<Long>

Default initial 3G bitrate estimates in bits per second.

static final ImmutableList<Long>

Default initial 4G bitrate estimates in bits per second.

static final ImmutableList<Long>

Default initial 5G-NSA bitrate estimates in bits per second.

static final ImmutableList<Long>

Default initial 5G-SA bitrate estimates in bits per second.

static final ImmutableList<Long>

Default initial Wifi bitrate estimate in bits per second.

static final float

Default percentile for estimating the time to first byte.

static final int

Default number of samples to keep in the sliding window for estimating the time to first byte.

Public methods

synchronized void
addEventListener(
    Handler eventHandler,
    BandwidthMeter.EventListener eventListener
)

Adds an EventListener.

synchronized long

Returns the estimated bitrate.

synchronized long

Returns the estimated time to first byte, in microseconds, or TIME_UNSET if no estimate is available.

TransferListener

Returns the TransferListener that this instance uses to gather bandwidth information from data transfers.

synchronized void
onBytesTransferred(
    DataSource source,
    DataSpec dataSpec,
    boolean isNetwork,
    int bytesTransferred
)

Called incrementally during a transfer.

synchronized void
onTransferEnd(DataSource source, DataSpec dataSpec, boolean isNetwork)

Called when a transfer ends.

synchronized void
onTransferInitializing(
    DataSource source,
    DataSpec dataSpec,
    boolean isNetwork
)

Called when a transfer is being initialized.

synchronized void
onTransferStart(DataSource source, DataSpec dataSpec, boolean isNetwork)

Called when a transfer starts.

synchronized void

Removes an EventListener.

synchronized void

Overrides the network type.

Constants

DEFAULT_INITIAL_BITRATE_ESTIMATE

public static final long DEFAULT_INITIAL_BITRATE_ESTIMATE = 1000000

Default initial bitrate estimate used when the device is offline or the network type cannot be determined, in bits per second.

DEFAULT_INITIAL_BITRATE_ESTIMATES_2G

public static final ImmutableList<LongDEFAULT_INITIAL_BITRATE_ESTIMATES_2G

Default initial 2G bitrate estimates in bits per second.

DEFAULT_INITIAL_BITRATE_ESTIMATES_3G

public static final ImmutableList<LongDEFAULT_INITIAL_BITRATE_ESTIMATES_3G

Default initial 3G bitrate estimates in bits per second.

DEFAULT_INITIAL_BITRATE_ESTIMATES_4G

public static final ImmutableList<LongDEFAULT_INITIAL_BITRATE_ESTIMATES_4G

Default initial 4G bitrate estimates in bits per second.

DEFAULT_INITIAL_BITRATE_ESTIMATES_5G_NSA

public static final ImmutableList<LongDEFAULT_INITIAL_BITRATE_ESTIMATES_5G_NSA

Default initial 5G-NSA bitrate estimates in bits per second.

DEFAULT_INITIAL_BITRATE_ESTIMATES_5G_SA

public static final ImmutableList<LongDEFAULT_INITIAL_BITRATE_ESTIMATES_5G_SA

Default initial 5G-SA bitrate estimates in bits per second.

DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI

public static final ImmutableList<LongDEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI

Default initial Wifi bitrate estimate in bits per second.

DEFAULT_TIME_TO_FIRST_BYTE_PERCENTILE

public static final float DEFAULT_TIME_TO_FIRST_BYTE_PERCENTILE = 0.5f

Default percentile for estimating the time to first byte.

DEFAULT_TIME_TO_FIRST_BYTE_SAMPLES

public static final int DEFAULT_TIME_TO_FIRST_BYTE_SAMPLES = 20

Default number of samples to keep in the sliding window for estimating the time to first byte.

Public methods

addEventListener

synchronized public void addEventListener(
    Handler eventHandler,
    BandwidthMeter.EventListener eventListener
)

Adds an EventListener.

Parameters
Handler eventHandler

A handler for events.

BandwidthMeter.EventListener eventListener

A listener of events.

getBitrateEstimate

synchronized public long getBitrateEstimate()

Returns the estimated bitrate.

getTimeToFirstByteEstimateUs

synchronized public long getTimeToFirstByteEstimateUs()

Returns the estimated time to first byte, in microseconds, or TIME_UNSET if no estimate is available.

getTransferListener

public TransferListener getTransferListener()

Returns the TransferListener that this instance uses to gather bandwidth information from data transfers. May be null if the implementation does not listen to data transfers.

onBytesTransferred

synchronized public void onBytesTransferred(
    DataSource source,
    DataSpec dataSpec,
    boolean isNetwork,
    int bytesTransferred
)

Called incrementally during a transfer.

Parameters
DataSource source

The source performing the transfer.

DataSpec dataSpec

Describes the data being transferred.

boolean isNetwork

Whether the data is transferred through a network.

int bytesTransferred

The number of bytes transferred since the previous call to this method.

onTransferEnd

synchronized public void onTransferEnd(DataSource source, DataSpec dataSpec, boolean isNetwork)

Called when a transfer ends.

Parameters
DataSource source

The source performing the transfer.

DataSpec dataSpec

Describes the data being transferred.

boolean isNetwork

Whether the data is transferred through a network.

onTransferInitializing

synchronized public void onTransferInitializing(
    DataSource source,
    DataSpec dataSpec,
    boolean isNetwork
)

Called when a transfer is being initialized.

Parameters
DataSource source

The source performing the transfer.

DataSpec dataSpec

Describes the data for which the transfer is initialized.

boolean isNetwork

Whether the data is transferred through a network.

onTransferStart

synchronized public void onTransferStart(DataSource source, DataSpec dataSpec, boolean isNetwork)

Called when a transfer starts.

Parameters
DataSource source

The source performing the transfer.

DataSpec dataSpec

Describes the data being transferred.

boolean isNetwork

Whether the data is transferred through a network.

removeEventListener

synchronized public void removeEventListener(BandwidthMeter.EventListener eventListener)

Removes an EventListener.

Parameters
BandwidthMeter.EventListener eventListener

The listener to be removed.

setNetworkTypeOverride

synchronized public void setNetworkTypeOverride(@C.NetworkType int networkType)

Overrides the network type. Handled in the same way as if the meter had detected a change from the current network type to the specified network type internally.

Applications should not normally call this method. It is intended for testing purposes.

Parameters
@C.NetworkType int networkType

The overriding network type.