use of com.google.android.exoplayer2.decoder.DecoderInputBuffer in project ExoPlayer by google.
the class OpusDecoder method decode.
@Override
public OpusDecoderException decode(DecoderInputBuffer inputBuffer, SimpleOutputBuffer outputBuffer, boolean reset) {
if (reset) {
opusReset(nativeDecoderContext);
// When seeking to 0, skip number of samples as specified in opus header. When seeking to
// any other time, skip number of samples as specified by seek preroll.
skipSamples = (inputBuffer.timeUs == 0) ? headerSkipSamples : headerSeekPreRollSamples;
}
ByteBuffer inputData = inputBuffer.data;
CryptoInfo cryptoInfo = inputBuffer.cryptoInfo;
int result = inputBuffer.isEncrypted() ? opusSecureDecode(nativeDecoderContext, inputBuffer.timeUs, inputData, inputData.limit(), outputBuffer, SAMPLE_RATE, exoMediaCrypto, cryptoInfo.mode, cryptoInfo.key, cryptoInfo.iv, cryptoInfo.numSubSamples, cryptoInfo.numBytesOfClearData, cryptoInfo.numBytesOfEncryptedData) : opusDecode(nativeDecoderContext, inputBuffer.timeUs, inputData, inputData.limit(), outputBuffer, SAMPLE_RATE);
if (result < 0) {
if (result == DRM_ERROR) {
String message = "Drm error: " + opusGetErrorMessage(nativeDecoderContext);
DecryptionException cause = new DecryptionException(opusGetErrorCode(nativeDecoderContext), message);
return new OpusDecoderException(message, cause);
} else {
return new OpusDecoderException("Decode error: " + opusGetErrorMessage(result));
}
}
ByteBuffer outputData = outputBuffer.data;
outputData.position(0);
outputData.limit(result);
if (skipSamples > 0) {
int bytesPerSample = channelCount * 2;
int skipBytes = skipSamples * bytesPerSample;
if (result <= skipBytes) {
skipSamples -= result / bytesPerSample;
outputBuffer.addFlag(C.BUFFER_FLAG_DECODE_ONLY);
outputData.position(result);
} else {
skipSamples = 0;
outputData.position(skipBytes);
}
}
return null;
}
use of com.google.android.exoplayer2.decoder.DecoderInputBuffer in project ExoPlayer by google.
the class VpxDecoder method decode.
@Override
@Nullable
protected VpxDecoderException decode(DecoderInputBuffer inputBuffer, VideoDecoderOutputBuffer outputBuffer, boolean reset) {
if (reset && lastSupplementalData != null) {
// Don't propagate supplemental data across calls to flush the decoder.
lastSupplementalData.clear();
}
ByteBuffer inputData = Util.castNonNull(inputBuffer.data);
int inputSize = inputData.limit();
CryptoInfo cryptoInfo = inputBuffer.cryptoInfo;
final long result = inputBuffer.isEncrypted() ? vpxSecureDecode(vpxDecContext, inputData, inputSize, cryptoConfig, cryptoInfo.mode, Assertions.checkNotNull(cryptoInfo.key), Assertions.checkNotNull(cryptoInfo.iv), cryptoInfo.numSubSamples, cryptoInfo.numBytesOfClearData, cryptoInfo.numBytesOfEncryptedData) : vpxDecode(vpxDecContext, inputData, inputSize);
if (result != NO_ERROR) {
if (result == DRM_ERROR) {
String message = "Drm error: " + vpxGetErrorMessage(vpxDecContext);
CryptoException cause = new CryptoException(vpxGetErrorCode(vpxDecContext), message);
return new VpxDecoderException(message, cause);
} else {
return new VpxDecoderException("Decode error: " + vpxGetErrorMessage(vpxDecContext));
}
}
if (inputBuffer.hasSupplementalData()) {
ByteBuffer supplementalData = Assertions.checkNotNull(inputBuffer.supplementalData);
int size = supplementalData.remaining();
if (size > 0) {
if (lastSupplementalData == null || lastSupplementalData.capacity() < size) {
lastSupplementalData = ByteBuffer.allocate(size);
} else {
lastSupplementalData.clear();
}
lastSupplementalData.put(supplementalData);
lastSupplementalData.flip();
}
}
if (!inputBuffer.isDecodeOnly()) {
outputBuffer.init(inputBuffer.timeUs, outputMode, lastSupplementalData);
int getFrameResult = vpxGetFrame(vpxDecContext, outputBuffer);
if (getFrameResult == 1) {
outputBuffer.addFlag(C.BUFFER_FLAG_DECODE_ONLY);
} else if (getFrameResult == -1) {
return new VpxDecoderException("Buffer initialization failed.");
}
outputBuffer.format = inputBuffer.format;
}
return null;
}
use of com.google.android.exoplayer2.decoder.DecoderInputBuffer in project ExoPlayer by google.
the class SampleQueue method peekSampleMetadata.
// See comments in setUpstreamFormat
@SuppressWarnings("ReferenceEquality")
private synchronized int peekSampleMetadata(FormatHolder formatHolder, DecoderInputBuffer buffer, boolean formatRequired, boolean loadingFinished, SampleExtrasHolder extrasHolder) {
buffer.waitingForKeys = false;
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;
}
}
Format format = sharedSampleMetadata.get(getReadIndex()).format;
if (formatRequired || format != downstreamFormat) {
onFormatResult(format, formatHolder);
return C.RESULT_FORMAT_READ;
}
int relativeReadIndex = getRelativeIndex(readPosition);
if (!mayReadSample(relativeReadIndex)) {
buffer.waitingForKeys = true;
return C.RESULT_NOTHING_READ;
}
buffer.setFlags(flags[relativeReadIndex]);
buffer.timeUs = timesUs[relativeReadIndex];
if (buffer.timeUs < startTimeUs) {
buffer.addFlag(C.BUFFER_FLAG_DECODE_ONLY);
}
extrasHolder.size = sizes[relativeReadIndex];
extrasHolder.offset = offsets[relativeReadIndex];
extrasHolder.cryptoData = cryptoDatas[relativeReadIndex];
return C.RESULT_BUFFER_READ;
}
use of com.google.android.exoplayer2.decoder.DecoderInputBuffer in project ExoPlayer by google.
the class MergingMediaPeriodTest method selectTracks_createsSampleStreamsFromChildPeriods.
@Test
public void selectTracks_createsSampleStreamsFromChildPeriods() throws Exception {
MergingMediaPeriod mergingMediaPeriod = prepareMergingPeriod(new MergingPeriodDefinition(/* timeOffsetUs= */
0, /* singleSampleTimeUs= */
0, childFormat11, childFormat12), new MergingPeriodDefinition(/* timeOffsetUs= */
0, /* singleSampleTimeUs= */
0, childFormat21, childFormat22));
ExoTrackSelection selectionForChild1 = new FixedTrackSelection(mergingMediaPeriod.getTrackGroups().get(1), /* track= */
0);
ExoTrackSelection selectionForChild2 = new FixedTrackSelection(mergingMediaPeriod.getTrackGroups().get(2), /* track= */
0);
SampleStream[] streams = new SampleStream[4];
mergingMediaPeriod.selectTracks(/* selections= */
new ExoTrackSelection[] { null, selectionForChild1, selectionForChild2, null }, /* mayRetainStreamFlags= */
new boolean[] { false, false, false, false }, streams, /* streamResetFlags= */
new boolean[] { false, false, false, false }, /* positionUs= */
0);
mergingMediaPeriod.continueLoading(/* positionUs= */
0);
assertThat(streams[0]).isNull();
assertThat(streams[3]).isNull();
FormatHolder formatHolder = new FormatHolder();
DecoderInputBuffer inputBuffer = new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);
assertThat(streams[1].readData(formatHolder, inputBuffer, FLAG_REQUIRE_FORMAT)).isEqualTo(C.RESULT_FORMAT_READ);
assertThat(formatHolder.format).isEqualTo(childFormat12);
assertThat(streams[2].readData(formatHolder, inputBuffer, FLAG_REQUIRE_FORMAT)).isEqualTo(C.RESULT_FORMAT_READ);
assertThat(formatHolder.format).isEqualTo(childFormat21);
}
use of com.google.android.exoplayer2.decoder.DecoderInputBuffer in project ExoPlayer by google.
the class C2Mp3TimestampTrackerTest method setUp.
@Before
public void setUp() {
timestampTracker = new C2Mp3TimestampTracker();
buffer = new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_DISABLED);
buffer.data = ByteBuffer.wrap(createByteArray(0xFF, 0xFB, 0xE8, 0x3C));
buffer.timeUs = 100_000;
invalidBuffer = new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_DISABLED);
invalidBuffer.data = ByteBuffer.wrap(createByteArray(0, 0, 0, 0));
invalidBuffer.timeUs = 120_000;
}
Aggregations