mirror of
https://github.com/TeamNewPipe/NewPipe.git
synced 2024-11-22 11:02:35 +01:00
Use DefaultLoadcontrol
This commit is contained in:
parent
9e44053e22
commit
9c9730b152
@ -1,81 +1,28 @@
|
|||||||
package org.schabi.newpipe.player.helper;
|
package org.schabi.newpipe.player.helper;
|
||||||
|
|
||||||
import com.google.android.exoplayer2.DefaultLoadControl;
|
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";
|
public static final String TAG = "LoadController";
|
||||||
|
|
||||||
private final long initialPlaybackBufferUs;
|
|
||||||
private final LoadControl internalLoadControl;
|
|
||||||
private boolean preloadingEnabled = true;
|
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
|
@Override
|
||||||
public void onPrepared() {
|
public void onPrepared() {
|
||||||
preloadingEnabled = true;
|
preloadingEnabled = true;
|
||||||
internalLoadControl.onPrepared();
|
super.onPrepared();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTracksSelected(final Renderer[] renderers, final TrackGroupArray trackGroups,
|
|
||||||
final ExoTrackSelection[] trackSelections) {
|
|
||||||
internalLoadControl.onTracksSelected(renderers, trackGroups, trackSelections);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStopped() {
|
public void onStopped() {
|
||||||
preloadingEnabled = true;
|
preloadingEnabled = true;
|
||||||
internalLoadControl.onStopped();
|
super.onStopped();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReleased() {
|
public void onReleased() {
|
||||||
preloadingEnabled = true;
|
preloadingEnabled = true;
|
||||||
internalLoadControl.onReleased();
|
super.onReleased();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Allocator getAllocator() {
|
|
||||||
return internalLoadControl.getAllocator();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long getBackBufferDurationUs() {
|
|
||||||
return internalLoadControl.getBackBufferDurationUs();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean retainBackBufferFromKeyframe() {
|
|
||||||
return internalLoadControl.retainBackBufferFromKeyframe();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -85,21 +32,10 @@ public class LoadController implements LoadControl {
|
|||||||
if (!preloadingEnabled) {
|
if (!preloadingEnabled) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return internalLoadControl.shouldContinueLoading(
|
return super.shouldContinueLoading(
|
||||||
playbackPositionUs, bufferedDurationUs, playbackSpeed);
|
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() {
|
public void disablePreloadingOfCurrentTrack() {
|
||||||
preloadingEnabled = false;
|
preloadingEnabled = false;
|
||||||
}
|
}
|
||||||
|
@ -305,13 +305,6 @@ public final class PlayerHelper {
|
|||||||
return 2 * 1024 * 1024L; // ExoPlayer CacheDataSink.MIN_RECOMMENDED_FRAGMENT_SIZE
|
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() {
|
public static ExoTrackSelection.Factory getQualitySelector() {
|
||||||
return new AdaptiveTrackSelection.Factory(
|
return new AdaptiveTrackSelection.Factory(
|
||||||
1000,
|
1000,
|
||||||
|
Loading…
Reference in New Issue
Block a user