com.google.android.exoplayer2.upstream.cache.CacheUtil Java Examples

The following examples show how to use com.google.android.exoplayer2.upstream.cache.CacheUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProgressiveDownloader.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
@Override
public void download(@Nullable ProgressListener progressListener)
    throws InterruptedException, IOException {
  priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
  try {
    CacheUtil.cache(
        dataSpec,
        cache,
        cacheKeyFactory,
        dataSource,
        new byte[BUFFER_SIZE_BYTES],
        priorityTaskManager,
        C.PRIORITY_DOWNLOAD,
        progressListener == null ? null : new ProgressForwarder(progressListener),
        isCanceled,
        /* enableEOFException= */ true);
  } finally {
    priorityTaskManager.remove(C.PRIORITY_DOWNLOAD);
  }
}
 
Example #2
Source File: ProgressiveDownloader.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void download(@Nullable ProgressListener progressListener)
    throws InterruptedException, IOException {
  priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
  try {
    CacheUtil.cache(
        dataSpec,
        cache,
        cacheKeyFactory,
        dataSource,
        new byte[BUFFER_SIZE_BYTES],
        priorityTaskManager,
        C.PRIORITY_DOWNLOAD,
        progressListener == null ? null : new ProgressForwarder(progressListener),
        isCanceled,
        /* enableEOFException= */ true);
  } finally {
    priorityTaskManager.remove(C.PRIORITY_DOWNLOAD);
  }
}
 
Example #3
Source File: ProgressiveDownloader.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void download(@Nullable ProgressListener progressListener)
    throws InterruptedException, IOException {
  priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
  try {
    CacheUtil.cache(
        dataSpec,
        cache,
        cacheKeyFactory,
        dataSource,
        new byte[BUFFER_SIZE_BYTES],
        priorityTaskManager,
        C.PRIORITY_DOWNLOAD,
        progressListener == null ? null : new ProgressForwarder(progressListener),
        isCanceled,
        /* enableEOFException= */ true);
  } finally {
    priorityTaskManager.remove(C.PRIORITY_DOWNLOAD);
  }
}
 
Example #4
Source File: SegmentDownloader.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/** Initializes the download, returning a list of {@link Segment}s that need to be downloaded. */
// Writes to downloadedSegments and downloadedBytes are safe. See the comment on download().
@SuppressWarnings("NonAtomicVolatileUpdate")
private List<Segment> initDownload() throws IOException, InterruptedException {
  M manifest = getManifest(dataSource, manifestUri);
  if (!streamKeys.isEmpty()) {
    manifest = manifest.copy(streamKeys);
  }
  List<Segment> segments = getSegments(dataSource, manifest, /* allowIncompleteList= */ false);
  CachingCounters cachingCounters = new CachingCounters();
  totalSegments = segments.size();
  downloadedSegments = 0;
  downloadedBytes = 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);
    }
  }
  return segments;
}
 
Example #5
Source File: ExoSourceManager.java    From GSYVideoPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 根据缓存块判断是否缓存成功
 *
 * @param cache
 */
private static boolean resolveCacheState(Cache cache, String url) {
    boolean isCache = true;
    if (!TextUtils.isEmpty(url)) {
        String key = CacheUtil.generateKey(Uri.parse(url));
        if (!TextUtils.isEmpty(key)) {
            NavigableSet<CacheSpan> cachedSpans = cache.getCachedSpans(key);
            if (cachedSpans.size() == 0) {
                isCache = false;
            } else {
                long contentLength = cache.getContentMetadata(key).get(ContentMetadata.KEY_CONTENT_LENGTH, C.LENGTH_UNSET);
                long currentLength = 0;
                for (CacheSpan cachedSpan : cachedSpans) {
                    currentLength += cache.getCachedLength(key, cachedSpan.position, cachedSpan.length);
                }
                isCache = currentLength >= contentLength;
            }
        } else {
            isCache = false;
        }
    }
    return isCache;
}
 
