com.google.android.exoplayer2.extractor.DefaultExtractorInput Java Examples

The following examples show how to use com.google.android.exoplayer2.extractor.DefaultExtractorInput. 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: HlsMediaChunk.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private void maybeLoadInitData() throws IOException, InterruptedException {
  if (initLoadCompleted || initDataSpec == null) {
    // Note: The HLS spec forbids initialization segments for packed audio.
    return;
  }
  DataSpec initSegmentDataSpec = initDataSpec.subrange(initSegmentBytesLoaded);
  try {
    DefaultExtractorInput input = prepareExtraction(initDataSource, 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(initDataSource);
  }
  initLoadCompleted = true;
}
 
Example #2
Source File: InitializationChunk.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public 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) {
      extractorWrapper.init(/* trackOutputProvider= */ null, C.TIME_UNSET);
    }
    // Load and decode the initialization 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);
  }
}
 
Example #3
Source File: HlsMediaChunk.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private void maybeLoadInitData() throws IOException, InterruptedException {
  if (initLoadCompleted || initDataSpec == null) {
    // Note: The HLS spec forbids initialization segments for packed audio.
    return;
  }
  DataSpec initSegmentDataSpec = initDataSpec.subrange(initSegmentBytesLoaded);
  try {
    DefaultExtractorInput input = prepareExtraction(initDataSource, 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(initDataSource);
  }
  initLoadCompleted = true;
}
 
Example #4
Source File: InitializationChunk.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public 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) {
      extractorWrapper.init(/* trackOutputProvider= */ null, C.TIME_UNSET);
    }
    // Load and decode the initialization 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);
  }
}
 
Example #5
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 #6
Source File: InitializationChunk.java    From K-Sonic with MIT License 6 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public 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) {
      extractorWrapper.init(null);
    }
    // Load and decode the initialization 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);
  }
}
 
Example #7
Source File: ContainerMediaChunk.java    From Telegram 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 #8
Source File: SingleSampleMediaChunk.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public void load() throws IOException, InterruptedException {
  DataSpec loadDataSpec = dataSpec.subrange(nextLoadPosition);
  try {
    // Create and open the input.
    long length = dataSource.open(loadDataSpec);
    if (length != C.LENGTH_UNSET) {
      length += nextLoadPosition;
    }
    ExtractorInput extractorInput =
        new DefaultExtractorInput(dataSource, nextLoadPosition, length);
    BaseMediaChunkOutput output = getOutput();
    output.setSampleOffsetUs(0);
    TrackOutput trackOutput = output.track(0, trackType);
    trackOutput.format(sampleFormat);
    // Load the sample data.
    int result = 0;
    while (result != C.RESULT_END_OF_INPUT) {
      nextLoadPosition += result;
      result = trackOutput.sampleData(extractorInput, Integer.MAX_VALUE, true);
    }
    int sampleSize = (int) nextLoadPosition;
    trackOutput.sampleMetadata(startTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
  } finally {
    Util.closeQuietly(dataSource);
  }
  loadCompleted = true;
}
 
Example #9
Source File: InitializationChunk.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public 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) {
      extractorWrapper.init(
          /* trackOutputProvider= */ null,
          /* startTimeUs= */ C.TIME_UNSET,
          /* endTimeUs= */ C.TIME_UNSET);
    }
    // Load and decode the initialization 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);
  }
}
 
Example #10
Source File: HlsMediaChunk.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private DefaultExtractorInput prepareExtraction(DataSource dataSource, DataSpec dataSpec)
    throws IOException, InterruptedException {
  long bytesToRead = dataSource.open(dataSpec);

  DefaultExtractorInput extractorInput =
      new DefaultExtractorInput(dataSource, dataSpec.absoluteStreamPosition, bytesToRead);

  if (extractor == null) {
    long id3Timestamp = peekId3PrivTimestamp(extractorInput);
    extractorInput.resetPeekPosition();

    HlsExtractorFactory.Result result =
        extractorFactory.createExtractor(
            previousExtractor,
            dataSpec.uri,
            trackFormat,
            muxedCaptionFormats,
            drmInitData,
            timestampAdjuster,
            dataSource.getResponseHeaders(),
            extractorInput);
    extractor = result.extractor;
    isExtractorReusable = result.isReusable;
    if (result.isPackedAudioExtractor) {
      output.setSampleOffsetUs(
          id3Timestamp != C.TIME_UNSET
              ? timestampAdjuster.adjustTsTimestamp(id3Timestamp)
              : startTimeUs);
    } else {
      // In case the container format changes mid-stream to non-packed-audio, we need to reset
      // the timestamp offset.
      output.setSampleOffsetUs(/* sampleOffsetUs= */ 0L);
    }
    output.init(uid, shouldSpliceIn, /* reusingExtractor= */ false);
    extractor.init(output);
  }

  return extractorInput;
}
 
