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

The following examples show how to use com.google.android.exoplayer2.extractor.ExtractorInput#readFully() . 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: FlvExtractor.java    From Telegram-FOSS 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 2
Source File: FlvExtractor.java    From LiveVideoBroadcaster with Apache License 2.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
  parserState = STATE_READING_TAG_DATA;
  return true;
}
 
Example 3
Source File: FlvExtractor.java    From TelePlus-Android 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 4
Source File: RawCcExtractor.java    From TelePlus-Android 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 5
Source File: RawCcExtractor.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void parseSamples(ExtractorInput input) throws IOException, InterruptedException {
  for (; remainingSampleCount > 0; remainingSampleCount--) {
    dataScratch.reset();
    input.readFully(dataScratch.data, 0, 3);

    trackOutput.sampleData(dataScratch, 3);
    sampleBytesWritten += 3;
  }

  if (sampleBytesWritten > 0) {
    trackOutput.sampleMetadata(timestampUs, C.BUFFER_FLAG_KEY_FRAME, sampleBytesWritten, 0, null);
  }
}
 
Example 6
Source File: FlvExtractor.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private ParsableByteArray prepareTagData(ExtractorInput input) throws IOException,
    InterruptedException {
  if (tagDataSize > tagData.capacity()) {
    tagData.reset(new byte[Math.max(tagData.capacity() * 2, tagDataSize)], 0);
  } else {
    tagData.setPosition(0);
  }
  tagData.setLimit(tagDataSize);
  input.readFully(tagData.data, 0, tagDataSize);
  return tagData;
}
 
Example 7
Source File: FragmentedMp4Extractor.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void readAtomPayload(ExtractorInput input) throws IOException, InterruptedException {
  int atomPayloadSize = (int) atomSize - atomHeaderBytesRead;
  if (atomData != null) {
    input.readFully(atomData.data, Atom.HEADER_SIZE, atomPayloadSize);
    onLeafAtomRead(new LeafAtom(atomType, atomData), input.getPosition());
  } else {
    input.skipFully(atomPayloadSize);
  }
  processAtomEnded(input.getPosition());
}
 
Example 8
Source File: FlvExtractor.java    From LiveVideoBroadcaster with Apache License 2.0 5 votes vote down vote up
private ParsableByteArray prepareTagData(ExtractorInput input) throws IOException,
    InterruptedException {
  if (tagDataSize > tagData.capacity()) {
    tagData.reset(new byte[Math.max(tagData.capacity() * 2, tagDataSize)], 0);
  } else {
    tagData.setPosition(0);
  }
  tagData.setLimit(tagDataSize);
  input.readFully(tagData.data, 0, tagDataSize);
  return tagData;
}
 
Example 9
Source File: FlvExtractor.java    From K-Sonic with MIT License 5 votes vote down vote up
/**
 * Reads an FLV container header from the provided {@link ExtractorInput}.
 *
 * @param input The {@link ExtractorInput} from which to read.
 * @return True if header was read successfully. False if the end of stream was reached.
 * @throws IOException If an error occurred reading or parsing data from the source.
 * @throws InterruptedException If the thread was interrupted.
 */
private boolean readFlvHeader(ExtractorInput input) throws IOException, InterruptedException {
  if (!input.readFully(headerBuffer.data, 0, FLV_HEADER_SIZE, true)) {
    // We've reached the end of the stream.
    return false;
  }

  headerBuffer.setPosition(0);
  headerBuffer.skipBytes(4);
  int flags = headerBuffer.readUnsignedByte();
  boolean hasAudio = (flags & 0x04) != 0;
  boolean hasVideo = (flags & 0x01) != 0;
  if (hasAudio && audioReader == null) {
    audioReader = new AudioTagPayloadReader(
        extractorOutput.track(TAG_TYPE_AUDIO, C.TRACK_TYPE_AUDIO));
  }
  if (hasVideo && videoReader == null) {
    videoReader = new VideoTagPayloadReader(
        extractorOutput.track(TAG_TYPE_VIDEO, C.TRACK_TYPE_VIDEO));
  }
  if (metadataReader == null) {
    metadataReader = new ScriptTagPayloadReader(null);
  }
  extractorOutput.endTracks();
  extractorOutput.seekMap(this);

  // We need to skip any additional content in the FLV header, plus the 4 byte previous tag size.
  bytesToNextTagHeader = headerBuffer.readInt() - FLV_HEADER_SIZE + 4;
  parserState = STATE_SKIPPING_TO_TAG_HEADER;
  return true;
}
 
Example 10
Source File: RawCcExtractor.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private boolean parseHeader(ExtractorInput input) throws IOException, InterruptedException {
  dataScratch.reset();
  if (input.readFully(dataScratch.data, 0, HEADER_SIZE, true)) {
    if (dataScratch.readInt() != HEADER_ID) {
      throw new IOException("Input not RawCC");
    }
    version = dataScratch.readUnsignedByte();
    // no versions use the flag fields yet
    return true;
  } else {
    return false;
  }
}
 
Example 11
Source File: FlvExtractor.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private ParsableByteArray prepareTagData(ExtractorInput input) throws IOException,
    InterruptedException {
  if (tagDataSize > tagData.capacity()) {
    tagData.reset(new byte[Math.max(tagData.capacity() * 2, tagDataSize)], 0);
  } else {
    tagData.setPosition(0);
  }
  tagData.setLimit(tagDataSize);
  input.readFully(tagData.data, 0, tagDataSize);
  return tagData;
}
 
