Avoid creating empty playback metrics

In some cases we create empty playback metrics with no corresponding
events (e.g. when an app seeks to a new media item and immediately
releases the player). There is no benefit in having completely empty
metrics entries, so it's cleaner to not report them in such cases.

#minor-release

PiperOrigin-RevId: 425609010
This commit is contained in:
tonihei 2022-02-01 14:25:04 +00:00 committed by Ian Baker
parent 2848240a8c
commit fc528ebed7

View File

@ -134,6 +134,7 @@ public final class MediaMetricsListener
private int droppedFrames;
private int playedFrames;
private int audioUnderruns;
private boolean reportedEventsForCurrentSession;
/**
* Creates the listener.
@ -359,6 +360,7 @@ public final class MediaMetricsListener
.setSubErrorCode(errorInfo.subErrorCode)
.setException(error)
.build());
reportedEventsForCurrentSession = true;
pendingPlayerError = null;
}
@ -429,6 +431,7 @@ public final class MediaMetricsListener
int newPlaybackState = resolveNewPlaybackState(player);
if (currentPlaybackState != newPlaybackState) {
currentPlaybackState = newPlaybackState;
reportedEventsForCurrentSession = true;
playbackSession.reportPlaybackStateEvent(
new PlaybackStateEvent.Builder()
.setState(currentPlaybackState)
@ -561,6 +564,7 @@ public final class MediaMetricsListener
} else {
builder.setTrackState(TrackChangeEvent.TRACK_STATE_OFF);
}
reportedEventsForCurrentSession = true;
playbackSession.reportTrackChangeEvent(builder.build());
}
@ -586,12 +590,11 @@ public final class MediaMetricsListener
}
metricsBuilder.setPlaybackType(
window.isLive() ? PlaybackMetrics.PLAYBACK_TYPE_LIVE : PlaybackMetrics.PLAYBACK_TYPE_VOD);
reportedEventsForCurrentSession = true;
}
private void finishCurrentSession() {
if (metricsBuilder == null) {
return;
}
if (metricsBuilder != null && reportedEventsForCurrentSession) {
metricsBuilder.setAudioUnderrunCount(audioUnderruns);
metricsBuilder.setVideoFramesDropped(droppedFrames);
metricsBuilder.setVideoFramesPlayed(playedFrames);
@ -606,6 +609,7 @@ public final class MediaMetricsListener
? PlaybackMetrics.STREAM_SOURCE_NETWORK
: PlaybackMetrics.STREAM_SOURCE_UNKNOWN);
playbackSession.reportPlaybackMetrics(metricsBuilder.build());
}
metricsBuilder = null;
activeSessionId = null;
audioUnderruns = 0;
@ -614,6 +618,7 @@ public final class MediaMetricsListener
currentVideoFormat = null;
currentAudioFormat = null;
currentTextFormat = null;
reportedEventsForCurrentSession = false;
}
private static int getTrackChangeReason(@C.SelectionReason int trackSelectionReason) {