Commit d15350ab by tonihei Committed by Ian Baker

Avoid ending session early if an unrelated other session is finished

Currently, we always end the current session if onSessionFinished is
called. However, the finished session may not be the active one (for
example when discarding prebuffered items in a playlist). To make this
code more robust, we can save the active session id explicitly and only
end this session.

PiperOrigin-RevId: 422788542
parent dbc4dcf0
......@@ -16,6 +16,7 @@
package com.google.android.exoplayer2.analytics;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Assertions.checkState;
import static com.google.android.exoplayer2.util.Util.castNonNull;
import android.annotation.SuppressLint;
......@@ -113,6 +114,7 @@ public final class MediaMetricsListener
private final Timeline.Window window;
private final Timeline.Period period;
@Nullable private String activeSessionId;
@Nullable private PlaybackMetrics.Builder metricsBuilder;
@Player.DiscontinuityReason private int discontinuityReason;
private int currentPlaybackState;
......@@ -167,7 +169,8 @@ public final class MediaMetricsListener
// Ignore ad sessions.
return;
}
finishCurrentSession();
checkState(activeSessionId == null);
activeSessionId = sessionId;
metricsBuilder =
new PlaybackMetrics.Builder()
.setPlayerName(ExoPlayerLibraryInfo.TAG)
......@@ -182,9 +185,9 @@ public final class MediaMetricsListener
@Override
public void onSessionFinished(
EventTime eventTime, String sessionId, boolean automaticTransitionToNextPlayback) {
if (eventTime.mediaPeriodId != null && eventTime.mediaPeriodId.isAd()) {
// Ignore ad sessions.
return;
if ((eventTime.mediaPeriodId != null && eventTime.mediaPeriodId.isAd())
|| !sessionId.equals(activeSessionId)) {
// Ignore ad sessions and other sessions that are finished before becoming active.
}
finishCurrentSession();
}
......@@ -588,6 +591,7 @@ public final class MediaMetricsListener
: PlaybackMetrics.STREAM_SOURCE_UNKNOWN);
playbackSession.reportPlaybackMetrics(metricsBuilder.build());
metricsBuilder = null;
activeSessionId = null;
}
private static int getTrackChangeReason(@C.SelectionReason int trackSelectionReason) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment