Package com.google.android.exoplayer2.analytics

## Class PlaybackStatsListener

- java.lang.Object
- - com.google.android.exoplayer2.analytics.PlaybackStatsListener

- All Implemented Interfaces:
    `AnalyticsListener`, `PlaybackSessionManager.Listener`

      public final class PlaybackStatsListener
      extends Object
      implements AnalyticsListener, PlaybackSessionManager.Listener

  `AnalyticsListener` to gather `PlaybackStats` from the player.

  For accurate measurements, the listener should be added to the player before loading media, i.e., `Player.getPlaybackState()` should be `Player.STATE_IDLE`.

  Playback stats are gathered separately for each playback session, i.e. each window in the `Timeline` and each single ad.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class | Description
    --- | --- | ---
    `static interface` | `PlaybackStatsListener.Callback` | A listener for `PlaybackStats` updates.

    - ### Nested classes/interfaces inherited from interface com.google.android.exoplayer2.analytics.AnalyticsListener

      `AnalyticsListener.EventFlags, AnalyticsListener.Events, AnalyticsListener.EventTime`

  - ### Field Summary

    - ### Fields inherited from interface com.google.android.exoplayer2.analytics.AnalyticsListener

      `EVENT_AUDIO_ATTRIBUTES_CHANGED, EVENT_AUDIO_CODEC_ERROR, EVENT_AUDIO_DECODER_INITIALIZED, EVENT_AUDIO_DECODER_RELEASED, EVENT_AUDIO_DISABLED, EVENT_AUDIO_ENABLED, EVENT_AUDIO_INPUT_FORMAT_CHANGED, EVENT_AUDIO_POSITION_ADVANCING, EVENT_AUDIO_SESSION_ID, EVENT_AUDIO_SINK_ERROR, EVENT_AUDIO_UNDERRUN, EVENT_BANDWIDTH_ESTIMATE, EVENT_DOWNSTREAM_FORMAT_CHANGED, EVENT_DRM_KEYS_LOADED, EVENT_DRM_KEYS_REMOVED, EVENT_DRM_KEYS_RESTORED, EVENT_DRM_SESSION_ACQUIRED, EVENT_DRM_SESSION_MANAGER_ERROR, EVENT_DRM_SESSION_RELEASED, EVENT_DROPPED_VIDEO_FRAMES, EVENT_IS_LOADING_CHANGED, EVENT_IS_PLAYING_CHANGED, EVENT_LOAD_CANCELED, EVENT_LOAD_COMPLETED, EVENT_LOAD_ERROR, EVENT_LOAD_STARTED, EVENT_MEDIA_ITEM_TRANSITION, EVENT_MEDIA_METADATA_CHANGED, EVENT_METADATA, EVENT_PLAY_WHEN_READY_CHANGED, EVENT_PLAYBACK_PARAMETERS_CHANGED, EVENT_PLAYBACK_STATE_CHANGED, EVENT_PLAYBACK_SUPPRESSION_REASON_CHANGED, EVENT_PLAYER_ERROR, EVENT_PLAYER_RELEASED, EVENT_POSITION_DISCONTINUITY, EVENT_RENDERED_FIRST_FRAME, EVENT_REPEAT_MODE_CHANGED, EVENT_SHUFFLE_MODE_ENABLED_CHANGED, EVENT_SKIP_SILENCE_ENABLED_CHANGED, EVENT_STATIC_METADATA_CHANGED, EVENT_SURFACE_SIZE_CHANGED, EVENT_TIMELINE_CHANGED, EVENT_TRACKS_CHANGED, EVENT_UPSTREAM_DISCARDED, EVENT_VIDEO_CODEC_ERROR, EVENT_VIDEO_DECODER_INITIALIZED, EVENT_VIDEO_DECODER_RELEASED, EVENT_VIDEO_DISABLED, EVENT_VIDEO_ENABLED, EVENT_VIDEO_FRAME_PROCESSING_OFFSET, EVENT_VIDEO_INPUT_FORMAT_CHANGED, EVENT_VIDEO_SIZE_CHANGED, EVENT_VOLUME_CHANGED`

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `PlaybackStatsListener​(boolean keepHistory, PlaybackStatsListener.Callback callback)` | Creates listener for playback stats.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `PlaybackStats` | `getCombinedPlaybackStats()` | Returns the combined `PlaybackStats` for all playback sessions this listener was and is listening to.
    `PlaybackStats` | `getPlaybackStats()` | Returns the `PlaybackStats` for the currently playback session, or null if no session is active.
    `void` | `onAdPlaybackStarted​(AnalyticsListener.EventTime eventTime, String contentSession, String adSession)` | Called when a session is interrupted by ad playback.
    `void` | `onBandwidthEstimate​(AnalyticsListener.EventTime eventTime, int totalLoadTimeMs, long totalBytesLoaded, long bitrateEstimate)` | Called when the bandwidth estimate for the current data source has been updated.
    `void` | `onDownstreamFormatChanged​(AnalyticsListener.EventTime eventTime, MediaLoadData mediaLoadData)` | Called when the downstream format sent to the renderers changed.
    `void` | `onDrmSessionManagerError​(AnalyticsListener.EventTime eventTime, Exception error)` | Called when a drm error occurs.
    `void` | `onDroppedVideoFrames​(AnalyticsListener.EventTime eventTime, int droppedFrames, long elapsedMs)` | Called after video frames have been dropped.
    `void` | `onEvents​(Player player, AnalyticsListener.Events events)` | Called after one or more events occurred.
    `void` | `onLoadError​(AnalyticsListener.EventTime eventTime, LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData, IOException error, boolean wasCanceled)` | Called when a media source loading error occurred.
    `void` | `onPositionDiscontinuity​(AnalyticsListener.EventTime eventTime, Player.PositionInfo oldPositionInfo, Player.PositionInfo newPositionInfo, int reason)` | Called when a position discontinuity occurred.
    `void` | `onSessionActive​(AnalyticsListener.EventTime eventTime, String session)` | Called when a session becomes active, i.e.
    `void` | `onSessionCreated​(AnalyticsListener.EventTime eventTime, String session)` | Called when a new session is created as a result of `PlaybackSessionManager.updateSessions(EventTime)`.
    `void` | `onSessionFinished​(AnalyticsListener.EventTime eventTime, String session, boolean automaticTransition)` | Called when a session is permanently finished.
    `void` | `onVideoSizeChanged​(AnalyticsListener.EventTime eventTime, VideoSize videoSize)` | Called before a frame is rendered for the first time since setting the surface, and each time there's a change in the size or pixel aspect ratio of the video being rendered.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface com.google.android.exoplayer2.analytics.AnalyticsListener

      `onAudioAttributesChanged, onAudioCodecError, onAudioDecoderInitialized, onAudioDecoderInitialized, onAudioDecoderReleased, onAudioDisabled, onAudioEnabled, onAudioInputFormatChanged, onAudioInputFormatChanged, onAudioPositionAdvancing, onAudioSessionIdChanged, onAudioSinkError, onAudioUnderrun, onDecoderDisabled, onDecoderEnabled, onDecoderInitialized, onDecoderInputFormatChanged, onDrmKeysLoaded, onDrmKeysRemoved, onDrmKeysRestored, onDrmSessionAcquired, onDrmSessionAcquired, onDrmSessionReleased, onIsLoadingChanged, onIsPlayingChanged, onLoadCanceled, onLoadCompleted, onLoadingChanged, onLoadStarted, onMediaItemTransition, onMediaMetadataChanged, onMetadata, onPlaybackParametersChanged, onPlaybackStateChanged, onPlaybackSuppressionReasonChanged, onPlayerError, onPlayerReleased, onPlayerStateChanged, onPlayWhenReadyChanged, onPositionDiscontinuity, onRenderedFirstFrame, onRepeatModeChanged, onSeekProcessed, onSeekStarted, onShuffleModeChanged, onSkipSilenceEnabledChanged, onStaticMetadataChanged, onSurfaceSizeChanged, onTimelineChanged, onTracksChanged, onUpstreamDiscarded, onVideoCodecError, onVideoDecoderInitialized, onVideoDecoderInitialized, onVideoDecoderReleased, onVideoDisabled, onVideoEnabled, onVideoFrameProcessingOffset, onVideoInputFormatChanged, onVideoInputFormatChanged, onVideoSizeChanged, onVolumeChanged`

