mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +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. */
|
/** 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.
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,11 @@ public final class ProgressiveDownloader implements Downloader {
|
||||||
return cachingCounters.totalCachedBytes();
|
return cachingCounters.totalCachedBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getTotalBytes() {
|
||||||
|
return cachingCounters.contentLength;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getDownloadPercentage() {
|
public float getDownloadPercentage() {
|
||||||
long contentLength = cachingCounters.contentLength;
|
long contentLength = cachingCounters.contentLength;
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -142,9 +144,18 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
|
||||||
return downloadedBytes;
|
return downloadedBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getTotalBytes() {
|
||||||
|
return totalBytes;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
||||||
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.alreadyCachedBytes == cachingCounters.contentLength) {
|
if (cachingCounters.contentLength != C.LENGTH_UNSET) {
|
||||||
// The segment is fully downloaded.
|
if (cachingCounters.alreadyCachedBytes == cachingCounters.contentLength) {
|
||||||
downloadedSegments++;
|
// The segment is fully downloaded.
|
||||||
segments.remove(i);
|
downloadedSegments++;
|
||||||
|
segments.remove(i);
|
||||||
|
}
|
||||||
|
if (totalBytes != C.LENGTH_UNSET) {
|
||||||
|
totalBytes += cachingCounters.contentLength;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
totalBytes = C.LENGTH_UNSET;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.totalBytes = totalBytes;
|
||||||
return segments;
|
return segments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -623,9 +623,14 @@ public class DownloadManagerTest {
|
||||||
return downloadedBytes;
|
return downloadedBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getTotalBytes() {
|
||||||
|
return C.LENGTH_UNSET;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getDownloadPercentage() {
|
public float getDownloadPercentage() {
|
||||||
return Float.NaN;
|
return C.PERCENTAGE_UNSET;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue