diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/Downloader.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/Downloader.java index 10523d6bc6..b050e9770d 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/Downloader.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/Downloader.java @@ -38,6 +38,9 @@ public interface Downloader { /** Returns the total number of downloaded bytes. */ long getDownloadedBytes(); + /** Returns the total size of the media, or {@link C#LENGTH_UNSET} if unknown. */ + long getTotalBytes(); + /** * Returns the estimated download percentage, or {@link C#PERCENTAGE_UNSET} if no estimate is * available. diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloader.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloader.java index eedb5f7b00..1a241f0b4e 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloader.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloader.java @@ -86,6 +86,11 @@ public final class ProgressiveDownloader implements Downloader { return cachingCounters.totalCachedBytes(); } + @Override + public long getTotalBytes() { + return cachingCounters.contentLength; + } + @Override public float getDownloadPercentage() { long contentLength = cachingCounters.contentLength; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java index 2fdaaa4cbd..62a6a9294a 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java @@ -72,6 +72,7 @@ public abstract class SegmentDownloader> impleme private volatile int totalSegments; private volatile int downloadedSegments; private volatile long downloadedBytes; + private volatile long totalBytes; /** * @param manifestUri The {@link Uri} of the manifest to be downloaded. @@ -88,6 +89,7 @@ public abstract class SegmentDownloader> impleme this.offlineDataSource = constructorHelper.createOfflineCacheDataSource(); this.priorityTaskManager = constructorHelper.getPriorityTaskManager(); totalSegments = C.LENGTH_UNSET; + totalBytes = C.LENGTH_UNSET; isCanceled = new AtomicBoolean(); } @@ -142,9 +144,18 @@ public abstract class SegmentDownloader> impleme return downloadedBytes; } + @Override + public long getTotalBytes() { + return totalBytes; + } + @Override public final float getDownloadPercentage() { // Take local snapshot of the volatile fields + long totalBytes = this.totalBytes; + if (totalBytes != C.LENGTH_UNSET) { + return totalBytes == 0 ? 100f : (downloadedBytes * 100f) / totalBytes; + } int totalSegments = this.totalSegments; int downloadedSegments = this.downloadedSegments; if (totalSegments == C.LENGTH_UNSET || downloadedSegments == C.LENGTH_UNSET) { @@ -211,16 +222,25 @@ public abstract class SegmentDownloader> impleme totalSegments = segments.size(); downloadedSegments = 0; downloadedBytes = 0; + long totalBytes = 0; for (int i = segments.size() - 1; i >= 0; i--) { Segment segment = segments.get(i); CacheUtil.getCached(segment.dataSpec, cache, cachingCounters); downloadedBytes += cachingCounters.alreadyCachedBytes; - if (cachingCounters.alreadyCachedBytes == cachingCounters.contentLength) { - // The segment is fully downloaded. - downloadedSegments++; - segments.remove(i); + if (cachingCounters.contentLength != C.LENGTH_UNSET) { + if (cachingCounters.alreadyCachedBytes == cachingCounters.contentLength) { + // The segment is fully downloaded. + downloadedSegments++; + segments.remove(i); + } + if (totalBytes != C.LENGTH_UNSET) { + totalBytes += cachingCounters.contentLength; + } + } else { + totalBytes = C.LENGTH_UNSET; } } + this.totalBytes = totalBytes; return segments; } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java index f21f16b8ce..294e8a14a2 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java @@ -623,9 +623,14 @@ public class DownloadManagerTest { return downloadedBytes; } + @Override + public long getTotalBytes() { + return C.LENGTH_UNSET; + } + @Override public float getDownloadPercentage() { - return Float.NaN; + return C.PERCENTAGE_UNSET; } } }