com.google.android.exoplayer2.upstream.DataSpec Java Examples

The following examples show how to use com.google.android.exoplayer2.upstream.DataSpec. 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: AdsMediaSource.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
@Override
public void onAdLoadError(final AdLoadException error, DataSpec dataSpec) {
  if (released) {
    return;
  }
  createEventDispatcher(/* mediaPeriodId= */ null)
      .loadError(
          dataSpec,
          dataSpec.uri,
          /* responseHeaders= */ Collections.emptyMap(),
          C.DATA_TYPE_AD,
          C.TRACK_TYPE_UNKNOWN,
          /* loadDurationMs= */ 0,
          /* bytesLoaded= */ 0,
          error,
          /* wasCanceled= */ true);
}
 
Example #2
Source File: HlsMediaChunk.java    From K-Sonic with MIT License 6 votes vote down vote up
private void maybeLoadInitData() throws IOException, InterruptedException {
  if (previousExtractor == extractor || initLoadCompleted || initDataSpec == null) {
    // According to spec, for packed audio, initDataSpec is expected to be null.
    return;
  }
  DataSpec initSegmentDataSpec = Util.getRemainderDataSpec(initDataSpec, initSegmentBytesLoaded);
  try {
    ExtractorInput input = new DefaultExtractorInput(initDataSource,
        initSegmentDataSpec.absoluteStreamPosition, initDataSource.open(initSegmentDataSpec));
    try {
      int result = Extractor.RESULT_CONTINUE;
      while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
        result = extractor.read(input, null);
      }
    } finally {
      initSegmentBytesLoaded = (int) (input.getPosition() - initDataSpec.absoluteStreamPosition);
    }
  } finally {
    Util.closeQuietly(dataSource);
  }
  initLoadCompleted = true;
}
 
Example #3
Source File: DefaultDashChunkSource.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
protected Chunk newInitializationChunk(
    RepresentationHolder representationHolder,
    DataSource dataSource,
    Format trackFormat,
    int trackSelectionReason,
    Object trackSelectionData,
    RangedUri initializationUri,
    RangedUri indexUri) {
  RangedUri requestUri;
  String baseUrl = representationHolder.representation.baseUrl;
  if (initializationUri != null) {
    // It's common for initialization and index data to be stored adjacently. Attempt to merge
    // the two requests together to request both at once.
    requestUri = initializationUri.attemptMerge(indexUri, baseUrl);
    if (requestUri == null) {
      requestUri = initializationUri;
    }
  } else {
    requestUri = indexUri;
  }
  DataSpec dataSpec = new DataSpec(requestUri.resolveUri(baseUrl), requestUri.start,
      requestUri.length, representationHolder.representation.getCacheKey());
  return new InitializationChunk(dataSource, dataSpec, trackFormat,
      trackSelectionReason, trackSelectionData, representationHolder.extractorWrapper);
}
 
Example #4
Source File: CacheUtil.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Queries the cache to obtain the request length and the number of bytes already cached for a
 * given {@link DataSpec}.
 *
 * @param dataSpec Defines the data to be checked.
 * @param cache A {@link Cache} which has the data.
 * @param cacheKeyFactory An optional factory for cache keys.
 * @return A pair containing the request length and the number of bytes that are already cached.
 */
public static Pair<Long, Long> getCached(
    DataSpec dataSpec, Cache cache, @Nullable CacheKeyFactory cacheKeyFactory) {
  String key = buildCacheKey(dataSpec, cacheKeyFactory);
  long position = dataSpec.absoluteStreamPosition;
  long requestLength = getRequestLength(dataSpec, cache, key);
  long bytesAlreadyCached = 0;
  long bytesLeft = requestLength;
  while (bytesLeft != 0) {
    long blockLength =
        cache.getCachedLength(
            key, position, bytesLeft != C.LENGTH_UNSET ? bytesLeft : Long.MAX_VALUE);
    if (blockLength > 0) {
      bytesAlreadyCached += blockLength;
    } else {
      blockLength = -blockLength;
      if (blockLength == Long.MAX_VALUE) {
        break;
      }
    }
    position += blockLength;
    bytesLeft -= bytesLeft == C.LENGTH_UNSET ? 0 : blockLength;
  }
  return Pair.create(requestLength, bytesAlreadyCached);
}
 
Example #5
Source File: MainActivity.java    From ExoplayerExample with The Unlicense 6 votes vote down vote up
/**
 * Prepares exoplayer for audio playback from a local file
 * @param uri
 */
