Search in sources :

Example 26 with FormatHolder

use of com.google.android.exoplayer2.FormatHolder in project ExoPlayer by google.

the class MergingMediaPeriodTest method selectTracks_withPeriodOffsets_selectTracksWithOffset_andCreatesSampleStreamsCorrectingOffset.

@Test
public void selectTracks_withPeriodOffsets_selectTracksWithOffset_andCreatesSampleStreamsCorrectingOffset() throws Exception {
    MergingMediaPeriod mergingMediaPeriod = prepareMergingPeriod(new MergingPeriodDefinition(/* timeOffsetUs= */
    0, /* singleSampleTimeUs= */
    123_000, childFormat11, childFormat12), new MergingPeriodDefinition(/* timeOffsetUs= */
    -3000, /* singleSampleTimeUs= */
    456_000, childFormat21, childFormat22));
    ExoTrackSelection selectionForChild1 = new FixedTrackSelection(mergingMediaPeriod.getTrackGroups().get(0), /* track= */
    0);
    ExoTrackSelection selectionForChild2 = new FixedTrackSelection(mergingMediaPeriod.getTrackGroups().get(2), /* track= */
    0);
    SampleStream[] streams = new SampleStream[2];
    mergingMediaPeriod.selectTracks(/* selections= */
    new ExoTrackSelection[] { selectionForChild1, selectionForChild2 }, /* mayRetainStreamFlags= */
    new boolean[] { false, false }, streams, /* streamResetFlags= */
    new boolean[] { false, false }, /* positionUs= */
    0);
    mergingMediaPeriod.continueLoading(/* positionUs= */
    0);
    FormatHolder formatHolder = new FormatHolder();
    DecoderInputBuffer inputBuffer = new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);
    streams[0].readData(formatHolder, inputBuffer, FLAG_REQUIRE_FORMAT);
    streams[1].readData(formatHolder, inputBuffer, FLAG_REQUIRE_FORMAT);
    FakeMediaPeriodWithSelectTracksPosition childMediaPeriod1 = (FakeMediaPeriodWithSelectTracksPosition) mergingMediaPeriod.getChildPeriod(0);
    assertThat(childMediaPeriod1.selectTracksPositionUs).isEqualTo(0);
    assertThat(streams[0].readData(formatHolder, inputBuffer, /* readFlags= */
    0)).isEqualTo(C.RESULT_BUFFER_READ);
    assertThat(inputBuffer.timeUs).isEqualTo(123_000L);
    FakeMediaPeriodWithSelectTracksPosition childMediaPeriod2 = (FakeMediaPeriodWithSelectTracksPosition) mergingMediaPeriod.getChildPeriod(1);
    assertThat(childMediaPeriod2.selectTracksPositionUs).isEqualTo(3000L);
    assertThat(streams[1].readData(formatHolder, inputBuffer, /* readFlags= */
    0)).isEqualTo(C.RESULT_BUFFER_READ);
    assertThat(inputBuffer.timeUs).isEqualTo(456_000 - 3000);
}
Also used : ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection) DecoderInputBuffer(com.google.android.exoplayer2.decoder.DecoderInputBuffer) FormatHolder(com.google.android.exoplayer2.FormatHolder) FixedTrackSelection(com.google.android.exoplayer2.trackselection.FixedTrackSelection) Test(org.junit.Test)

Example 27 with FormatHolder

use of com.google.android.exoplayer2.FormatHolder in project ExoPlayer by google.

the class DecoderVideoRenderer method onInputFormatChanged.

/**
 * Called when a new format is read from the upstream source.
 *
 * @param formatHolder A {@link FormatHolder} that holds the new {@link Format}.
 * @throws ExoPlaybackException If an error occurs (re-)initializing the decoder.
 */
