Search in sources :

Example 6 with END_OF_STREAM_ITEM

use of androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project media by androidx.

the class MediaCodecVideoRendererTest method render_sendsVideoSizeChangeWithCurrentFormatValues.

@Test
public void render_sendsVideoSizeChangeWithCurrentFormatValues() throws Exception {
    FakeSampleStream fakeSampleStream = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    VIDEO_H264, ImmutableList.of(oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    mediaCodecVideoRenderer.enable(RendererConfiguration.DEFAULT, new Format[] { VIDEO_H264 }, fakeSampleStream, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    true, /* startPositionUs= */
    0, /* offsetUs */
    0);
    mediaCodecVideoRenderer.setCurrentStreamFinal();
    mediaCodecVideoRenderer.start();
    int positionUs = 0;
    do {
        mediaCodecVideoRenderer.render(positionUs, SystemClock.elapsedRealtime() * 1000);
        positionUs += 10;
    } while (!mediaCodecVideoRenderer.isEnded());
    shadowOf(testMainLooper).idle();
    verify(eventListener).onVideoSizeChanged(new VideoSize(VIDEO_H264.width, VIDEO_H264.height, VIDEO_H264.rotationDegrees, VIDEO_H264.pixelWidthHeightRatio));
}
Also used : FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) VideoSize(androidx.media3.common.VideoSize) DefaultAllocator(androidx.media3.exoplayer.upstream.DefaultAllocator) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Test(org.junit.Test)

Example 7 with END_OF_STREAM_ITEM

use of androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project media by androidx.

the class MediaCodecVideoRendererTest method replaceStream_rendersFirstFrameOnlyAfterStartPosition.

