Search in sources :

Example 6 with AnalyticsListener

use of com.google.android.exoplayer2.analytics.AnalyticsListener in project ExoPlayer by google.

the class DefaultAnalyticsCollectorTest method release_withCallbacksArrivingAfterRelease_onPlayerReleasedForwardedLast.

@Test
public void release_withCallbacksArrivingAfterRelease_onPlayerReleasedForwardedLast() throws Exception {
    FakeClock fakeClock = new FakeClock(/* initialTimeMs= */
    0, /* isAutoAdvancing= */
    true);
    ExoPlayer exoPlayer = new TestExoPlayerBuilder(ApplicationProvider.getApplicationContext()).setClock(fakeClock).build();
    AnalyticsListener analyticsListener = spy(new AnalyticsListener() {

        @Override
        public void onVideoDisabled(EventTime eventTime, DecoderCounters decoderCounters) {
            // Add delay in callback to test whether event timestamp and release timestamp are
            // in the correct order.
            fakeClock.advanceTime(1);
        }
    });
    exoPlayer.addAnalyticsListener(analyticsListener);
    // Prepare with media to ensure video renderer is enabled.
    exoPlayer.setMediaSource(new FakeMediaSource(new FakeTimeline(), ExoPlayerTestRunner.VIDEO_FORMAT));
    exoPlayer.prepare();
    TestPlayerRunHelper.runUntilPlaybackState(exoPlayer, Player.STATE_READY);
    // Release and add delay on releasing thread to verify timestamps of events.
    exoPlayer.release();
    long releaseTimeMs = fakeClock.currentTimeMillis();
    fakeClock.advanceTime(1);
    ShadowLooper.idleMainLooper();
    // Verify video disable events and release events arrived in order.
    ArgumentCaptor<AnalyticsListener.EventTime> videoDisabledEventTime = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    ArgumentCaptor<AnalyticsListener.EventTime> releasedEventTime = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    InOrder inOrder = inOrder(analyticsListener);
    inOrder.verify(analyticsListener).onVideoDisabled(videoDisabledEventTime.capture(), any());
    inOrder.verify(analyticsListener).onEvents(same(exoPlayer), argThat(events -> events.contains(EVENT_VIDEO_DISABLED)));
    inOrder.verify(analyticsListener).onPlayerReleased(releasedEventTime.capture());
    inOrder.verify(analyticsListener).onEvents(same(exoPlayer), argThat(events -> events.contains(EVENT_PLAYER_RELEASED)));
    // Verify order of timestamps of these events.
    // This verification is needed as a regression test against [internal ref: b/195396384]. The
    // root cause of the regression was an onPlayerReleased timestamp that was less than the
    // previously reported timestamps for other events triggered as part of the release.
    long videoDisableTimeMs = videoDisabledEventTime.getValue().realtimeMs;
    assertThat(videoDisableTimeMs).isGreaterThan(releaseTimeMs);
    assertThat(releasedEventTime.getValue().realtimeMs).isGreaterThan(videoDisableTimeMs);
}
Also used : ExoPlaybackException(com.google.android.exoplayer2.ExoPlaybackException) LoadEventInfo(com.google.android.exoplayer2.source.LoadEventInfo) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) EVENT_TIMELINE_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_TIMELINE_CHANGED) ExoMediaDrm(com.google.android.exoplayer2.drm.ExoMediaDrm) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) EVENT_RENDERED_FIRST_FRAME(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_RENDERED_FIRST_FRAME) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) MimeTypes(com.google.android.exoplayer2.util.MimeTypes) Looper(android.os.Looper) EVENT_AUDIO_DECODER_INITIALIZED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_AUDIO_DECODER_INITIALIZED) C(com.google.android.exoplayer2.C) EVENT_LOAD_STARTED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_LOAD_STARTED) DefaultDrmSessionManager(com.google.android.exoplayer2.drm.DefaultDrmSessionManager) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) Surface(android.view.Surface) EVENT_AUDIO_ENABLED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_AUDIO_ENABLED) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) FakeClock(com.google.android.exoplayer2.testutil.FakeClock) FakeAudioRenderer(com.google.android.exoplayer2.testutil.FakeAudioRenderer) Nullable(androidx.annotation.Nullable) Timeline(com.google.android.exoplayer2.Timeline) EVENT_PLAYER_ERROR(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_PLAYER_ERROR) RobolectricUtil(com.google.android.exoplayer2.robolectric.RobolectricUtil) ArgumentMatchers.anyFloat(org.mockito.ArgumentMatchers.anyFloat) EVENT_POSITION_DISCONTINUITY(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_POSITION_DISCONTINUITY) EVENT_VIDEO_INPUT_FORMAT_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_VIDEO_INPUT_FORMAT_CHANGED) EVENT_IS_PLAYING_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_IS_PLAYING_CHANGED) Mockito.mock(org.mockito.Mockito.mock) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) RunWith(org.junit.runner.RunWith) PlaybackParameters(com.google.android.exoplayer2.PlaybackParameters) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) EVENT_PLAYBACK_PARAMETERS_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_PLAYBACK_PARAMETERS_CHANGED) TestUtil(com.google.android.exoplayer2.testutil.TestUtil) EVENT_VIDEO_SIZE_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_VIDEO_SIZE_CHANGED) ArrayList(java.util.ArrayList) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) EVENT_VIDEO_FRAME_PROCESSING_OFFSET(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_VIDEO_FRAME_PROCESSING_OFFSET) MediaDrmCallbackException(com.google.android.exoplayer2.drm.MediaDrmCallbackException) EVENT_DRM_SESSION_ACQUIRED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_DRM_SESSION_ACQUIRED) Clock(com.google.android.exoplayer2.util.Clock) ShadowLooper(org.robolectric.shadows.ShadowLooper) IOException(java.io.IOException) Test(org.junit.Test) EVENT_VIDEO_DISABLED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_VIDEO_DISABLED) TestPlayerRunHelper(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper) TracksInfo(com.google.android.exoplayer2.TracksInfo) DrmSessionManager(com.google.android.exoplayer2.drm.DrmSessionManager) DrmInitData(com.google.android.exoplayer2.drm.DrmInitData) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) Util(com.google.android.exoplayer2.util.Util) EVENT_VIDEO_ENABLED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_VIDEO_ENABLED) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) Player(com.google.android.exoplayer2.Player) EVENT_VIDEO_DECODER_INITIALIZED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_VIDEO_DECODER_INITIALIZED) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) ApplicationProvider(androidx.test.core.app.ApplicationProvider) Format(com.google.android.exoplayer2.Format) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EVENT_IS_LOADING_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_IS_LOADING_CHANGED) Method(java.lang.reflect.Method) MediaSource(com.google.android.exoplayer2.source.MediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) EVENT_PLAYER_RELEASED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_PLAYER_RELEASED) RenderersFactory(com.google.android.exoplayer2.RenderersFactory) EVENT_PLAYBACK_STATE_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_PLAYBACK_STATE_CHANGED) MediaItem(com.google.android.exoplayer2.MediaItem) EVENT_DRM_KEYS_LOADED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_DRM_KEYS_LOADED) END_OF_STREAM_ITEM(com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM) EVENT_TRACKS_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_TRACKS_CHANGED) UUID(java.util.UUID) EVENT_DRM_SESSION_MANAGER_ERROR(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_DRM_SESSION_MANAGER_ERROR) SparseArray(android.util.SparseArray) FakeSampleStreamItem.oneByteSample(com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.oneByteSample) DrmSession(com.google.android.exoplayer2.drm.DrmSession) List(java.util.List) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) Mockito.inOrder(org.mockito.Mockito.inOrder) EVENT_DRM_SESSION_RELEASED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_DRM_SESSION_RELEASED) PlaybackException(com.google.android.exoplayer2.PlaybackException) MediaLoadData(com.google.android.exoplayer2.source.MediaLoadData) FakeVideoRenderer(com.google.android.exoplayer2.testutil.FakeVideoRenderer) EVENT_AUDIO_POSITION_ADVANCING(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_AUDIO_POSITION_ADVANCING) Metadata(com.google.android.exoplayer2.metadata.Metadata) EVENT_LOAD_COMPLETED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_LOAD_COMPLETED) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DecoderCounters(com.google.android.exoplayer2.decoder.DecoderCounters) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) AtomicReference(java.util.concurrent.atomic.AtomicReference) EVENT_LOAD_ERROR(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_LOAD_ERROR) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) FakeRenderer(com.google.android.exoplayer2.testutil.FakeRenderer) ConditionVariable(com.google.android.exoplayer2.util.ConditionVariable) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) VideoSize(com.google.android.exoplayer2.video.VideoSize) SurfaceTexture(android.graphics.SurfaceTexture) Window(com.google.android.exoplayer2.Timeline.Window) EVENT_MEDIA_ITEM_TRANSITION(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_MEDIA_ITEM_TRANSITION) MediaDrmCallback(com.google.android.exoplayer2.drm.MediaDrmCallback) InOrder(org.mockito.InOrder) Iterator(java.util.Iterator) EVENT_AUDIO_DISABLED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_AUDIO_DISABLED) EVENT_DOWNSTREAM_FORMAT_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_DOWNSTREAM_FORMAT_CHANGED) Truth.assertThat(com.google.common.truth.Truth.assertThat) EVENT_DROPPED_VIDEO_FRAMES(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_DROPPED_VIDEO_FRAMES) Mockito.verify(org.mockito.Mockito.verify) EVENT_PLAY_WHEN_READY_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_PLAY_WHEN_READY_CHANGED) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeExoMediaDrm(com.google.android.exoplayer2.testutil.FakeExoMediaDrm) EVENT_AUDIO_INPUT_FORMAT_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_AUDIO_INPUT_FORMAT_CHANGED) Renderer(com.google.android.exoplayer2.Renderer) InOrder(org.mockito.InOrder) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeClock(com.google.android.exoplayer2.testutil.FakeClock) DecoderCounters(com.google.android.exoplayer2.decoder.DecoderCounters) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Example 7 with AnalyticsListener

