Avoid rebuffering when modifying TrackSelector parameters
[REQUIRED] Use case description
I have an app using adaptive media, where in certain circumstances I want to reduce the bandwidth consumed by the video/audio. I do so by setting new filtering criteria in the track selector, as follows:
DefaultTrackSelector.Parameters currentParameters = ((DefaultTrackSelector) trackSelector).getParameters();
DefaultTrackSelector.Parameters newParameters = currentParameters
.buildUpon()
.setForceLowestBitrate(true)
.build();
((DefaultTrackSelector) trackSelector).setParameters(newParameters);
However this apparently forces a reset in the playback (removal of chunks already available), with rebuff for a while. The same happens when I remove that limitaton. Of course rebuffering is more evident if I force the selection of a high bitrate track. I need instead an uninterrupted playback
Proposed solution
I need something like:
trackSelector.SetParameters(newParameters, booleanWetherResetOrNot);
such that (in case of "don't reset") the playback is not interrupted, and chunks already downloaded or being downloaded are not removed; simply, the new trackSelector parameters would take effect at the next choice of the chunk to be downloaded.
Alternatives considered
There is a potential alternative approach: blacklists I would need a different logic however, since as of today they are time-related
That's a duplicate of #2250 and/or #5082 (somewhere in-between the two). You can implement a custom solution if you like by subclassing AdaptiveTrackSelection.canSelectFormat. In there you can filter out unwanted formats during playback.
Thanks for the prompt reply. I am studying the code but I failed so far to implement your proposed approach. Altering the behavior of canSelectFormat is indeed a nice idea, but the hard part is how to dynamically instruct the TrackSelection object about how to alter that behavior. The TrackSelection object is associated to the DashChunkSource and apparently not available outside. It seems that I need to go through (and modify) several classes, by replicating (with changes) the steps of invalidate() and reselectTracksInternal(). Maybe there is an easier solution but I've been unable to figure it out.
You can set-up the DefaultTrackSelector with a TrackSelection.Factory in the constructor that returns your AdaptiveTrackSelection subclass.
Be mindful that AdaptiveTrackSelection.canSelectFormat is called on an internal playback thread and you need some form of message passing or synchronization to prevent threading issues.
Thanks Tonihei for your kindness. In the end I succeeded in implementing something working fine for my use case
#9896 requested an example for the usage of canSelectFormat to dynamically update the eligible formats.
You need a custom track selection class overriding AdaptiveTrackSelection, which allows setting a custom logic. To do this effectively, you also need to store the enabled selections and synchronize access because the track selection methods are called on the playback thread.
ArrayList<CustomTrackSelection> enabledSelections = new ArrayList();
/** update all selections **/
synchonized(enabledSelections) {
for (CustomTrackSelection selection: enabledSelections) {
selection.setCustomLogic(customLogic);
}
}
class CustomTrackSelection extends AdaptiveTrackSelection {
public CustomTrackSelection(/** all arguments of the long AdaptiveTrackSelection constructor **/) {
super(/** all arguments **/);
}
@Override
public void enable() {
super.enable();
synchronized (enabledSelections) {
enabledSelections.add(this);
}
}
@Override
public void disable() {
super.disable();
synchronized (enabledSelections) {
enabledSelections.remove(this);
}
}
@Override
protected boolean canSelectFormat(Format format, int trackBitrate, long effectiveBitrate) {
synchronized (this) {
return customLogicAllowsFormat();
}
}
public void setCustomLogic(Object customLogic) {
synchronized(this) {
this.customLogic = customLogic;
}
}
}
In order to inject your custom type into the player, you need to implement a Factory and set it to the DefaultTrackSelector:
class CustomTrackSelectionFactory extends AdaptiveTrackSelection.Factory {
@Override
protected AdaptiveTrackSelection createAdaptiveTrackSelection(
TrackGroup group,
int[] tracks,
int type,
BandwidthMeter bandwidthMeter,
ImmutableList<AdaptationCheckpoint> adaptationCheckpoints) {
return new CustomTrackSelection(
group,
tracks,
type,
bandwidthMeter,
DEFAULT_MIN_DURATION_FOR_QUALITY_INCREASE_MS,
DEFAULT_MAX_DURATION_FOR_QUALITY_DECREASE_MS,
DEFAULT_MIN_DURATION_TO_RETAIN_AFTER_DISCARD_MS,
DEFAULT_MAX_WIDTH_TO_DISCARD,
DEFAULT_MAX_HEIGHT_TO_DISCARD,
DEFAULT_BANDWIDTH_FRACTION,
DEFAULT_BUFFERED_FRACTION_TO_LIVE_EDGE_FOR_QUALITY_INCREASE,
adaptationCheckpoints,
Clock.DEFAULT);
}
}
/** build the player **/
new ExoPlayer.Builder()
.setTrackSelector(new DefaultTrackSelector(context, new CustomTrackSelectionFactory()))
.build();