Search in sources :

Example 1 with END_OF_STREAM_ITEM

use of com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project ExoPlayer by google.

the class DefaultAnalyticsCollectorTest method adPlayback.

@Test
public void adPlayback() throws Exception {
    long contentDurationsUs = 11 * C.MICROS_PER_SECOND;
    long windowOffsetInFirstPeriodUs = TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US;
    AtomicReference<AdPlaybackState> adPlaybackState = new AtomicReference<>(FakeTimeline.createAdPlaybackState(/* adsPerAdGroup= */
    1, /* adGroupTimesUs...= */
    windowOffsetInFirstPeriodUs, windowOffsetInFirstPeriodUs + 5 * C.MICROS_PER_SECOND, C.TIME_END_OF_SOURCE));
    AtomicInteger playedAdCount = new AtomicInteger(0);
    Timeline adTimeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    0, /* isSeekable= */
    true, /* isDynamic= */
    false, contentDurationsUs, adPlaybackState.get()));
    FakeMediaSource fakeMediaSource = new FakeMediaSource(adTimeline, DrmSessionManager.DRM_UNSUPPORTED, (unusedFormat, mediaPeriodId) -> {
        if (mediaPeriodId.isAd()) {
            return ImmutableList.of(oneByteSample(/* timeUs= */
            0, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM);
        } else {
            // postroll.
            return ImmutableList.of(oneByteSample(windowOffsetInFirstPeriodUs + C.MICROS_PER_SECOND, C.BUFFER_FLAG_KEY_FRAME), oneByteSample(windowOffsetInFirstPeriodUs + 6 * C.MICROS_PER_SECOND, C.BUFFER_FLAG_KEY_FRAME), oneByteSample(windowOffsetInFirstPeriodUs + contentDurationsUs, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM);
        }
    }, ExoPlayerTestRunner.VIDEO_FORMAT);
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            player.addListener(new Player.Listener() {

                @Override
                public void onPositionDiscontinuity(Player.PositionInfo oldPosition, Player.PositionInfo newPosition, @Player.DiscontinuityReason int reason) {
                    if (!player.isPlayingAd() && reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) {
                        // Finished playing ad. Marked as played.
                        adPlaybackState.set(adPlaybackState.get().withPlayedAd(/* adGroupIndex= */
                        playedAdCount.getAndIncrement(), /* adIndexInAdGroup= */
                        0));
                        fakeMediaSource.setNewSourceInfo(new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
                        1, /* id= */
                        0, /* isSeekable= */
                        true, /* isDynamic= */
                        false, contentDurationsUs, adPlaybackState.get())), /* sendManifestLoadEvents= */
                        false);
                    }
                }
            });
        }
    }).pause().waitForIsLoading(true).waitForIsLoading(false).waitForPlaybackState(Player.STATE_READY).playUntilPosition(/* mediaItemIndex= */
    0, /* positionMs= */
    3_000).waitForPendingPlayerCommands().playUntilPosition(/* mediaItemIndex= */
    0, /* positionMs= */
    8_000).waitForPendingPlayerCommands().play().waitForPlaybackState(Player.STATE_ENDED).waitForTimelineChanged().build();
    TestAnalyticsListener listener = runAnalyticsTest(fakeMediaSource, actionSchedule);
    Object periodUid = listener.lastReportedTimeline.getUidOfPeriod(/* periodIndex= */
    0);
    EventWindowAndPeriodId prerollAd = new EventWindowAndPeriodId(/* windowIndex= */
    0, new MediaPeriodId(periodUid, /* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0, /* windowSequenceNumber= */
    0));
    EventWindowAndPeriodId midrollAd = new EventWindowAndPeriodId(/* windowIndex= */
    0, new MediaPeriodId(periodUid, /* adGroupIndex= */
    1, /* adIndexInAdGroup= */
    0, /* windowSequenceNumber= */
    0));
    EventWindowAndPeriodId postrollAd = new EventWindowAndPeriodId(/* windowIndex= */
    0, new MediaPeriodId(periodUid, /* adGroupIndex= */
    2, /* adIndexInAdGroup= */
    0, /* windowSequenceNumber= */
    0));
    EventWindowAndPeriodId contentAfterPreroll = new EventWindowAndPeriodId(/* windowIndex= */
    0, new MediaPeriodId(periodUid, /* windowSequenceNumber= */
    0, /* nextAdGroupIndex= */
    1));
    EventWindowAndPeriodId contentAfterMidroll = new EventWindowAndPeriodId(/* windowIndex= */
    0, new MediaPeriodId(periodUid, /* windowSequenceNumber= */
    0, /* nextAdGroupIndex= */
    2));
    EventWindowAndPeriodId contentAfterPostroll = new EventWindowAndPeriodId(/* windowIndex= */
    0, new MediaPeriodId(periodUid, /* windowSequenceNumber= */
    0, /* nextAdGroupIndex= */
    C.INDEX_UNSET));
    assertThat(listener.getEvents(EVENT_PLAYER_STATE_CHANGED)).containsExactly(WINDOW_0, /* setPlayWhenReady=true */
    WINDOW_0, /* setPlayWhenReady=false */
    WINDOW_0, /* BUFFERING */
    prerollAd, /* READY */
    prerollAd, /* setPlayWhenReady=true */
    contentAfterPreroll, /* setPlayWhenReady=false */
    contentAfterPreroll, /* setPlayWhenReady=true */
    contentAfterMidroll, /* setPlayWhenReady=false */
    contentAfterMidroll, /* setPlayWhenReady=true */
    contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_TIMELINE_CHANGED)).containsExactly(WINDOW_0, /* PLAYLIST_CHANGED */
    prerollAd, /* SOURCE_UPDATE (initial) */
    contentAfterPreroll, /* SOURCE_UPDATE (played preroll) */
    contentAfterMidroll, /* SOURCE_UPDATE (played midroll) */
    contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_POSITION_DISCONTINUITY)).containsExactly(contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_IS_LOADING_CHANGED)).containsExactly(prerollAd, prerollAd).inOrder();
    assertThat(listener.getEvents(EVENT_TRACKS_CHANGED)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_LOAD_STARTED)).containsExactly(WINDOW_0, /* content manifest */
    prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_LOAD_COMPLETED)).containsExactly(WINDOW_0, /* content manifest */
    prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_DOWNSTREAM_FORMAT_CHANGED)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_DECODER_ENABLED)).containsExactly(prerollAd);
    assertThat(listener.getEvents(EVENT_DECODER_INIT)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_DECODER_FORMAT_CHANGED)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_VIDEO_ENABLED)).containsExactly(prerollAd);
    assertThat(listener.getEvents(EVENT_VIDEO_DECODER_INITIALIZED)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_VIDEO_INPUT_FORMAT_CHANGED)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_DROPPED_VIDEO_FRAMES)).containsExactly(contentAfterPreroll, contentAfterMidroll, contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_VIDEO_SIZE_CHANGED)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_RENDERED_FIRST_FRAME)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
    assertThat(listener.getEvents(EVENT_VIDEO_FRAME_PROCESSING_OFFSET)).containsExactly(contentAfterPreroll, contentAfterMidroll, contentAfterPostroll).inOrder();
    listener.assertNoMoreEvents();
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) Timeline(com.google.android.exoplayer2.Timeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) Test(org.junit.Test)