use of com.google.android.exoplayer2.analytics.AnalyticsListener in project ExoPlayer by google.

the class ExoPlayerTest method onPlaylistMetadataChanged_calledWhenPlaylistMetadataSet.

@Test
public void onPlaylistMetadataChanged_calledWhenPlaylistMetadataSet() {
    ExoPlayer player = new TestExoPlayerBuilder(ApplicationProvider.getApplicationContext()).build();
    Player.Listener playerListener = mock(Player.Listener.class);
    player.addListener(playerListener);
    AnalyticsListener analyticsListener = mock(AnalyticsListener.class);
    player.addAnalyticsListener(analyticsListener);
    MediaMetadata mediaMetadata = new MediaMetadata.Builder().setTitle("test").build();
    player.setPlaylistMetadata(mediaMetadata);
    verify(playerListener).onPlaylistMetadataChanged(mediaMetadata);
    verify(analyticsListener).onPlaylistMetadataChanged(any(), eq(mediaMetadata));
}
Also used : Listener(com.google.android.exoplayer2.Player.Listener) AnalyticsListener(com.google.android.exoplayer2.analytics.AnalyticsListener) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Example 8 with AnalyticsListener

use of com.google.android.exoplayer2.analytics.AnalyticsListener in project ExoPlayer by google.

