Java Code Examples for com.google.android.exoplayer2.decoder.DecoderInputBuffer#isFlagsOnly()

The following examples show how to use com.google.android.exoplayer2.decoder.DecoderInputBuffer#isFlagsOnly() . 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: SingleSampleMediaPeriod.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@Override
public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer,
    boolean requireFormat) {
  maybeNotifyDownstreamFormat();
  if (streamState == STREAM_STATE_END_OF_STREAM) {
    buffer.addFlag(C.BUFFER_FLAG_END_OF_STREAM);
    return C.RESULT_BUFFER_READ;
  } else if (requireFormat || streamState == STREAM_STATE_SEND_FORMAT) {
    formatHolder.format = format;
    streamState = STREAM_STATE_SEND_SAMPLE;
    return C.RESULT_FORMAT_READ;
  } else if (loadingFinished) {
    if (sampleData != null) {
      buffer.addFlag(C.BUFFER_FLAG_KEY_FRAME);
      buffer.timeUs = 0;
      if (buffer.isFlagsOnly()) {
        return C.RESULT_BUFFER_READ;
      }
      buffer.ensureSpaceForWrite(sampleSize);
      buffer.data.put(sampleData, 0, sampleSize);
    } else {
      buffer.addFlag(C.BUFFER_FLAG_END_OF_STREAM);
    }
    streamState = STREAM_STATE_END_OF_STREAM;
    return C.RESULT_BUFFER_READ;
  }
  return C.RESULT_NOTHING_READ;
}
 