private void prepareExoPlayerFromFileUri(Uri uri){
    exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(null), new DefaultLoadControl());
    exoPlayer.addListener(eventListener);

    DataSpec dataSpec = new DataSpec(uri);
    final FileDataSource fileDataSource = new FileDataSource();
    try {
        fileDataSource.open(dataSpec);
    } catch (FileDataSource.FileDataSourceException e) {
        e.printStackTrace();
    }

    DataSource.Factory factory = new DataSource.Factory() {
        @Override
        public DataSource createDataSource() {
            return fileDataSource;
        }
    };
    MediaSource audioSource = new ExtractorMediaSource(fileDataSource.getUri(),
            factory, new DefaultExtractorsFactory(), null, null);

    exoPlayer.prepare(audioSource);
    initMediaControls();
}
 
Example #6
Source File: HlsDownloader.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private void addSegment(
    HlsMediaPlaylist mediaPlaylist,
    HlsMediaPlaylist.Segment segment,
    HashSet<Uri> seenEncryptionKeyUris,
    ArrayList<Segment> out) {
  String baseUri = mediaPlaylist.baseUri;
  long startTimeUs = mediaPlaylist.startTimeUs + segment.relativeStartTimeUs;
  if (segment.fullSegmentEncryptionKeyUri != null) {
    Uri keyUri = UriUtil.resolveToUri(baseUri, segment.fullSegmentEncryptionKeyUri);
    if (seenEncryptionKeyUris.add(keyUri)) {
      out.add(new Segment(startTimeUs, SegmentDownloader.getCompressibleDataSpec(keyUri)));
    }
  }
  Uri segmentUri = UriUtil.resolveToUri(baseUri, segment.url);
  DataSpec dataSpec =
      new DataSpec(segmentUri, segment.byterangeOffset, segment.byterangeLength, /* key= */ null);
  out.add(new Segment(startTimeUs, dataSpec));
}
 
Example #7
Source File: SingleSampleMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private SingleSampleMediaSource(
    Uri uri,
    DataSource.Factory dataSourceFactory,
    Format format,
    long durationUs,
    int minLoadableRetryCount,
    boolean treatLoadErrorsAsEndOfStream,
    @Nullable Object tag) {
  this.dataSourceFactory = dataSourceFactory;
  this.format = format;
  this.durationUs = durationUs;
  this.minLoadableRetryCount = minLoadableRetryCount;
  this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
  dataSpec = new DataSpec(uri);
  timeline =
      new SinglePeriodTimeline(durationUs, /* isSeekable= */ true, /* isDynamic= */ false, tag);
}
 
Example #8
Source File: SingleSampleMediaPeriod.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
public SingleSampleMediaPeriod(
    DataSpec dataSpec,
    DataSource.Factory dataSourceFactory,
    @Nullable TransferListener transferListener,
    Format format,
    long durationUs,
    int minLoadableRetryCount,
    EventDispatcher eventDispatcher,
    boolean treatLoadErrorsAsEndOfStream) {
  this.dataSpec = dataSpec;
  this.dataSourceFactory = dataSourceFactory;
  this.transferListener = transferListener;
  this.format = format;
  this.durationUs = durationUs;
  this.minLoadableRetryCount = minLoadableRetryCount;
  this.eventDispatcher = eventDispatcher;
  this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
  tracks = new TrackGroupArray(new TrackGroup(format));
  sampleStreams = new ArrayList<>();
  loader = new Loader("Loader:SingleSampleMediaPeriod");
  eventDispatcher.mediaPeriodCreated();
}
 
Example #9
Source File: MediaSourceEventListener.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/** Dispatches {@link #onLoadCanceled(int, MediaPeriodId, LoadEventInfo, MediaLoadData)}. */
public void loadCanceled(
    DataSpec dataSpec,
    Uri uri,
    int dataType,
    int trackType,
    @Nullable Format trackFormat,
    int trackSelectionReason,
    @Nullable Object trackSelectionData,
    long mediaStartTimeUs,
    long mediaEndTimeUs,
    long elapsedRealtimeMs,
    long loadDurationMs,
    long bytesLoaded) {
  loadCanceled(
      new LoadEventInfo(dataSpec, uri, elapsedRealtimeMs, loadDurationMs, bytesLoaded),
      new MediaLoadData(
          dataType,
          trackType,
          trackFormat,
          trackSelectionReason,
          trackSelectionData,
          adjustMediaTime(mediaStartTimeUs),
          adjustMediaTime(mediaEndTimeUs)));
}
 