Example 2 with END_OF_STREAM_ITEM

use of com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project ExoPlayer by google.

the class DecoderAudioRendererTest method immediatelyReadEndOfStreamPlaysAudioSinkToEndOfStream.

@Test
public void immediatelyReadEndOfStreamPlaysAudioSinkToEndOfStream() throws Exception {
    FakeSampleStream fakeSampleStream = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), FORMAT, ImmutableList.of(END_OF_STREAM_ITEM));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    audioRenderer.enable(RendererConfiguration.DEFAULT, new Format[] { FORMAT }, fakeSampleStream, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    true, /* startPositionUs= */
    0, /* offsetUs= */
    0);
    audioRenderer.setCurrentStreamFinal();
    when(mockAudioSink.isEnded()).thenReturn(true);
    while (!audioRenderer.isEnded()) {
        audioRenderer.render(0, 0);
    }
    verify(mockAudioSink, times(1)).playToEndOfStream();
    audioRenderer.disable();
    audioRenderer.reset();
    verify(mockAudioSink, times(1)).reset();
}
Also used : FakeSampleStream(com.google.android.exoplayer2.testutil.FakeSampleStream) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) Test(org.junit.Test)

Example 3 with END_OF_STREAM_ITEM

use of com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project ExoPlayer by google.

the class DecoderVideoRendererTest method replaceStream_whenStarted_rendersFirstFrameOfNewStream.

