package androidx.media3.exoplayer.analytics; import android.util.Base64; import androidx.media3.common.Timeline; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.Util; import androidx.media3.exoplayer.analytics.AnalyticsListener; import androidx.media3.exoplayer.analytics.PlaybackSessionManager; import androidx.media3.exoplayer.source.MediaSource; import com.google.common.base.Supplier; import java.util.HashMap; import java.util.Iterator; import java.util.Random; import org.checkerframework.checker.nullness.qual.RequiresNonNull; /* loaded from: classes2.dex */ public final class DefaultPlaybackSessionManager implements PlaybackSessionManager { public static final Supplier DEFAULT_SESSION_ID_GENERATOR = new Supplier() { // from class: androidx.media3.exoplayer.analytics.DefaultPlaybackSessionManager$$ExternalSyntheticLambda0 @Override // com.google.common.base.Supplier public final Object get() { String generateDefaultSessionId; generateDefaultSessionId = DefaultPlaybackSessionManager.generateDefaultSessionId(); return generateDefaultSessionId; } }; private static final Random RANDOM = new Random(); private static final int SESSION_ID_LENGTH = 12; private String currentSessionId; private Timeline currentTimeline; private long lastRemovedCurrentWindowSequenceNumber; private PlaybackSessionManager.Listener listener; private final Timeline.Period period; private final Supplier sessionIdGenerator; private final HashMap sessions; private final Timeline.Window window; @Override // androidx.media3.exoplayer.analytics.PlaybackSessionManager public void setListener(PlaybackSessionManager.Listener listener) { this.listener = listener; } public DefaultPlaybackSessionManager() { this(DEFAULT_SESSION_ID_GENERATOR); } public DefaultPlaybackSessionManager(Supplier supplier) { this.sessionIdGenerator = supplier; this.window = new Timeline.Window(); this.period = new Timeline.Period(); this.sessions = new HashMap<>(); this.currentTimeline = Timeline.EMPTY; this.lastRemovedCurrentWindowSequenceNumber = -1L; } @Override // androidx.media3.exoplayer.analytics.PlaybackSessionManager public synchronized String getSessionForMediaPeriodId(Timeline timeline, MediaSource.MediaPeriodId mediaPeriodId) { return getOrAddSession(timeline.getPeriodByUid(mediaPeriodId.periodUid, this.period).windowIndex, mediaPeriodId).sessionId; } @Override // androidx.media3.exoplayer.analytics.PlaybackSessionManager public synchronized boolean belongsToSession(AnalyticsListener.EventTime eventTime, String str) { SessionDescriptor sessionDescriptor = this.sessions.get(str); if (sessionDescriptor == null) { return false; } sessionDescriptor.maybeSetWindowSequenceNumber(eventTime.windowIndex, eventTime.mediaPeriodId); return sessionDescriptor.belongsToSession(eventTime.windowIndex, eventTime.mediaPeriodId); } /* JADX WARN: Removed duplicated region for block: B:36:0x00e3 A[Catch: all -> 0x011a, TryCatch #0 {, blocks: (B:4:0x0005, B:9:0x0014, B:11:0x0018, B:16:0x0026, B:18:0x0032, B:20:0x003c, B:24:0x0046, B:26:0x0052, B:27:0x0058, B:29:0x005d, B:31:0x0065, B:33:0x0082, B:34:0x00dd, B:36:0x00e3, B:37:0x00f9, B:39:0x0105, B:41:0x010b), top: B:3:0x0005 }] */ /* JADX WARN: Removed duplicated region for block: B:45:0x00f5 */ @Override // androidx.media3.exoplayer.analytics.PlaybackSessionManager /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public synchronized void updateSessions(androidx.media3.exoplayer.analytics.AnalyticsListener.EventTime r25) { /* Method dump skipped, instructions count: 285 To view this dump add '--comments-level debug' option */ throw new UnsupportedOperationException("Method not decompiled: androidx.media3.exoplayer.analytics.DefaultPlaybackSessionManager.updateSessions(androidx.media3.exoplayer.analytics.AnalyticsListener$EventTime):void"); } @Override // androidx.media3.exoplayer.analytics.PlaybackSessionManager public synchronized void updateSessionsWithTimelineChange(AnalyticsListener.EventTime eventTime) { Assertions.checkNotNull(this.listener); Timeline timeline = this.currentTimeline; this.currentTimeline = eventTime.timeline; Iterator it = this.sessions.values().iterator(); while (it.hasNext()) { SessionDescriptor next = it.next(); if (!next.tryResolvingToNewTimeline(timeline, this.currentTimeline) || next.isFinishedAtEventTime(eventTime)) { it.remove(); if (next.isCreated) { if (next.sessionId.equals(this.currentSessionId)) { clearCurrentSession(next); } this.listener.onSessionFinished(eventTime, next.sessionId, false); } } } updateCurrentSession(eventTime); } @Override // androidx.media3.exoplayer.analytics.PlaybackSessionManager public synchronized void updateSessionsWithDiscontinuity(AnalyticsListener.EventTime eventTime, int i) { Assertions.checkNotNull(this.listener); boolean z = i == 0; Iterator it = this.sessions.values().iterator(); while (it.hasNext()) { SessionDescriptor next = it.next(); if (next.isFinishedAtEventTime(eventTime)) { it.remove(); if (next.isCreated) { boolean equals = next.sessionId.equals(this.currentSessionId); boolean z2 = z && equals && next.isActive; if (equals) { clearCurrentSession(next); } this.listener.onSessionFinished(eventTime, next.sessionId, z2); } } } updateCurrentSession(eventTime); } @Override // androidx.media3.exoplayer.analytics.PlaybackSessionManager public synchronized String getActiveSessionId() { return this.currentSessionId; } @Override // androidx.media3.exoplayer.analytics.PlaybackSessionManager public synchronized void finishAllSessions(AnalyticsListener.EventTime eventTime) { PlaybackSessionManager.Listener listener; String str = this.currentSessionId; if (str != null) { clearCurrentSession((SessionDescriptor) Assertions.checkNotNull(this.sessions.get(str))); } Iterator it = this.sessions.values().iterator(); while (it.hasNext()) { SessionDescriptor next = it.next(); it.remove(); if (next.isCreated && (listener = this.listener) != null) { listener.onSessionFinished(eventTime, next.sessionId, false); } } } @RequiresNonNull({"listener"}) private void updateCurrentSession(AnalyticsListener.EventTime eventTime) { if (eventTime.timeline.isEmpty()) { String str = this.currentSessionId; if (str != null) { clearCurrentSession((SessionDescriptor) Assertions.checkNotNull(this.sessions.get(str))); return; } return; } SessionDescriptor sessionDescriptor = this.sessions.get(this.currentSessionId); SessionDescriptor orAddSession = getOrAddSession(eventTime.windowIndex, eventTime.mediaPeriodId); this.currentSessionId = orAddSession.sessionId; updateSessions(eventTime); if (eventTime.mediaPeriodId == null || !eventTime.mediaPeriodId.isAd()) { return; } if (sessionDescriptor != null && sessionDescriptor.windowSequenceNumber == eventTime.mediaPeriodId.windowSequenceNumber && sessionDescriptor.adMediaPeriodId != null && sessionDescriptor.adMediaPeriodId.adGroupIndex == eventTime.mediaPeriodId.adGroupIndex && sessionDescriptor.adMediaPeriodId.adIndexInAdGroup == eventTime.mediaPeriodId.adIndexInAdGroup) { return; } this.listener.onAdPlaybackStarted(eventTime, getOrAddSession(eventTime.windowIndex, new MediaSource.MediaPeriodId(eventTime.mediaPeriodId.periodUid, eventTime.mediaPeriodId.windowSequenceNumber)).sessionId, orAddSession.sessionId); } private void clearCurrentSession(SessionDescriptor sessionDescriptor) { if (sessionDescriptor.windowSequenceNumber != -1) { this.lastRemovedCurrentWindowSequenceNumber = sessionDescriptor.windowSequenceNumber; } this.currentSessionId = null; } /* JADX INFO: Access modifiers changed from: private */ public long getMinWindowSequenceNumber() { SessionDescriptor sessionDescriptor = this.sessions.get(this.currentSessionId); return (sessionDescriptor == null || sessionDescriptor.windowSequenceNumber == -1) ? this.lastRemovedCurrentWindowSequenceNumber + 1 : sessionDescriptor.windowSequenceNumber; } private SessionDescriptor getOrAddSession(int i, MediaSource.MediaPeriodId mediaPeriodId) { SessionDescriptor sessionDescriptor = null; long j = Long.MAX_VALUE; for (SessionDescriptor sessionDescriptor2 : this.sessions.values()) { sessionDescriptor2.maybeSetWindowSequenceNumber(i, mediaPeriodId); if (sessionDescriptor2.belongsToSession(i, mediaPeriodId)) { long j2 = sessionDescriptor2.windowSequenceNumber; if (j2 == -1 || j2 < j) { sessionDescriptor = sessionDescriptor2; j = j2; } else if (j2 == j && ((SessionDescriptor) Util.castNonNull(sessionDescriptor)).adMediaPeriodId != null && sessionDescriptor2.adMediaPeriodId != null) { sessionDescriptor = sessionDescriptor2; } } } if (sessionDescriptor != null) { return sessionDescriptor; } String str = this.sessionIdGenerator.get(); SessionDescriptor sessionDescriptor3 = new SessionDescriptor(str, i, mediaPeriodId); this.sessions.put(str, sessionDescriptor3); return sessionDescriptor3; } /* JADX INFO: Access modifiers changed from: private */ public static String generateDefaultSessionId() { byte[] bArr = new byte[12]; RANDOM.nextBytes(bArr); return Base64.encodeToString(bArr, 10); } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes2.dex */ public final class SessionDescriptor { private MediaSource.MediaPeriodId adMediaPeriodId; private boolean isActive; private boolean isCreated; private final String sessionId; private int windowIndex; private long windowSequenceNumber; public SessionDescriptor(String str, int i, MediaSource.MediaPeriodId mediaPeriodId) { this.sessionId = str; this.windowIndex = i; this.windowSequenceNumber = mediaPeriodId == null ? -1L : mediaPeriodId.windowSequenceNumber; if (mediaPeriodId == null || !mediaPeriodId.isAd()) { return; } this.adMediaPeriodId = mediaPeriodId; } public boolean tryResolvingToNewTimeline(Timeline timeline, Timeline timeline2) { int resolveWindowIndexToNewTimeline = resolveWindowIndexToNewTimeline(timeline, timeline2, this.windowIndex); this.windowIndex = resolveWindowIndexToNewTimeline; if (resolveWindowIndexToNewTimeline == -1) { return false; } MediaSource.MediaPeriodId mediaPeriodId = this.adMediaPeriodId; return mediaPeriodId == null || timeline2.getIndexOfPeriod(mediaPeriodId.periodUid) != -1; } public boolean belongsToSession(int i, MediaSource.MediaPeriodId mediaPeriodId) { return mediaPeriodId == null ? i == this.windowIndex : this.adMediaPeriodId == null ? !mediaPeriodId.isAd() && mediaPeriodId.windowSequenceNumber == this.windowSequenceNumber : mediaPeriodId.windowSequenceNumber == this.adMediaPeriodId.windowSequenceNumber && mediaPeriodId.adGroupIndex == this.adMediaPeriodId.adGroupIndex && mediaPeriodId.adIndexInAdGroup == this.adMediaPeriodId.adIndexInAdGroup; } public void maybeSetWindowSequenceNumber(int i, MediaSource.MediaPeriodId mediaPeriodId) { if (this.windowSequenceNumber != -1 || i != this.windowIndex || mediaPeriodId == null || mediaPeriodId.windowSequenceNumber < DefaultPlaybackSessionManager.this.getMinWindowSequenceNumber()) { return; } this.windowSequenceNumber = mediaPeriodId.windowSequenceNumber; } public boolean isFinishedAtEventTime(AnalyticsListener.EventTime eventTime) { if (eventTime.mediaPeriodId == null) { return this.windowIndex != eventTime.windowIndex; } if (this.windowSequenceNumber == -1) { return false; } if (eventTime.mediaPeriodId.windowSequenceNumber > this.windowSequenceNumber) { return true; } if (this.adMediaPeriodId == null) { return false; } int indexOfPeriod = eventTime.timeline.getIndexOfPeriod(eventTime.mediaPeriodId.periodUid); int indexOfPeriod2 = eventTime.timeline.getIndexOfPeriod(this.adMediaPeriodId.periodUid); if (eventTime.mediaPeriodId.windowSequenceNumber < this.adMediaPeriodId.windowSequenceNumber || indexOfPeriod < indexOfPeriod2) { return false; } if (indexOfPeriod > indexOfPeriod2) { return true; } if (!eventTime.mediaPeriodId.isAd()) { return eventTime.mediaPeriodId.nextAdGroupIndex == -1 || eventTime.mediaPeriodId.nextAdGroupIndex > this.adMediaPeriodId.adGroupIndex; } int i = eventTime.mediaPeriodId.adGroupIndex; int i2 = eventTime.mediaPeriodId.adIndexInAdGroup; if (i <= this.adMediaPeriodId.adGroupIndex) { return i == this.adMediaPeriodId.adGroupIndex && i2 > this.adMediaPeriodId.adIndexInAdGroup; } return true; } private int resolveWindowIndexToNewTimeline(Timeline timeline, Timeline timeline2, int i) { if (i < timeline.getWindowCount()) { timeline.getWindow(i, DefaultPlaybackSessionManager.this.window); for (int i2 = DefaultPlaybackSessionManager.this.window.firstPeriodIndex; i2 <= DefaultPlaybackSessionManager.this.window.lastPeriodIndex; i2++) { int indexOfPeriod = timeline2.getIndexOfPeriod(timeline.getUidOfPeriod(i2)); if (indexOfPeriod != -1) { return timeline2.getPeriod(indexOfPeriod, DefaultPlaybackSessionManager.this.period).windowIndex; } } return -1; } if (i < timeline2.getWindowCount()) { return i; } return -1; } } }