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

The following examples show how to use com.google.android.exoplayer2.extractor.ExtractorInput. 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: StreamReader.java    From K-Sonic with MIT License 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 #2
Source File: StreamReader.java    From Telegram 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 #3
Source File: RawCcExtractor.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
private boolean parseTimestampAndSampleCount(ExtractorInput input) throws IOException,
    InterruptedException {
  dataScratch.reset();
  if (version == 0) {
    if (!input.readFully(dataScratch.data, 0, TIMESTAMP_SIZE_V0 + 1, true)) {
      return false;
    }
    // version 0 timestamps are 45kHz, so we need to convert them into us
    timestampUs = dataScratch.readUnsignedInt() * 1000 / 45;
  } else if (version == 1) {
    if (!input.readFully(dataScratch.data, 0, TIMESTAMP_SIZE_V1 + 1, true)) {
      return false;
    }
    timestampUs = dataScratch.readLong();
  } else {
    throw new ParserException("Unsupported version number: " + version);
  }

  remainingSampleCount = dataScratch.readUnsignedByte();
  sampleBytesWritten = 0;
  return true;
}
 
Example #4
Source File: OggExtractor.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int read(ExtractorInput input, PositionHolder seekPosition)
    throws IOException, InterruptedException {
  if (streamReader == null) {
    if (!sniffInternal(input)) {
      throw new ParserException("Failed to determine bitstream type");
    }
    input.resetPeekPosition();
  }
  if (!streamReaderInitialized) {
    TrackOutput trackOutput = output.track(0, C.TRACK_TYPE_AUDIO);
    output.endTracks();
    streamReader.init(output, trackOutput);
    streamReaderInitialized = true;
  }
  return streamReader.read(input, seekPosition);
}
 
Example #5
Source File: Sniffer.java    From K-Sonic with MIT License 6 votes vote down vote up
/**
 * Peeks a variable-length unsigned EBML integer from the input.
 */
private long readUint(ExtractorInput input) throws IOException, InterruptedException {
  input.peekFully(scratch.data, 0, 1);
  int value = scratch.data[0] & 0xFF;
  if (value == 0) {
    return Long.MIN_VALUE;
  }
  int mask = 0x80;
  int length = 0;
  while ((value & mask) == 0) {
    mask >>= 1;
    length++;
  }
  value &= ~mask;
  input.peekFully(scratch.data, 1, length);
  for (int i = 0; i < length; i++) {
    value <<= 8;
    value += scratch.data[i + 1] & 0xFF;
  }
  peekLength += length + 1;
  return value;
}
 
Example #6
Source File: PsDurationReader.java    From Telegram with GNU General Public License v2.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 #7
Source File: Ac4Extractor.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, /* offset= */ 0, /* length= */ READ_BUFFER_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 #8
Source File: Mp4Extractor.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 {
  while (true) {
    switch (parserState) {
      case STATE_READING_ATOM_HEADER:
        if (!readAtomHeader(input)) {
          return RESULT_END_OF_INPUT;
        }
        break;
      case STATE_READING_ATOM_PAYLOAD:
        if (readAtomPayload(input, seekPosition)) {
          return RESULT_SEEK;
        }
        break;
      case STATE_READING_SAMPLE:
        return readSample(input, seekPosition);
      default:
        throw new IllegalStateException();
    }
  }
}
 
Example #9
Source File: OggExtractor.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
private boolean sniffInternal(ExtractorInput input) throws IOException, InterruptedException {
  OggPageHeader header = new OggPageHeader();
  if (!header.populate(input, true) || (header.type & 0x02) != 0x02) {
    return false;
  }

  int length = Math.min(header.bodySize, MAX_VERIFICATION_BYTES);
  ParsableByteArray scratch = new ParsableByteArray(length);
  input.peekFully(scratch.data, 0, length);

  if (FlacReader.verifyBitstreamType(resetPosition(scratch))) {
    streamReader = new FlacReader();
  } else if (VorbisReader.verifyBitstreamType(resetPosition(scratch))) {
    streamReader = new VorbisReader();
  } else if (OpusReader.verifyBitstreamType(resetPosition(scratch))) {
    streamReader = new OpusReader();
  } else {
    return false;
  }
  return true;
}
 
