Commit 56ec7052 by tonihei Committed by Ian Baker

Copy getting-stuck-prevention into DefaultLoadControl.

We will eventually remove the workaround from ExoPlayerImplInternal
added in
https://github.com/google/ExoPlayer/commit/b84bde025258e7307c52eaf6bbe58157d788aa06
and replace it by an error that gets thrown if the LoadControl behaves
badly.

PiperOrigin-RevId: 295556131
parent faff66e9
...@@ -246,7 +246,7 @@ public class DefaultLoadControl implements LoadControl { ...@@ -246,7 +246,7 @@ public class DefaultLoadControl implements LoadControl {
private final long backBufferDurationUs; private final long backBufferDurationUs;
private final boolean retainBackBufferFromKeyframe; private final boolean retainBackBufferFromKeyframe;
private int targetBufferSize; private int targetBufferBytes;
private boolean isBuffering; private boolean isBuffering;
private boolean hasVideo; private boolean hasVideo;
...@@ -334,6 +334,10 @@ public class DefaultLoadControl implements LoadControl { ...@@ -334,6 +334,10 @@ public class DefaultLoadControl implements LoadControl {
this.bufferForPlaybackUs = C.msToUs(bufferForPlaybackMs); this.bufferForPlaybackUs = C.msToUs(bufferForPlaybackMs);
this.bufferForPlaybackAfterRebufferUs = C.msToUs(bufferForPlaybackAfterRebufferMs); this.bufferForPlaybackAfterRebufferUs = C.msToUs(bufferForPlaybackAfterRebufferMs);
this.targetBufferBytesOverwrite = targetBufferBytes; this.targetBufferBytesOverwrite = targetBufferBytes;
this.targetBufferBytes =
targetBufferBytesOverwrite != C.LENGTH_UNSET
? targetBufferBytesOverwrite
: DEFAULT_MUXED_BUFFER_SIZE;
this.prioritizeTimeOverSizeThresholds = prioritizeTimeOverSizeThresholds; this.prioritizeTimeOverSizeThresholds = prioritizeTimeOverSizeThresholds;
this.backBufferDurationUs = C.msToUs(backBufferDurationMs); this.backBufferDurationUs = C.msToUs(backBufferDurationMs);
this.retainBackBufferFromKeyframe = retainBackBufferFromKeyframe; this.retainBackBufferFromKeyframe = retainBackBufferFromKeyframe;
...@@ -348,11 +352,11 @@ public class DefaultLoadControl implements LoadControl { ...@@ -348,11 +352,11 @@ public class DefaultLoadControl implements LoadControl {
public void onTracksSelected(Renderer[] renderers, TrackGroupArray trackGroups, public void onTracksSelected(Renderer[] renderers, TrackGroupArray trackGroups,
TrackSelectionArray trackSelections) { TrackSelectionArray trackSelections) {
hasVideo = hasVideo(renderers, trackSelections); hasVideo = hasVideo(renderers, trackSelections);
targetBufferSize = targetBufferBytes =
targetBufferBytesOverwrite == C.LENGTH_UNSET targetBufferBytesOverwrite == C.LENGTH_UNSET
? calculateTargetBufferSize(renderers, trackSelections) ? calculateTargetBufferBytes(renderers, trackSelections)
: targetBufferBytesOverwrite; : targetBufferBytesOverwrite;
allocator.setTargetBufferSize(targetBufferSize); allocator.setTargetBufferSize(targetBufferBytes);
} }
@Override @Override
...@@ -382,7 +386,7 @@ public class DefaultLoadControl implements LoadControl { ...@@ -382,7 +386,7 @@ public class DefaultLoadControl implements LoadControl {
@Override @Override
public boolean shouldContinueLoading(long bufferedDurationUs, float playbackSpeed) { public boolean shouldContinueLoading(long bufferedDurationUs, float playbackSpeed) {
boolean targetBufferSizeReached = allocator.getTotalBytesAllocated() >= targetBufferSize; boolean targetBufferSizeReached = allocator.getTotalBytesAllocated() >= targetBufferBytes;
long minBufferUs = hasVideo ? minBufferVideoUs : minBufferAudioUs; long minBufferUs = hasVideo ? minBufferVideoUs : minBufferAudioUs;
if (playbackSpeed > 1) { if (playbackSpeed > 1) {
// The playback speed is faster than real time, so scale up the minimum required media // The playback speed is faster than real time, so scale up the minimum required media
...@@ -391,6 +395,8 @@ public class DefaultLoadControl implements LoadControl { ...@@ -391,6 +395,8 @@ public class DefaultLoadControl implements LoadControl {
Util.getMediaDurationForPlayoutDuration(minBufferUs, playbackSpeed); Util.getMediaDurationForPlayoutDuration(minBufferUs, playbackSpeed);
minBufferUs = Math.min(mediaDurationMinBufferUs, maxBufferUs); minBufferUs = Math.min(mediaDurationMinBufferUs, maxBufferUs);
} }
// Prevent playback from getting stuck if minBufferUs is too small.
minBufferUs = Math.max(minBufferUs, 500_000);
if (bufferedDurationUs < minBufferUs) { if (bufferedDurationUs < minBufferUs) {
isBuffering = prioritizeTimeOverSizeThresholds || !targetBufferSizeReached; isBuffering = prioritizeTimeOverSizeThresholds || !targetBufferSizeReached;
} else if (bufferedDurationUs >= maxBufferUs || targetBufferSizeReached) { } else if (bufferedDurationUs >= maxBufferUs || targetBufferSizeReached) {
...@@ -407,7 +413,7 @@ public class DefaultLoadControl implements LoadControl { ...@@ -407,7 +413,7 @@ public class DefaultLoadControl implements LoadControl {
return minBufferDurationUs <= 0 return minBufferDurationUs <= 0
|| bufferedDurationUs >= minBufferDurationUs || bufferedDurationUs >= minBufferDurationUs
|| (!prioritizeTimeOverSizeThresholds || (!prioritizeTimeOverSizeThresholds
&& allocator.getTotalBytesAllocated() >= targetBufferSize); && allocator.getTotalBytesAllocated() >= targetBufferBytes);
} }
/** /**
...@@ -418,7 +424,7 @@ public class DefaultLoadControl implements LoadControl { ...@@ -418,7 +424,7 @@ public class DefaultLoadControl implements LoadControl {
* @param trackSelectionArray The selected tracks. * @param trackSelectionArray The selected tracks.
* @return The target buffer size in bytes. * @return The target buffer size in bytes.
*/ */
protected int calculateTargetBufferSize( protected int calculateTargetBufferBytes(
Renderer[] renderers, TrackSelectionArray trackSelectionArray) { Renderer[] renderers, TrackSelectionArray trackSelectionArray) {
int targetBufferSize = 0; int targetBufferSize = 0;
for (int i = 0; i < renderers.length; i++) { for (int i = 0; i < renderers.length; i++) {
...@@ -430,7 +436,10 @@ public class DefaultLoadControl implements LoadControl { ...@@ -430,7 +436,10 @@ public class DefaultLoadControl implements LoadControl {
} }
private void reset(boolean resetAllocator) { private void reset(boolean resetAllocator) {
targetBufferSize = 0; targetBufferBytes =
targetBufferBytesOverwrite == C.LENGTH_UNSET
? DEFAULT_MUXED_BUFFER_SIZE
: targetBufferBytesOverwrite;
isBuffering = false; isBuffering = false;
if (resetAllocator) { if (resetAllocator) {
allocator.reset(); allocator.reset();
......
...@@ -46,6 +46,7 @@ public class DefaultLoadControlTest { ...@@ -46,6 +46,7 @@ public class DefaultLoadControlTest {
@Test @Test
public void testShouldContinueLoading_untilMaxBufferExceeded() { public void testShouldContinueLoading_untilMaxBufferExceeded() {
createDefaultLoadControl(); createDefaultLoadControl();
assertThat(loadControl.shouldContinueLoading(/* bufferedDurationUs= */ 0, SPEED)).isTrue(); assertThat(loadControl.shouldContinueLoading(/* bufferedDurationUs= */ 0, SPEED)).isTrue();
assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US, SPEED)).isTrue(); assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US, SPEED)).isTrue();
assertThat(loadControl.shouldContinueLoading(MAX_BUFFER_US - 1, SPEED)).isTrue(); assertThat(loadControl.shouldContinueLoading(MAX_BUFFER_US - 1, SPEED)).isTrue();
...@@ -56,12 +57,28 @@ public class DefaultLoadControlTest { ...@@ -56,12 +57,28 @@ public class DefaultLoadControlTest {
public void testShouldNotContinueLoadingOnceBufferingStopped_untilBelowMinBuffer() { public void testShouldNotContinueLoadingOnceBufferingStopped_untilBelowMinBuffer() {
createDefaultLoadControl(); createDefaultLoadControl();
assertThat(loadControl.shouldContinueLoading(MAX_BUFFER_US, SPEED)).isFalse(); assertThat(loadControl.shouldContinueLoading(MAX_BUFFER_US, SPEED)).isFalse();
assertThat(loadControl.shouldContinueLoading(MAX_BUFFER_US - 1, SPEED)).isFalse(); assertThat(loadControl.shouldContinueLoading(MAX_BUFFER_US - 1, SPEED)).isFalse();
assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US, SPEED)).isFalse(); assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US, SPEED)).isFalse();
assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US - 1, SPEED)).isTrue(); assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US - 1, SPEED)).isTrue();
} }
@Test @Test
public void
testContinueLoadingOnceBufferingStopped_andBufferAlmostEmpty_evenIfMinBufferNotReached() {
builder.setBufferDurationsMs(
/* minBufferMs= */ 0,
/* maxBufferMs= */ (int) C.usToMs(MAX_BUFFER_US),
/* bufferForPlaybackMs= */ 0,
/* bufferForPlaybackAfterRebufferMs= */ 0);
createDefaultLoadControl();
assertThat(loadControl.shouldContinueLoading(MAX_BUFFER_US, SPEED)).isFalse();
assertThat(loadControl.shouldContinueLoading(5 * C.MICROS_PER_SECOND, SPEED)).isFalse();
assertThat(loadControl.shouldContinueLoading(500L, SPEED)).isTrue();
}
@Test
public void testShouldContinueLoadingWithTargetBufferBytesReached_untilMinBufferReached() { public void testShouldContinueLoadingWithTargetBufferBytesReached_untilMinBufferReached() {
createDefaultLoadControl(); createDefaultLoadControl();
makeSureTargetBufferBytesReached(); makeSureTargetBufferBytesReached();
...@@ -81,6 +98,7 @@ public class DefaultLoadControlTest { ...@@ -81,6 +98,7 @@ public class DefaultLoadControlTest {
makeSureTargetBufferBytesReached(); makeSureTargetBufferBytesReached();
assertThat(loadControl.shouldContinueLoading(/* bufferedDurationUs= */ 0, SPEED)).isFalse(); assertThat(loadControl.shouldContinueLoading(/* bufferedDurationUs= */ 0, SPEED)).isFalse();
assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US - 1, SPEED)).isFalse();
assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US, SPEED)).isFalse(); assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US, SPEED)).isFalse();
assertThat(loadControl.shouldContinueLoading(MAX_BUFFER_US, SPEED)).isFalse(); assertThat(loadControl.shouldContinueLoading(MAX_BUFFER_US, SPEED)).isFalse();
} }
...@@ -91,7 +109,6 @@ public class DefaultLoadControlTest { ...@@ -91,7 +109,6 @@ public class DefaultLoadControlTest {
// At normal playback speed, we stop buffering when the buffer reaches the minimum. // At normal playback speed, we stop buffering when the buffer reaches the minimum.
assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US, SPEED)).isFalse(); assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US, SPEED)).isFalse();
// At double playback speed, we continue loading. // At double playback speed, we continue loading.
assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US, /* playbackSpeed= */ 2f)).isTrue(); assertThat(loadControl.shouldContinueLoading(MIN_BUFFER_US, /* playbackSpeed= */ 2f)).isTrue();
} }
...@@ -107,6 +124,7 @@ public class DefaultLoadControlTest { ...@@ -107,6 +124,7 @@ public class DefaultLoadControlTest {
@Test @Test
public void testStartsPlayback_whenMinBufferSizeReached() { public void testStartsPlayback_whenMinBufferSizeReached() {
createDefaultLoadControl(); createDefaultLoadControl();
assertThat(loadControl.shouldStartPlayback(MIN_BUFFER_US, SPEED, /* rebuffering= */ false)) assertThat(loadControl.shouldStartPlayback(MIN_BUFFER_US, SPEED, /* rebuffering= */ false))
.isTrue(); .isTrue();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment