mirror of
https://github.com/samsonjs/media.git
synced 2026-03-27 09:45:47 +00:00
Add Downloader.getTotalBytes() method
PiperOrigin-RevId: 223787832
This commit is contained in:
parent
9f1e32f112
commit
500b1faf14
4 changed files with 38 additions and 5 deletions
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -72,6 +72,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> 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<M extends FilterableManifest<M>> 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<M extends FilterableManifest<M>> 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<M extends FilterableManifest<M>> 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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue