Java Code Examples for com.google.android.exoplayer2.extractor.Extractor#RESULT_CONTINUE

The following examples show how to use com.google.android.exoplayer2.extractor.Extractor#RESULT_CONTINUE . 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: 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 2
Source File: WebvttExtractor.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int read(ExtractorInput input, PositionHolder seekPosition)
    throws IOException, InterruptedException {
  int currentFileSize = (int) input.getLength();

  // Increase the size of sampleData if necessary.
  if (sampleSize == sampleData.length) {
    sampleData = Arrays.copyOf(sampleData,
        (currentFileSize != C.LENGTH_UNSET ? currentFileSize : sampleData.length) * 3 / 2);
  }

  // Consume to the input.
  int bytesRead = input.read(sampleData, sampleSize, sampleData.length - sampleSize);
  if (bytesRead != C.RESULT_END_OF_INPUT) {
    sampleSize += bytesRead;
    if (currentFileSize == C.LENGTH_UNSET || sampleSize != currentFileSize) {
      return Extractor.RESULT_CONTINUE;
    }
  }

  // We've reached the end of the input, which corresponds to the end of the current file.
  processSample();
  return Extractor.RESULT_END_OF_INPUT;
}
 
Example 3
Source File: PsDurationReader.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private int readFirstScrValue(ExtractorInput input, PositionHolder seekPositionHolder)
    throws IOException, InterruptedException {
  if (input.getPosition() != 0) {
    seekPositionHolder.position = 0;
    return Extractor.RESULT_SEEK;
  }

  int bytesToRead = (int) Math.min(DURATION_READ_BYTES, input.getLength());
  input.resetPeekPosition();
  input.peekFully(packetBuffer.data, /* offset= */ 0, bytesToRead);
  packetBuffer.setPosition(0);
  packetBuffer.setLimit(bytesToRead);

  firstScrValue = readFirstScrValueFromBuffer(packetBuffer);
  isFirstScrValueRead = true;
  return Extractor.RESULT_CONTINUE;
}
 
Example 4
Source File: StreamReader.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @see Extractor#read(ExtractorInput, PositionHolder)
 */
final int read(ExtractorInput input, PositionHolder seekPosition)
    throws IOException, InterruptedException {
  switch (state) {
    case STATE_READ_HEADERS:
      return readHeaders(input);
    case STATE_SKIP_HEADERS:
      input.skipFully((int) payloadStartPosition);
      state = STATE_READ_PAYLOAD;
      return Extractor.RESULT_CONTINUE;
    case STATE_READ_PAYLOAD:
      return readPayload(input, seekPosition);
    default:
      // Never happens.
      throw new IllegalStateException();
  }
}
 
Example 5
Source File: WebvttExtractor.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int read(ExtractorInput input, PositionHolder seekPosition)
    throws IOException, InterruptedException {
  int currentFileSize = (int) input.getLength();

  // Increase the size of sampleData if necessary.
  if (sampleSize == sampleData.length) {
    sampleData = Arrays.copyOf(sampleData,
        (currentFileSize != C.LENGTH_UNSET ? currentFileSize : sampleData.length) * 3 / 2);
  }

  // Consume to the input.
  int bytesRead = input.read(sampleData, sampleSize, sampleData.length - sampleSize);
  if (bytesRead != C.RESULT_END_OF_INPUT) {
    sampleSize += bytesRead;
    if (currentFileSize == C.LENGTH_UNSET || sampleSize != currentFileSize) {
      return Extractor.RESULT_CONTINUE;
    }
  }

  // We've reached the end of the input, which corresponds to the end of the current file.
  processSample();
  return Extractor.RESULT_END_OF_INPUT;
}
 
Example 6
Source File: TsDurationReader.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private int readLastPcrValue(ExtractorInput input, PositionHolder seekPositionHolder, int pcrPid)
    throws IOException, InterruptedException {
  long inputLength = input.getLength();
  int bytesToSearch = (int) Math.min(TIMESTAMP_SEARCH_BYTES, inputLength);
  long searchStartPosition = inputLength - bytesToSearch;
  if (input.getPosition() != searchStartPosition) {
    seekPositionHolder.position = searchStartPosition;
    return Extractor.RESULT_SEEK;
  }

  packetBuffer.reset(bytesToSearch);
  input.resetPeekPosition();
  input.peekFully(packetBuffer.data, /* offset= */ 0, bytesToSearch);

  lastPcrValue = readLastPcrValueFromBuffer(packetBuffer, pcrPid);
  isLastPcrValueRead = true;
  return Extractor.RESULT_CONTINUE;
}
 