the class DefaultAnalyticsCollectorTest method onEvents_isReportedWithCorrectEventTimes.

@Test
public void onEvents_isReportedWithCorrectEventTimes() throws Exception {
    ExoPlayer player = new TestExoPlayerBuilder(ApplicationProvider.getApplicationContext()).build();
    Surface surface = new Surface(new SurfaceTexture(/* texName= */
    0));
    player.setVideoSurface(surface);
    AnalyticsListener listener = mock(AnalyticsListener.class);
    Format[] formats = new Format[] { new Format.Builder().setSampleMimeType(MimeTypes.VIDEO_H264).build(), new Format.Builder().setSampleMimeType(MimeTypes.AUDIO_AAC).build() };
    player.addAnalyticsListener(listener);
    // Trigger some simultaneous events.
    player.setMediaSource(new FakeMediaSource(new FakeTimeline(), formats));
    player.seekTo(2_000);
    player.setPlaybackParameters(new PlaybackParameters(/* speed= */
    2.0f));
    ShadowLooper.runMainLooperToNextTask();
    // Move to another item and fail with a third one to trigger events with different EventTimes.
    player.prepare();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY);
    player.addMediaSource(new FakeMediaSource(new FakeTimeline(), formats));
    player.play();
    TestPlayerRunHelper.runUntilPositionDiscontinuity(player, Player.DISCONTINUITY_REASON_AUTO_TRANSITION);
    player.setMediaItem(MediaItem.fromUri("http://this-will-throw-an-exception.mp4"));
    TestPlayerRunHelper.runUntilError(player);
    ShadowLooper.runMainLooperToNextTask();
    player.release();
    surface.release();
    // Verify that expected individual callbacks have been called and capture EventTimes.
    ArgumentCaptor<AnalyticsListener.EventTime> individualTimelineChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onTimelineChanged(individualTimelineChangedEventTimes.capture(), anyInt());
    ArgumentCaptor<AnalyticsListener.EventTime> individualMediaItemTransitionEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onMediaItemTransition(individualMediaItemTransitionEventTimes.capture(), any(), anyInt());
    ArgumentCaptor<AnalyticsListener.EventTime> individualPositionDiscontinuityEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onPositionDiscontinuity(individualPositionDiscontinuityEventTimes.capture(), anyInt());
    ArgumentCaptor<AnalyticsListener.EventTime> individualPlaybackStateChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onPlaybackStateChanged(individualPlaybackStateChangedEventTimes.capture(), anyInt());
    ArgumentCaptor<AnalyticsListener.EventTime> individualIsLoadingChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onIsLoadingChanged(individualIsLoadingChangedEventTimes.capture(), anyBoolean());
    ArgumentCaptor<AnalyticsListener.EventTime> individualTracksChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onTracksChanged(individualTracksChangedEventTimes.capture(), any(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualPlayWhenReadyChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onPlayWhenReadyChanged(individualPlayWhenReadyChangedEventTimes.capture(), anyBoolean(), anyInt());
    ArgumentCaptor<AnalyticsListener.EventTime> individualIsPlayingChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onIsPlayingChanged(individualIsPlayingChangedEventTimes.capture(), anyBoolean());
    ArgumentCaptor<AnalyticsListener.EventTime> individualPlayerErrorEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onPlayerError(individualPlayerErrorEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualPlaybackParametersChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onPlaybackParametersChanged(individualPlaybackParametersChangedEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualLoadStartedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onLoadStarted(individualLoadStartedEventTimes.capture(), any(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualLoadCompletedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onLoadCompleted(individualLoadCompletedEventTimes.capture(), any(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualLoadErrorEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onLoadError(individualLoadErrorEventTimes.capture(), any(), any(), any(), anyBoolean());
    ArgumentCaptor<AnalyticsListener.EventTime> individualVideoEnabledEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onVideoEnabled(individualVideoEnabledEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualAudioEnabledEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onAudioEnabled(individualAudioEnabledEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualDownstreamFormatChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onDownstreamFormatChanged(individualDownstreamFormatChangedEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualVideoInputFormatChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onVideoInputFormatChanged(individualVideoInputFormatChangedEventTimes.capture(), any(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualAudioInputFormatChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onAudioInputFormatChanged(individualAudioInputFormatChangedEventTimes.capture(), any(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualVideoDecoderInitializedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onVideoDecoderInitialized(individualVideoDecoderInitializedEventTimes.capture(), any(), anyLong(), anyLong());
    ArgumentCaptor<AnalyticsListener.EventTime> individualAudioDecoderInitializedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onAudioDecoderInitialized(individualAudioDecoderInitializedEventTimes.capture(), any(), anyLong(), anyLong());
    ArgumentCaptor<AnalyticsListener.EventTime> individualVideoDisabledEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onVideoDisabled(individualVideoDisabledEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualAudioDisabledEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onAudioDisabled(individualAudioDisabledEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualRenderedFirstFrameEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onRenderedFirstFrame(individualRenderedFirstFrameEventTimes.capture(), any(), anyLong());
    ArgumentCaptor<AnalyticsListener.EventTime> individualVideoSizeChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onVideoSizeChanged(individualVideoSizeChangedEventTimes.capture(), any());
    verify(listener, atLeastOnce()).onVideoSizeChanged(individualVideoSizeChangedEventTimes.capture(), anyInt(), anyInt(), anyInt(), anyFloat());
    ArgumentCaptor<AnalyticsListener.EventTime> individualAudioPositionAdvancingEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onAudioPositionAdvancing(individualAudioPositionAdvancingEventTimes.capture(), anyLong());
    ArgumentCaptor<AnalyticsListener.EventTime> individualVideoProcessingOffsetEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onVideoFrameProcessingOffset(individualVideoProcessingOffsetEventTimes.capture(), anyLong(), anyInt());
    ArgumentCaptor<AnalyticsListener.EventTime> individualDroppedFramesEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onDroppedVideoFrames(individualDroppedFramesEventTimes.capture(), anyInt(), anyLong());
    // Verify the EventTimes reported with onEvents are a non-empty subset of the individual
    // callback EventTimes. We can only assert they are a non-empty subset because there may be
    // multiple events of the same type arriving in the same message queue iteration.
    ArgumentCaptor<AnalyticsListener.Events> eventsCaptor = ArgumentCaptor.forClass(AnalyticsListener.Events.class);
    verify(listener, atLeastOnce()).onEvents(eq(player), eventsCaptor.capture());
    SparseArray<List<AnalyticsListener.EventTime>> onEventsEventTimes = new SparseArray<>();
    for (AnalyticsListener.Events events : eventsCaptor.getAllValues()) {
        for (int i = 0; i < events.size(); i++) {
            @AnalyticsListener.EventFlags int event = events.get(i);
            if (onEventsEventTimes.get(event) == null) {
                onEventsEventTimes.put(event, new ArrayList<>());
            }
            onEventsEventTimes.get(event).add(events.getEventTime(event));
        }
    }
    // SparseArray.get returns null if the key doesn't exist, thus verifying the sets are non-empty.
    assertThat(individualTimelineChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_TIMELINE_CHANGED)).inOrder();
    assertThat(individualMediaItemTransitionEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_MEDIA_ITEM_TRANSITION)).inOrder();
    assertThat(individualPositionDiscontinuityEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_POSITION_DISCONTINUITY)).inOrder();
    assertThat(individualPlaybackStateChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_PLAYBACK_STATE_CHANGED)).inOrder();
    assertThat(individualIsLoadingChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_IS_LOADING_CHANGED)).inOrder();
    assertThat(individualTracksChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_TRACKS_CHANGED)).inOrder();
    assertThat(individualPlayWhenReadyChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_PLAY_WHEN_READY_CHANGED)).inOrder();
    assertThat(individualIsPlayingChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_IS_PLAYING_CHANGED)).inOrder();
    assertThat(individualPlayerErrorEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_PLAYER_ERROR)).inOrder();
    assertThat(individualPlaybackParametersChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_PLAYBACK_PARAMETERS_CHANGED)).inOrder();
    assertThat(individualLoadStartedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_LOAD_STARTED)).inOrder();
    assertThat(individualLoadCompletedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_LOAD_COMPLETED)).inOrder();
    assertThat(individualLoadErrorEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_LOAD_ERROR)).inOrder();
    assertThat(individualVideoEnabledEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_VIDEO_ENABLED)).inOrder();
    assertThat(individualAudioEnabledEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_AUDIO_ENABLED)).inOrder();
    assertThat(individualDownstreamFormatChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_DOWNSTREAM_FORMAT_CHANGED)).inOrder();
    assertThat(individualVideoInputFormatChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_VIDEO_INPUT_FORMAT_CHANGED)).inOrder();
    assertThat(individualAudioInputFormatChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_AUDIO_INPUT_FORMAT_CHANGED)).inOrder();
    assertThat(individualVideoDecoderInitializedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_VIDEO_DECODER_INITIALIZED)).inOrder();
    assertThat(individualAudioDecoderInitializedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_AUDIO_DECODER_INITIALIZED)).inOrder();
    assertThat(individualVideoDisabledEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_VIDEO_DISABLED)).inOrder();
    assertThat(individualAudioDisabledEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_AUDIO_DISABLED)).inOrder();
    assertThat(individualRenderedFirstFrameEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_RENDERED_FIRST_FRAME)).inOrder();
    assertThat(individualVideoSizeChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_VIDEO_SIZE_CHANGED)).inOrder();
    assertThat(individualAudioPositionAdvancingEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_AUDIO_POSITION_ADVANCING)).inOrder();
    assertThat(individualVideoProcessingOffsetEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_VIDEO_FRAME_PROCESSING_OFFSET)).inOrder();
    assertThat(individualDroppedFramesEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_DROPPED_VIDEO_FRAMES)).inOrder();
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) Surface(android.view.Surface) SparseArray(android.util.SparseArray) SurfaceTexture(android.graphics.SurfaceTexture) Format(com.google.android.exoplayer2.Format) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) PlaybackParameters(com.google.android.exoplayer2.PlaybackParameters) Test(org.junit.Test)