Example #6
Source File: ProgressiveDownloader.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void download() throws InterruptedException, IOException {
  priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
  try {
    CacheUtil.cache(
        dataSpec,
        cache,
        dataSource,
        new byte[BUFFER_SIZE_BYTES],
        priorityTaskManager,
        C.PRIORITY_DOWNLOAD,
        cachingCounters,
        isCanceled,
        /* enableEOFException= */ true);
  } finally {
    priorityTaskManager.remove(C.PRIORITY_DOWNLOAD);
  }
}
 
Example #7
Source File: ExoSourceManager.java    From GSYVideoPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * Cache需要release之后才能clear
 *
 * @param context
 * @param cacheDir
 * @param url
 */
public static void clearCache(Context context, File cacheDir, String url) {
    try {
        Cache cache = getCacheSingleInstance(context, cacheDir);
        if (!TextUtils.isEmpty(url)) {
            if (cache != null) {
                CacheUtil.remove(cache, CacheUtil.generateKey(Uri.parse(url)));
            }
        } else {
            if (cache != null) {
                for (String key : cache.getKeys()) {
                    CacheUtil.remove(cache, key);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #8
Source File: ProgressiveDownloader.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void download() throws InterruptedException, IOException {
  priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
  try {
    CacheUtil.cache(
        dataSpec,
        cache,
        dataSource,
        new byte[BUFFER_SIZE_BYTES],
        priorityTaskManager,
        C.PRIORITY_DOWNLOAD,
        cachingCounters,
        isCanceled,
        /* enableEOFException= */ true);
  } finally {
    priorityTaskManager.remove(C.PRIORITY_DOWNLOAD);
  }
}
 
Example #9
Source File: SegmentDownloader.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/** Initializes the download, returning a list of {@link Segment}s that need to be downloaded. */
// Writes to downloadedSegments and downloadedBytes are safe. See the comment on download().
@SuppressWarnings("NonAtomicVolatileUpdate")
private List<Segment> initDownload() throws IOException, InterruptedException {
  M manifest = getManifest(dataSource, manifestUri);
  if (!streamKeys.isEmpty()) {
    manifest = manifest.copy(streamKeys);
  }
  List<Segment> segments = getSegments(dataSource, manifest, /* allowIncompleteList= */ false);
  CachingCounters cachingCounters = new CachingCounters();
  totalSegments = segments.size();
  downloadedSegments = 0;
  downloadedBytes = 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);
    }
  }
  return segments;
}
 
Example #10
Source File: DownloaderConstructorHelper.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/** Returns the {@link CacheKeyFactory}. */
public CacheKeyFactory getCacheKeyFactory() {
  return cacheKeyFactory != null ? cacheKeyFactory : CacheUtil.DEFAULT_CACHE_KEY_FACTORY;
}
 
Example #11
Source File: ProgressiveDownloader.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void remove() {
  CacheUtil.remove(dataSpec, cache, cacheKeyFactory);
}
 
Example #12
Source File: SegmentDownloader.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private void removeDataSpec(DataSpec dataSpec) {
  CacheUtil.remove(dataSpec, cache, cacheKeyFactory);
}
 
Example #13
Source File: ProgressiveDownloader.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@Override
public void remove() {
  CacheUtil.remove(dataSpec, cache, cacheKeyFactory);
}
 
Example #14
Source File: DownloaderConstructorHelper.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/** Returns the {@link CacheKeyFactory}. */
public CacheKeyFactory getCacheKeyFactory() {
  return cacheKeyFactory != null ? cacheKeyFactory : CacheUtil.DEFAULT_CACHE_KEY_FACTORY;
}
 
Example #15
Source File: SegmentDownloader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Downloads the selected streams in the media. If multiple streams are selected, they are
 * downloaded in sync with one another.
 *
 * @throws IOException Thrown when there is an error downloading.
 * @throws InterruptedException If the thread has been interrupted.
 */
// downloadedSegments and downloadedBytes are only written from this method, and this method
// should not be called from more than one thread. Hence non-atomic updates are valid.
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public final void download() throws IOException, InterruptedException {
  priorityTaskManager.add(C.PRIORITY_DOWNLOAD);

  try {
    List<Segment> segments = initDownload();
    Collections.sort(segments);
    byte[] buffer = new byte[BUFFER_SIZE_BYTES];
    CachingCounters cachingCounters = new CachingCounters();
    for (int i = 0; i < segments.size(); i++) {
      try {
        CacheUtil.cache(
            segments.get(i).dataSpec,
            cache,
            dataSource,
            buffer,
            priorityTaskManager,
            C.PRIORITY_DOWNLOAD,
            cachingCounters,
            isCanceled,
            true);
        downloadedSegments++;
      } finally {
        downloadedBytes += cachingCounters.newlyCachedBytes;
      }
    }
  } finally {
    priorityTaskManager.remove(C.PRIORITY_DOWNLOAD);
  }
}
 
Example #16
Source File: SegmentDownloader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Downloads the selected streams in the media. If multiple streams are selected, they are
 * downloaded in sync with one another.
 *
 * @throws IOException Thrown when there is an error downloading.
 * @throws InterruptedException If the thread has been interrupted.
 */
// downloadedSegments and downloadedBytes are only written from this method, and this method
// should not be called from more than one thread. Hence non-atomic updates are valid.
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public final void download() throws IOException, InterruptedException {
  priorityTaskManager.add(C.PRIORITY_DOWNLOAD);

  try {
    List<Segment> segments = initDownload();
    Collections.sort(segments);
    byte[] buffer = new byte[BUFFER_SIZE_BYTES];
    CachingCounters cachingCounters = new CachingCounters();
    for (int i = 0; i < segments.size(); i++) {
      try {
        CacheUtil.cache(
            segments.get(i).dataSpec,
            cache,
            dataSource,
            buffer,
            priorityTaskManager,
            C.PRIORITY_DOWNLOAD,
            cachingCounters,
            isCanceled,
            true);
        downloadedSegments++;
      } finally {
        downloadedBytes += cachingCounters.newlyCachedBytes;
      }
    }
  } finally {
    priorityTaskManager.remove(C.PRIORITY_DOWNLOAD);
  }
}
 
Example #17
Source File: SegmentDownloader.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private void removeDataSpec(DataSpec dataSpec) {
  CacheUtil.remove(dataSpec, cache, cacheKeyFactory);
}
 
Example #18
Source File: SegmentDownloader.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
/**
 * Downloads the selected streams in the media. If multiple streams are selected, they are
 * downloaded in sync with one another.
 *
 * @throws IOException Thrown when there is an error downloading.
 * @throws InterruptedException If the thread has been interrupted.
 */
@Override
public final void download(@Nullable ProgressListener progressListener)
    throws IOException, InterruptedException {
  priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
  try {
    // Get the manifest and all of the segments.
    M manifest = getManifest(dataSource, manifestDataSpec);
    if (!streamKeys.isEmpty()) {
      manifest = manifest.copy(streamKeys);
    }
    List<Segment> segments = getSegments(dataSource, manifest, /* allowIncompleteList= */ false);

    // Scan the segments, removing any that are fully downloaded.
    int totalSegments = segments.size();
    int segmentsDownloaded = 0;
    long contentLength = 0;
    long bytesDownloaded = 0;
    for (int i = segments.size() - 1; i >= 0; i--) {
      Segment segment = segments.get(i);
      Pair<Long, Long> segmentLengthAndBytesDownloaded =
          CacheUtil.getCached(segment.dataSpec, cache, cacheKeyFactory);
      long segmentLength = segmentLengthAndBytesDownloaded.first;
      long segmentBytesDownloaded = segmentLengthAndBytesDownloaded.second;
      bytesDownloaded += segmentBytesDownloaded;
      if (segmentLength != C.LENGTH_UNSET) {
        if (segmentLength == segmentBytesDownloaded) {
          // The segment is fully downloaded.
          segmentsDownloaded++;
          segments.remove(i);
        }
        if (contentLength != C.LENGTH_UNSET) {
          contentLength += segmentLength;
        }
      } else {
        contentLength = C.LENGTH_UNSET;
      }
    }
    Collections.sort(segments);

    // Download the segments.
    @Nullable ProgressNotifier progressNotifier = null;
    if (progressListener != null) {
      progressNotifier =
          new ProgressNotifier(
              progressListener,
              contentLength,
              totalSegments,
              bytesDownloaded,
              segmentsDownloaded);
    }
    byte[] buffer = new byte[BUFFER_SIZE_BYTES];
    for (int i = 0; i < segments.size(); i++) {
      CacheUtil.cache(
          segments.get(i).dataSpec,
          cache,
          cacheKeyFactory,
          dataSource,
          buffer,
          priorityTaskManager,
          C.PRIORITY_DOWNLOAD,
          progressNotifier,
          isCanceled,
          true);
      if (progressNotifier != null) {
        progressNotifier.onSegmentDownloaded();
      }
    }
  } finally {
    priorityTaskManager.remove(C.PRIORITY_DOWNLOAD);
  }
}
 
Example #19
Source File: SegmentDownloader.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private void removeDataSpec(DataSpec dataSpec) {
  CacheUtil.remove(dataSpec, cache, cacheKeyFactory);
}
 
Example #20
Source File: SegmentDownloader.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Downloads the selected streams in the media. If multiple streams are selected, they are
 * downloaded in sync with one another.
 *
 * @throws IOException Thrown when there is an error downloading.
 * @throws InterruptedException If the thread has been interrupted.
 */
@Override
public final void download(@Nullable ProgressListener progressListener)
    throws IOException, InterruptedException {
  priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
  try {
    // Get the manifest and all of the segments.
    M manifest = getManifest(dataSource, manifestDataSpec);
    if (!streamKeys.isEmpty()) {
      manifest = manifest.copy(streamKeys);
    }
    List<Segment> segments = getSegments(dataSource, manifest, /* allowIncompleteList= */ false);

    // Scan the segments, removing any that are fully downloaded.
    int totalSegments = segments.size();
    int segmentsDownloaded = 0;
    long contentLength = 0;
    long bytesDownloaded = 0;
    for (int i = segments.size() - 1; i >= 0; i--) {
      Segment segment = segments.get(i);
      Pair<Long, Long> segmentLengthAndBytesDownloaded =
          CacheUtil.getCached(segment.dataSpec, cache, cacheKeyFactory);
      long segmentLength = segmentLengthAndBytesDownloaded.first;
      long segmentBytesDownloaded = segmentLengthAndBytesDownloaded.second;
      bytesDownloaded += segmentBytesDownloaded;
      if (segmentLength != C.LENGTH_UNSET) {
        if (segmentLength == segmentBytesDownloaded) {
          // The segment is fully downloaded.
          segmentsDownloaded++;
          segments.remove(i);
        }
        if (contentLength != C.LENGTH_UNSET) {
          contentLength += segmentLength;
        }
      } else {
        contentLength = C.LENGTH_UNSET;
      }
    }
    Collections.sort(segments);

    // Download the segments.
    ProgressNotifier progressNotifier = null;
    if (progressListener != null) {
      progressNotifier =
          new ProgressNotifier(
              progressListener,
              contentLength,
              totalSegments,
              bytesDownloaded,
              segmentsDownloaded);
    }
    byte[] buffer = new byte[BUFFER_SIZE_BYTES];
    for (int i = 0; i < segments.size(); i++) {
      CacheUtil.cache(
          segments.get(i).dataSpec,
          cache,
          cacheKeyFactory,
          dataSource,
          buffer,
          priorityTaskManager,
          C.PRIORITY_DOWNLOAD,
          progressNotifier,
          isCanceled,
          true);
      if (progressNotifier != null) {
        progressNotifier.onSegmentDownloaded();
      }
    }
  } finally {
    priorityTaskManager.remove(C.PRIORITY_DOWNLOAD);
  }
}
 
Example #21
Source File: ProgressiveDownloader.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void remove() {
  CacheUtil.remove(dataSpec, cache, cacheKeyFactory);
}
 
Example #22
Source File: SegmentDownloader.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Downloads the selected streams in the media. If multiple streams are selected, they are
 * downloaded in sync with one another.
 *
 * @throws IOException Thrown when there is an error downloading.
 * @throws InterruptedException If the thread has been interrupted.
 */
@Override
public final void download(@Nullable ProgressListener progressListener)
    throws IOException, InterruptedException {
  priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
  try {
    // Get the manifest and all of the segments.
    M manifest = getManifest(dataSource, manifestDataSpec);
    if (!streamKeys.isEmpty()) {
      manifest = manifest.copy(streamKeys);
    }
    List<Segment> segments = getSegments(dataSource, manifest, /* allowIncompleteList= */ false);

    // Scan the segments, removing any that are fully downloaded.
    int totalSegments = segments.size();
    int segmentsDownloaded = 0;
    long contentLength = 0;
    long bytesDownloaded = 0;
    for (int i = segments.size() - 1; i >= 0; i--) {
      Segment segment = segments.get(i);
      Pair<Long, Long> segmentLengthAndBytesDownloaded =
          CacheUtil.getCached(segment.dataSpec, cache, cacheKeyFactory);
      long segmentLength = segmentLengthAndBytesDownloaded.first;
      long segmentBytesDownloaded = segmentLengthAndBytesDownloaded.second;
      bytesDownloaded += segmentBytesDownloaded;
      if (segmentLength != C.LENGTH_UNSET) {
        if (segmentLength == segmentBytesDownloaded) {
          // The segment is fully downloaded.
          segmentsDownloaded++;
          segments.remove(i);
        }
        if (contentLength != C.LENGTH_UNSET) {
          contentLength += segmentLength;
        }
      } else {
        contentLength = C.LENGTH_UNSET;
      }
    }
    Collections.sort(segments);

    // Download the segments.
    ProgressNotifier progressNotifier = null;
    if (progressListener != null) {
      progressNotifier =
          new ProgressNotifier(
              progressListener,
              contentLength,
              totalSegments,
              bytesDownloaded,
              segmentsDownloaded);
    }
    byte[] buffer = new byte[BUFFER_SIZE_BYTES];
    for (int i = 0; i < segments.size(); i++) {
      CacheUtil.cache(
          segments.get(i).dataSpec,
          cache,
          cacheKeyFactory,
          dataSource,
          buffer,
          priorityTaskManager,
          C.PRIORITY_DOWNLOAD,
          progressNotifier,
          isCanceled,
          true);
      if (progressNotifier != null) {
        progressNotifier.onSegmentDownloaded();
      }
    }
  } finally {
    priorityTaskManager.remove(C.PRIORITY_DOWNLOAD);
  }
}
 
Example #23
Source File: SegmentDownloader.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void removeUri(Uri uri) {
  CacheUtil.remove(cache, CacheUtil.generateKey(uri));
}
 
Example #24
Source File: ProgressiveDownloader.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void remove() {
  CacheUtil.remove(cache, CacheUtil.getKey(dataSpec));
}
 
Example #25
Source File: ProgressiveDownloadAction.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private String getCacheKey() {
  return customCacheKey != null ? customCacheKey : CacheUtil.generateKey(uri);
}
 
Example #26
Source File: ProgressiveDownloader.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void remove() {
  CacheUtil.remove(cache, CacheUtil.getKey(dataSpec));
}
 
Example #27
Source File: ProgressiveDownloadAction.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private String getCacheKey() {
  return customCacheKey != null ? customCacheKey : CacheUtil.generateKey(uri);
}
 
Example #28
Source File: SegmentDownloader.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void removeUri(Uri uri) {
  CacheUtil.remove(cache, CacheUtil.generateKey(uri));
}
 
Example #29
Source File: DownloaderConstructorHelper.java    From Telegram-FOSS with GNU General Public License v2.0 3 votes vote down vote up
/** Returns the {@link CacheKeyFactory}. */
public CacheKeyFactory getCacheKeyFactory() {
  return cacheKeyFactory != null ? cacheKeyFactory : CacheUtil.DEFAULT_CACHE_KEY_FACTORY;
}