Example 7
Source File: WebvttExtractor.java    From K-Sonic with MIT License 6 votes vote down vote up
@Override
public int read(ExtractorInput input, PositionHolder seekPosition)
    throws IOException, InterruptedException {
  int currentFileSize = (int) input.getLength();

  // Increase the size of sampleData if necessary.
  if (sampleSize == sampleData.length) {
    sampleData = Arrays.copyOf(sampleData,
        (currentFileSize != C.LENGTH_UNSET ? currentFileSize : sampleData.length) * 3 / 2);
  }

  // Consume to the input.
  int bytesRead = input.read(sampleData, sampleSize, sampleData.length - sampleSize);
  if (bytesRead != C.RESULT_END_OF_INPUT) {
    sampleSize += bytesRead;
    if (currentFileSize == C.LENGTH_UNSET || sampleSize != currentFileSize) {
      return Extractor.RESULT_CONTINUE;
    }
  }

  // We've reached the end of the input, which corresponds to the end of the current file.
  processSample();
  return Extractor.RESULT_END_OF_INPUT;
}
 
Example 8
Source File: StreamReader.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * @see Extractor#read(ExtractorInput, PositionHolder)
 */
final int read(ExtractorInput input, PositionHolder seekPosition)
    throws IOException, InterruptedException {
  switch (state) {
    case STATE_READ_HEADERS:
      return readHeaders(input);
    case STATE_SKIP_HEADERS:
      input.skipFully((int) payloadStartPosition);
      state = STATE_READ_PAYLOAD;
      return Extractor.RESULT_CONTINUE;
    case STATE_READ_PAYLOAD:
      return readPayload(input, seekPosition);
    default:
      // Never happens.
      throw new IllegalStateException();
  }
}
 
Example 9
Source File: PsDurationReader.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
private int readLastScrValue(ExtractorInput input, PositionHolder seekPositionHolder)
    throws IOException, InterruptedException {
  long inputLength = input.getLength();
  int bytesToSearch = (int) Math.min(TIMESTAMP_SEARCH_BYTES, inputLength);
  long searchStartPosition = inputLength - bytesToSearch;
  if (input.getPosition() != searchStartPosition) {
    seekPositionHolder.position = searchStartPosition;
    return Extractor.RESULT_SEEK;
  }

  packetBuffer.reset(bytesToSearch);
  input.resetPeekPosition();
  input.peekFully(packetBuffer.data, /* offset= */ 0, bytesToSearch);

  lastScrValue = readLastScrValueFromBuffer(packetBuffer);
  isLastScrValueRead = true;
  return Extractor.RESULT_CONTINUE;
}
 
Example 10
Source File: PsDurationReader.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
private int readFirstScrValue(ExtractorInput input, PositionHolder seekPositionHolder)
    throws IOException, InterruptedException {
  int bytesToSearch = (int) Math.min(TIMESTAMP_SEARCH_BYTES, input.getLength());
  int searchStartPosition = 0;
  if (input.getPosition() != searchStartPosition) {
    seekPositionHolder.position = searchStartPosition;
    return Extractor.RESULT_SEEK;
  }

  packetBuffer.reset(bytesToSearch);
  input.resetPeekPosition();
  input.peekFully(packetBuffer.data, /* offset= */ 0, bytesToSearch);

  firstScrValue = readFirstScrValueFromBuffer(packetBuffer);
  isFirstScrValueRead = true;
  return Extractor.RESULT_CONTINUE;
}
 
Example 11
Source File: TsDurationReader.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private int readFirstPcrValue(ExtractorInput input, PositionHolder seekPositionHolder, int pcrPid)
    throws IOException, InterruptedException {
  int bytesToSearch = (int) Math.min(TIMESTAMP_SEARCH_BYTES, input.getLength());
  int searchStartPosition = 0;
  if (input.getPosition() != searchStartPosition) {
    seekPositionHolder.position = searchStartPosition;
    return Extractor.RESULT_SEEK;
  }

  packetBuffer.reset(bytesToSearch);
  input.resetPeekPosition();
  input.peekFully(packetBuffer.data, /* offset= */ 0, bytesToSearch);

  firstPcrValue = readFirstPcrValueFromBuffer(packetBuffer, pcrPid);
  isFirstPcrValueRead = true;
  return Extractor.RESULT_CONTINUE;
}
 
Example 12
Source File: StreamReader.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @see Extractor#read(ExtractorInput, PositionHolder)
 */
