Java Code Examples for com.google.android.exoplayer2.extractor.ExtractorInput#read()

The following examples show how to use com.google.android.exoplayer2.extractor.ExtractorInput#read() . 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: Ac3Extractor.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
@Override
public int read(ExtractorInput input, PositionHolder seekPosition) throws IOException,
    InterruptedException {
  int bytesRead = input.read(sampleData.data, 0, MAX_SYNC_FRAME_SIZE);
  if (bytesRead == C.RESULT_END_OF_INPUT) {
    return RESULT_END_OF_INPUT;
  }

  // Feed whatever data we have to the reader, regardless of whether the read finished or not.
  sampleData.setPosition(0);
  sampleData.setLimit(bytesRead);

  if (!startedPacket) {
    // Pass data to the reader as though it's contained within a single infinitely long packet.
    reader.packetStarted(/* pesTimeUs= */ 0, FLAG_DATA_ALIGNMENT_INDICATOR);
    startedPacket = true;
  }
  // TODO: Make it possible for the reader to consume the dataSource directly, so that it becomes
  // unnecessary to copy the data through packetBuffer.
  reader.consume(sampleData);
  return RESULT_CONTINUE;
}
 
Example 2
Source File: Ac3Extractor.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 bytesRead = input.read(sampleData.data, 0, MAX_SYNC_FRAME_SIZE);
  if (bytesRead == C.RESULT_END_OF_INPUT) {
    return RESULT_END_OF_INPUT;
  }

  // Feed whatever data we have to the reader, regardless of whether the read finished or not.
  sampleData.setPosition(0);
  sampleData.setLimit(bytesRead);

  if (!startedPacket) {
    // Pass data to the reader as though it's contained within a single infinitely long packet.
    reader.packetStarted(firstSampleTimestampUs, true);
    startedPacket = true;
  }
  // TODO: Make it possible for the reader to consume the dataSource directly, so that it becomes
  // unnecessary to copy the data through packetBuffer.
  reader.consume(sampleData);
  return RESULT_CONTINUE;
}
 
Example 3
Source File: TsExtractor.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private boolean fillBufferWithAtLeastOnePacket(ExtractorInput input)
    throws IOException, InterruptedException {
  byte[] data = tsPacketBuffer.data;
  // Shift bytes to the start of the buffer if there isn't enough space left at the end.
  if (BUFFER_SIZE - tsPacketBuffer.getPosition() < TS_PACKET_SIZE) {
    int bytesLeft = tsPacketBuffer.bytesLeft();
    if (bytesLeft > 0) {
      System.arraycopy(data, tsPacketBuffer.getPosition(), data, 0, bytesLeft);
    }
    tsPacketBuffer.reset(data, bytesLeft);
  }
  // Read more bytes until we have at least one packet.
  while (tsPacketBuffer.bytesLeft() < TS_PACKET_SIZE) {
    int limit = tsPacketBuffer.limit();
    int read = input.read(data, limit, BUFFER_SIZE - limit);
    if (read == C.RESULT_END_OF_INPUT) {
      return false;
    }
    tsPacketBuffer.setLimit(limit + read);
  }
  return true;
}
 
Example 4
Source File: AdtsExtractor.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 bytesRead = input.read(packetBuffer.data, 0, MAX_PACKET_SIZE);
  if (bytesRead == C.RESULT_END_OF_INPUT) {
    return RESULT_END_OF_INPUT;
  }

  // Feed whatever data we have to the reader, regardless of whether the read finished or not.
  packetBuffer.setPosition(0);
  packetBuffer.setLimit(bytesRead);

  if (!startedPacket) {
    // Pass data to the reader as though it's contained within a single infinitely long packet.
    reader.packetStarted(firstSampleTimestampUs, true);
    startedPacket = true;
  }
  // TODO: Make it possible for reader to consume the dataSource directly, so that it becomes
  // unnecessary to copy the data through packetBuffer.
  reader.consume(packetBuffer);
  return RESULT_CONTINUE;
}
 