@Test
public void replaceStream_rendersFirstFrameOnlyAfterStartPosition() throws Exception {
    ShadowLooper shadowLooper = shadowOf(testMainLooper);
    FakeSampleStream fakeSampleStream1 = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    VIDEO_H264, ImmutableList.of(oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
    fakeSampleStream1.writeData(/* startPositionUs= */
    0);
    FakeSampleStream fakeSampleStream2 = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    VIDEO_H264, ImmutableList.of(oneByteSample(/* timeUs= */
    1_000_000, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
    fakeSampleStream2.writeData(/* startPositionUs= */
    0);
    mediaCodecVideoRenderer.enable(RendererConfiguration.DEFAULT, new Format[] { VIDEO_H264 }, fakeSampleStream1, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    true, /* startPositionUs= */
    0, /* offsetUs */
    0);
    mediaCodecVideoRenderer.start();
    boolean replacedStream = false;
    for (int i = 0; i <= 10; i++) {
        mediaCodecVideoRenderer.render(/* positionUs= */
        i * 10, SystemClock.elapsedRealtime() * 1000);
        if (!replacedStream && mediaCodecVideoRenderer.hasReadStreamToEnd()) {
            mediaCodecVideoRenderer.replaceStream(new Format[] { VIDEO_H264 }, fakeSampleStream2, /* startPositionUs= */
            100, /* offsetUs= */
            100);
            replacedStream = true;
        }
    }
    // Expect only the first frame of the first stream to have been rendered.
    shadowLooper.idle();
    verify(eventListener, times(2)).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
    anyLong());
}
Also used : FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) ShadowLooper(org.robolectric.shadows.ShadowLooper) DefaultAllocator(androidx.media3.exoplayer.upstream.DefaultAllocator) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Test(org.junit.Test)

Example 8 with END_OF_STREAM_ITEM

use of androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project media by androidx.

the class MediaCodecVideoRendererTest method render_withMultipleQueued_sendsVideoSizeChangedWithCorrectPixelAspectRatioWhenMultipleQueued.

@Test
public void render_withMultipleQueued_sendsVideoSizeChangedWithCorrectPixelAspectRatioWhenMultipleQueued() throws Exception {
    Format pAsp1 = VIDEO_H264.buildUpon().setPixelWidthHeightRatio(1f).build();
    Format pAsp2 = VIDEO_H264.buildUpon().setPixelWidthHeightRatio(2f).build();
    Format pAsp3 = VIDEO_H264.buildUpon().setPixelWidthHeightRatio(3f).build();
    FakeSampleStream fakeSampleStream = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    pAsp1, ImmutableList.of(oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME)));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    mediaCodecVideoRenderer.enable(RendererConfiguration.DEFAULT, new Format[] { pAsp1, pAsp2, pAsp3 }, fakeSampleStream, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    false, /* startPositionUs= */
    0, /* offsetUs */
    0);
    mediaCodecVideoRenderer.start();
    mediaCodecVideoRenderer.render(/* positionUs= */
    0, SystemClock.elapsedRealtime() * 1000);
    mediaCodecVideoRenderer.render(/* positionUs= */
    250, SystemClock.elapsedRealtime() * 1000);
    fakeSampleStream.append(ImmutableList.of(format(pAsp2), oneByteSample(/* timeUs= */
    5_000), oneByteSample(/* timeUs= */
    10_000), format(pAsp3), oneByteSample(/* timeUs= */
    15_000), oneByteSample(/* timeUs= */
    20_000), END_OF_STREAM_ITEM));
    fakeSampleStream.writeData(/* startPositionUs= */
    5_000);
    mediaCodecVideoRenderer.setCurrentStreamFinal();
    int pos = 500;
    do {
        mediaCodecVideoRenderer.render(/* positionUs= */
        pos, SystemClock.elapsedRealtime() * 1000);
        pos += 250;
    } while (!mediaCodecVideoRenderer.isEnded());
    shadowOf(testMainLooper).idle();
    ArgumentCaptor<VideoSize> videoSizesCaptor = ArgumentCaptor.forClass(VideoSize.class);
    verify(eventListener, times(3)).onVideoSizeChanged(videoSizesCaptor.capture());
    assertThat(videoSizesCaptor.getAllValues().stream().map(videoSize -> videoSize.pixelWidthHeightRatio).collect(Collectors.toList())).containsExactly(1f, 2f, 3f);
}
Also used : FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) CodecProfileLevel(android.media.MediaCodecInfo.CodecProfileLevel) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) ApplicationProvider(androidx.test.core.app.ApplicationProvider) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) MediaCodecInfo(androidx.media3.exoplayer.mediacodec.MediaCodecInfo) END_OF_STREAM_ITEM(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM) Renderer(androidx.media3.exoplayer.Renderer) Handler(android.os.Handler) Looper(android.os.Looper) After(org.junit.After) CodecCapabilities(android.media.MediaCodecInfo.CodecCapabilities) RendererConfiguration(androidx.media3.exoplayer.RendererConfiguration) Surface(android.view.Surface) VideoSize(androidx.media3.common.VideoSize) FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) Collectors(java.util.stream.Collectors) Format(androidx.media3.common.Format) MediaCodecSelector(androidx.media3.exoplayer.mediacodec.MediaCodecSelector) Nullable(androidx.annotation.Nullable) FakeSampleStreamItem.format(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.format) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) MediaFormat(android.media.MediaFormat) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) SystemClock(android.os.SystemClock) Shadows.shadowOf(org.robolectric.Shadows.shadowOf) RendererCapabilities(androidx.media3.exoplayer.RendererCapabilities) Capabilities(androidx.media3.exoplayer.RendererCapabilities.Capabilities) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) MockitoJUnit(org.mockito.junit.MockitoJUnit) Before(org.junit.Before) SurfaceTexture(android.graphics.SurfaceTexture) ShadowLooper(org.robolectric.shadows.ShadowLooper) MimeTypes(androidx.media3.common.MimeTypes) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) DefaultAllocator(androidx.media3.exoplayer.upstream.DefaultAllocator) C(androidx.media3.common.C) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) FakeSampleStreamItem.oneByteSample(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.oneByteSample) PlayerId(androidx.media3.exoplayer.analytics.PlayerId) MockitoRule(org.mockito.junit.MockitoRule) Collections(java.util.Collections) Format(androidx.media3.common.Format) MediaFormat(android.media.MediaFormat) VideoSize(androidx.media3.common.VideoSize) DefaultAllocator(androidx.media3.exoplayer.upstream.DefaultAllocator) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Test(org.junit.Test)

