From b89dc8fbfd191e78727b0bf297741688cbb480c4 Mon Sep 17 00:00:00 2001 From: tonihei Date: Tue, 18 Jun 2019 14:50:06 +0100 Subject: [PATCH] Prevent negative total buffered duration at the point where it is calculated. In some edge cases the renderer position may be slightly ahead of the buffered position and the total buffered duration is thus negative. We already filter that in ExoPlayerImpl for the publicly accessible value. However, we forward the unfiltered value to other components like the LoadControl, which may be confusing. Issue:#6015 PiperOrigin-RevId: 253780460 --- .../com/google/android/exoplayer2/ExoPlayerImpl.java | 2 +- .../google/android/exoplayer2/ExoPlayerImplInternal.java | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java index de6be33686..c458f22050 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java @@ -512,7 +512,7 @@ import java.util.concurrent.CopyOnWriteArrayList; @Override public long getTotalBufferedDuration() { - return Math.max(0, C.usToMs(playbackInfo.totalBufferedDurationUs)); + return C.usToMs(playbackInfo.totalBufferedDurationUs); } @Override diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java index ff94567a48..7fbde3c7b2 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java @@ -1797,9 +1797,12 @@ import java.util.concurrent.atomic.AtomicBoolean; private long getTotalBufferedDurationUs(long bufferedPositionInLoadingPeriodUs) { MediaPeriodHolder loadingPeriodHolder = queue.getLoadingPeriod(); - return loadingPeriodHolder == null - ? 0 - : bufferedPositionInLoadingPeriodUs - loadingPeriodHolder.toPeriodTime(rendererPositionUs); + if (loadingPeriodHolder == null) { + return 0; + } + long totalBufferedDurationUs = + bufferedPositionInLoadingPeriodUs - loadingPeriodHolder.toPeriodTime(rendererPositionUs); + return Math.max(0, totalBufferedDurationUs); } private void updateLoadControlTrackSelection(