Example 2
Source File: SampleMetadataQueue.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Attempts to read from the queue.
 *
 * @param formatHolder A {@link FormatHolder} to populate in the case of reading a format.
 * @param buffer A {@link DecoderInputBuffer} to populate in the case of reading a sample or the
 *     end of the stream. If a sample is read then the buffer is populated with information
 *     about the sample, but not its data. The size and absolute position of the data in the
 *     rolling buffer is stored in {@code extrasHolder}, along with an encryption id if present
 *     and the absolute position of the first byte that may still be required after the current
 *     sample has been read. May be null if the caller requires that the format of the stream be
 *     read even if it's not changing.
 * @param formatRequired Whether the caller requires that the format of the stream be read even
 *     if it's not changing. A sample will never be read if set to true, however it is still
 *     possible for the end of stream or nothing to be read.
 * @param loadingFinished True if an empty queue should be considered the end of the stream.
 * @param downstreamFormat The current downstream {@link Format}. If the format of the next
 *     sample is different to the current downstream format then a format will be read.
 * @param extrasHolder The holder into which extra sample information should be written.
 * @return The result, which can be {@link C#RESULT_NOTHING_READ}, {@link C#RESULT_FORMAT_READ}
 *     or {@link C#RESULT_BUFFER_READ}.
 */
@SuppressWarnings("ReferenceEquality")
public synchronized int read(FormatHolder formatHolder, DecoderInputBuffer buffer,
    boolean formatRequired, boolean loadingFinished, Format downstreamFormat,
    SampleExtrasHolder extrasHolder) {
  if (!hasNextSample()) {
    if (loadingFinished) {
      buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
      return C.RESULT_BUFFER_READ;
    } else if (upstreamFormat != null
        && (formatRequired || upstreamFormat != downstreamFormat)) {
      formatHolder.format = upstreamFormat;
      return C.RESULT_FORMAT_READ;
    } else {
      return C.RESULT_NOTHING_READ;
    }
  }

  int relativeReadIndex = getRelativeIndex(readPosition);
  if (formatRequired || formats[relativeReadIndex] != downstreamFormat) {
    formatHolder.format = formats[relativeReadIndex];
    return C.RESULT_FORMAT_READ;
  }

  if (buffer.isFlagsOnly()) {
    return C.RESULT_NOTHING_READ;
  }

  buffer.timeUs = timesUs[relativeReadIndex];
  buffer.setFlags(flags[relativeReadIndex]);
  extrasHolder.size = sizes[relativeReadIndex];
  extrasHolder.offset = offsets[relativeReadIndex];
  extrasHolder.cryptoData = cryptoDatas[relativeReadIndex];

  readPosition++;
  return C.RESULT_BUFFER_READ;
}
 
Example 3
Source File: SampleMetadataQueue.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Attempts to read from the queue.
 *
 * @param formatHolder A {@link FormatHolder} to populate in the case of reading a format.
 * @param buffer A {@link DecoderInputBuffer} to populate in the case of reading a sample or the
 *     end of the stream. If a sample is read then the buffer is populated with information
 *     about the sample, but not its data. The size and absolute position of the data in the
 *     rolling buffer is stored in {@code extrasHolder}, along with an encryption id if present
 *     and the absolute position of the first byte that may still be required after the current
 *     sample has been read. May be null if the caller requires that the format of the stream be
 *     read even if it's not changing.
 * @param formatRequired Whether the caller requires that the format of the stream be read even
 *     if it's not changing. A sample will never be read if set to true, however it is still
 *     possible for the end of stream or nothing to be read.
 * @param loadingFinished True if an empty queue should be considered the end of the stream.
 * @param downstreamFormat The current downstream {@link Format}. If the format of the next
 *     sample is different to the current downstream format then a format will be read.
 * @param extrasHolder The holder into which extra sample information should be written.
 * @return The result, which can be {@link C#RESULT_NOTHING_READ}, {@link C#RESULT_FORMAT_READ}
 *     or {@link C#RESULT_BUFFER_READ}.
 */
@SuppressWarnings("ReferenceEquality")
public synchronized int read(FormatHolder formatHolder, DecoderInputBuffer buffer,
    boolean formatRequired, boolean loadingFinished, Format downstreamFormat,
    SampleExtrasHolder extrasHolder) {
  if (!hasNextSample()) {
    if (loadingFinished) {
      buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
      return C.RESULT_BUFFER_READ;
    } else if (upstreamFormat != null
        && (formatRequired || upstreamFormat != downstreamFormat)) {
      formatHolder.format = upstreamFormat;
      return C.RESULT_FORMAT_READ;
    } else {
      return C.RESULT_NOTHING_READ;
    }
  }

  int relativeReadIndex = getRelativeIndex(readPosition);
  if (formatRequired || formats[relativeReadIndex] != downstreamFormat) {
    formatHolder.format = formats[relativeReadIndex];
    return C.RESULT_FORMAT_READ;
  }

  if (buffer.isFlagsOnly()) {
    return C.RESULT_NOTHING_READ;
  }

  buffer.timeUs = timesUs[relativeReadIndex];
  buffer.setFlags(flags[relativeReadIndex]);
  extrasHolder.size = sizes[relativeReadIndex];
  extrasHolder.offset = offsets[relativeReadIndex];
  extrasHolder.cryptoData = cryptoDatas[relativeReadIndex];

  readPosition++;
  return C.RESULT_BUFFER_READ;
}
 
Example 4
Source File: SampleQueue.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Attempts to read from the queue.
 *
 * @param formatHolder A {@link FormatHolder} to populate in the case of reading a format.
 * @param buffer A {@link DecoderInputBuffer} to populate in the case of reading a sample or the
 *     end of the stream. If the end of the stream has been reached, the {@link
 *     C#BUFFER_FLAG_END_OF_STREAM} flag will be set on the buffer. If a {@link
 *     DecoderInputBuffer#isFlagsOnly() flags-only} buffer is passed, only the buffer flags may be
 *     populated by this method and the read position of the queue will not change.
 * @param formatRequired Whether the caller requires that the format of the stream be read even if
 *     it's not changing. A sample will never be read if set to true, however it is still possible
 *     for the end of stream or nothing to be read.
 * @param loadingFinished True if an empty queue should be considered the end of the stream.
 * @param decodeOnlyUntilUs If a buffer is read, the {@link C#BUFFER_FLAG_DECODE_ONLY} flag will
 *     be set if the buffer's timestamp is less than this value.
 * @return The result, which can be {@link C#RESULT_NOTHING_READ}, {@link C#RESULT_FORMAT_READ} or
 *     {@link C#RESULT_BUFFER_READ}.
 */
public int read(
    FormatHolder formatHolder,
    DecoderInputBuffer buffer,
    boolean formatRequired,
    boolean loadingFinished,
    long decodeOnlyUntilUs) {
  int result = metadataQueue.read(formatHolder, buffer, formatRequired, loadingFinished,
      downstreamFormat, extrasHolder);
  switch (result) {
    case C.RESULT_FORMAT_READ:
      downstreamFormat = formatHolder.format;
      return C.RESULT_FORMAT_READ;
    case C.RESULT_BUFFER_READ:
      if (!buffer.isEndOfStream()) {
        if (buffer.timeUs < decodeOnlyUntilUs) {
          buffer.addFlag(C.BUFFER_FLAG_DECODE_ONLY);
        }
        if (!buffer.isFlagsOnly()) {
          // Read encryption data if the sample is encrypted.
          if (buffer.isEncrypted()) {
            readEncryptionData(buffer, extrasHolder);
          }
          // Write the sample data into the holder.
          buffer.ensureSpaceForWrite(extrasHolder.size);
          readData(extrasHolder.offset, buffer.data, extrasHolder.size);
        }
      }
      return C.RESULT_BUFFER_READ;
    case C.RESULT_NOTHING_READ:
      return C.RESULT_NOTHING_READ;
    default:
      throw new IllegalStateException();
  }
}
 
Example 5
Source File: SingleSampleMediaPeriod.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer,
    boolean requireFormat) {
  maybeNotifyDownstreamFormat();
  if (streamState == STREAM_STATE_END_OF_STREAM) {
    buffer.addFlag(C.BUFFER_FLAG_END_OF_STREAM);
    return C.RESULT_BUFFER_READ;
  } else if (requireFormat || streamState == STREAM_STATE_SEND_FORMAT) {
    formatHolder.format = format;
    streamState = STREAM_STATE_SEND_SAMPLE;
    return C.RESULT_FORMAT_READ;
  } else if (loadingFinished) {
    if (loadingSucceeded) {
      buffer.addFlag(C.BUFFER_FLAG_KEY_FRAME);
      buffer.timeUs = 0;
      if (buffer.isFlagsOnly()) {
        return C.RESULT_BUFFER_READ;
      }
      buffer.ensureSpaceForWrite(sampleSize);
      buffer.data.put(sampleData, 0, sampleSize);
    } else {
      buffer.addFlag(C.BUFFER_FLAG_END_OF_STREAM);
    }
    streamState = STREAM_STATE_END_OF_STREAM;
    return C.RESULT_BUFFER_READ;
  }
  return C.RESULT_NOTHING_READ;
}
 
Example 6
Source File: SampleQueue.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Attempts to read from the queue.
 *
 * @param formatHolder A {@link FormatHolder} to populate in the case of reading a format.
 * @param buffer A {@link DecoderInputBuffer} to populate in the case of reading a sample or the
 *     end of the stream. If the end of the stream has been reached, the {@link
 *     C#BUFFER_FLAG_END_OF_STREAM} flag will be set on the buffer. If a {@link
 *     DecoderInputBuffer#isFlagsOnly() flags-only} buffer is passed, only the buffer flags may be
 *     populated by this method and the read position of the queue will not change.
 * @param formatRequired Whether the caller requires that the format of the stream be read even if
 *     it's not changing. A sample will never be read if set to true, however it is still possible
 *     for the end of stream or nothing to be read.
 * @param loadingFinished True if an empty queue should be considered the end of the stream.
 * @param decodeOnlyUntilUs If a buffer is read, the {@link C#BUFFER_FLAG_DECODE_ONLY} flag will
 *     be set if the buffer's timestamp is less than this value.
 * @return The result, which can be {@link C#RESULT_NOTHING_READ}, {@link C#RESULT_FORMAT_READ} or
 *     {@link C#RESULT_BUFFER_READ}.
 */
public int read(
    FormatHolder formatHolder,
    DecoderInputBuffer buffer,
    boolean formatRequired,
    boolean loadingFinished,
    long decodeOnlyUntilUs) {
  int result = metadataQueue.read(formatHolder, buffer, formatRequired, loadingFinished,
      downstreamFormat, extrasHolder);
  switch (result) {
    case C.RESULT_FORMAT_READ:
      downstreamFormat = formatHolder.format;
      return C.RESULT_FORMAT_READ;
    case C.RESULT_BUFFER_READ:
      if (!buffer.isEndOfStream()) {
        if (buffer.timeUs < decodeOnlyUntilUs) {
          buffer.addFlag(C.BUFFER_FLAG_DECODE_ONLY);
        }
        if (!buffer.isFlagsOnly()) {
          // Read encryption data if the sample is encrypted.
          if (buffer.isEncrypted()) {
            readEncryptionData(buffer, extrasHolder);
          }
          // Write the sample data into the holder.
          buffer.ensureSpaceForWrite(extrasHolder.size);
          readData(extrasHolder.offset, buffer.data, extrasHolder.size);
        }
      }
      return C.RESULT_BUFFER_READ;
    case C.RESULT_NOTHING_READ:
      return C.RESULT_NOTHING_READ;
    default:
      throw new IllegalStateException();
  }
}
 
Example 7
Source File: SingleSampleMediaPeriod.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer,
    boolean requireFormat) {
  maybeNotifyDownstreamFormat();
  if (streamState == STREAM_STATE_END_OF_STREAM) {
    buffer.addFlag(C.BUFFER_FLAG_END_OF_STREAM);
    return C.RESULT_BUFFER_READ;
  } else if (requireFormat || streamState == STREAM_STATE_SEND_FORMAT) {
    formatHolder.format = format;
    streamState = STREAM_STATE_SEND_SAMPLE;
    return C.RESULT_FORMAT_READ;
  } else if (loadingFinished) {
    if (loadingSucceeded) {
      buffer.addFlag(C.BUFFER_FLAG_KEY_FRAME);
      buffer.timeUs = 0;
      if (buffer.isFlagsOnly()) {
        return C.RESULT_BUFFER_READ;
      }
      buffer.ensureSpaceForWrite(sampleSize);
      buffer.data.put(sampleData, 0, sampleSize);
    } else {
      buffer.addFlag(C.BUFFER_FLAG_END_OF_STREAM);
    }
    streamState = STREAM_STATE_END_OF_STREAM;
    return C.RESULT_BUFFER_READ;
  }
  return C.RESULT_NOTHING_READ;
}
 
Example 8
Source File: SampleQueue.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to read from the queue.
 *
 * <p>{@link Format Formats} read from this method may be associated to a {@link DrmSession}
 * through {@link FormatHolder#drmSession}, which is populated in two scenarios:
 *
 * <ul>
 *   <li>The {@link Format} has a non-null {@link Format#drmInitData}.
 *   <li>The {@link DrmSessionManager} provides placeholder sessions for this queue's track type.
 *       See {@link DrmSessionManager#acquirePlaceholderSession(Looper, int)}.
 * </ul>
 *
 * @param formatHolder A {@link FormatHolder} to populate in the case of reading a format.
 * @param buffer A {@link DecoderInputBuffer} to populate in the case of reading a sample or the
 *     end of the stream. If the end of the stream has been reached, the {@link
 *     C#BUFFER_FLAG_END_OF_STREAM} flag will be set on the buffer. If a {@link
 *     DecoderInputBuffer#isFlagsOnly() flags-only} buffer is passed, only the buffer flags may be
 *     populated by this method and the read position of the queue will not change.
 * @param formatRequired Whether the caller requires that the format of the stream be read even if
 *     it's not changing. A sample will never be read if set to true, however it is still possible
 *     for the end of stream or nothing to be read.
 * @param loadingFinished True if an empty queue should be considered the end of the stream.
 * @param decodeOnlyUntilUs If a buffer is read, the {@link C#BUFFER_FLAG_DECODE_ONLY} flag will
 *     be set if the buffer's timestamp is less than this value.
 * @return The result, which can be {@link C#RESULT_NOTHING_READ}, {@link C#RESULT_FORMAT_READ} or
 *     {@link C#RESULT_BUFFER_READ}.
 */
@SuppressWarnings("ReferenceEquality")
public int read(
    FormatHolder formatHolder,
    DecoderInputBuffer buffer,
    boolean formatRequired,
    boolean loadingFinished,
    long decodeOnlyUntilUs) {
  int result =
      metadataQueue.read(formatHolder, buffer, formatRequired, loadingFinished, extrasHolder);
  if (result == C.RESULT_BUFFER_READ && !buffer.isEndOfStream()) {
    if (buffer.timeUs < decodeOnlyUntilUs) {
      buffer.addFlag(C.BUFFER_FLAG_DECODE_ONLY);
    }
    if (!buffer.isFlagsOnly()) {
      readToBuffer(buffer, extrasHolder);
    }
  }
  return result;
}
 
Example 9
Source File: SampleMetadataQueue.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to read from the queue.
 *
 * @param formatHolder A {@link FormatHolder} to populate in the case of reading a format.
 * @param buffer A {@link DecoderInputBuffer} to populate in the case of reading a sample or the
 *     end of the stream. If a sample is read then the buffer is populated with information about
 *     the sample, but not its data. The size and absolute position of the data in the rolling
 *     buffer is stored in {@code extrasHolder}, along with an encryption id if present and the
 *     absolute position of the first byte that may still be required after the current sample has
 *     been read. If a {@link DecoderInputBuffer#isFlagsOnly() flags-only} buffer is passed, only
 *     the buffer flags may be populated by this method and the read position of the queue will
 *     not change. May be null if the caller requires that the format of the stream be read even
 *     if it's not changing.
 * @param formatRequired Whether the caller requires that the format of the stream be read even if
 *     it's not changing. A sample will never be read if set to true, however it is still possible
 *     for the end of stream or nothing to be read.
 * @param loadingFinished True if an empty queue should be considered the end of the stream.
 * @param extrasHolder The holder into which extra sample information should be written.
 * @return The result, which can be {@link C#RESULT_NOTHING_READ}, {@link C#RESULT_FORMAT_READ} or
 *     {@link C#RESULT_BUFFER_READ}.
 */
@SuppressWarnings("ReferenceEquality")
public synchronized int read(
    FormatHolder formatHolder,
    DecoderInputBuffer buffer,
    boolean formatRequired,
    boolean loadingFinished,
    SampleExtrasHolder extrasHolder) {
  if (!hasNextSample()) {
    if (loadingFinished || isLastSampleQueued) {
      buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
      return C.RESULT_BUFFER_READ;
    } else if (upstreamFormat != null && (formatRequired || upstreamFormat != downstreamFormat)) {
      onFormatResult(Assertions.checkNotNull(upstreamFormat), formatHolder);
      return C.RESULT_FORMAT_READ;
    } else {
      return C.RESULT_NOTHING_READ;
    }
  }

  int relativeReadIndex = getRelativeIndex(readPosition);
  if (formatRequired || formats[relativeReadIndex] != downstreamFormat) {
    onFormatResult(formats[relativeReadIndex], formatHolder);
    return C.RESULT_FORMAT_READ;
  }

  if (!mayReadSample(relativeReadIndex)) {
    return C.RESULT_NOTHING_READ;
  }

  buffer.setFlags(flags[relativeReadIndex]);
  buffer.timeUs = timesUs[relativeReadIndex];
  if (buffer.isFlagsOnly()) {
    return C.RESULT_BUFFER_READ;
  }

  extrasHolder.size = sizes[relativeReadIndex];
  extrasHolder.offset = offsets[relativeReadIndex];
  extrasHolder.cryptoData = cryptoDatas[relativeReadIndex];

  readPosition++;
  return C.RESULT_BUFFER_READ;
}
 
Example 10
Source File: SampleMetadataQueue.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Attempts to read from the queue.
 *
 * @param formatHolder A {@link FormatHolder} to populate in the case of reading a format.
 * @param buffer A {@link DecoderInputBuffer} to populate in the case of reading a sample or the
 *     end of the stream. If a sample is read then the buffer is populated with information about
 *     the sample, but not its data. The size and absolute position of the data in the rolling
 *     buffer is stored in {@code extrasHolder}, along with an encryption id if present and the
 *     absolute position of the first byte that may still be required after the current sample has
 *     been read. If a {@link DecoderInputBuffer#isFlagsOnly() flags-only} buffer is passed, only
 *     the buffer flags may be populated by this method and the read position of the queue will
 *     not change. May be null if the caller requires that the format of the stream be read even
 *     if it's not changing.
 * @param formatRequired Whether the caller requires that the format of the stream be read even if
 *     it's not changing. A sample will never be read if set to true, however it is still possible
 *     for the end of stream or nothing to be read.
 * @param loadingFinished True if an empty queue should be considered the end of the stream.
 * @param downstreamFormat The current downstream {@link Format}. If the format of the next sample
 *     is different to the current downstream format then a format will be read.
 * @param extrasHolder The holder into which extra sample information should be written.
 * @return The result, which can be {@link C#RESULT_NOTHING_READ}, {@link C#RESULT_FORMAT_READ} or
 *     {@link C#RESULT_BUFFER_READ}.
 */
@SuppressWarnings("ReferenceEquality")
public synchronized int read(
    FormatHolder formatHolder,
    DecoderInputBuffer buffer,
    boolean formatRequired,
    boolean loadingFinished,
    Format downstreamFormat,
    SampleExtrasHolder extrasHolder) {
  if (!hasNextSample()) {
    if (loadingFinished || isLastSampleQueued) {
      buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
      return C.RESULT_BUFFER_READ;
    } else if (upstreamFormat != null
        && (formatRequired || upstreamFormat != downstreamFormat)) {
      formatHolder.format = upstreamFormat;
      return C.RESULT_FORMAT_READ;
    } else {
      return C.RESULT_NOTHING_READ;
    }
  }

  int relativeReadIndex = getRelativeIndex(readPosition);
  if (formatRequired || formats[relativeReadIndex] != downstreamFormat) {
    formatHolder.format = formats[relativeReadIndex];
    return C.RESULT_FORMAT_READ;
  }

  buffer.setFlags(flags[relativeReadIndex]);
  buffer.timeUs = timesUs[relativeReadIndex];
  if (buffer.isFlagsOnly()) {
    return C.RESULT_BUFFER_READ;
  }

  extrasHolder.size = sizes[relativeReadIndex];
  extrasHolder.offset = offsets[relativeReadIndex];
  extrasHolder.cryptoData = cryptoDatas[relativeReadIndex];

  readPosition++;
  return C.RESULT_BUFFER_READ;
}
 
Example 11
Source File: SampleMetadataQueue.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Attempts to read from the queue.
 *
 * @param formatHolder A {@link FormatHolder} to populate in the case of reading a format.
 * @param buffer A {@link DecoderInputBuffer} to populate in the case of reading a sample or the
 *     end of the stream. If a sample is read then the buffer is populated with information about
 *     the sample, but not its data. The size and absolute position of the data in the rolling
 *     buffer is stored in {@code extrasHolder}, along with an encryption id if present and the
 *     absolute position of the first byte that may still be required after the current sample has
 *     been read. If a {@link DecoderInputBuffer#isFlagsOnly() flags-only} buffer is passed, only
 *     the buffer flags may be populated by this method and the read position of the queue will
 *     not change. May be null if the caller requires that the format of the stream be read even
 *     if it's not changing.
 * @param formatRequired Whether the caller requires that the format of the stream be read even if
 *     it's not changing. A sample will never be read if set to true, however it is still possible
 *     for the end of stream or nothing to be read.
 * @param loadingFinished True if an empty queue should be considered the end of the stream.
 * @param downstreamFormat The current downstream {@link Format}. If the format of the next sample
 *     is different to the current downstream format then a format will be read.
 * @param extrasHolder The holder into which extra sample information should be written.
 * @return The result, which can be {@link C#RESULT_NOTHING_READ}, {@link C#RESULT_FORMAT_READ} or
 *     {@link C#RESULT_BUFFER_READ}.
 */
@SuppressWarnings("ReferenceEquality")
public synchronized int read(
    FormatHolder formatHolder,
    DecoderInputBuffer buffer,
    boolean formatRequired,
    boolean loadingFinished,
    Format downstreamFormat,
    SampleExtrasHolder extrasHolder) {
  if (!hasNextSample()) {
    if (loadingFinished || isLastSampleQueued) {
      buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
      return C.RESULT_BUFFER_READ;
    } else if (upstreamFormat != null
        && (formatRequired || upstreamFormat != downstreamFormat)) {
      formatHolder.format = upstreamFormat;
      return C.RESULT_FORMAT_READ;
    } else {
      return C.RESULT_NOTHING_READ;
    }
  }

  int relativeReadIndex = getRelativeIndex(readPosition);
  if (formatRequired || formats[relativeReadIndex] != downstreamFormat) {
    formatHolder.format = formats[relativeReadIndex];
    return C.RESULT_FORMAT_READ;
  }

  buffer.setFlags(flags[relativeReadIndex]);
  buffer.timeUs = timesUs[relativeReadIndex];
  if (buffer.isFlagsOnly()) {
    return C.RESULT_BUFFER_READ;
  }

  extrasHolder.size = sizes[relativeReadIndex];
  extrasHolder.offset = offsets[relativeReadIndex];
  extrasHolder.cryptoData = cryptoDatas[relativeReadIndex];

  readPosition++;
  return C.RESULT_BUFFER_READ;
}