Commit 500b1faf by eguven Committed by Oliver Woodman

Add Downloader.getTotalBytes() method

PiperOrigin-RevId: 223787832
parent 9f1e32f1
...@@ -38,6 +38,9 @@ public interface Downloader { ...@@ -38,6 +38,9 @@ public interface Downloader {
/** Returns the total number of downloaded bytes. */ /** Returns the total number of downloaded bytes. */
long getDownloadedBytes(); 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 * Returns the estimated download percentage, or {@link C#PERCENTAGE_UNSET} if no estimate is
* available. * available.
......
...@@ -87,6 +87,11 @@ public final class ProgressiveDownloader implements Downloader { ...@@ -87,6 +87,11 @@ public final class ProgressiveDownloader implements Downloader {
} }
@Override @Override
public long getTotalBytes() {
return cachingCounters.contentLength;
}
@Override
public float getDownloadPercentage() { public float getDownloadPercentage() {
long contentLength = cachingCounters.contentLength; long contentLength = cachingCounters.contentLength;
return contentLength == C.LENGTH_UNSET return contentLength == C.LENGTH_UNSET
......
...@@ -72,6 +72,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme ...@@ -72,6 +72,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
private volatile int totalSegments; private volatile int totalSegments;
private volatile int downloadedSegments; private volatile int downloadedSegments;
private volatile long downloadedBytes; private volatile long downloadedBytes;
private volatile long totalBytes;
/** /**
* @param manifestUri The {@link Uri} of the manifest to be downloaded. * @param manifestUri The {@link Uri} of the manifest to be downloaded.
...@@ -88,6 +89,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme ...@@ -88,6 +89,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
this.offlineDataSource = constructorHelper.createOfflineCacheDataSource(); this.offlineDataSource = constructorHelper.createOfflineCacheDataSource();
this.priorityTaskManager = constructorHelper.getPriorityTaskManager(); this.priorityTaskManager = constructorHelper.getPriorityTaskManager();
totalSegments = C.LENGTH_UNSET; totalSegments = C.LENGTH_UNSET;
totalBytes = C.LENGTH_UNSET;
isCanceled = new AtomicBoolean(); isCanceled = new AtomicBoolean();
} }
...@@ -143,8 +145,17 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme ...@@ -143,8 +145,17 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
} }
@Override @Override
public long getTotalBytes() {
return totalBytes;
}
@Override
public final float getDownloadPercentage() { public final float getDownloadPercentage() {
// Take local snapshot of the volatile fields // 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 totalSegments = this.totalSegments;
int downloadedSegments = this.downloadedSegments; int downloadedSegments = this.downloadedSegments;
if (totalSegments == C.LENGTH_UNSET || downloadedSegments == C.LENGTH_UNSET) { if (totalSegments == C.LENGTH_UNSET || downloadedSegments == C.LENGTH_UNSET) {
...@@ -211,16 +222,25 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme ...@@ -211,16 +222,25 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
totalSegments = segments.size(); totalSegments = segments.size();
downloadedSegments = 0; downloadedSegments = 0;
downloadedBytes = 0; downloadedBytes = 0;
long totalBytes = 0;
for (int i = segments.size() - 1; i >= 0; i--) { for (int i = segments.size() - 1; i >= 0; i--) {
Segment segment = segments.get(i); Segment segment = segments.get(i);
CacheUtil.getCached(segment.dataSpec, cache, cachingCounters); CacheUtil.getCached(segment.dataSpec, cache, cachingCounters);
downloadedBytes += cachingCounters.alreadyCachedBytes; downloadedBytes += cachingCounters.alreadyCachedBytes;
if (cachingCounters.contentLength != C.LENGTH_UNSET) {
if (cachingCounters.alreadyCachedBytes == cachingCounters.contentLength) { if (cachingCounters.alreadyCachedBytes == cachingCounters.contentLength) {
// The segment is fully downloaded. // The segment is fully downloaded.
downloadedSegments++; downloadedSegments++;
segments.remove(i); segments.remove(i);
} }
if (totalBytes != C.LENGTH_UNSET) {
totalBytes += cachingCounters.contentLength;
}
} else {
totalBytes = C.LENGTH_UNSET;
}
} }
this.totalBytes = totalBytes;
return segments; return segments;
} }
......
...@@ -624,8 +624,13 @@ public class DownloadManagerTest { ...@@ -624,8 +624,13 @@ public class DownloadManagerTest {
} }
@Override @Override
public long getTotalBytes() {
return C.LENGTH_UNSET;
}
@Override
public float getDownloadPercentage() { public float getDownloadPercentage() {
return Float.NaN; return C.PERCENTAGE_UNSET;
} }
} }
} }
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