- - ### Constructor Detail

    - #### PlaybackStatsListener

          public PlaybackStatsListener​(boolean keepHistory,
                                       @Nullable
                                       PlaybackStatsListener.Callback callback)

      Creates listener for playback stats.
      Parameters:
        `keepHistory` - Whether the reported `PlaybackStats` should keep the full history of events.
        `callback` - An optional callback for finished `PlaybackStats`.

  - ### Method Detail

    - #### getCombinedPlaybackStats

          public PlaybackStats getCombinedPlaybackStats()

      Returns the combined `PlaybackStats` for all playback sessions this listener was and is listening to.

      Note that these `PlaybackStats` will not contain the full history of events.

      Returns:
        The combined `PlaybackStats` for all playback sessions.

    - #### getPlaybackStats

          @Nullable
          public PlaybackStats getPlaybackStats()

      Returns the `PlaybackStats` for the currently playback session, or null if no session is active.
      Returns:
        `PlaybackStats` for the current playback session.

    - #### onSessionCreated

          public void onSessionCreated​(AnalyticsListener.EventTime eventTime,
                                       String session)

      Description copied from interface: `PlaybackSessionManager.Listener`
      Called when a new session is created as a result of `PlaybackSessionManager.updateSessions(EventTime)`.
      Specified by:
        `onSessionCreated` in interface `PlaybackSessionManager.Listener`
      Parameters:
        `eventTime` - The `AnalyticsListener.EventTime` at which the session is created.
        `session` - The identifier of the new session.

    - #### onSessionActive

          public void onSessionActive​(AnalyticsListener.EventTime eventTime,
                                      String session)

      Description copied from interface: `PlaybackSessionManager.Listener`
      Called when a session becomes active, i.e. playing in the foreground.
      Specified by:
        `onSessionActive` in interface `PlaybackSessionManager.Listener`
      Parameters:
        `eventTime` - The `AnalyticsListener.EventTime` at which the session becomes active.
        `session` - The identifier of the session.

    - #### onAdPlaybackStarted

          public void onAdPlaybackStarted​(AnalyticsListener.EventTime eventTime,
                                          String contentSession,
                                          String adSession)

      Description copied from interface: `PlaybackSessionManager.Listener`
      Called when a session is interrupted by ad playback.
      Specified by:
        `onAdPlaybackStarted` in interface `PlaybackSessionManager.Listener`
      Parameters:
        `eventTime` - The `AnalyticsListener.EventTime` at which the ad playback starts.
        `contentSession` - The session identifier of the content session.
        `adSession` - The identifier of the ad session.

    - #### onSessionFinished

          public void onSessionFinished​(AnalyticsListener.EventTime eventTime,
                                        String session,
                                        boolean automaticTransition)

      Description copied from interface: `PlaybackSessionManager.Listener`
      Called when a session is permanently finished.
      Specified by:
        `onSessionFinished` in interface `PlaybackSessionManager.Listener`
      Parameters:
        `eventTime` - The `AnalyticsListener.EventTime` at which the session finished.
        `session` - The identifier of the finished session.
        `automaticTransition` - Whether the session finished because of an automatic transition to the next playback item.

    - #### onPositionDiscontinuity

          public void onPositionDiscontinuity​(AnalyticsListener.EventTime eventTime,
                                              Player.PositionInfo oldPositionInfo,
                                              Player.PositionInfo newPositionInfo,
                                              @DiscontinuityReason
                                              int reason)

      Description copied from interface: `AnalyticsListener`
      Called when a position discontinuity occurred.
      Specified by:
        `onPositionDiscontinuity` in interface `AnalyticsListener`
      Parameters:
        `eventTime` - The event time.
        `oldPositionInfo` - The position before the discontinuity.
        `newPositionInfo` - The position after the discontinuity.
        `reason` - The reason for the position discontinuity.

    - #### onDroppedVideoFrames

          public void onDroppedVideoFrames​(AnalyticsListener.EventTime eventTime,
                                           int droppedFrames,
                                           long elapsedMs)

      Description copied from interface: `AnalyticsListener`
      Called after video frames have been dropped.
      Specified by:
        `onDroppedVideoFrames` in interface `AnalyticsListener`
      Parameters:
        `eventTime` - The event time.
        `droppedFrames` - The number of dropped frames since the last call to this method.
        `elapsedMs` - The duration in milliseconds over which the frames were dropped. This duration is timed from when the renderer was started or from when dropped frames were last reported (whichever was more recent), and not from when the first of the reported drops occurred.

    - #### onLoadError

          public void onLoadError​(AnalyticsListener.EventTime eventTime,
                                  LoadEventInfo loadEventInfo,
                                  MediaLoadData mediaLoadData,
                                  IOException error,
                                  boolean wasCanceled)

      Description copied from interface: `AnalyticsListener`
      Called when a media source loading error occurred.

      This method being called does not indicate that playback has failed, or that it will fail. The player may be able to recover from the error. Hence applications should not implement this method to display a user visible error or initiate an application level retry. `Player.EventListener.onPlayerError(com.google.android.exoplayer2.ExoPlaybackException)` is the appropriate place to implement such behavior. This method is called to provide the application with an opportunity to log the error if it wishes to do so.

      Specified by:
        `onLoadError` in interface `AnalyticsListener`
      Parameters:
        `eventTime` - The event time.
        `loadEventInfo` - The `LoadEventInfo` defining the load event.
        `mediaLoadData` - The `MediaLoadData` defining the data being loaded.
        `error` - The load error.
        `wasCanceled` - Whether the load was canceled as a result of the error.

    - #### onDrmSessionManagerError

          public void onDrmSessionManagerError​(AnalyticsListener.EventTime eventTime,
                                               Exception error)

      Description copied from interface: `AnalyticsListener`
      Called when a drm error occurs.

      This method being called does not indicate that playback has failed, or that it will fail. The player may be able to recover from the error. Hence applications should not implement this method to display a user visible error or initiate an application level retry. `Player.EventListener.onPlayerError(com.google.android.exoplayer2.ExoPlaybackException)` is the appropriate place to implement such behavior. This method is called to provide the application with an opportunity to log the error if it wishes to do so.

      Specified by:
        `onDrmSessionManagerError` in interface `AnalyticsListener`
      Parameters:
        `eventTime` - The event time.
        `error` - The error.

    - #### onBandwidthEstimate

          public void onBandwidthEstimate​(AnalyticsListener.EventTime eventTime,
                                          int totalLoadTimeMs,
                                          long totalBytesLoaded,
                                          long bitrateEstimate)

      Description copied from interface: `AnalyticsListener`
      Called when the bandwidth estimate for the current data source has been updated.
      Specified by:
        `onBandwidthEstimate` in interface `AnalyticsListener`
      Parameters:
        `eventTime` - The event time.
        `totalLoadTimeMs` - The total time spend loading this update is based on, in milliseconds.
        `totalBytesLoaded` - The total bytes loaded this update is based on.
        `bitrateEstimate` - The bandwidth estimate, in bits per second.

    - #### onDownstreamFormatChanged

          public void onDownstreamFormatChanged​(AnalyticsListener.EventTime eventTime,
                                                MediaLoadData mediaLoadData)

      Description copied from interface: `AnalyticsListener`
      Called when the downstream format sent to the renderers changed.
      Specified by:
        `onDownstreamFormatChanged` in interface `AnalyticsListener`
      Parameters:
        `eventTime` - The event time.
        `mediaLoadData` - The `MediaLoadData` defining the newly selected media data.

    - #### onVideoSizeChanged

          public void onVideoSizeChanged​(AnalyticsListener.EventTime eventTime,
                                         VideoSize videoSize)

      Description copied from interface: `AnalyticsListener`
      Called before a frame is rendered for the first time since setting the surface, and each time there's a change in the size or pixel aspect ratio of the video being rendered.
      Specified by:
        `onVideoSizeChanged` in interface `AnalyticsListener`
      Parameters:
        `eventTime` - The event time.
        `videoSize` - The new size of the video.

    - #### onEvents

          public void onEvents​(Player player,
                               AnalyticsListener.Events events)

      Description copied from interface: `AnalyticsListener`
      Called after one or more events occurred.

      State changes and events that happen within one `Looper` message queue iteration are reported together and only after all individual callbacks were triggered.

      Listeners should prefer this method over individual callbacks in the following cases:

      - They intend to trigger the same logic for multiple events (e.g. when updating a UI for both `AnalyticsListener.onPlaybackStateChanged(EventTime, int)` and `AnalyticsListener.onPlayWhenReadyChanged(EventTime, boolean, int)`).
      - They need access to the `Player` object to trigger further events (e.g. to call `Player.seekTo(long)` after a `AnalyticsListener.onMediaItemTransition(EventTime, MediaItem, int)`).
      - They intend to use multiple state values together or in combination with `Player` getter methods. For example using `Player.getCurrentWindowIndex()` with the `timeline` provided in `AnalyticsListener.onTimelineChanged(EventTime, int)` is only safe from within this method.
      - They are interested in events that logically happened together (e.g `AnalyticsListener.onPlaybackStateChanged(EventTime, int)` to `Player.STATE_BUFFERING` because of `AnalyticsListener.onMediaItemTransition(EventTime, MediaItem, int)`).
      Specified by:
        `onEvents` in interface `AnalyticsListener`
      Parameters:
        `player` - The `Player`.
        `events` - The `AnalyticsListener.Events` that occurred in this iteration.