Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DefaultLoadcontrol #7406

Merged
merged 1 commit into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,81 +1,28 @@
package org.schabi.newpipe.player.helper;

import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.LoadControl;
import com.google.android.exoplayer2.Renderer;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.trackselection.ExoTrackSelection;
import com.google.android.exoplayer2.upstream.Allocator;

public class LoadController implements LoadControl {
public class LoadController extends DefaultLoadControl {

public static final String TAG = "LoadController";

private final long initialPlaybackBufferUs;
private final LoadControl internalLoadControl;
private boolean preloadingEnabled = true;

/*//////////////////////////////////////////////////////////////////////////
// Default Load Control
//////////////////////////////////////////////////////////////////////////*/

public LoadController() {
this(PlayerHelper.getPlaybackStartBufferMs());
}

private LoadController(final int initialPlaybackBufferMs) {
this.initialPlaybackBufferUs = initialPlaybackBufferMs * 1000;

final DefaultLoadControl.Builder builder = new DefaultLoadControl.Builder();
builder.setBufferDurationsMs(
DefaultLoadControl.DEFAULT_MIN_BUFFER_MS,
DefaultLoadControl.DEFAULT_MAX_BUFFER_MS,
initialPlaybackBufferMs,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS);
internalLoadControl = builder.build();
}

/*//////////////////////////////////////////////////////////////////////////
// Custom behaviours
//////////////////////////////////////////////////////////////////////////*/

@Override
public void onPrepared() {
preloadingEnabled = true;
internalLoadControl.onPrepared();
}

@Override
public void onTracksSelected(final Renderer[] renderers, final TrackGroupArray trackGroups,
final ExoTrackSelection[] trackSelections) {
internalLoadControl.onTracksSelected(renderers, trackGroups, trackSelections);
super.onPrepared();
}

@Override
public void onStopped() {
preloadingEnabled = true;
internalLoadControl.onStopped();
super.onStopped();
}

@Override
public void onReleased() {
preloadingEnabled = true;
internalLoadControl.onReleased();
}

@Override
public Allocator getAllocator() {
return internalLoadControl.getAllocator();
}

@Override
public long getBackBufferDurationUs() {
return internalLoadControl.getBackBufferDurationUs();
}

@Override
public boolean retainBackBufferFromKeyframe() {
return internalLoadControl.retainBackBufferFromKeyframe();
super.onReleased();
}

@Override
Expand All @@ -85,21 +32,10 @@ public boolean shouldContinueLoading(final long playbackPositionUs,
if (!preloadingEnabled) {
return false;
}
return internalLoadControl.shouldContinueLoading(
return super.shouldContinueLoading(
playbackPositionUs, bufferedDurationUs, playbackSpeed);
}

@Override
public boolean shouldStartPlayback(final long bufferedDurationUs, final float playbackSpeed,
final boolean rebuffering, final long targetLiveOffsetUs) {
final boolean isInitialPlaybackBufferFilled
= bufferedDurationUs >= this.initialPlaybackBufferUs * playbackSpeed;
final boolean isInternalStartingPlayback = internalLoadControl
.shouldStartPlayback(bufferedDurationUs, playbackSpeed, rebuffering,
targetLiveOffsetUs);
return isInitialPlaybackBufferFilled || isInternalStartingPlayback;
}

public void disablePreloadingOfCurrentTrack() {
preloadingEnabled = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,6 @@ public static long getPreferredFileSize() {
return 2 * 1024 * 1024L; // ExoPlayer CacheDataSink.MIN_RECOMMENDED_FRAGMENT_SIZE
}

/**
* @return the number of milliseconds the player buffers for before starting playback
*/
public static int getPlaybackStartBufferMs() {
return 500;
}

public static ExoTrackSelection.Factory getQualitySelector() {
return new AdaptiveTrackSelection.Factory(
1000,
Expand Down