Example #10
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 #11
Source File: PsDurationReader.java    From Telegram-FOSS with GNU General Public License v2.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 #12
Source File: FlvExtractor.java    From K-Sonic with MIT License 6 votes vote down vote up
@Override
public int read(ExtractorInput input, PositionHolder seekPosition) throws IOException,
    InterruptedException {
  while (true) {
    switch (parserState) {
      case STATE_READING_FLV_HEADER:
        if (!readFlvHeader(input)) {
          return RESULT_END_OF_INPUT;
        }
        break;
      case STATE_SKIPPING_TO_TAG_HEADER:
        skipToTagHeader(input);
        break;
      case STATE_READING_TAG_HEADER:
        if (!readTagHeader(input)) {
          return RESULT_END_OF_INPUT;
        }
        break;
      case STATE_READING_TAG_DATA:
        if (readTagData(input)) {
          return RESULT_CONTINUE;
        }
        break;
    }
  }
}
 
Example #13
Source File: TsDurationReader.java    From Telegram 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 #14
Source File: AdtsExtractor.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
private int peekId3Header(ExtractorInput input) throws IOException, InterruptedException {
  int firstFramePosition = 0;
  while (true) {
    input.peekFully(scratch.data, 0, 10);
    scratch.setPosition(0);
    if (scratch.readUnsignedInt24() != ID3_TAG) {
      break;
    }
    scratch.skipBytes(3);
    int length = scratch.readSynchSafeInt();
    firstFramePosition += 10 + length;
    input.advancePeekPosition(length);
  }
  input.resetPeekPosition();
  input.advancePeekPosition(firstFramePosition);
  if (this.firstFramePosition == C.POSITION_UNSET) {
    this.firstFramePosition = firstFramePosition;
  }
  return firstFramePosition;
}
 
Example #15
Source File: FragmentedMp4Extractor.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 {
  while (true) {
    switch (parserState) {
      case STATE_READING_ATOM_HEADER:
        if (!readAtomHeader(input)) {
          return Extractor.RESULT_END_OF_INPUT;
        }
        break;
      case STATE_READING_ATOM_PAYLOAD:
        readAtomPayload(input);
        break;
      case STATE_READING_ENCRYPTION_DATA:
        readEncryptionData(input);
        break;
      default:
        if (readSample(input)) {
          return RESULT_CONTINUE;
        }
    }
  }
}
 
Example #16
Source File: FlvExtractor.java    From LiveVideoBroadcaster with Apache License 2.0 6 votes vote down vote up
@Override
public int read(ExtractorInput input, PositionHolder seekPosition) throws IOException,
    InterruptedException {
  while (true) {
    switch (parserState) {
      case STATE_READING_FLV_HEADER:
        if (!readFlvHeader(input)) {
          return RESULT_END_OF_INPUT;
        }
        break;
      case STATE_SKIPPING_TO_TAG_HEADER:
        skipToTagHeader(input);
        break;
      case STATE_READING_TAG_HEADER:
        if (!readTagHeader(input)) {
          return RESULT_END_OF_INPUT;
        }
        break;
      case STATE_READING_TAG_DATA:
        if (readTagData(input)) {
          return RESULT_CONTINUE;
        }
        break;
    }
  }
}
 
Example #17
Source File: WebvttExtractor.java    From Telegram-FOSS 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 #18
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 #19
Source File: Sniffer.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Peeks a variable-length unsigned EBML integer from the input.
 */
private long readUint(ExtractorInput input) throws IOException, InterruptedException {
  input.peekFully(scratch.data, 0, 1);
  int value = scratch.data[0] & 0xFF;
  if (value == 0) {
    return Long.MIN_VALUE;
  }
  int mask = 0x80;
  int length = 0;
  while ((value & mask) == 0) {
    mask >>= 1;
    length++;
  }
  value &= ~mask;
  input.peekFully(scratch.data, 1, length);
  for (int i = 0; i < length; i++) {
    value <<= 8;
    value += scratch.data[i + 1] & 0xFF;
  }
  peekLength += length + 1;
  return value;
}
 
Example #20
Source File: WebvttExtractor.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
  // Check whether there is a header without BOM.
  input.peekFully(
      sampleData, /* offset= */ 0, /* length= */ HEADER_MIN_LENGTH, /* allowEndOfInput= */ false);
  sampleDataWrapper.reset(sampleData, HEADER_MIN_LENGTH);
  if (WebvttParserUtil.isWebvttHeaderLine(sampleDataWrapper)) {
    return true;
  }
  // The header did not match, try including the BOM.
  input.peekFully(
      sampleData,
      /* offset= */ HEADER_MIN_LENGTH,
      HEADER_MAX_LENGTH - HEADER_MIN_LENGTH,
      /* allowEndOfInput= */ false);
  sampleDataWrapper.reset(sampleData, HEADER_MAX_LENGTH);
  return WebvttParserUtil.isWebvttHeaderLine(sampleDataWrapper);
}
 
