Java Code Examples for com.google.android.exoplayer2.C#RESULT_END_OF_INPUT

The following examples show how to use com.google.android.exoplayer2.C#RESULT_END_OF_INPUT . 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: DefaultExtractorInput.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean advancePeekPosition(int length, boolean allowEndOfInput)
    throws IOException, InterruptedException {
  ensureSpaceForPeek(length);
  int bytesPeeked = peekBufferLength - peekBufferPosition;
  while (bytesPeeked < length) {
    bytesPeeked = readFromDataSource(peekBuffer, peekBufferPosition, length, bytesPeeked,
        allowEndOfInput);
    if (bytesPeeked == C.RESULT_END_OF_INPUT) {
      return false;
    }
    peekBufferLength = peekBufferPosition + bytesPeeked;
  }
  peekBufferPosition += length;
  return true;
}
 
Example 2
Source File: EncryptedFileDataSource.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int read(byte[] buffer, int offset, int readLength) throws EncryptedFileDataSourceException {
    if (readLength == 0) {
        return 0;
    } else if (bytesRemaining == 0) {
        return C.RESULT_END_OF_INPUT;
    } else {
        int bytesRead;
        try {
            bytesRead = file.read(buffer, offset, (int) Math.min(bytesRemaining, readLength));
            Utilities.aesCtrDecryptionByteArray(buffer, key, iv, offset, bytesRead, fileOffset);
            fileOffset += bytesRead;
        } catch (IOException e) {
            throw new EncryptedFileDataSourceException(e);
        }

        if (bytesRead > 0) {
            bytesRemaining -= bytesRead;
            if (listener != null) {
                listener.onBytesTransferred(this, dataSpec, false, bytesRead);
            }
        }

        return bytesRead;
    }
}
 
Example 3
Source File: DefaultExtractorInput.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
@Override
public boolean advancePeekPosition(int length, boolean allowEndOfInput)
    throws IOException, InterruptedException {
  ensureSpaceForPeek(length);
  int bytesPeeked = peekBufferLength - peekBufferPosition;
  while (bytesPeeked < length) {
    bytesPeeked = readFromDataSource(peekBuffer, peekBufferPosition, length, bytesPeeked,
        allowEndOfInput);
    if (bytesPeeked == C.RESULT_END_OF_INPUT) {
      return false;
    }
    peekBufferLength = peekBufferPosition + bytesPeeked;
  }
  peekBufferPosition += length;
  return true;
}
 