Example 5
Source File: TsExtractor.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private boolean fillBufferWithAtLeastOnePacket(ExtractorInput input)
    throws IOException, InterruptedException {
  byte[] data = tsPacketBuffer.data;
  // Shift bytes to the start of the buffer if there isn't enough space left at the end.
  if (BUFFER_SIZE - tsPacketBuffer.getPosition() < TS_PACKET_SIZE) {
    int bytesLeft = tsPacketBuffer.bytesLeft();
    if (bytesLeft > 0) {
      System.arraycopy(data, tsPacketBuffer.getPosition(), data, 0, bytesLeft);
    }
    tsPacketBuffer.reset(data, bytesLeft);
  }
  // Read more bytes until we have at least one packet.
  while (tsPacketBuffer.bytesLeft() < TS_PACKET_SIZE) {
    int limit = tsPacketBuffer.limit();
    int read = input.read(data, limit, BUFFER_SIZE - limit);
    if (read == C.RESULT_END_OF_INPUT) {
      return false;
    }
    tsPacketBuffer.setLimit(limit + read);
  }
  return true;
}
 
Example 6
Source File: Ac3Extractor.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 bytesRead = input.read(sampleData.data, 0, MAX_SYNC_FRAME_SIZE);
  if (bytesRead == C.RESULT_END_OF_INPUT) {
    return RESULT_END_OF_INPUT;
  }

  // Feed whatever data we have to the reader, regardless of whether the read finished or not.
  sampleData.setPosition(0);
  sampleData.setLimit(bytesRead);

  if (!startedPacket) {
    // Pass data to the reader as though it's contained within a single infinitely long packet.
    reader.packetStarted(firstSampleTimestampUs, true);
    startedPacket = true;
  }
  // TODO: Make it possible for the reader to consume the dataSource directly, so that it becomes
  // unnecessary to copy the data through packetBuffer.
  reader.consume(sampleData);
  return RESULT_CONTINUE;
}
 
Example 7
Source File: AdtsExtractor.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 bytesRead = input.read(packetBuffer.data, 0, MAX_PACKET_SIZE);
  if (bytesRead == C.RESULT_END_OF_INPUT) {
    return RESULT_END_OF_INPUT;
  }

  // Feed whatever data we have to the reader, regardless of whether the read finished or not.
  packetBuffer.setPosition(0);
  packetBuffer.setLimit(bytesRead);

  if (!startedPacket) {
    // Pass data to the reader as though it's contained within a single infinitely long packet.
    reader.packetStarted(firstSampleTimestampUs, true);
    startedPacket = true;
  }
  // TODO: Make it possible for reader to consume the dataSource directly, so that it becomes
  // unnecessary to copy the data through packetBuffer.
  reader.consume(packetBuffer);
  return RESULT_CONTINUE;
}
 