Example #21
Source File: FragmentedMp4Extractor.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 {
  while (true) {
    switch (parserState) {
      case STATE_READING_ATOM_HEADER:
        if (!readAtomHeader(input)) {
          return Extractor.RESULT_END_OF_INPUT;
        }
        break;
      case STATE_READING_ATOM_PAYLOAD:
        readAtomPayload(input);
        break;
      case STATE_READING_ENCRYPTION_DATA:
        readEncryptionData(input);
        break;
      default:
        if (readSample(input)) {
          return RESULT_CONTINUE;
        }
    }
  }
}
 
Example #22
Source File: FlvExtractor.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads a tag header from the provided {@link ExtractorInput}.
 *
 * @param input The {@link ExtractorInput} from which to read.
 * @return True if tag header was read successfully. Otherwise, false.
 * @throws IOException If an error occurred reading or parsing data from the source.
 * @throws InterruptedException If the thread was interrupted.
 */
private boolean readTagHeader(ExtractorInput input) throws IOException, InterruptedException {
  if (!input.readFully(tagHeaderBuffer.data, 0, FLV_TAG_HEADER_SIZE, true)) {
    // We've reached the end of the stream.
    return false;
  }

  tagHeaderBuffer.setPosition(0);
  tagType = tagHeaderBuffer.readUnsignedByte();
  tagDataSize = tagHeaderBuffer.readUnsignedInt24();
  tagTimestampUs = tagHeaderBuffer.readUnsignedInt24();
  tagTimestampUs = ((tagHeaderBuffer.readUnsignedByte() << 24) | tagTimestampUs) * 1000L;
  tagHeaderBuffer.skipBytes(3); // streamId
  state = STATE_READING_TAG_DATA;
  return true;
}
 
Example #23
Source File: OggExtractor.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private boolean sniffInternal(ExtractorInput input) throws IOException, InterruptedException {
  OggPageHeader header = new OggPageHeader();
  if (!header.populate(input, true) || (header.type & 0x02) != 0x02) {
    return false;
  }

  int length = Math.min(header.bodySize, MAX_VERIFICATION_BYTES);
  ParsableByteArray scratch = new ParsableByteArray(length);
  input.peekFully(scratch.data, 0, length);

  if (FlacReader.verifyBitstreamType(resetPosition(scratch))) {
    streamReader = new FlacReader();
  } else if (VorbisReader.verifyBitstreamType(resetPosition(scratch))) {
    streamReader = new VorbisReader();
  } else if (OpusReader.verifyBitstreamType(resetPosition(scratch))) {
    streamReader = new OpusReader();
  } else {
    return false;
  }
  return true;
}
 
Example #24
Source File: Sniffer.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Peeks a variable-length unsigned EBML integer from the input.
 */
private long readUint(ExtractorInput input) throws IOException, InterruptedException {
  input.peekFully(scratch.data, 0, 1);
  int value = scratch.data[0] & 0xFF;
  if (value == 0) {
    return Long.MIN_VALUE;
  }
  int mask = 0x80;
  int length = 0;
  while ((value & mask) == 0) {
    mask >>= 1;
    length++;
  }
  value &= ~mask;
  input.peekFully(scratch.data, 1, length);
  for (int i = 0; i < length; i++) {
    value <<= 8;
    value += scratch.data[i + 1] & 0xFF;
  }
  peekLength += length + 1;
  return value;
}
 
Example #25
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 #26
Source File: DefaultEbmlReader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads and returns a float of length {@code byteLength} from the {@link ExtractorInput}.
 *
 * @param input The {@link ExtractorInput} from which to read.
 * @param byteLength The length of the float being read.
 * @return The read float value.
 * @throws IOException If an error occurs reading from the input.
 * @throws InterruptedException If the thread is interrupted.
 */
private double readFloat(ExtractorInput input, int byteLength)
    throws IOException, InterruptedException {
  long integerValue = readInteger(input, byteLength);
  double floatValue;
  if (byteLength == VALID_FLOAT32_ELEMENT_SIZE_BYTES) {
    floatValue = Float.intBitsToFloat((int) integerValue);
  } else {
    floatValue = Double.longBitsToDouble(integerValue);
  }
  return floatValue;
}
 