Example 9 with END_OF_STREAM_ITEM

use of androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project media by androidx.

the class MergingMediaPeriodTest method prepareMergingPeriod.

private MergingMediaPeriod prepareMergingPeriod(MergingPeriodDefinition... definitions) throws Exception {
    MediaPeriod[] mediaPeriods = new MediaPeriod[definitions.length];
    long[] timeOffsetsUs = new long[definitions.length];
    for (int i = 0; i < definitions.length; i++) {
        MergingPeriodDefinition definition = definitions[i];
        timeOffsetsUs[i] = definition.timeOffsetUs;
        TrackGroup[] trackGroups = new TrackGroup[definition.formats.length];
        for (int j = 0; j < definition.formats.length; j++) {
            trackGroups[j] = new TrackGroup(definition.formats[j]);
        }
        mediaPeriods[i] = new FakeMediaPeriodWithSelectTracksPosition(new TrackGroupArray(trackGroups), new EventDispatcher().withParameters(/* windowIndex= */
        i, new MediaPeriodId(/* periodUid= */
        i), /* mediaTimeOffsetMs= */
        0), /* trackDataFactory= */
        (unusedFormat, unusedMediaPeriodId) -> ImmutableList.of(oneByteSample(definition.singleSampleTimeUs, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
    }
    MergingMediaPeriod mergingMediaPeriod = new MergingMediaPeriod(new DefaultCompositeSequenceableLoaderFactory(), timeOffsetsUs, mediaPeriods);
    CountDownLatch prepareCountDown = new CountDownLatch(1);
    mergingMediaPeriod.prepare(new MediaPeriod.Callback() {

        @Override
        public void onPrepared(MediaPeriod mediaPeriod) {
            prepareCountDown.countDown();
        }

        @Override
        public void onContinueLoadingRequested(MediaPeriod source) {
            mergingMediaPeriod.continueLoading(/* positionUs= */
            0);
        }
    }, /* positionUs= */
    0);
    prepareCountDown.await();
    return mergingMediaPeriod;
}
Also used : TrackGroup(androidx.media3.common.TrackGroup) ExoTrackSelection(androidx.media3.exoplayer.trackselection.ExoTrackSelection) RunWith(org.junit.runner.RunWith) EventDispatcher(androidx.media3.exoplayer.source.MediaSourceEventListener.EventDispatcher) FixedTrackSelection(androidx.media3.exoplayer.trackselection.FixedTrackSelection) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) TrackGroupArray(androidx.media3.common.TrackGroupArray) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Format(androidx.media3.common.Format) DefaultAllocator(androidx.media3.exoplayer.upstream.DefaultAllocator) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod) C(androidx.media3.common.C) DecoderInputBuffer(androidx.media3.decoder.DecoderInputBuffer) CountDownLatch(java.util.concurrent.CountDownLatch) END_OF_STREAM_ITEM(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM) FormatHolder(androidx.media3.exoplayer.FormatHolder) ImmutableList(com.google.common.collect.ImmutableList) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) FakeSampleStreamItem.oneByteSample(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.oneByteSample) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) FLAG_REQUIRE_FORMAT(androidx.media3.exoplayer.source.SampleStream.FLAG_REQUIRE_FORMAT) NullableType(org.checkerframework.checker.nullness.compatqual.NullableType) TrackGroupArray(androidx.media3.common.TrackGroupArray) CountDownLatch(java.util.concurrent.CountDownLatch) EventDispatcher(androidx.media3.exoplayer.source.MediaSourceEventListener.EventDispatcher) TrackGroup(androidx.media3.common.TrackGroup) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod)

Example 10 with END_OF_STREAM_ITEM