Example #11
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 #12
Source File: SingleSampleMediaChunk.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public void load() throws IOException, InterruptedException {
  DataSpec loadDataSpec = dataSpec.subrange(nextLoadPosition);
  try {
    // Create and open the input.
    long length = dataSource.open(loadDataSpec);
    if (length != C.LENGTH_UNSET) {
      length += nextLoadPosition;
    }
    ExtractorInput extractorInput =
        new DefaultExtractorInput(dataSource, nextLoadPosition, length);
    BaseMediaChunkOutput output = getOutput();
    output.setSampleOffsetUs(0);
    TrackOutput trackOutput = output.track(0, trackType);
    trackOutput.format(sampleFormat);
    // Load the sample data.
    int result = 0;
    while (result != C.RESULT_END_OF_INPUT) {
      nextLoadPosition += result;
      result = trackOutput.sampleData(extractorInput, Integer.MAX_VALUE, true);
    }
    int sampleSize = (int) nextLoadPosition;
    trackOutput.sampleMetadata(startTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
  } finally {
    Util.closeQuietly(dataSource);
  }
  loadCompleted = true;
}
 
Example #13
Source File: InitializationChunk.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public 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) {
      extractorWrapper.init(
          /* trackOutputProvider= */ null,
          /* startTimeUs= */ C.TIME_UNSET,
          /* endTimeUs= */ C.TIME_UNSET);
    }
    // Load and decode the initialization 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);
  }
}
 
Example #14
Source File: HlsMediaChunk.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private DefaultExtractorInput prepareExtraction(DataSource dataSource, DataSpec dataSpec)
    throws IOException, InterruptedException {
  long bytesToRead = dataSource.open(dataSpec);

  DefaultExtractorInput extractorInput =
      new DefaultExtractorInput(dataSource, dataSpec.absoluteStreamPosition, bytesToRead);

  if (extractor == null) {
    long id3Timestamp = peekId3PrivTimestamp(extractorInput);
    extractorInput.resetPeekPosition();

    HlsExtractorFactory.Result result =
        extractorFactory.createExtractor(
            previousExtractor,
            dataSpec.uri,
            trackFormat,
            muxedCaptionFormats,
            drmInitData,
            timestampAdjuster,
            dataSource.getResponseHeaders(),
            extractorInput);
    extractor = result.extractor;
    isExtractorReusable = result.isReusable;
    if (result.isPackedAudioExtractor) {
      output.setSampleOffsetUs(
          id3Timestamp != C.TIME_UNSET
              ? timestampAdjuster.adjustTsTimestamp(id3Timestamp)
              : startTimeUs);
    } else {
      // In case the container format changes mid-stream to non-packed-audio, we need to reset
      // the timestamp offset.
      output.setSampleOffsetUs(/* sampleOffsetUs= */ 0L);
    }
    output.init(uid, shouldSpliceIn, /* reusingExtractor= */ false);
    extractor.init(output);
  }

  return extractorInput;
}
 
