Search in sources :

Example 6 with DecoderInputBuffer

use of androidx.media3.decoder.DecoderInputBuffer in project media by androidx.

the class EventSampleStreamTest method skipDataThenUpdateStreamContinueToReadFromSkippedPosition.

/**
 * Tests that {@link EventSampleStream#updateEventStream(EventStream, boolean)} will update the
 * underlying event stream, but keep the timestamp the stream has skipped to, so the next {@link
 * EventSampleStream#readData(FormatHolder, DecoderInputBuffer, int)} call will return sample data
 * from the skipped position.
 */
@Test
public void skipDataThenUpdateStreamContinueToReadFromSkippedPosition() {
    long presentationTimeUs1 = 1000000;
    long presentationTimeUs2 = 2000000;
    long presentationTimeUs3 = 3000000;
    EventMessage eventMessage1 = newEventMessageWithId(1);
    EventMessage eventMessage2 = newEventMessageWithId(2);
    EventMessage eventMessage3 = newEventMessageWithId(3);
    EventStream eventStream1 = new EventStream(SCHEME_ID, VALUE, TIME_SCALE, new long[] { presentationTimeUs1, presentationTimeUs2 }, new EventMessage[] { eventMessage1, eventMessage2 });
    EventStream eventStream2 = new EventStream(SCHEME_ID, VALUE, TIME_SCALE, new long[] { presentationTimeUs1, presentationTimeUs2, presentationTimeUs3 }, new EventMessage[] { eventMessage1, eventMessage2, eventMessage3 });
    EventSampleStream sampleStream = new EventSampleStream(eventStream1, FORMAT, true);
    // first read - read format
    readData(sampleStream);
    sampleStream.skipData(presentationTimeUs2 + 1);
    sampleStream.updateEventStream(eventStream2, true);
    int result = readData(sampleStream);
    assertThat(result).isEqualTo(C.RESULT_BUFFER_READ);
    assertThat(inputBuffer.data.array()).isEqualTo(getEncodedMessage(eventMessage3));
}
Also used : EventMessage(androidx.media3.extractor.metadata.emsg.EventMessage) EventStream(androidx.media3.exoplayer.dash.manifest.EventStream) Test(org.junit.Test)

Example 7 with DecoderInputBuffer

use of androidx.media3.decoder.DecoderInputBuffer in project media by androidx.

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;
}
Also used : DecoderInputBuffer(androidx.media3.decoder.DecoderInputBuffer) Before(org.junit.Before)

Example 8 with DecoderInputBuffer

use of androidx.media3.decoder.DecoderInputBuffer in project media by androidx.

the class DecoderVideoRendererTest method setUp.