Example #27
Source File: VarintReader.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Reads an EBML variable-length integer (varint) from an {@link ExtractorInput} such that
 * reading can be resumed later if an error occurs having read only some of it.
 * <p>
 * If an value is successfully read, then the reader will automatically reset itself ready to
 * read another value.
 * <p>
 * If an {@link IOException} or {@link InterruptedException} is throw, the read can be resumed
 * later by calling this method again, passing an {@link ExtractorInput} providing data starting
 * where the previous one left off.
 *
 * @param input The {@link ExtractorInput} from which the integer should be read.
 * @param allowEndOfInput True if encountering the end of the input having read no data is
 *     allowed, and should result in {@link C#RESULT_END_OF_INPUT} being returned. False if it
 *     should be considered an error, causing an {@link EOFException} to be thrown.
 * @param removeLengthMask Removes the variable-length integer length mask from the value.
 * @param maximumAllowedLength Maximum allowed length of the variable integer to be read.
 * @return The read value, or {@link C#RESULT_END_OF_INPUT} if {@code allowEndOfStream} is true
 *     and the end of the input was encountered, or {@link C#RESULT_MAX_LENGTH_EXCEEDED} if the
 *     length of the varint exceeded maximumAllowedLength.
 * @throws IOException If an error occurs reading from the input.
 * @throws InterruptedException If the thread is interrupted.
 */
public long readUnsignedVarint(ExtractorInput input, boolean allowEndOfInput,
    boolean removeLengthMask, int maximumAllowedLength) throws IOException, InterruptedException {
  if (state == STATE_BEGIN_READING) {
    // Read the first byte to establish the length.
    if (!input.readFully(scratch, 0, 1, allowEndOfInput)) {
      return C.RESULT_END_OF_INPUT;
    }
    int firstByte = scratch[0] & 0xFF;
    length = parseUnsignedVarintLength(firstByte);
    if (length == C.LENGTH_UNSET) {
      throw new IllegalStateException("No valid varint length mask found");
    }
    state = STATE_READ_CONTENTS;
  }

  if (length > maximumAllowedLength) {
    state = STATE_BEGIN_READING;
    return C.RESULT_MAX_LENGTH_EXCEEDED;
  }

  if (length != 1) {
    // Read the remaining bytes.
    input.readFully(scratch, 1, length - 1);
  }

  state = STATE_BEGIN_READING;
  return assembleVarint(scratch, length, removeLengthMask);
}
 
Example #28
Source File: MatroskaExtractor.java    From K-Sonic with MIT License 5 votes vote down vote up
/**
 * Outputs up to {@code length} bytes of sample data to {@code output}, consisting of either
 * {@link #sampleStrippedBytes} or data read from {@code input}.
 */
private int readToOutput(ExtractorInput input, TrackOutput output, int length)
    throws IOException, InterruptedException {
  int bytesRead;
  int strippedBytesLeft = sampleStrippedBytes.bytesLeft();
  if (strippedBytesLeft > 0) {
    bytesRead = Math.min(length, strippedBytesLeft);
    output.sampleData(sampleStrippedBytes, bytesRead);
  } else {
    bytesRead = output.sampleData(input, length, false);
  }
  sampleBytesRead += bytesRead;
  sampleBytesWritten += bytesRead;
  return bytesRead;
}
 
Example #29
Source File: DefaultEbmlReader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads and returns a float of length {@code byteLength} from the {@link ExtractorInput}.
 *
 * @param input The {@link ExtractorInput} from which to read.
 * @param byteLength The length of the float being read.
 * @return The read float value.
 * @throws IOException If an error occurs reading from the input.
 * @throws InterruptedException If the thread is interrupted.
 */
private double readFloat(ExtractorInput input, int byteLength)
    throws IOException, InterruptedException {
  long integerValue = readInteger(input, byteLength);
  double floatValue;
  if (byteLength == VALID_FLOAT32_ELEMENT_SIZE_BYTES) {
    floatValue = Float.intBitsToFloat((int) integerValue);
  } else {
    floatValue = Double.longBitsToDouble(integerValue);
  }
  return floatValue;
}
 
Example #30
Source File: RawCcExtractor.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 {
  while (true) {
    switch (parserState) {
      case STATE_READING_HEADER:
        if (parseHeader(input)) {
          parserState = STATE_READING_TIMESTAMP_AND_COUNT;
        } else {
          return RESULT_END_OF_INPUT;
        }
        break;
      case STATE_READING_TIMESTAMP_AND_COUNT:
        if (parseTimestampAndSampleCount(input)) {
          parserState = STATE_READING_SAMPLES;
        } else {
          parserState = STATE_READING_HEADER;
          return RESULT_END_OF_INPUT;
        }
        break;
      case STATE_READING_SAMPLES:
        parseSamples(input);
        parserState = STATE_READING_TIMESTAMP_AND_COUNT;
        return RESULT_CONTINUE;
      default:
        throw new IllegalStateException();
    }
  }
}