Example 4
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 5
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 6
Source File: Ac4Extractor.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 {
  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(firstSampleTimestampUs, 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 7
Source File: ByteArrayDataSource.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int read(byte[] buffer, int offset, int readLength) throws IOException {
  if (readLength == 0) {
    return 0;
  } else if (bytesRemaining == 0) {
    return C.RESULT_END_OF_INPUT;
  }

  readLength = Math.min(readLength, bytesRemaining);
  System.arraycopy(data, readPosition, buffer, offset, readLength);
  readPosition += readLength;
  bytesRemaining -= readLength;
  bytesTransferred(readLength);
  return readLength;
}
 
Example 8
Source File: DataSchemeDataSource.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int read(byte[] buffer, int offset, int readLength) {
  if (readLength == 0) {
    return 0;
  }
  int remainingBytes = endPosition - readPosition;
  if (remainingBytes == 0) {
    return C.RESULT_END_OF_INPUT;
  }
  readLength = Math.min(readLength, remainingBytes);
  System.arraycopy(castNonNull(data), readPosition, buffer, offset, readLength);
  readPosition += readLength;
  bytesTransferred(readLength);
  return readLength;
}
 
Example 9
Source File: RawResourceDataSource.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int read(byte[] buffer, int offset, int readLength) throws RawResourceDataSourceException {
  if (readLength == 0) {
    return 0;
  } else if (bytesRemaining == 0) {
    return C.RESULT_END_OF_INPUT;
  }

  int bytesRead;
  try {
    int bytesToRead = bytesRemaining == C.LENGTH_UNSET ? readLength
        : (int) Math.min(bytesRemaining, readLength);
    bytesRead = inputStream.read(buffer, offset, bytesToRead);
  } catch (IOException e) {
    throw new RawResourceDataSourceException(e);
  }

  if (bytesRead == -1) {
    if (bytesRemaining != C.LENGTH_UNSET) {
      // End of stream reached having not read sufficient data.
      throw new RawResourceDataSourceException(new EOFException());
    }
    return C.RESULT_END_OF_INPUT;
  }
  if (bytesRemaining != C.LENGTH_UNSET) {
    bytesRemaining -= bytesRead;
  }
  bytesTransferred(bytesRead);
  return bytesRead;
}
 
Example 10
Source File: DataSchemeDataSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int read(byte[] buffer, int offset, int readLength) {
  if (readLength == 0) {
    return 0;
  }
  int remainingBytes = data.length - bytesRead;
  if (remainingBytes == 0) {
    return C.RESULT_END_OF_INPUT;
  }
  readLength = Math.min(readLength, remainingBytes);
  System.arraycopy(data, bytesRead, buffer, offset, readLength);
  bytesRead += readLength;
  bytesTransferred(readLength);
  return readLength;
}
 
Example 11
Source File: DefaultExtractorInput.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean readFully(byte[] target, int offset, int length, boolean allowEndOfInput)
    throws IOException, InterruptedException {
  int bytesRead = readFromPeekBuffer(target, offset, length);
  while (bytesRead < length && bytesRead != C.RESULT_END_OF_INPUT) {
    bytesRead = readFromDataSource(target, offset, length, bytesRead, allowEndOfInput);
  }
  commitBytesRead(bytesRead);
  return bytesRead != C.RESULT_END_OF_INPUT;
}
 
Example 12
Source File: FlacDecoderJni.java    From Telegram 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: ContentDataSource.java    From K-Sonic with MIT License 5 votes vote down vote up
@Override
public int read(byte[] buffer, int offset, int readLength) throws ContentDataSourceException {
  if (readLength == 0) {
    return 0;
  } else if (bytesRemaining == 0) {
    return C.RESULT_END_OF_INPUT;
  }

  int bytesRead;
  try {
    int bytesToRead = bytesRemaining == C.LENGTH_UNSET ? readLength
        : (int) Math.min(bytesRemaining, readLength);
    bytesRead = inputStream.read(buffer, offset, bytesToRead);
  } catch (IOException e) {
    throw new ContentDataSourceException(e);
  }

  if (bytesRead == -1) {
    if (bytesRemaining != C.LENGTH_UNSET) {
      // End of stream reached having not read sufficient data.
      throw new ContentDataSourceException(new EOFException());
    }
    return C.RESULT_END_OF_INPUT;
  }
  if (bytesRemaining != C.LENGTH_UNSET) {
    bytesRemaining -= bytesRead;
  }
  if (listener != null) {
    listener.onBytesTransferred(this, bytesRead);
  }
  return bytesRead;
}
 
Example 14
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 15
Source File: Aes128DataSource.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final int read(byte[] buffer, int offset, int readLength) throws IOException {
  Assertions.checkNotNull(cipherInputStream);
  int bytesRead = cipherInputStream.read(buffer, offset, readLength);
  if (bytesRead < 0) {
    return C.RESULT_END_OF_INPUT;
  }
  return bytesRead;
}
 
Example 16
Source File: TeeDataSource.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int read(byte[] buffer, int offset, int max) throws IOException {
  if (bytesRemaining == 0) {
    return C.RESULT_END_OF_INPUT;
  }
  int bytesRead = upstream.read(buffer, offset, max);
  if (bytesRead > 0) {
    // TODO: Consider continuing even if writes to the sink fail.
    dataSink.write(buffer, offset, bytesRead);
    if (bytesRemaining != C.LENGTH_UNSET) {
      bytesRemaining -= bytesRead;
    }
  }
  return bytesRead;
}
 
Example 17
Source File: DefaultExtractorInput.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Advances the position by the specified number of bytes read.
 *
 * @param bytesRead The number of bytes read.
 */
private void commitBytesRead(int bytesRead) {
  if (bytesRead != C.RESULT_END_OF_INPUT) {
    position += bytesRead;
  }
}
 
Example 18
Source File: DefaultExtractorInput.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Advances the position by the specified number of bytes read.
 *
 * @param bytesRead The number of bytes read.
 */
private void commitBytesRead(int bytesRead) {
  if (bytesRead != C.RESULT_END_OF_INPUT) {
    position += bytesRead;
  }
}
 
Example 19
Source File: DefaultExtractorInput.java    From Telegram-FOSS with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Starts or continues a read from the data source.
 *
 * @param target A target array into which data should be written.
 * @param offset The offset into the target array at which to write.
 * @param length The maximum number of bytes to read from the input.
 * @param bytesAlreadyRead The number of bytes already read from the input.
 * @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.
 * @return The total number of bytes read so far, or {@link C#RESULT_END_OF_INPUT} if
 *     {@code allowEndOfInput} is true and the input has ended having read no bytes.
 * @throws EOFException If the end of input was encountered having partially satisfied the read
 *     (i.e. having read at least one byte, but fewer than {@code length}), or if no bytes were
 *     read and {@code allowEndOfInput} is false.
 * @throws IOException If an error occurs reading from the input.
 * @throws InterruptedException If the thread is interrupted.
 */
private int readFromDataSource(byte[] target, int offset, int length, int bytesAlreadyRead,
    boolean allowEndOfInput) throws InterruptedException, IOException {
  if (Thread.interrupted()) {
    throw new InterruptedException();
  }
  int bytesRead = dataSource.read(target, offset + bytesAlreadyRead, length - bytesAlreadyRead);
  if (bytesRead == C.RESULT_END_OF_INPUT) {
    if (bytesAlreadyRead == 0 && allowEndOfInput) {
      return C.RESULT_END_OF_INPUT;
    }
    throw new EOFException();
  }
  return bytesAlreadyRead + bytesRead;
}
 
Example 20
Source File: DefaultExtractorInput.java    From K-Sonic with MIT License 3 votes vote down vote up
/**
 * Starts or continues a read from the data source.
 *
 * @param target A target array into which data should be written.
 * @param offset The offset into the target array at which to write.
 * @param length The maximum number of bytes to read from the input.
 * @param bytesAlreadyRead The number of bytes already read from the input.
 * @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.
 * @return The total number of bytes read so far, or {@link C#RESULT_END_OF_INPUT} if
 *     {@code allowEndOfInput} is true and the input has ended having read no bytes.
 * @throws EOFException If the end of input was encountered having partially satisfied the read
 *     (i.e. having read at least one byte, but fewer than {@code length}), or if no bytes were
 *     read and {@code allowEndOfInput} is false.
 * @throws IOException If an error occurs reading from the input.
 * @throws InterruptedException If the thread is interrupted.
 */
private int readFromDataSource(byte[] target, int offset, int length, int bytesAlreadyRead,
    boolean allowEndOfInput) throws InterruptedException, IOException {
  if (Thread.interrupted()) {
    throw new InterruptedException();
  }
  int bytesRead = dataSource.read(target, offset + bytesAlreadyRead, length - bytesAlreadyRead);
  if (bytesRead == C.RESULT_END_OF_INPUT) {
    if (bytesAlreadyRead == 0 && allowEndOfInput) {
      return C.RESULT_END_OF_INPUT;
    }
    throw new EOFException();
  }
  return bytesAlreadyRead + bytesRead;
}