Example 9 with AnalyticsListener

use of com.google.android.exoplayer2.analytics.AnalyticsListener in project ExoPlayer by google.

the class DefaultAnalyticsCollectorTest method recursiveListenerInvocation_arrivesInCorrectOrder.

@Test
public void recursiveListenerInvocation_arrivesInCorrectOrder() {
    AnalyticsCollector analyticsCollector = new DefaultAnalyticsCollector(Clock.DEFAULT);
    analyticsCollector.setPlayer(new ExoPlayer.Builder(ApplicationProvider.getApplicationContext()).build(), Looper.myLooper());
    AnalyticsListener listener1 = mock(AnalyticsListener.class);
    AnalyticsListener listener2 = spy(new AnalyticsListener() {

        @Override
        public void onPlayerError(EventTime eventTime, PlaybackException error) {
            analyticsCollector.onSurfaceSizeChanged(/* width= */
            0, /* height= */
            0);
        }
    });
    AnalyticsListener listener3 = mock(AnalyticsListener.class);
    analyticsCollector.addListener(listener1);
    analyticsCollector.addListener(listener2);
    analyticsCollector.addListener(listener3);
    analyticsCollector.onPlayerError(ExoPlaybackException.createForSource(new IOException(), PlaybackException.ERROR_CODE_IO_UNSPECIFIED));
    InOrder inOrder = inOrder(listener1, listener2, listener3);
    inOrder.verify(listener1).onPlayerError(any(), any());
    inOrder.verify(listener2).onPlayerError(any(), any());
    inOrder.verify(listener3).onPlayerError(any(), any());
    inOrder.verify(listener1).onSurfaceSizeChanged(any(), eq(0), eq(0));
    inOrder.verify(listener2).onSurfaceSizeChanged(any(), eq(0), eq(0));
    inOrder.verify(listener3).onSurfaceSizeChanged(any(), eq(0), eq(0));
}
Also used : ExoPlaybackException(com.google.android.exoplayer2.ExoPlaybackException) PlaybackException(com.google.android.exoplayer2.PlaybackException) InOrder(org.mockito.InOrder) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with AnalyticsListener

