mirror of
https://github.com/androidx/media.git
synced 2025-05-06 23:20:42 +08:00
Use CachingCounters in SegmentDownloader
PiperOrigin-RevId: 241543543
This commit is contained in:
parent
d5b19694fa
commit
9b064fb9d5
@ -70,11 +70,10 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
|
|||||||
private final PriorityTaskManager priorityTaskManager;
|
private final PriorityTaskManager priorityTaskManager;
|
||||||
private final ArrayList<StreamKey> streamKeys;
|
private final ArrayList<StreamKey> streamKeys;
|
||||||
private final AtomicBoolean isCanceled;
|
private final AtomicBoolean isCanceled;
|
||||||
|
private final CacheUtil.CachingCounters counters;
|
||||||
|
|
||||||
private volatile int totalSegments;
|
private volatile int totalSegments;
|
||||||
private volatile int downloadedSegments;
|
private volatile int downloadedSegments;
|
||||||
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.
|
||||||
@ -92,8 +91,8 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
|
|||||||
this.cacheKeyFactory = constructorHelper.getCacheKeyFactory();
|
this.cacheKeyFactory = constructorHelper.getCacheKeyFactory();
|
||||||
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();
|
||||||
|
counters = new CachingCounters();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -130,7 +129,8 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
|
|||||||
true);
|
true);
|
||||||
downloadedSegments++;
|
downloadedSegments++;
|
||||||
} finally {
|
} finally {
|
||||||
downloadedBytes += cachingCounters.newlyCachedBytes;
|
counters.newlyCachedBytes += cachingCounters.newlyCachedBytes;
|
||||||
|
updatePercentage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@ -145,27 +145,17 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final long getDownloadedBytes() {
|
public final long getDownloadedBytes() {
|
||||||
return downloadedBytes;
|
return counters.totalCachedBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getTotalBytes() {
|
public long getTotalBytes() {
|
||||||
return totalBytes;
|
return counters.contentLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final float getDownloadPercentage() {
|
public final float getDownloadPercentage() {
|
||||||
// Take local snapshot of the volatile fields
|
return counters.percentage;
|
||||||
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) {
|
|
||||||
return C.PERCENTAGE_UNSET;
|
|
||||||
}
|
|
||||||
return totalSegments == 0 ? 100f : (downloadedSegments * 100f) / totalSegments;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -225,12 +215,13 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
|
|||||||
CachingCounters cachingCounters = new CachingCounters();
|
CachingCounters cachingCounters = new CachingCounters();
|
||||||
totalSegments = segments.size();
|
totalSegments = segments.size();
|
||||||
downloadedSegments = 0;
|
downloadedSegments = 0;
|
||||||
downloadedBytes = 0;
|
counters.alreadyCachedBytes = 0;
|
||||||
|
counters.newlyCachedBytes = 0;
|
||||||
long totalBytes = 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, cacheKeyFactory, cachingCounters);
|
CacheUtil.getCached(segment.dataSpec, cache, cacheKeyFactory, cachingCounters);
|
||||||
downloadedBytes += cachingCounters.alreadyCachedBytes;
|
counters.alreadyCachedBytes += cachingCounters.alreadyCachedBytes;
|
||||||
if (cachingCounters.contentLength != C.LENGTH_UNSET) {
|
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.
|
||||||
@ -244,10 +235,23 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
|
|||||||
totalBytes = C.LENGTH_UNSET;
|
totalBytes = C.LENGTH_UNSET;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.totalBytes = totalBytes;
|
counters.contentLength = totalBytes;
|
||||||
|
updatePercentage();
|
||||||
return segments;
|
return segments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updatePercentage() {
|
||||||
|
counters.updatePercentage();
|
||||||
|
if (counters.percentage == C.PERCENTAGE_UNSET) {
|
||||||
|
int totalSegments = this.totalSegments;
|
||||||
|
int downloadedSegments = this.downloadedSegments;
|
||||||
|
if (totalSegments != C.LENGTH_UNSET && downloadedSegments != C.LENGTH_UNSET) {
|
||||||
|
counters.percentage =
|
||||||
|
totalSegments == 0 ? 100f : (downloadedSegments * 100f) / totalSegments;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void removeDataSpec(DataSpec dataSpec) {
|
private void removeDataSpec(DataSpec dataSpec) {
|
||||||
CacheUtil.remove(dataSpec, cache, cacheKeyFactory);
|
CacheUtil.remove(dataSpec, cache, cacheKeyFactory);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user