Example 8
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 9
Source File: WebvttExtractor.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
@Override
public int read(ExtractorInput input, PositionHolder seekPosition)
    throws IOException, InterruptedException {
  // output == null suggests init() hasn't been called
  Assertions.checkNotNull(output);
  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 10
Source File: TsExtractor.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private boolean fillBufferWithAtLeastOnePacket(ExtractorInput input)
    throws IOException, InterruptedException {
  byte[] data = tsPacketBuffer.data;
  // Shift bytes to the start of the buffer if there isn't enough space left at the end.
  if (BUFFER_SIZE - tsPacketBuffer.getPosition() < TS_PACKET_SIZE) {
    int bytesLeft = tsPacketBuffer.bytesLeft();
    if (bytesLeft > 0) {
      System.arraycopy(data, tsPacketBuffer.getPosition(), data, 0, bytesLeft);
    }
    tsPacketBuffer.reset(data, bytesLeft);
  }
  // Read more bytes until we have at least one packet.
  while (tsPacketBuffer.bytesLeft() < TS_PACKET_SIZE) {
    int limit = tsPacketBuffer.limit();
    int read = input.read(data, limit, BUFFER_SIZE - limit);
    if (read == C.RESULT_END_OF_INPUT) {
      return false;
    }
    tsPacketBuffer.setLimit(limit + read);
  }
  return true;
}
 
Example 11
Source File: TsExtractor.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
private boolean fillBufferWithAtLeastOnePacket(ExtractorInput input)
    throws IOException, InterruptedException {
  byte[] data = tsPacketBuffer.data;
  // Shift bytes to the start of the buffer if there isn't enough space left at the end.
  if (BUFFER_SIZE - tsPacketBuffer.getPosition() < TS_PACKET_SIZE) {
    int bytesLeft = tsPacketBuffer.bytesLeft();
    if (bytesLeft > 0) {
      System.arraycopy(data, tsPacketBuffer.getPosition(), data, 0, bytesLeft);
    }
    tsPacketBuffer.reset(data, bytesLeft);
  }
  // Read more bytes until we have at least one packet.
  while (tsPacketBuffer.bytesLeft() < TS_PACKET_SIZE) {
    int limit = tsPacketBuffer.limit();
    int read = input.read(data, limit, BUFFER_SIZE - limit);
    if (read == C.RESULT_END_OF_INPUT) {
      return false;
    }
    tsPacketBuffer.setLimit(limit + read);
  }
  return true;
}
 
Example 12
Source File: FlacDecoderJni.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private int readFromExtractorInput(
    ExtractorInput extractorInput, byte[] tempBuffer, int offset, int length)
    throws IOException, InterruptedException {
  int read = extractorInput.read(tempBuffer, offset, length);
  if (read == C.RESULT_END_OF_INPUT) {
    endOfExtractorInput = true;
    read = 0;
  }
  return read;
}
 
Example 13
Source File: SampleQueue.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput)
    throws IOException, InterruptedException {
  length = preAppend(length);
  int bytesAppended = input.read(writeAllocationNode.allocation.data,
      writeAllocationNode.translateOffset(totalBytesWritten), length);
  if (bytesAppended == C.RESULT_END_OF_INPUT) {
    if (allowEndOfInput) {
      return C.RESULT_END_OF_INPUT;
    }
    throw new EOFException();
  }
  postAppend(bytesAppended);
  return bytesAppended;
}
 
Example 14
Source File: AdtsExtractor.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int read(ExtractorInput input, PositionHolder seekPosition)
    throws IOException, InterruptedException {
  long inputLength = input.getLength();
  boolean canUseConstantBitrateSeeking =
      (flags & FLAG_ENABLE_CONSTANT_BITRATE_SEEKING) != 0 && inputLength != C.LENGTH_UNSET;
  if (canUseConstantBitrateSeeking) {
    calculateAverageFrameSize(input);
  }

  int bytesRead = input.read(packetBuffer.data, 0, MAX_PACKET_SIZE);
  boolean readEndOfStream = bytesRead == RESULT_END_OF_INPUT;
  maybeOutputSeekMap(inputLength, canUseConstantBitrateSeeking, readEndOfStream);
  if (readEndOfStream) {
    return RESULT_END_OF_INPUT;
  }

  // Feed whatever data we have to the reader, regardless of whether the read finished or not.
  packetBuffer.setPosition(0);
  packetBuffer.setLimit(bytesRead);

  if (!startedPacket) {
    // Pass data to the reader as though it's contained within a single infinitely long packet.
    reader.packetStarted(firstSampleTimestampUs, FLAG_DATA_ALIGNMENT_INDICATOR);
    startedPacket = true;
  }
  // TODO: Make it possible for reader to consume the dataSource directly, so that it becomes
  // unnecessary to copy the data through packetBuffer.
  reader.consume(packetBuffer);
  return RESULT_CONTINUE;
}
 
Example 15
Source File: HlsSampleStreamWrapper.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput)
    throws IOException, InterruptedException {
  ensureBufferCapacity(bufferPosition + length);
  int numBytesRead = input.read(buffer, bufferPosition, length);
  if (numBytesRead == C.RESULT_END_OF_INPUT) {
    if (allowEndOfInput) {
      return C.RESULT_END_OF_INPUT;
    } else {
      throw new EOFException();
    }
  }
  bufferPosition += numBytesRead;
  return numBytesRead;
}
 
