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

The following examples show how to use com.google.android.exoplayer2.decoder.DecoderInputBuffer#ensureSpaceForWrite() . 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: SilenceMediaSource.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
@Override
public int readData(
    FormatHolder formatHolder, DecoderInputBuffer buffer, boolean formatRequired) {
  if (!sentFormat || formatRequired) {
    formatHolder.format = FORMAT;
    sentFormat = true;
    return C.RESULT_FORMAT_READ;
  }

  long bytesRemaining = durationBytes - positionBytes;
  if (bytesRemaining == 0) {
    buffer.addFlag(C.BUFFER_FLAG_END_OF_STREAM);
    return C.RESULT_BUFFER_READ;
  }

  int bytesToWrite = (int) Math.min(SILENCE_SAMPLE.length, bytesRemaining);
  buffer.ensureSpaceForWrite(bytesToWrite);
  buffer.data.put(SILENCE_SAMPLE, /* offset= */ 0, bytesToWrite);
  buffer.timeUs = getAudioPositionUs(positionBytes);
  buffer.addFlag(C.BUFFER_FLAG_KEY_FRAME);
  positionBytes += bytesToWrite;
  return C.RESULT_BUFFER_READ;
}
 
Example 2
Source File: SilenceMediaSource.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int readData(
    FormatHolder formatHolder, DecoderInputBuffer buffer, boolean formatRequired) {
  if (!sentFormat || formatRequired) {
    formatHolder.format = FORMAT;
    sentFormat = true;
    return C.RESULT_FORMAT_READ;
  }

  long bytesRemaining = durationBytes - positionBytes;
  if (bytesRemaining == 0) {
    buffer.addFlag(C.BUFFER_FLAG_END_OF_STREAM);
    return C.RESULT_BUFFER_READ;
  }

  int bytesToWrite = (int) Math.min(SILENCE_SAMPLE.length, bytesRemaining);
  buffer.ensureSpaceForWrite(bytesToWrite);
  buffer.addFlag(C.BUFFER_FLAG_KEY_FRAME);
  buffer.data.put(SILENCE_SAMPLE, /* offset= */ 0, bytesToWrite);
  buffer.timeUs = getAudioPositionUs(positionBytes);
  positionBytes += bytesToWrite;
  return C.RESULT_BUFFER_READ;
}
 
Example 3
Source File: SingleSampleMediaPeriod.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer,
    boolean requireFormat) {
  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.timeUs = 0;
      buffer.addFlag(C.BUFFER_FLAG_KEY_FRAME);
      buffer.ensureSpaceForWrite(sampleSize);
      buffer.data.put(sampleData, 0, sampleSize);
      sendFormat();
    } 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 4
Source File: SingleSampleMediaPeriod.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer,
    boolean requireFormat) {
  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.timeUs = 0;
      buffer.addFlag(C.BUFFER_FLAG_KEY_FRAME);
      buffer.ensureSpaceForWrite(sampleSize);
      buffer.data.put(sampleData, 0, sampleSize);
      sendFormat();
    } 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 5
Source File: SilenceMediaSource.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int readData(
    FormatHolder formatHolder, DecoderInputBuffer buffer, boolean formatRequired) {
  if (!sentFormat || formatRequired) {
    formatHolder.format = FORMAT;
    sentFormat = true;
    return C.RESULT_FORMAT_READ;
  }

  long bytesRemaining = durationBytes - positionBytes;
  if (bytesRemaining == 0) {
    buffer.addFlag(C.BUFFER_FLAG_END_OF_STREAM);
    return C.RESULT_BUFFER_READ;
  }

  int bytesToWrite = (int) Math.min(SILENCE_SAMPLE.length, bytesRemaining);
  buffer.ensureSpaceForWrite(bytesToWrite);
  buffer.addFlag(C.BUFFER_FLAG_KEY_FRAME);
  buffer.data.put(SILENCE_SAMPLE, /* offset= */ 0, bytesToWrite);
  buffer.timeUs = getAudioPositionUs(positionBytes);
  positionBytes += bytesToWrite;
  return C.RESULT_BUFFER_READ;
}
 