@CallSuper
protected void onInputFormatChanged(FormatHolder formatHolder) throws ExoPlaybackException {
    waitingForFirstSampleInFormat = true;
    Format newFormat = Assertions.checkNotNull(formatHolder.format);
    setSourceDrmSession(formatHolder.drmSession);
    Format oldFormat = inputFormat;
    inputFormat = newFormat;
    if (decoder == null) {
        maybeInitDecoder();
        eventDispatcher.inputFormatChanged(inputFormat, /* decoderReuseEvaluation= */
        null);
        return;
    }
    DecoderReuseEvaluation evaluation;
    if (sourceDrmSession != decoderDrmSession) {
        evaluation = new DecoderReuseEvaluation(decoder.getName(), oldFormat, newFormat, REUSE_RESULT_NO, DISCARD_REASON_DRM_SESSION_CHANGED);
    } else {
        evaluation = canReuseDecoder(decoder.getName(), oldFormat, newFormat);
    }
    if (evaluation.result == REUSE_RESULT_NO) {
        if (decoderReceivedBuffers) {
            // Signal end of stream and wait for any final output buffers before re-initialization.
            decoderReinitializationState = REINITIALIZATION_STATE_SIGNAL_END_OF_STREAM;
        } else {
            // There aren't any final output buffers, so release the decoder immediately.
            releaseDecoder();
            maybeInitDecoder();
        }
    }
    eventDispatcher.inputFormatChanged(inputFormat, evaluation);
}
Also used : Format(com.google.android.exoplayer2.Format) DecoderReuseEvaluation(com.google.android.exoplayer2.decoder.DecoderReuseEvaluation) CallSuper(androidx.annotation.CallSuper)

Example 28 with FormatHolder

use of com.google.android.exoplayer2.FormatHolder in project ExoPlayer by google.

the class DecoderVideoRenderer method feedInputBuffer.

private boolean feedInputBuffer() throws DecoderException, ExoPlaybackException {
    if (decoder == null || decoderReinitializationState == REINITIALIZATION_STATE_WAIT_END_OF_STREAM || inputStreamEnded) {
        // We need to reinitialize the decoder or the input stream has ended.
        return false;
    }
    if (inputBuffer == null) {
        inputBuffer = decoder.dequeueInputBuffer();
        if (inputBuffer == null) {
            return false;
        }
    }
    if (decoderReinitializationState == REINITIALIZATION_STATE_SIGNAL_END_OF_STREAM) {
        inputBuffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
        decoder.queueInputBuffer(inputBuffer);
        inputBuffer = null;
        decoderReinitializationState = REINITIALIZATION_STATE_WAIT_END_OF_STREAM;
        return false;
    }
    FormatHolder formatHolder = getFormatHolder();
    switch(readSource(formatHolder, inputBuffer, /* readFlags= */
    0)) {
        case C.RESULT_NOTHING_READ:
            return false;
        case C.RESULT_FORMAT_READ:
            onInputFormatChanged(formatHolder);
            return true;
        case C.RESULT_BUFFER_READ:
            if (inputBuffer.isEndOfStream()) {
                inputStreamEnded = true;
                decoder.queueInputBuffer(inputBuffer);
                inputBuffer = null;
                return false;
            }
            if (waitingForFirstSampleInFormat) {
                formatQueue.add(inputBuffer.timeUs, inputFormat);
                waitingForFirstSampleInFormat = false;
            }
            inputBuffer.flip();
            inputBuffer.format = inputFormat;
            onQueueInputBuffer(inputBuffer);
            decoder.queueInputBuffer(inputBuffer);
            buffersInCodecCount++;
            decoderReceivedBuffers = true;
            decoderCounters.queuedInputBufferCount++;
            inputBuffer = null;
            return true;
        default:
            throw new IllegalStateException();
    }
}
Also used : FormatHolder(com.google.android.exoplayer2.FormatHolder)

Example 29 with FormatHolder

use of com.google.android.exoplayer2.FormatHolder in project ExoPlayer by google.

the class DecoderVideoRenderer method render.