Example 16
Source File: SampleQueue.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput)
    throws IOException, InterruptedException {
  length = preAppend(length);
  int bytesAppended = input.read(writeAllocationNode.allocation.data,
      writeAllocationNode.translateOffset(totalBytesWritten), length);
  if (bytesAppended == C.RESULT_END_OF_INPUT) {
    if (allowEndOfInput) {
      return C.RESULT_END_OF_INPUT;
    }
    throw new EOFException();
  }
  postAppend(bytesAppended);
  return bytesAppended;
}
 
Example 17
Source File: SampleQueue.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput)
    throws IOException, InterruptedException {
  length = preAppend(length);
  int bytesAppended = input.read(writeAllocationNode.allocation.data,
      writeAllocationNode.translateOffset(totalBytesWritten), length);
  if (bytesAppended == C.RESULT_END_OF_INPUT) {
    if (allowEndOfInput) {
      return C.RESULT_END_OF_INPUT;
    }
    throw new EOFException();
  }
  postAppend(bytesAppended);
  return bytesAppended;
}
 
Example 18
Source File: FakeTrackOutput.java    From ExoPlayer-Offline with Apache License 2.0 5 votes vote down vote up
@Override
public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput)
    throws IOException, InterruptedException {
  byte[] newData = new byte[length];
  int bytesAppended = input.read(newData, 0, length);
  if (bytesAppended == C.RESULT_END_OF_INPUT) {
    if (allowEndOfInput) {
      return C.RESULT_END_OF_INPUT;
    }
    throw new EOFException();
  }
  newData = Arrays.copyOf(newData, bytesAppended);
  sampleData = TestUtil.joinByteArrays(sampleData, newData);
  return bytesAppended;
}
 
Example 19
Source File: SampleQueue.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput)
    throws IOException, InterruptedException {
  length = preAppend(length);
  int bytesAppended = input.read(writeAllocationNode.allocation.data,
      writeAllocationNode.translateOffset(totalBytesWritten), length);
  if (bytesAppended == C.RESULT_END_OF_INPUT) {
    if (allowEndOfInput) {
      return C.RESULT_END_OF_INPUT;
    }
    throw new EOFException();
  }
  postAppend(bytesAppended);
  return bytesAppended;
}
 
Example 20
Source File: AdtsExtractor.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int read(ExtractorInput input, PositionHolder seekPosition)
    throws IOException, InterruptedException {
  long inputLength = input.getLength();
  boolean canUseConstantBitrateSeeking =
      (flags & FLAG_ENABLE_CONSTANT_BITRATE_SEEKING) != 0 && inputLength != C.LENGTH_UNSET;
  if (canUseConstantBitrateSeeking) {
    calculateAverageFrameSize(input);
  }

  int bytesRead = input.read(packetBuffer.data, 0, MAX_PACKET_SIZE);
  boolean readEndOfStream = bytesRead == RESULT_END_OF_INPUT;
  maybeOutputSeekMap(inputLength, canUseConstantBitrateSeeking, readEndOfStream);
  if (readEndOfStream) {
    return RESULT_END_OF_INPUT;
  }

  // Feed whatever data we have to the reader, regardless of whether the read finished or not.
  packetBuffer.setPosition(0);
  packetBuffer.setLimit(bytesRead);

  if (!startedPacket) {
    // Pass data to the reader as though it's contained within a single infinitely long packet.
    reader.packetStarted(firstSampleTimestampUs, FLAG_DATA_ALIGNMENT_INDICATOR);
    startedPacket = true;
  }
  // TODO: Make it possible for reader to consume the dataSource directly, so that it becomes
  // unnecessary to copy the data through packetBuffer.
  reader.consume(packetBuffer);
  return RESULT_CONTINUE;
}