Java Code Examples for com.google.android.exoplayer2.upstream.cache.CacheUtil#getCached()

The following examples show how to use com.google.android.exoplayer2.upstream.cache.CacheUtil#getCached() . 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: 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 2
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 3
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 4
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 5
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);
  }
}