public final class DefaultLoadControl extends Object implements LoadControl
LoadControl
implementation that allows loads to continue in a sequence that prevents
any loader from getting too far ahead or behind any of the other loaders.
Loads are scheduled so as to fill the available buffer space as rapidly as possible. Once the duration of buffered media and the buffer utilization both exceed respective thresholds, the control switches to a draining state during which no loads are permitted to start. During draining periods, resources such as the device radio have an opportunity to switch into low power modes. The control reverts back to the loading state when either the duration of buffered media or the buffer utilization fall below respective thresholds.
This implementation of LoadControl
integrates with NetworkLock
, by registering
itself as a task with priority NetworkLock.STREAMING_PRIORITY
during loading periods,
and unregistering itself during draining periods.
Modifier and Type | Class and Description |
---|---|
static interface |
DefaultLoadControl.EventListener
Interface definition for a callback to be notified of
DefaultLoadControl events. |
Modifier and Type | Field and Description |
---|---|
static float |
DEFAULT_HIGH_BUFFER_LOAD |
static int |
DEFAULT_HIGH_WATERMARK_MS |
static float |
DEFAULT_LOW_BUFFER_LOAD |
static int |
DEFAULT_LOW_WATERMARK_MS |
Constructor and Description |
---|
DefaultLoadControl(Allocator allocator)
Constructs a new instance, using the
DEFAULT_* constants defined in this class. |
DefaultLoadControl(Allocator allocator,
Handler eventHandler,
DefaultLoadControl.EventListener eventListener)
Constructs a new instance, using the
DEFAULT_* constants defined in this class. |
DefaultLoadControl(Allocator allocator,
Handler eventHandler,
DefaultLoadControl.EventListener eventListener,
int lowWatermarkMs,
int highWatermarkMs,
float lowBufferLoad,
float highBufferLoad)
Constructs a new instance.
|
Modifier and Type | Method and Description |
---|---|
Allocator |
getAllocator()
Gets the
Allocator that loaders should use to obtain memory allocations into which
data can be loaded. |
void |
register(Object loader,
int bufferSizeContribution)
Registers a loader.
|
void |
trimAllocator()
Hints to the control that it should consider trimming any unused memory being held in order
to satisfy allocation requests.
|
void |
unregister(Object loader)
Unregisters a loader.
|
boolean |
update(Object loader,
long playbackPositionUs,
long nextLoadPositionUs,
boolean loading)
Invoked by a loader to update the control with its current state.
|
public static final int DEFAULT_LOW_WATERMARK_MS
public static final int DEFAULT_HIGH_WATERMARK_MS
public static final float DEFAULT_LOW_BUFFER_LOAD
public static final float DEFAULT_HIGH_BUFFER_LOAD
public DefaultLoadControl(Allocator allocator)
DEFAULT_*
constants defined in this class.allocator
- The Allocator
used by the loader.public DefaultLoadControl(Allocator allocator, Handler eventHandler, DefaultLoadControl.EventListener eventListener)
DEFAULT_*
constants defined in this class.allocator
- The Allocator
used by the loader.eventHandler
- A handler to use when delivering events to eventListener
. May be
null if delivery of events is not required.eventListener
- A listener of events. May be null if delivery of events is not required.public DefaultLoadControl(Allocator allocator, Handler eventHandler, DefaultLoadControl.EventListener eventListener, int lowWatermarkMs, int highWatermarkMs, float lowBufferLoad, float highBufferLoad)
allocator
- The Allocator
used by the loader.eventHandler
- A handler to use when delivering events to eventListener
. May be
null if delivery of events is not required.eventListener
- A listener of events. May be null if delivery of events is not required.lowWatermarkMs
- The minimum duration of media that can be buffered for the control to
be in the draining state. If less media is buffered, then the control will transition to
the filling state.highWatermarkMs
- The minimum duration of media that can be buffered for the control to
transition from filling to draining.lowBufferLoad
- The minimum fraction of the buffer that must be utilized for the control
to be in the draining state. If the utilization is lower, then the control will transition
to the filling state.highBufferLoad
- The minimum fraction of the buffer that must be utilized for the control
to transition from the loading state to the draining state.public void register(Object loader, int bufferSizeContribution)
LoadControl
register
in interface LoadControl
loader
- The loader being registered.bufferSizeContribution
- For instances whose Allocator
maintains a pool of memory
for the purpose of satisfying allocation requests, this is a hint indicating the loader's
desired contribution to the size of the pool, in bytes.public void unregister(Object loader)
LoadControl
unregister
in interface LoadControl
loader
- The loader being unregistered.public void trimAllocator()
LoadControl
This method is typically invoked by a recently unregistered loader, once it has released all
of its allocations back to the Allocator
.
trimAllocator
in interface LoadControl
public Allocator getAllocator()
LoadControl
Allocator
that loaders should use to obtain memory allocations into which
data can be loaded.getAllocator
in interface LoadControl
Allocator
to use.public boolean update(Object loader, long playbackPositionUs, long nextLoadPositionUs, boolean loading)
LoadControl
This method must be called by a registered loader whenever its state changes. This is true even if the registered loader does not itself wish to start its next load (since the state of the loader will still affect whether other registered loaders are allowed to proceed).
update
in interface LoadControl
loader
- The loader invoking the update.playbackPositionUs
- The loader's playback position.nextLoadPositionUs
- The loader's next load position. -1 if finished, failed, or if the
next load position is not yet known.loading
- Whether the loader is currently loading data.