Example 6
Source File: SingleSampleMediaPeriod.java    From K-Sonic with MIT License 6 votes vote down vote up
@Override
public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer,
    boolean requireFormat) {
  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;
  }

  Assertions.checkState(streamState == STREAM_STATE_SEND_SAMPLE);
  if (!loadingFinished) {
    return C.RESULT_NOTHING_READ;
  } else {
    buffer.timeUs = 0;
    buffer.addFlag(C.BUFFER_FLAG_KEY_FRAME);
    buffer.ensureSpaceForWrite(sampleSize);
    buffer.data.put(sampleData, 0, sampleSize);
    streamState = STREAM_STATE_END_OF_STREAM;
    return C.RESULT_BUFFER_READ;
  }
}
 
Example 7
Source File: EventSampleStream.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 formatRequired) {
  if (formatRequired || !isFormatSentDownstream) {
    formatHolder.format = upstreamFormat;
    isFormatSentDownstream = true;
    return C.RESULT_FORMAT_READ;
  }
  if (currentIndex == eventTimesUs.length) {
    if (!eventStreamAppendable) {
      buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
      return C.RESULT_BUFFER_READ;
    } else {
      return C.RESULT_NOTHING_READ;
    }
  }
  int sampleIndex = currentIndex++;
  byte[] serializedEvent = eventMessageEncoder.encode(eventStream.events[sampleIndex]);
  if (serializedEvent != null) {
    buffer.ensureSpaceForWrite(serializedEvent.length);
    buffer.setFlags(C.BUFFER_FLAG_KEY_FRAME);
    buffer.data.put(serializedEvent);
    buffer.timeUs = eventTimesUs[sampleIndex];
    return C.RESULT_BUFFER_READ;
  } else {
    return C.RESULT_NOTHING_READ;
  }
}
 
Example 8
Source File: SampleQueue.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Reads data from the rolling buffer to populate a decoder input buffer.
 *
 * @param buffer The buffer to populate.
 * @param extrasHolder The extras holder whose offset should be read and subsequently adjusted.
 */
private void readToBuffer(DecoderInputBuffer buffer, SampleExtrasHolder extrasHolder) {
  // Read encryption data if the sample is encrypted.
  if (buffer.isEncrypted()) {
    readEncryptionData(buffer, extrasHolder);
  }
  // Read sample data, extracting supplemental data into a separate buffer if needed.
  if (buffer.hasSupplementalData()) {
    // If there is supplemental data, the sample data is prefixed by its size.
    scratch.reset(4);
    readData(extrasHolder.offset, scratch.data, 4);
    int sampleSize = scratch.readUnsignedIntToInt();
    extrasHolder.offset += 4;
    extrasHolder.size -= 4;

    // Write the sample data.
    buffer.ensureSpaceForWrite(sampleSize);
    readData(extrasHolder.offset, buffer.data, sampleSize);
    extrasHolder.offset += sampleSize;
    extrasHolder.size -= sampleSize;

    // Write the remaining data as supplemental data.
    buffer.resetSupplementalData(extrasHolder.size);
    readData(extrasHolder.offset, buffer.supplementalData, extrasHolder.size);
  } else {
    // Write the sample data.
    buffer.ensureSpaceForWrite(extrasHolder.size);
    readData(extrasHolder.offset, buffer.data, extrasHolder.size);
  }
}
 
Example 9
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 10
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 11
Source File: EventSampleStream.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 formatRequired) {
  if (formatRequired || !isFormatSentDownstream) {
    formatHolder.format = upstreamFormat;
    isFormatSentDownstream = true;
    return C.RESULT_FORMAT_READ;
  }
  if (currentIndex == eventTimesUs.length) {
    if (!eventStreamAppendable) {
      buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
      return C.RESULT_BUFFER_READ;
    } else {
      return C.RESULT_NOTHING_READ;
    }
  }
  int sampleIndex = currentIndex++;
  byte[] serializedEvent = eventMessageEncoder.encode(eventStream.events[sampleIndex]);
  if (serializedEvent != null) {
    buffer.ensureSpaceForWrite(serializedEvent.length);
    buffer.setFlags(C.BUFFER_FLAG_KEY_FRAME);
    buffer.data.put(serializedEvent);
    buffer.timeUs = eventTimesUs[sampleIndex];
    return C.RESULT_BUFFER_READ;
  } else {
    return C.RESULT_NOTHING_READ;
  }
}
 
