DefaultBandwidthMeter


@UnstableApi
public final class DefaultBandwidthMeter implements BandwidthMeter, TransferListener


Estimates bandwidth by listening to data transfers.

The bandwidth estimate is calculated using a SlidingPercentile and is updated each time a transfer ends. 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 int

Default maximum weight for the sliding window.

Public fields

long
@GuardedBy(value = "this")
bitrateEstimate

Public methods

void
addEventListener(
    Handler eventHandler,
    BandwidthMeter.EventListener eventListener
)

Adds an EventListener.

synchronized long

Returns the estimated bitrate.

synchronized static DefaultBandwidthMeter

Returns a singleton instance of a DefaultBandwidthMeter with default configuration.

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.

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.

void

Removes an EventListener.

synchronized void

Overrides the network type.

Inherited methods

From androidx.media3.exoplayer.upstream.BandwidthMeter
long

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

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_SLIDING_WINDOW_MAX_WEIGHT

public static final int DEFAULT_SLIDING_WINDOW_MAX_WEIGHT = 2000

Default maximum weight for the sliding window.

Public fields

bitrateEstimate

@GuardedBy(value = "this")
public long bitrateEstimate

Public methods

addEventListener

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.

getSingletonInstance

synchronized public static DefaultBandwidthMeter getSingletonInstance(Context context)

Returns a singleton instance of a DefaultBandwidthMeter with default configuration.

Parameters
Context context

A Context.

Returns
DefaultBandwidthMeter

The singleton instance.

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

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

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.