Example 12
Source File: DefaultEbmlReader.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads and returns an integer of length {@code byteLength} from the {@link ExtractorInput}.
 *
 * @param input The {@link ExtractorInput} from which to read.
 * @param byteLength The length of the integer being read.
 * @return The read integer value.
 * @throws IOException If an error occurs reading from the input.
 * @throws InterruptedException If the thread is interrupted.
 */
private long readInteger(ExtractorInput input, int byteLength)
    throws IOException, InterruptedException {
  input.readFully(scratch, 0, byteLength);
  long value = 0;
  for (int i = 0; i < byteLength; i++) {
    value = (value << 8) | (scratch[i] & 0xFF);
  }
  return value;
}
 
Example 13
Source File: VarintReader.java    From TelePlus-Android with GNU General Public License v2.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 14
Source File: DefaultEbmlReader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads a string of length {@code byteLength} from the {@link ExtractorInput}. Zero padding is
 * removed, so the returned string may be shorter than {@code byteLength}.
 *
 * @param input The {@link ExtractorInput} from which to read.
 * @param byteLength The length of the string being read, including zero padding.
 * @return The read string value.
 * @throws IOException If an error occurs reading from the input.
 * @throws InterruptedException If the thread is interrupted.
 */
private String readString(ExtractorInput input, int byteLength)
    throws IOException, InterruptedException {
  if (byteLength == 0) {
    return "";
  }
  byte[] stringBytes = new byte[byteLength];
  input.readFully(stringBytes, 0, byteLength);
  // Remove zero padding.
  int trimmedLength = byteLength;
  while (trimmedLength > 0 && stringBytes[trimmedLength - 1] == 0) {
    trimmedLength--;
  }
  return new String(stringBytes, 0, trimmedLength);
}
 
Example 15
Source File: DefaultEbmlReader.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads and returns an integer of length {@code byteLength} from the {@link ExtractorInput}.
 *
 * @param input The {@link ExtractorInput} from which to read.
 * @param byteLength The length of the integer being read.
 * @return The read integer value.
 * @throws IOException If an error occurs reading from the input.
 * @throws InterruptedException If the thread is interrupted.
 */
private long readInteger(ExtractorInput input, int byteLength)
    throws IOException, InterruptedException {
  input.readFully(scratch, 0, byteLength);
  long value = 0;
  for (int i = 0; i < byteLength; i++) {
    value = (value << 8) | (scratch[i] & 0xFF);
  }
  return value;
}
 
Example 16
Source File: FlvExtractor.java    From K-Sonic with MIT License 5 votes vote down vote up
private ParsableByteArray prepareTagData(ExtractorInput input) throws IOException,
    InterruptedException {
  if (tagDataSize > tagData.capacity()) {
    tagData.reset(new byte[Math.max(tagData.capacity() * 2, tagDataSize)], 0);
  } else {
    tagData.setPosition(0);
  }
  tagData.setLimit(tagDataSize);
  input.readFully(tagData.data, 0, tagDataSize);
  return tagData;
}
 
Example 17
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 18
Source File: DefaultEbmlReader.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a string of length {@code byteLength} from the {@link ExtractorInput}. Zero padding is
 * removed, so the returned string may be shorter than {@code byteLength}.
 *
 * @param input The {@link ExtractorInput} from which to read.
 * @param byteLength The length of the string being read, including zero padding.
 * @return The read string value.
 * @throws IOException If an error occurs reading from the input.
 * @throws InterruptedException If the thread is interrupted.
 */
private String readString(ExtractorInput input, int byteLength)
    throws IOException, InterruptedException {
  if (byteLength == 0) {
    return "";
  }
  byte[] stringBytes = new byte[byteLength];
  input.readFully(stringBytes, 0, byteLength);
  // Remove zero padding.
  int trimmedLength = byteLength;
  while (trimmedLength > 0 && stringBytes[trimmedLength - 1] == 0) {
    trimmedLength--;
  }
  return new String(stringBytes, 0, trimmedLength);
}
 
Example 19
Source File: FragmentedMp4Extractor.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
private void readAtomPayload(ExtractorInput input) throws IOException, InterruptedException {
  int atomPayloadSize = (int) atomSize - atomHeaderBytesRead;
  if (atomData != null) {
    input.readFully(atomData.data, Atom.HEADER_SIZE, atomPayloadSize);
    onLeafAtomRead(new LeafAtom(atomType, atomData), input.getPosition());
  } else {
    input.skipFully(atomPayloadSize);
  }
  processAtomEnded(input.getPosition());
}
 
Example 20
Source File: TrackFragment.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
/**
 * Fills {@link #sampleEncryptionData} from the provided input.
 *
 * @param input An {@link ExtractorInput} from which to read the encryption data.
 */
public void fillEncryptionData(ExtractorInput input) throws IOException, InterruptedException {
  input.readFully(sampleEncryptionData.data, 0, sampleEncryptionDataLength);
  sampleEncryptionData.setPosition(0);
  sampleEncryptionDataNeedsFill = false;
}