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 Andrew Lewis
parent 45cbfd2977
commit aef808cb91

View File

@ -136,6 +136,7 @@ public final class MediaMetricsListener
private int droppedFrames;
private int playedFrames;
private int audioUnderruns;
private boolean reportedEventsForCurrentSession;
/**
* Creates the listener.
@ -361,6 +362,7 @@ public final class MediaMetricsListener
.setSubErrorCode(errorInfo.subErrorCode)
.setException(error)
.build());
reportedEventsForCurrentSession = true;
pendingPlayerError = null;
}
@ -431,6 +433,7 @@ public final class MediaMetricsListener
int newPlaybackState = resolveNewPlaybackState(player);
if (currentPlaybackState != newPlaybackState) {
currentPlaybackState = newPlaybackState;
reportedEventsForCurrentSession = true;
playbackSession.reportPlaybackStateEvent(
new PlaybackStateEvent.Builder()
.setState(currentPlaybackState)
@ -563,6 +566,7 @@ public final class MediaMetricsListener
} else {
builder.setTrackState(TrackChangeEvent.TRACK_STATE_OFF);
}
reportedEventsForCurrentSession = true;
playbackSession.reportTrackChangeEvent(builder.build());
}
@ -588,12 +592,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);
@ -608,6 +611,7 @@ public final class MediaMetricsListener
? PlaybackMetrics.STREAM_SOURCE_NETWORK
: PlaybackMetrics.STREAM_SOURCE_UNKNOWN);
playbackSession.reportPlaybackMetrics(metricsBuilder.build());
}
metricsBuilder = null;
activeSessionId = null;
audioUnderruns = 0;
@ -616,6 +620,7 @@ public final class MediaMetricsListener
currentVideoFormat = null;
currentAudioFormat = null;
currentTextFormat = null;
reportedEventsForCurrentSession = false;
}
private static int getTrackChangeReason(@C.SelectionReason int trackSelectionReason) {