use of androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project media by androidx.

the class MediaCodecAudioRendererTest method render_throwsExoPlaybackExceptionJustOnce_whenSet.

@Test
public void render_throwsExoPlaybackExceptionJustOnce_whenSet() throws Exception {
    MediaCodecAudioRenderer exceptionThrowingRenderer = new MediaCodecAudioRenderer(ApplicationProvider.getApplicationContext(), mediaCodecSelector, /* eventHandler= */
    null, /* eventListener= */
    null) {

        @Override
        protected void onOutputFormatChanged(Format format, @Nullable MediaFormat mediaFormat) throws ExoPlaybackException {
            super.onOutputFormatChanged(format, mediaFormat);
            if (!format.equals(AUDIO_AAC)) {
                setPendingPlaybackException(ExoPlaybackException.createForRenderer(new AudioSink.ConfigurationException("Test", format), "rendererName", /* rendererIndex= */
                0, format, C.FORMAT_HANDLED, /* isRecoverable= */
                false, PlaybackException.ERROR_CODE_UNSPECIFIED));
            }
        }
    };
    Format changedFormat = AUDIO_AAC.buildUpon().setSampleRate(32_000).build();
    FakeSampleStream fakeSampleStream = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    AUDIO_AAC, ImmutableList.of(oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    exceptionThrowingRenderer.init(/* index= */
    0, PlayerId.UNSET);
    exceptionThrowingRenderer.enable(RendererConfiguration.DEFAULT, new Format[] { AUDIO_AAC, changedFormat }, fakeSampleStream, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    false, /* startPositionUs= */
    0, /* offsetUs */
    0);
    exceptionThrowingRenderer.start();
    exceptionThrowingRenderer.render(/* positionUs= */
    0, SystemClock.elapsedRealtime() * 1000);
    exceptionThrowingRenderer.render(/* positionUs= */
    250, SystemClock.elapsedRealtime() * 1000);
    MediaFormat mediaFormat = new MediaFormat();
    mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 2);
    mediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, 32_000);
    // Simulating the exception being thrown when not traceable back to render.
    exceptionThrowingRenderer.onOutputFormatChanged(changedFormat, mediaFormat);
    assertThrows(ExoPlaybackException.class, () -> exceptionThrowingRenderer.render(/* positionUs= */
    500, SystemClock.elapsedRealtime() * 1000));
    // Doesn't throw an exception because it's cleared after being thrown in the previous call to
    // render.
    exceptionThrowingRenderer.render(/* positionUs= */
    750, SystemClock.elapsedRealtime() * 1000);
}
Also used : MediaFormat(android.media.MediaFormat) FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) MediaFormat(android.media.MediaFormat) Format(androidx.media3.common.Format) DefaultAllocator(androidx.media3.exoplayer.upstream.DefaultAllocator) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 DrmSessionEventListener (androidx.media3.exoplayer.drm.DrmSessionEventListener)14 DefaultAllocator (androidx.media3.exoplayer.upstream.DefaultAllocator)14 FakeSampleStream (androidx.media3.test.utils.FakeSampleStream)13 Format (androidx.media3.common.Format)5 MediaFormat (android.media.MediaFormat)4 Timeline (androidx.media3.common.Timeline)3 MediaPeriodId (androidx.media3.exoplayer.source.MediaSource.MediaPeriodId)3 ShadowLooper (org.robolectric.shadows.ShadowLooper)3 Nullable (androidx.annotation.Nullable)2 C (androidx.media3.common.C)2 VideoSize (androidx.media3.common.VideoSize)2 DrmSessionManager (androidx.media3.exoplayer.drm.DrmSessionManager)2 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)2 END_OF_STREAM_ITEM (androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM)2 FakeSampleStreamItem.oneByteSample (androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.oneByteSample)2 FakeTimeline (androidx.media3.test.utils.FakeTimeline)2 TimelineWindowDefinition (androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition)2 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)2 ImmutableList (com.google.common.collect.ImmutableList)2