@Before
public void setUp() {
    surface = new Surface(new SurfaceTexture(/* texName= */
    0));
    renderer = new DecoderVideoRenderer(/* allowedJoiningTimeMs= */
    0, new Handler(), eventListener, /* maxDroppedFramesToNotify= */
    -1) {

        private final Phaser inputBuffersInCodecPhaser = new Phaser();

        @C.VideoOutputMode
        private int outputMode;

        @Override
        public String getName() {
            return "TestVideoRenderer";
        }

        @Override
        @Capabilities
        public int supportsFormat(Format format) {
            return RendererCapabilities.create(C.FORMAT_HANDLED);
        }

        @Override
        protected void setDecoderOutputMode(@C.VideoOutputMode int outputMode) {
            this.outputMode = outputMode;
        }

        @Override
        protected void renderOutputBufferToSurface(VideoDecoderOutputBuffer outputBuffer, Surface surface) {
        // Do nothing.
        }

        @Override
        protected void onQueueInputBuffer(DecoderInputBuffer buffer) {
            // Decoding is done on a background thread we have no control about from the test.
            // Ensure the background calls are predictably serialized by waiting for them to finish:
            // 1. Register queued input buffers here.
            // 2. Deregister the input buffer when it's cleared. If an input buffer is cleared it
            // will have been fully handled by the decoder.
            // 3. Send a message on the test thread to wait for all currently pending input buffers
            // to be cleared.
            // 4. The tests need to call ShadowLooper.idleMainThread() to execute the wait message
            // sent in step (3).
            int currentPhase = inputBuffersInCodecPhaser.register();
            new Handler().post(() -> inputBuffersInCodecPhaser.awaitAdvance(currentPhase));
            super.onQueueInputBuffer(buffer);
        }

        @Override
        protected SimpleDecoder<DecoderInputBuffer, ? extends VideoDecoderOutputBuffer, ? extends DecoderException> createDecoder(Format format, @Nullable CryptoConfig cryptoConfig) {
            return new SimpleDecoder<DecoderInputBuffer, VideoDecoderOutputBuffer, DecoderException>(new DecoderInputBuffer[10], new VideoDecoderOutputBuffer[10]) {

                @Override
                protected DecoderInputBuffer createInputBuffer() {
                    return new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_DIRECT) {

                        @Override
                        public void clear() {
                            super.clear();
                            inputBuffersInCodecPhaser.arriveAndDeregister();
                        }
                    };
                }

                @Override
                protected VideoDecoderOutputBuffer createOutputBuffer() {
                    return new VideoDecoderOutputBuffer(this::releaseOutputBuffer);
                }

                @Override
                protected DecoderException createUnexpectedDecodeException(Throwable error) {
                    return new DecoderException("error", error);
                }

                @Nullable
                @Override
                protected DecoderException decode(DecoderInputBuffer inputBuffer, VideoDecoderOutputBuffer outputBuffer, boolean reset) {
                    outputBuffer.init(inputBuffer.timeUs, outputMode, /* supplementalData= */
                    null);
                    return null;
                }

                @Override
                public String getName() {
                    return "TestDecoder";
                }
            };
        }
    };
    renderer.setOutput(surface);
}
Also used : SimpleDecoder(androidx.media3.decoder.SimpleDecoder) Handler(android.os.Handler) Surface(android.view.Surface) VideoDecoderOutputBuffer(androidx.media3.decoder.VideoDecoderOutputBuffer) DecoderException(androidx.media3.decoder.DecoderException) SurfaceTexture(android.graphics.SurfaceTexture) Format(androidx.media3.common.Format) DecoderInputBuffer(androidx.media3.decoder.DecoderInputBuffer) RendererCapabilities(androidx.media3.exoplayer.RendererCapabilities) Phaser(java.util.concurrent.Phaser) CryptoConfig(androidx.media3.decoder.CryptoConfig) Nullable(androidx.annotation.Nullable) Before(org.junit.Before)

Example 9 with DecoderInputBuffer

use of androidx.media3.decoder.DecoderInputBuffer in project media by androidx.

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);
}
Also used : ExoTrackSelection(androidx.media3.exoplayer.trackselection.ExoTrackSelection) DecoderInputBuffer(androidx.media3.decoder.DecoderInputBuffer) FormatHolder(androidx.media3.exoplayer.FormatHolder) FixedTrackSelection(androidx.media3.exoplayer.trackselection.FixedTrackSelection) Test(org.junit.Test)

Example 10 with DecoderInputBuffer

use of androidx.media3.decoder.DecoderInputBuffer in project media by androidx.

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(androidx.media3.exoplayer.trackselection.ExoTrackSelection) DecoderInputBuffer(androidx.media3.decoder.DecoderInputBuffer) FormatHolder(androidx.media3.exoplayer.FormatHolder) FixedTrackSelection(androidx.media3.exoplayer.trackselection.FixedTrackSelection) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)13 Nullable (androidx.annotation.Nullable)10 EventStream (androidx.media3.exoplayer.dash.manifest.EventStream)9 DecoderInputBuffer (androidx.media3.decoder.DecoderInputBuffer)8 EventMessage (androidx.media3.extractor.metadata.emsg.EventMessage)8 DecoderInputBuffer (com.google.android.exoplayer2.decoder.DecoderInputBuffer)8 Format (androidx.media3.common.Format)6 Before (org.junit.Before)6 FormatHolder (androidx.media3.exoplayer.FormatHolder)5 RequiresNonNull (org.checkerframework.checker.nullness.qual.RequiresNonNull)4 CryptoInfo (androidx.media3.decoder.CryptoInfo)3 ReadDataResult (androidx.media3.exoplayer.source.SampleStream.ReadDataResult)3 FormatHolder (com.google.android.exoplayer2.FormatHolder)3 ByteBuffer (java.nio.ByteBuffer)3 SurfaceTexture (android.graphics.SurfaceTexture)2 Handler (android.os.Handler)2 Surface (android.view.Surface)2 CryptoException (androidx.media3.decoder.CryptoException)2 ExoTrackSelection (androidx.media3.exoplayer.trackselection.ExoTrackSelection)2 FixedTrackSelection (androidx.media3.exoplayer.trackselection.FixedTrackSelection)2