// TODO: Fix rendering of first frame at stream transition.
@Ignore
@Test
public void replaceStream_whenStarted_rendersFirstFrameOfNewStream() throws Exception {
    FakeSampleStream fakeSampleStream1 = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    H264_FORMAT, 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= */
    H264_FORMAT, ImmutableList.of(oneByteSample(/* timeUs= */
    0), END_OF_STREAM_ITEM));
    fakeSampleStream2.writeData(/* startPositionUs= */
    0);
    renderer.enable(RendererConfiguration.DEFAULT, new Format[] { H264_FORMAT }, fakeSampleStream1, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    true, /* startPositionUs= */
    0, /* offsetUs */
    0);
    renderer.start();
    boolean replacedStream = false;
    for (int i = 0; i <= 10; i++) {
        renderer.render(/* positionUs= */
        i * 10, SystemClock.elapsedRealtime() * 1000);
        if (!replacedStream && renderer.hasReadStreamToEnd()) {
            renderer.replaceStream(new Format[] { H264_FORMAT }, fakeSampleStream2, /* startPositionUs= */
            100, /* offsetUs= */
            100);
            replacedStream = true;
        }
        // Ensure pending messages are delivered.
        ShadowLooper.idleMainLooper();
    }
    verify(eventListener, times(2)).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
    anyLong());
}
Also used : FakeSampleStream(com.google.android.exoplayer2.testutil.FakeSampleStream) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with END_OF_STREAM_ITEM

use of com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project ExoPlayer by google.

the class DecoderVideoRendererTest method replaceStream_whenNotStarted_doesNotRenderFirstFrameOfNewStream.

// TODO: Fix rendering of first frame at stream transition.
@Ignore
@Test
public void replaceStream_whenNotStarted_doesNotRenderFirstFrameOfNewStream() throws Exception {
    FakeSampleStream fakeSampleStream1 = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    H264_FORMAT, 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= */
    H264_FORMAT, ImmutableList.of(oneByteSample(/* timeUs= */
    0), END_OF_STREAM_ITEM));
    fakeSampleStream2.writeData(/* startPositionUs= */
    0);
    renderer.enable(RendererConfiguration.DEFAULT, new Format[] { H264_FORMAT }, fakeSampleStream1, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    true, /* startPositionUs= */
    0, /* offsetUs */
    0);
    boolean replacedStream = false;
    for (int i = 0; i < 10; i++) {
        renderer.render(/* positionUs= */
        i * 10, SystemClock.elapsedRealtime() * 1000);
        if (!replacedStream && renderer.hasReadStreamToEnd()) {
            renderer.replaceStream(new Format[] { H264_FORMAT }, fakeSampleStream2, /* startPositionUs= */
            100, /* offsetUs= */
            100);
            replacedStream = true;
        }
        // Ensure pending messages are delivered.
        ShadowLooper.idleMainLooper();
    }
    verify(eventListener).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
    anyLong());
    // Render to streamOffsetUs and verify the new first frame gets rendered.
    renderer.render(/* positionUs= */
    100, SystemClock.elapsedRealtime() * 1000);
    verify(eventListener, times(2)).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
    anyLong());
}
Also used : FakeSampleStream(com.google.android.exoplayer2.testutil.FakeSampleStream) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with END_OF_STREAM_ITEM

use of com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project ExoPlayer by google.

the class MediaCodecVideoRendererTest method render_dropsLateBuffer.

@Test
public void render_dropsLateBuffer() 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(// First buffer.
    oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME), // Late buffer.
    oneByteSample(/* timeUs= */
    50_000), // Last buffer.
    oneByteSample(/* timeUs= */
    100_000), 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.start();
    mediaCodecVideoRenderer.render(0, SystemClock.elapsedRealtime() * 1000);
    mediaCodecVideoRenderer.render(40_000, SystemClock.elapsedRealtime() * 1000);
    mediaCodecVideoRenderer.setCurrentStreamFinal();
    // Ensures buffer will be 30_001us late.
    int posUs = 80_001;
    while (!mediaCodecVideoRenderer.isEnded()) {
        mediaCodecVideoRenderer.render(posUs, SystemClock.elapsedRealtime() * 1000);
        posUs += 40_000;
    }
    shadowOf(testMainLooper).idle();
    verify(eventListener).onDroppedFrames(eq(1), anyLong());
}
Also used : FakeSampleStream(com.google.android.exoplayer2.testutil.FakeSampleStream) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 DrmSessionEventListener (com.google.android.exoplayer2.drm.DrmSessionEventListener)14 DefaultAllocator (com.google.android.exoplayer2.upstream.DefaultAllocator)14 FakeSampleStream (com.google.android.exoplayer2.testutil.FakeSampleStream)13 Format (com.google.android.exoplayer2.Format)5 MediaFormat (android.media.MediaFormat)4 MediaPeriodId (com.google.android.exoplayer2.source.MediaSource.MediaPeriodId)3 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)3 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)3 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)3 ShadowLooper (org.robolectric.shadows.ShadowLooper)3 Nullable (androidx.annotation.Nullable)2 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)2 C (com.google.android.exoplayer2.C)2 Timeline (com.google.android.exoplayer2.Timeline)2 DrmSessionManager (com.google.android.exoplayer2.drm.DrmSessionManager)2 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)2 FakeMediaPeriod (com.google.android.exoplayer2.testutil.FakeMediaPeriod)2 END_OF_STREAM_ITEM (com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM)2 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)2