Example #15
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 #16
Source File: SingleSampleMediaChunk.java    From K-Sonic with MIT License 5 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public void load() throws IOException, InterruptedException {
  DataSpec loadDataSpec = Util.getRemainderDataSpec(dataSpec, bytesLoaded);
  try {
    // Create and open the input.
    long length = dataSource.open(loadDataSpec);
    if (length != C.LENGTH_UNSET) {
      length += bytesLoaded;
    }
    ExtractorInput extractorInput = new DefaultExtractorInput(dataSource, bytesLoaded, length);
    BaseMediaChunkOutput output = getOutput();
    output.setSampleOffsetUs(0);
    TrackOutput trackOutput = output.track(0, trackType);
    trackOutput.format(sampleFormat);
    // Load the sample data.
    int result = 0;
    while (result != C.RESULT_END_OF_INPUT) {
      bytesLoaded += result;
      result = trackOutput.sampleData(extractorInput, Integer.MAX_VALUE, true);
    }
    int sampleSize = bytesLoaded;
    trackOutput.sampleMetadata(startTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
  } finally {
    Util.closeQuietly(dataSource);
  }
  loadCompleted = true;
}
 
Example #17
Source File: ContainerMediaChunk.java    From TelePlus-Android 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(
          output, seekTimeUs == C.TIME_UNSET ? 0 : (seekTimeUs - 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 #18
Source File: SingleSampleMediaChunk.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public void load() throws IOException, InterruptedException {
  DataSpec loadDataSpec = dataSpec.subrange(nextLoadPosition);
  try {
    // Create and open the input.
    long length = dataSource.open(loadDataSpec);
    if (length != C.LENGTH_UNSET) {
      length += nextLoadPosition;
    }
    ExtractorInput extractorInput =
        new DefaultExtractorInput(dataSource, nextLoadPosition, length);
    BaseMediaChunkOutput output = getOutput();
    output.setSampleOffsetUs(0);
    TrackOutput trackOutput = output.track(0, trackType);
    trackOutput.format(sampleFormat);
    // Load the sample data.
    int result = 0;
    while (result != C.RESULT_END_OF_INPUT) {
      nextLoadPosition += result;
      result = trackOutput.sampleData(extractorInput, Integer.MAX_VALUE, true);
    }
    int sampleSize = (int) nextLoadPosition;
    trackOutput.sampleMetadata(startTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
  } finally {
    Util.closeQuietly(dataSource);
  }
  loadCompleted = true;
}
 
Example #19
Source File: ContainerMediaChunk.java    From TelePlus-Android 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(
          output, seekTimeUs == C.TIME_UNSET ? 0 : (seekTimeUs - 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 #20
Source File: SingleSampleMediaChunk.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public void load() throws IOException, InterruptedException {
  DataSpec loadDataSpec = dataSpec.subrange(nextLoadPosition);
  try {
    // Create and open the input.
    long length = dataSource.open(loadDataSpec);
    if (length != C.LENGTH_UNSET) {
      length += nextLoadPosition;
    }
    ExtractorInput extractorInput =
        new DefaultExtractorInput(dataSource, nextLoadPosition, length);
    BaseMediaChunkOutput output = getOutput();
    output.setSampleOffsetUs(0);
    TrackOutput trackOutput = output.track(0, trackType);
    trackOutput.format(sampleFormat);
    // Load the sample data.
    int result = 0;
    while (result != C.RESULT_END_OF_INPUT) {
      nextLoadPosition += result;
      result = trackOutput.sampleData(extractorInput, Integer.MAX_VALUE, true);
    }
    int sampleSize = (int) nextLoadPosition;
    trackOutput.sampleMetadata(startTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
  } finally {
    Util.closeQuietly(dataSource);
  }
  loadCompleted = true;
}
 
Example #21
Source File: ContainerMediaChunk.java    From MediaSDK with Apache License 2.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 #22
Source File: SingleSampleMediaChunk.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public void load() throws IOException, InterruptedException {
  DataSpec loadDataSpec = dataSpec.subrange(nextLoadPosition);
  try {
    // Create and open the input.
    long length = dataSource.open(loadDataSpec);
    if (length != C.LENGTH_UNSET) {
      length += nextLoadPosition;
    }
    ExtractorInput extractorInput =
        new DefaultExtractorInput(dataSource, nextLoadPosition, length);
    BaseMediaChunkOutput output = getOutput();
    output.setSampleOffsetUs(0);
    TrackOutput trackOutput = output.track(0, trackType);
    trackOutput.format(sampleFormat);
    // Load the sample data.
    int result = 0;
    while (result != C.RESULT_END_OF_INPUT) {
      nextLoadPosition += result;
      result = trackOutput.sampleData(extractorInput, Integer.MAX_VALUE, true);
    }
    int sampleSize = (int) nextLoadPosition;
    trackOutput.sampleMetadata(startTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
  } finally {
    Util.closeQuietly(dataSource);
  }
  loadCompleted = true;
}
 
Example #23
Source File: InitializationChunk.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public 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) {
      extractorWrapper.init(
          /* trackOutputProvider= */ null,
          /* startTimeUs= */ C.TIME_UNSET,
          /* endTimeUs= */ C.TIME_UNSET);
    }
    // Load and decode the initialization 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);
  }
}
 
Example #24
Source File: HlsMediaChunk.java    From K-Sonic with MIT License 4 votes vote down vote up
private void loadMedia() throws IOException, InterruptedException {
  // If we previously fed part of this chunk to the extractor, we need to skip it this time. For
  // encrypted content we need to skip the data by reading it through the source, so as to ensure
  // correct decryption of the remainder of the chunk. For clear content, we can request the
  // remainder of the chunk directly.
  DataSpec loadDataSpec;
  boolean skipLoadedBytes;
  if (isEncrypted) {
    loadDataSpec = dataSpec;
    skipLoadedBytes = bytesLoaded != 0;
  } else {
    loadDataSpec = Util.getRemainderDataSpec(dataSpec, bytesLoaded);
    skipLoadedBytes = false;
  }
  if (!isMasterTimestampSource) {
    timestampAdjuster.waitUntilInitialized();
  } else if (timestampAdjuster.getFirstSampleTimestampUs() == TimestampAdjuster.DO_NOT_OFFSET) {
    // We're the master and we haven't set the desired first sample timestamp yet.
    timestampAdjuster.setFirstSampleTimestampUs(startTimeUs);
  }
  try {
    ExtractorInput input = new DefaultExtractorInput(dataSource,
        loadDataSpec.absoluteStreamPosition, dataSource.open(loadDataSpec));
    if (extractor == null) {
      // Media segment format is packed audio.
      long id3Timestamp = peekId3PrivTimestamp(input);
      extractor = buildPackedAudioExtractor(id3Timestamp != C.TIME_UNSET
          ? timestampAdjuster.adjustTsTimestamp(id3Timestamp) : startTimeUs);
    }
    if (skipLoadedBytes) {
      input.skipFully(bytesLoaded);
    }
    try {
      int result = Extractor.RESULT_CONTINUE;
      while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
        result = extractor.read(input, null);
      }
    } finally {
      bytesLoaded = (int) (input.getPosition() - dataSpec.absoluteStreamPosition);
    }
  } finally {
    Util.closeQuietly(dataSource);
  }
  loadCompleted = true;
}
 
Example #25
Source File: HlsMediaChunk.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@RequiresNonNull("output")
@EnsuresNonNull("extractor")
private DefaultExtractorInput prepareExtraction(DataSource dataSource, DataSpec dataSpec)
    throws IOException, InterruptedException {
  long bytesToRead = dataSource.open(dataSpec);

  DefaultExtractorInput extractorInput =
      new DefaultExtractorInput(dataSource, dataSpec.absoluteStreamPosition, bytesToRead);

  if (extractor == null) {
    long id3Timestamp = peekId3PrivTimestamp(extractorInput);
    extractorInput.resetPeekPosition();

    HlsExtractorFactory.Result result =
        extractorFactory.createExtractor(
            previousExtractor,
            dataSpec.uri,
            trackFormat,
            muxedCaptionFormats,
            drmInitData,
            timestampAdjuster,
            dataSource.getResponseHeaders(),
            extractorInput);
    extractor = result.extractor;
    isExtractorReusable = result.isReusable;
    if (result.isPackedAudioExtractor) {
      output.setSampleOffsetUs(
          id3Timestamp != C.TIME_UNSET
              ? timestampAdjuster.adjustTsTimestamp(id3Timestamp)
              : startTimeUs);
    } else {
      // In case the container format changes mid-stream to non-packed-audio, we need to reset
      // the timestamp offset.
      output.setSampleOffsetUs(/* sampleOffsetUs= */ 0L);
    }
    output.init(uid, shouldSpliceIn, /* reusingExtractor= */ false);
    extractor.init(output);
  }

  return extractorInput;
}