final int read(ExtractorInput input, PositionHolder seekPosition)
    throws IOException, InterruptedException {
  switch (state) {
    case STATE_READ_HEADERS:
      return readHeaders(input);
    case STATE_SKIP_HEADERS:
      input.skipFully((int) payloadStartPosition);
      state = STATE_READ_PAYLOAD;
      return Extractor.RESULT_CONTINUE;
    case STATE_READ_PAYLOAD:
      return readPayload(input, seekPosition);
    default:
      // Never happens.
      throw new IllegalStateException();
  }
}
 
Example 13
Source File: HlsMediaChunk.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Attempts to feed the given {@code dataSpec} to {@code this.extractor}. Whenever the operation
 * concludes (because of a thrown exception or because the operation finishes), the number of fed
 * bytes is written to {@code nextLoadPosition}.
 */
private void feedDataToExtractor(
    DataSource dataSource, DataSpec dataSpec, boolean dataIsEncrypted)
    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 (dataIsEncrypted) {
    loadDataSpec = dataSpec;
    skipLoadedBytes = nextLoadPosition != 0;
  } else {
    loadDataSpec = dataSpec.subrange(nextLoadPosition);
    skipLoadedBytes = false;
  }
  try {
    ExtractorInput input = prepareExtraction(dataSource, loadDataSpec);
    if (skipLoadedBytes) {
      input.skipFully(nextLoadPosition);
    }
    try {
      int result = Extractor.RESULT_CONTINUE;
      while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
        result = extractor.read(input, /* seekPosition= */ null);
      }
    } finally {
      nextLoadPosition = (int) (input.getPosition() - dataSpec.absoluteStreamPosition);
    }
  } finally {
    Util.closeQuietly(dataSource);
  }
}
 
Example 14
Source File: StreamReader.java    From K-Sonic with MIT License 5 votes vote down vote up
private int readHeaders(ExtractorInput input) throws IOException, InterruptedException {
  boolean readingHeaders = true;
  while (readingHeaders) {
    if (!oggPacket.populate(input)) {
      state = STATE_END_OF_INPUT;
      return Extractor.RESULT_END_OF_INPUT;
    }
    lengthOfReadPacket = input.getPosition() - payloadStartPosition;

    readingHeaders = readHeaders(oggPacket.getPayload(), payloadStartPosition, setupData);
    if (readingHeaders) {
      payloadStartPosition = input.getPosition();
    }
  }

  sampleRate = setupData.format.sampleRate;
  if (!formatSet) {
    trackOutput.format(setupData.format);
    formatSet = true;
  }

  if (setupData.oggSeeker != null) {
    oggSeeker = setupData.oggSeeker;
  } else if (input.getLength() == C.LENGTH_UNSET) {
    oggSeeker = new UnseekableOggSeeker();
  } else {
    OggPageHeader firstPayloadPageHeader = oggPacket.getPageHeader();
    oggSeeker = new DefaultOggSeeker(payloadStartPosition, input.getLength(), this,
        firstPayloadPageHeader.headerSize + firstPayloadPageHeader.bodySize,
        firstPayloadPageHeader.granulePosition);
  }

  setupData = null;
  state = STATE_READ_PAYLOAD;
  return Extractor.RESULT_CONTINUE;
}
 
Example 15
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 16
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 17
Source File: HlsMediaChunk.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Attempts to feed the given {@code dataSpec} to {@code this.extractor}. Whenever the operation
 * concludes (because of a thrown exception or because the operation finishes), the number of fed
 * bytes is written to {@code nextLoadPosition}.
 */
private void feedDataToExtractor(
    DataSource dataSource, DataSpec dataSpec, boolean dataIsEncrypted)
    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 (dataIsEncrypted) {
    loadDataSpec = dataSpec;
    skipLoadedBytes = nextLoadPosition != 0;
  } else {
    loadDataSpec = dataSpec.subrange(nextLoadPosition);
    skipLoadedBytes = false;
  }
  try {
    ExtractorInput input = prepareExtraction(dataSource, loadDataSpec);
    if (skipLoadedBytes) {
      input.skipFully(nextLoadPosition);
    }
    try {
      int result = Extractor.RESULT_CONTINUE;
      while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
        result = extractor.read(input, /* seekPosition= */ null);
      }
    } finally {
      nextLoadPosition = (int) (input.getPosition() - dataSpec.absoluteStreamPosition);
    }
  } finally {
    Util.closeQuietly(dataSource);
  }
}
 
Example 18
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 19
Source File: TsDurationReader.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private int finishReadDuration(ExtractorInput input) {
  isDurationRead = true;
  input.resetPeekPosition();
  return Extractor.RESULT_CONTINUE;
}
 
Example 20
Source File: TsDurationReader.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private int finishReadDuration(ExtractorInput input) {
  isDurationRead = true;
  input.resetPeekPosition();
  return Extractor.RESULT_CONTINUE;
}