use of com.google.android.exoplayer2.analytics.AnalyticsListener in project ExoPlayer by google.

the class ServerSideAdInsertionMediaSourceTest method playbackWithSeek_isHandledCorrectly.

@Test
public void playbackWithSeek_isHandledCorrectly() throws Exception {
    Context context = ApplicationProvider.getApplicationContext();
    ExoPlayer player = new ExoPlayer.Builder(context).setClock(new FakeClock(/* isAutoAdvancing= */
    true)).build();
    player.setVideoSurface(new Surface(new SurfaceTexture(/* texName= */
    1)));
    AdPlaybackState adPlaybackState = new AdPlaybackState(/* adsId= */
    new Object());
    adPlaybackState = addAdGroupToAdPlaybackState(adPlaybackState, /* fromPositionUs= */
    0, /* contentResumeOffsetUs= */
    0, /* adDurationsUs...= */
    100_000);
    adPlaybackState = addAdGroupToAdPlaybackState(adPlaybackState, /* fromPositionUs= */
    600_000, /* contentResumeOffsetUs= */
    1_000_000, /* adDurationsUs...= */
    100_000);
    AdPlaybackState firstAdPlaybackState = addAdGroupToAdPlaybackState(adPlaybackState, /* fromPositionUs= */
    900_000, /* contentResumeOffsetUs= */
    0, /* adDurationsUs...= */
    100_000);
    AtomicReference<ServerSideAdInsertionMediaSource> mediaSourceRef = new AtomicReference<>();
    mediaSourceRef.set(new ServerSideAdInsertionMediaSource(new DefaultMediaSourceFactory(context).createMediaSource(MediaItem.fromUri(TEST_ASSET)), /* adPlaybackStateUpdater= */
    contentTimeline -> {
        Object periodUid = checkNotNull(contentTimeline.getPeriod(/* periodIndex= */
        0, new Timeline.Period(), /* setIds= */
        true).uid);
        mediaSourceRef.get().setAdPlaybackStates(ImmutableMap.of(periodUid, firstAdPlaybackState));
        return true;
    }));
    AnalyticsListener listener = mock(AnalyticsListener.class);
    player.addAnalyticsListener(listener);
    player.setMediaSource(mediaSourceRef.get());
    player.prepare();
    // Play to the first content part, then seek past the midroll.
    playUntilPosition(player, /* mediaItemIndex= */
    0, /* positionMs= */
    100);
    player.seekTo(/* positionMs= */
    1_600);
    runUntilPendingCommandsAreFullyHandled(player);
    long positionAfterSeekMs = player.getCurrentPosition();
    long contentPositionAfterSeekMs = player.getContentPosition();
    player.play();
    runUntilPlaybackState(player, Player.STATE_ENDED);
    player.release();
    // Assert playback has been reported with ads: [ad0][content] seek [ad1][content][ad2][content]
    // 6*2(audio+video) format changes, 4 auto-transitions between parts, 1 seek with adjustment.
    verify(listener, times(4)).onPositionDiscontinuity(any(), any(), any(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
    verify(listener, times(1)).onPositionDiscontinuity(any(), any(), any(), eq(Player.DISCONTINUITY_REASON_SEEK));
    verify(listener, times(1)).onPositionDiscontinuity(any(), any(), any(), eq(Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT));
    verify(listener, times(12)).onDownstreamFormatChanged(any(), any());
    assertThat(contentPositionAfterSeekMs).isEqualTo(1_600);
    // Beginning of second ad.
    assertThat(positionAfterSeekMs).isEqualTo(0);
    // Assert renderers played through without reset, except for the seek.
    verify(listener, times(2)).onVideoEnabled(any(), any());
    verify(listener, times(2)).onAudioEnabled(any(), any());
    // Assert playback progression was smooth (=no unexpected delays that cause audio to underrun)
    verify(listener, never()).onAudioUnderrun(any(), anyInt(), anyLong(), anyLong());
}
Also used : Context(android.content.Context) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AnalyticsListener(com.google.android.exoplayer2.analytics.AnalyticsListener) Context(android.content.Context) TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Pair(android.util.Pair) RunWith(org.junit.runner.RunWith) Player(com.google.android.exoplayer2.Player) DefaultMediaSourceFactory(com.google.android.exoplayer2.source.DefaultMediaSourceFactory) TestPlayerRunHelper.playUntilPosition(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilPosition) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) PlayerId(com.google.android.exoplayer2.analytics.PlayerId) ShadowMediaCodecConfig(com.google.android.exoplayer2.robolectric.ShadowMediaCodecConfig) ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) RobolectricUtil.runMainLooperUntil(com.google.android.exoplayer2.robolectric.RobolectricUtil.runMainLooperUntil) SurfaceTexture(android.graphics.SurfaceTexture) MediaItem(com.google.android.exoplayer2.MediaItem) TestPlayerRunHelper.runUntilPlaybackState(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPlaybackState) Surface(android.view.Surface) ImmutableMap(com.google.common.collect.ImmutableMap) CapturingRenderersFactory(com.google.android.exoplayer2.testutil.CapturingRenderersFactory) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) FakeClock(com.google.android.exoplayer2.testutil.FakeClock) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) Mockito.never(org.mockito.Mockito.never) Timeline(com.google.android.exoplayer2.Timeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) Rule(org.junit.Rule) DumpFileAsserts(com.google.android.exoplayer2.testutil.DumpFileAsserts) PlaybackOutput(com.google.android.exoplayer2.robolectric.PlaybackOutput) Assert(org.junit.Assert) Assertions.checkNotNull(com.google.android.exoplayer2.util.Assertions.checkNotNull) Mockito.mock(org.mockito.Mockito.mock) AnalyticsListener(com.google.android.exoplayer2.analytics.AnalyticsListener) FakeClock(com.google.android.exoplayer2.testutil.FakeClock) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) Surface(android.view.Surface) SurfaceTexture(android.graphics.SurfaceTexture) ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) DefaultMediaSourceFactory(com.google.android.exoplayer2.source.DefaultMediaSourceFactory) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 AnalyticsListener (com.google.android.exoplayer2.analytics.AnalyticsListener)10 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)9 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)9 SurfaceTexture (android.graphics.SurfaceTexture)7 Surface (android.view.Surface)7 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 ApplicationProvider (androidx.test.core.app.ApplicationProvider)6 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)6 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)6 FakeClock (com.google.android.exoplayer2.testutil.FakeClock)6 Truth.assertThat (com.google.common.truth.Truth.assertThat)6 RunWith (org.junit.runner.RunWith)6 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)6 ArgumentMatchers.anyInt (org.mockito.ArgumentMatchers.anyInt)6 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)6 Mockito.mock (org.mockito.Mockito.mock)6 Context (android.content.Context)5 MediaItem (com.google.android.exoplayer2.MediaItem)5