// BaseRenderer implementation.
@Override
public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
    if (outputStreamEnded) {
        return;
    }
    if (inputFormat == null) {
        // We don't have a format yet, so try and read one.
        FormatHolder formatHolder = getFormatHolder();
        flagsOnlyBuffer.clear();
        @ReadDataResult int result = readSource(formatHolder, flagsOnlyBuffer, FLAG_REQUIRE_FORMAT);
        if (result == C.RESULT_FORMAT_READ) {
            onInputFormatChanged(formatHolder);
        } else if (result == C.RESULT_BUFFER_READ) {
            // End of stream read having not read a format.
            Assertions.checkState(flagsOnlyBuffer.isEndOfStream());
            inputStreamEnded = true;
            outputStreamEnded = true;
            return;
        } else {
            // We still don't have a format and can't make progress without one.
            return;
        }
    }
    // If we don't have a decoder yet, we need to instantiate one.
    maybeInitDecoder();
    if (decoder != null) {
        try {
            // Rendering loop.
            TraceUtil.beginSection("drainAndFeed");
            while (drainOutputBuffer(positionUs, elapsedRealtimeUs)) {
            }
            while (feedInputBuffer()) {
            }
            TraceUtil.endSection();
        } catch (DecoderException e) {
            Log.e(TAG, "Video codec error", e);
            eventDispatcher.videoCodecError(e);
            throw createRendererException(e, inputFormat, PlaybackException.ERROR_CODE_DECODING_FAILED);
        }
        decoderCounters.ensureUpdated();
    }
}
Also used : DecoderException(com.google.android.exoplayer2.decoder.DecoderException) ReadDataResult(com.google.android.exoplayer2.source.SampleStream.ReadDataResult) FormatHolder(com.google.android.exoplayer2.FormatHolder)

Example 30 with FormatHolder

use of com.google.android.exoplayer2.FormatHolder in project ExoPlayer by google.

the class MediaCodecVideoRenderer method onInputFormatChanged.

@Override
@Nullable
protected DecoderReuseEvaluation onInputFormatChanged(FormatHolder formatHolder) throws ExoPlaybackException {
    @Nullable DecoderReuseEvaluation evaluation = super.onInputFormatChanged(formatHolder);
    eventDispatcher.inputFormatChanged(formatHolder.format, evaluation);
    return evaluation;
}
Also used : DecoderReuseEvaluation(com.google.android.exoplayer2.decoder.DecoderReuseEvaluation) Nullable(androidx.annotation.Nullable) Nullable(androidx.annotation.Nullable)

Aggregations

FormatHolder (com.google.android.exoplayer2.FormatHolder)16 Test (org.junit.Test)13 ReadDataResult (com.google.android.exoplayer2.source.SampleStream.ReadDataResult)11 Format (com.google.android.exoplayer2.Format)10 EventStream (com.google.android.exoplayer2.source.dash.manifest.EventStream)9 EventMessage (com.google.android.exoplayer2.metadata.emsg.EventMessage)8 Nullable (androidx.annotation.Nullable)7 DecoderReuseEvaluation (com.google.android.exoplayer2.decoder.DecoderReuseEvaluation)5 DecoderInputBuffer (com.google.android.exoplayer2.decoder.DecoderInputBuffer)4 DrmSession (com.google.android.exoplayer2.drm.DrmSession)4 CallSuper (androidx.annotation.CallSuper)2 DecoderException (com.google.android.exoplayer2.decoder.DecoderException)2 ExoTrackSelection (com.google.android.exoplayer2.trackselection.ExoTrackSelection)2 FixedTrackSelection (com.google.android.exoplayer2.trackselection.FixedTrackSelection)2 Before (org.junit.Before)2 CryptoException (android.media.MediaCodec.CryptoException)1 MediaCryptoException (android.media.MediaCryptoException)1 MediaFormat (android.media.MediaFormat)1 InsufficientCapacityException (com.google.android.exoplayer2.decoder.DecoderInputBuffer.InsufficientCapacityException)1 DecoderDiscardReasons (com.google.android.exoplayer2.decoder.DecoderReuseEvaluation.DecoderDiscardReasons)1