Example #10
Source File: Chunk.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param dataSource The source from which the data should be loaded.
 * @param dataSpec Defines the data to be loaded.
 * @param type See {@link #type}.
 * @param trackFormat See {@link #trackFormat}.
 * @param trackSelectionReason See {@link #trackSelectionReason}.
 * @param trackSelectionData See {@link #trackSelectionData}.
 * @param startTimeUs See {@link #startTimeUs}.
 * @param endTimeUs See {@link #endTimeUs}.
 */
public Chunk(
    DataSource dataSource,
    DataSpec dataSpec,
    int type,
    Format trackFormat,
    int trackSelectionReason,
    @Nullable Object trackSelectionData,
    long startTimeUs,
    long endTimeUs) {
  this.dataSource = new StatsDataSource(dataSource);
  this.dataSpec = Assertions.checkNotNull(dataSpec);
  this.type = type;
  this.trackFormat = trackFormat;
  this.trackSelectionReason = trackSelectionReason;
  this.trackSelectionData = trackSelectionData;
  this.startTimeUs = startTimeUs;
  this.endTimeUs = endTimeUs;
}
 
Example #11
Source File: SsDownloader.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected List<Segment> getSegments(
    DataSource dataSource, SsManifest manifest, boolean allowIncompleteList) {
  ArrayList<Segment> segments = new ArrayList<>();
  for (StreamElement streamElement : manifest.streamElements) {
    for (int i = 0; i < streamElement.formats.length; i++) {
      for (int j = 0; j < streamElement.chunkCount; j++) {
        segments.add(
            new Segment(
                streamElement.getStartTimeUs(j),
                new DataSpec(streamElement.buildRequestUri(i, j))));
      }
    }
  }
  return segments;
}
 
Example #12
Source File: ProgressiveDownloader.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param uri Uri of the data to be downloaded.
 * @param customCacheKey A custom key that uniquely identifies the original stream. Used for cache
 *     indexing. May be null.
 * @param constructorHelper A {@link DownloaderConstructorHelper} instance.
 */
public ProgressiveDownloader(
    Uri uri, @Nullable String customCacheKey, DownloaderConstructorHelper constructorHelper) {
  this.dataSpec =
      new DataSpec(
          uri,
          /* absoluteStreamPosition= */ 0,
          C.LENGTH_UNSET,
          customCacheKey,
          /* flags= */ DataSpec.FLAG_ALLOW_CACHE_FRAGMENTATION);
  this.cache = constructorHelper.getCache();
  this.dataSource = constructorHelper.createCacheDataSource();
  this.cacheKeyFactory = constructorHelper.getCacheKeyFactory();
  this.priorityTaskManager = constructorHelper.getPriorityTaskManager();
  isCanceled = new AtomicBoolean();
}
 
Example #13
Source File: DefaultDashChunkSource.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
protected Chunk newInitializationChunk(
    RepresentationHolder representationHolder,
    DataSource dataSource,
    Format trackFormat,
    int trackSelectionReason,
    Object trackSelectionData,
    RangedUri initializationUri,
    RangedUri indexUri) {
  RangedUri requestUri;
  String baseUrl = representationHolder.representation.baseUrl;
  if (initializationUri != null) {
    // It's common for initialization and index data to be stored adjacently. Attempt to merge
    // the two requests together to request both at once.
    requestUri = initializationUri.attemptMerge(indexUri, baseUrl);
    if (requestUri == null) {
      requestUri = initializationUri;
    }
  } else {
    requestUri = indexUri;
  }
  DataSpec dataSpec = new DataSpec(requestUri.resolveUri(baseUrl), requestUri.start,
      requestUri.length, representationHolder.representation.getCacheKey());
  return new InitializationChunk(dataSource, dataSpec, trackFormat,
      trackSelectionReason, trackSelectionData, representationHolder.extractorWrapper);
}
 
Example #14
Source File: MediaSourceEventListener.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/** Dispatches {@link #onLoadStarted(int, MediaPeriodId, LoadEventInfo, MediaLoadData)}. */
public void loadStarted(
    DataSpec dataSpec,
    Uri uri,
    int dataType,
    int trackType,
    @Nullable Format trackFormat,
    int trackSelectionReason,
    @Nullable Object trackSelectionData,
    long mediaStartTimeUs,
    long mediaEndTimeUs,
    long elapsedRealtimeMs) {
  loadStarted(
      new LoadEventInfo(
          dataSpec, uri, elapsedRealtimeMs, /* loadDurationMs= */ 0, /* bytesLoaded= */ 0),
      new MediaLoadData(
          dataType,
          trackType,
          trackFormat,
          trackSelectionReason,
          trackSelectionData,
          adjustMediaTime(mediaStartTimeUs),
          adjustMediaTime(mediaEndTimeUs)));
}
 
Example #15
Source File: MediaSourceEventListener.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/** Dispatches {@link #onLoadCanceled(int, MediaPeriodId, LoadEventInfo, MediaLoadData)}. */
public void loadCanceled(
    DataSpec dataSpec,
    Uri uri,
    int dataType,
    long elapsedRealtimeMs,
    long loadDurationMs,
    long bytesLoaded) {
  loadCanceled(
      dataSpec,
      uri,
      dataType,
      C.TRACK_TYPE_UNKNOWN,
      null,
      C.SELECTION_REASON_UNKNOWN,
      null,
      C.TIME_UNSET,
      C.TIME_UNSET,
      elapsedRealtimeMs,
      loadDurationMs,
      bytesLoaded);
}
 
Example #16
Source File: DefaultDashChunkSource.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
protected Chunk newInitializationChunk(
    RepresentationHolder representationHolder,
    DataSource dataSource,
    Format trackFormat,
    int trackSelectionReason,
    Object trackSelectionData,
    RangedUri initializationUri,
    RangedUri indexUri) {
  RangedUri requestUri;
  String baseUrl = representationHolder.representation.baseUrl;
  if (initializationUri != null) {
    // It's common for initialization and index data to be stored adjacently. Attempt to merge
    // the two requests together to request both at once.
    requestUri = initializationUri.attemptMerge(indexUri, baseUrl);
    if (requestUri == null) {
      requestUri = initializationUri;
    }
  } else {
    requestUri = indexUri;
  }
  DataSpec dataSpec = new DataSpec(requestUri.resolveUri(baseUrl), requestUri.start,
      requestUri.length, representationHolder.representation.getCacheKey());
  return new InitializationChunk(dataSource, dataSpec, trackFormat,
      trackSelectionReason, trackSelectionData, representationHolder.extractorWrapper);
}
 
Example #17
Source File: EncryptedFileDataSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long open(DataSpec dataSpec) throws EncryptedFileDataSourceException {
    try {
        this.dataSpec = dataSpec;
        uri = dataSpec.uri;
        File path = new File(dataSpec.uri.getPath());
        String name = path.getName();
        File keyPath = new File(FileLoader.getInternalCacheDir(), name + ".key");
        RandomAccessFile keyFile = new RandomAccessFile(keyPath, "r");
        keyFile.read(key);
        keyFile.read(iv);
        keyFile.close();

        file = new RandomAccessFile(path, "r");
        file.seek(dataSpec.position);
        fileOffset = (int) dataSpec.position;
        bytesRemaining = dataSpec.length == C.LENGTH_UNSET ? file.length() - dataSpec.position : dataSpec.length;
        if (bytesRemaining < 0) {
            throw new EOFException();
        }
    } catch (IOException e) {
        throw new EncryptedFileDataSourceException(e);
    }

    opened = true;
    if (listener != null) {
        listener.onTransferStart(this, dataSpec, false);
    }

    return bytesRemaining;
}
 
Example #18
Source File: MediaSourceEventListener.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/** Dispatches {@link #onLoadStarted(int, MediaPeriodId, LoadEventInfo, MediaLoadData)}. */
public void loadStarted(
    DataSpec dataSpec,
    int dataType,
    int trackType,
    @Nullable Format trackFormat,
    int trackSelectionReason,
    @Nullable Object trackSelectionData,
    long mediaStartTimeUs,
    long mediaEndTimeUs,
    long elapsedRealtimeMs) {
  loadStarted(
      new LoadEventInfo(
          dataSpec,
          dataSpec.uri,
          /* responseHeaders= */ Collections.emptyMap(),
          elapsedRealtimeMs,
          /* loadDurationMs= */ 0,
          /* bytesLoaded= */ 0),
      new MediaLoadData(
          dataType,
          trackType,
          trackFormat,
          trackSelectionReason,
          trackSelectionData,
          adjustMediaTime(mediaStartTimeUs),
          adjustMediaTime(mediaEndTimeUs)));
}
 
Example #19
Source File: SegmentDownloader.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
protected static DataSpec getCompressibleDataSpec(Uri uri) {
  return new DataSpec(
      uri,
      /* absoluteStreamPosition= */ 0,
      /* length= */ C.LENGTH_UNSET,
      /* key= */ null,
      /* flags= */ DataSpec.FLAG_ALLOW_GZIP);
}
 
Example #20
Source File: ContainerMediaChunk.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param dataSource The source from which the data should be loaded.
 * @param dataSpec Defines the data to be loaded.
 * @param trackFormat See {@link #trackFormat}.
 * @param trackSelectionReason See {@link #trackSelectionReason}.
 * @param trackSelectionData See {@link #trackSelectionData}.
 * @param startTimeUs The start time of the media contained by the chunk, in microseconds.
 * @param endTimeUs The end time of the media contained by the chunk, in microseconds.
 * @param seekTimeUs The media time from which output will begin, or {@link C#TIME_UNSET} if the
 *     whole chunk should be output.
 * @param chunkIndex The index of the chunk, or {@link C#INDEX_UNSET} if it is not known.
 * @param chunkCount The number of chunks in the underlying media that are spanned by this
 *     instance. Normally equal to one, but may be larger if multiple chunks as defined by the
 *     underlying media are being merged into a single load.
 * @param sampleOffsetUs An offset to add to the sample timestamps parsed by the extractor.
 * @param extractorWrapper A wrapped extractor to use for parsing the data.
 */
public ContainerMediaChunk(
    DataSource dataSource,
    DataSpec dataSpec,
    Format trackFormat,
    int trackSelectionReason,
    Object trackSelectionData,
    long startTimeUs,
    long endTimeUs,
    long seekTimeUs,
    long chunkIndex,
    int chunkCount,
    long sampleOffsetUs,
    ChunkExtractorWrapper extractorWrapper) {
  super(
      dataSource,
      dataSpec,
      trackFormat,
      trackSelectionReason,
      trackSelectionData,
      startTimeUs,
      endTimeUs,
      seekTimeUs,
      chunkIndex);
  this.chunkCount = chunkCount;
  this.sampleOffsetUs = sampleOffsetUs;
  this.extractorWrapper = extractorWrapper;
}
 
Example #21
Source File: AesCipherDataSource.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@Override
public long open(DataSpec dataSpec) throws IOException {
  long dataLength = upstream.open(dataSpec);
  long nonce = CryptoUtil.getFNV64Hash(dataSpec.key);
  cipher = new AesFlushingCipher(Cipher.DECRYPT_MODE, secretKey, nonce,
      dataSpec.absoluteStreamPosition);
  return dataLength;
}
 
Example #22
Source File: DashUtil.java    From K-Sonic with MIT License 5 votes vote down vote up
/**
 * Loads a DASH manifest.
 *
 * @param dataSource The {@link HttpDataSource} from which the manifest should be read.
 * @param manifestUriString The URI of the manifest to be read.
 * @return An instance of {@link DashManifest}.
 * @throws IOException If an error occurs reading data from the stream.
 * @see DashManifestParser
 */
public static DashManifest loadManifest(DataSource dataSource, String manifestUriString)
    throws IOException {
  DataSourceInputStream inputStream = new DataSourceInputStream(dataSource,
      new DataSpec(Uri.parse(manifestUriString), DataSpec.FLAG_ALLOW_CACHING_UNKNOWN_LENGTH));
  try {
    inputStream.open();
    DashManifestParser parser = new DashManifestParser();
    return parser.parse(dataSource.getUri(), inputStream);
  } finally {
    inputStream.close();
  }
}
 
Example #23
Source File: BaseMeter.java    From ARVI with Apache License 2.0 5 votes vote down vote up
@Override
public final void onTransferEnd(DataSource dataSource,
                                DataSpec dataSpec,
                                boolean isNetwork) {
    this.transferListener.onTransferEnd(
        dataSource,
        dataSpec,
        isNetwork
    );
}
 
Example #24
Source File: AesCipherDataSink.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@Override
public void open(DataSpec dataSpec) throws IOException {
  wrappedDataSink.open(dataSpec);
  long nonce = CryptoUtil.getFNV64Hash(dataSpec.key);
  cipher = new AesFlushingCipher(Cipher.ENCRYPT_MODE, secretKey, nonce,
      dataSpec.absoluteStreamPosition);
}
 
Example #25
Source File: ContainerMediaChunk.java    From K-Sonic with MIT License 5 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public final void load() throws IOException, InterruptedException {
  DataSpec loadDataSpec = Util.getRemainderDataSpec(dataSpec, bytesLoaded);
  try {
    // Create and open the input.
    ExtractorInput input = new DefaultExtractorInput(dataSource,
        loadDataSpec.absoluteStreamPosition, dataSource.open(loadDataSpec));
    if (bytesLoaded == 0) {
      // Configure the output and set it as the target for the extractor wrapper.
      BaseMediaChunkOutput output = getOutput();
      output.setSampleOffsetUs(sampleOffsetUs);
      extractorWrapper.init(output);
    }
    // Load and decode the sample data.
    try {
      Extractor extractor = extractorWrapper.extractor;
      int result = Extractor.RESULT_CONTINUE;
      while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
        result = extractor.read(input, null);
      }
      Assertions.checkState(result != Extractor.RESULT_SEEK);
    } finally {
      bytesLoaded = (int) (input.getPosition() - dataSpec.absoluteStreamPosition);
    }
  } finally {
    Util.closeQuietly(dataSource);
  }
  loadCompleted = true;
}
 
Example #26
Source File: ContainerMediaChunk.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public final void load() throws IOException, InterruptedException {
  DataSpec loadDataSpec = dataSpec.subrange(nextLoadPosition);
  try {
    // Create and open the input.
    ExtractorInput input = new DefaultExtractorInput(dataSource,
        loadDataSpec.absoluteStreamPosition, dataSource.open(loadDataSpec));
    if (nextLoadPosition == 0) {
      // Configure the output and set it as the target for the extractor wrapper.
      BaseMediaChunkOutput output = getOutput();
      output.setSampleOffsetUs(sampleOffsetUs);
      extractorWrapper.init(
          getTrackOutputProvider(output),
          clippedStartTimeUs == C.TIME_UNSET
              ? C.TIME_UNSET
              : (clippedStartTimeUs - sampleOffsetUs),
          clippedEndTimeUs == C.TIME_UNSET ? C.TIME_UNSET : (clippedEndTimeUs - sampleOffsetUs));
    }
    // Load and decode the sample data.
    try {
      Extractor extractor = extractorWrapper.extractor;
      int result = Extractor.RESULT_CONTINUE;
      while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
        result = extractor.read(input, DUMMY_POSITION_HOLDER);
      }
      Assertions.checkState(result != Extractor.RESULT_SEEK);
    } finally {
      nextLoadPosition = input.getPosition() - dataSpec.absoluteStreamPosition;
    }
  } finally {
    Util.closeQuietly(dataSource);
  }
  loadCompleted = true;
}
 
Example #27
Source File: AesCipherDataSource.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long open(DataSpec dataSpec) throws IOException {
  long dataLength = upstream.open(dataSpec);
  long nonce = CryptoUtil.getFNV64Hash(dataSpec.key);
  cipher = new AesFlushingCipher(Cipher.DECRYPT_MODE, secretKey, nonce,
      dataSpec.absoluteStreamPosition);
  return dataLength;
}
 
Example #28
Source File: SegmentDownloader.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
protected static DataSpec getCompressibleDataSpec(Uri uri) {
  return new DataSpec(
      uri,
      /* absoluteStreamPosition= */ 0,
      /* length= */ C.LENGTH_UNSET,
      /* key= */ null,
      /* flags= */ DataSpec.FLAG_ALLOW_GZIP);
}
 
Example #29
Source File: ProgressiveMediaPeriod.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private DataSpec buildDataSpec(long position) {
  // Disable caching if the content length cannot be resolved, since this is indicative of a
  // progressive live stream.
  return new DataSpec(
      uri,
      position,
      C.LENGTH_UNSET,
      customCacheKey,
      DataSpec.FLAG_ALLOW_ICY_METADATA
          | DataSpec.FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN
          | DataSpec.FLAG_ALLOW_CACHE_FRAGMENTATION);
}
 
Example #30
Source File: CacheUtil.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private static long getRequestLength(DataSpec dataSpec, Cache cache, String key) {
  if (dataSpec.length != C.LENGTH_UNSET) {
    return dataSpec.length;
  } else {
    long contentLength = ContentMetadata.getContentLength(cache.getContentMetadata(key));
    return contentLength == C.LENGTH_UNSET
        ? C.LENGTH_UNSET
        : contentLength - dataSpec.absoluteStreamPosition;
  }
}