Example 12
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 13
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 14
Source File: DefaultTrackOutput.java    From K-Sonic with MIT License 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.
 * @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 readData(FormatHolder formatHolder, DecoderInputBuffer buffer, boolean formatRequired,
    boolean loadingFinished, long decodeOnlyUntilUs) {
  int result = infoQueue.readData(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);
        }
        // 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);
        // Advance the read head.
        dropDownstreamTo(extrasHolder.nextOffset);
      }
      return C.RESULT_BUFFER_READ;
    case C.RESULT_NOTHING_READ:
      return C.RESULT_NOTHING_READ;
    default:
      throw new IllegalStateException();
  }
}
 
Example 15
Source File: EventSampleStream.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer,
    boolean formatRequired) {
  if (formatRequired || !isFormatSentDownstream) {
    formatHolder.format = upstreamFormat;
    isFormatSentDownstream = true;
    return C.RESULT_FORMAT_READ;
  }
  if (currentIndex == eventTimesUs.length) {
    if (!eventStreamUpdatable) {
      buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
      return C.RESULT_BUFFER_READ;
    } else {
      return C.RESULT_NOTHING_READ;
    }
  }
  int sampleIndex = currentIndex++;
  byte[] serializedEvent = eventMessageEncoder.encode(eventStream.events[sampleIndex],
      eventStream.timescale);
  if (serializedEvent != null) {
    buffer.ensureSpaceForWrite(serializedEvent.length);
    buffer.setFlags(C.BUFFER_FLAG_KEY_FRAME);
    buffer.data.put(serializedEvent);
    buffer.timeUs = eventTimesUs[sampleIndex];
    return C.RESULT_BUFFER_READ;
  } else {
    return C.RESULT_NOTHING_READ;
  }
}
 
Example 16
Source File: SampleQueue.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 the end of the stream has been reached, the
 *     {@link C#BUFFER_FLAG_END_OF_STREAM} flag will be set on the buffer.
 * @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);
        }
        // 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 17
Source File: EventSampleStream.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer,
    boolean formatRequired) {
  if (formatRequired || !isFormatSentDownstream) {
    formatHolder.format = upstreamFormat;
    isFormatSentDownstream = true;
    return C.RESULT_FORMAT_READ;
  }
  if (currentIndex == eventTimesUs.length) {
    if (!eventStreamUpdatable) {
      buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
      return C.RESULT_BUFFER_READ;
    } else {
      return C.RESULT_NOTHING_READ;
    }
  }
  int sampleIndex = currentIndex++;
  byte[] serializedEvent = eventMessageEncoder.encode(eventStream.events[sampleIndex],
      eventStream.timescale);
  if (serializedEvent != null) {
    buffer.ensureSpaceForWrite(serializedEvent.length);
    buffer.setFlags(C.BUFFER_FLAG_KEY_FRAME);
    buffer.data.put(serializedEvent);
    buffer.timeUs = eventTimesUs[sampleIndex];
    return C.RESULT_BUFFER_READ;
  } else {
    return C.RESULT_NOTHING_READ;
  }
}
 
Example 18
Source File: SampleQueue.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 the end of the stream has been reached, the
 *     {@link C#BUFFER_FLAG_END_OF_STREAM} flag will be set on the buffer.
 * @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);
        }
        // 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 19
Source File: EventSampleStream.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@Override
public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer,
    boolean formatRequired) {
  if (formatRequired || !isFormatSentDownstream) {
    formatHolder.format = upstreamFormat;
    isFormatSentDownstream = true;
    return C.RESULT_FORMAT_READ;
  }
  if (currentIndex == eventTimesUs.length) {
    if (!eventStreamAppendable) {
      buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
      return C.RESULT_BUFFER_READ;
    } else {
      return C.RESULT_NOTHING_READ;
    }
  }
  int sampleIndex = currentIndex++;
  byte[] serializedEvent = eventMessageEncoder.encode(eventStream.events[sampleIndex]);
  if (serializedEvent != null) {
    buffer.ensureSpaceForWrite(serializedEvent.length);
    buffer.data.put(serializedEvent);
    buffer.timeUs = eventTimesUs[sampleIndex];
    buffer.setFlags(C.BUFFER_FLAG_KEY_FRAME);
    return C.RESULT_BUFFER_READ;
  } else {
    return C.RESULT_NOTHING_READ;
  }
}
 
Example 20
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;
}