package com.discord.stores; import c.d.b.a.a; import com.discord.api.guild.Guild; import com.discord.api.voice.state.VoiceState; import com.discord.app.AppLog; import com.discord.models.domain.ModelApplicationStream; import com.discord.models.domain.ModelPayload; import com.discord.models.domain.StreamCreateOrUpdate; import com.discord.models.domain.StreamDelete; import com.discord.stores.updates.ObservationDeck; import com.discord.stores.updates.ObservationDeckProvider; import d0.t.h0; import d0.t.n; import d0.z.d.m; import java.util.HashMap; import java.util.List; import java.util.Map; import kotlin.collections.ArrayDeque; import kotlin.jvm.internal.DefaultConstructorMarker; import rx.Observable; /* compiled from: StoreApplicationStreaming.kt */ public final class StoreApplicationStreaming extends StoreV2 { private ActiveApplicationStream activeApplicationStream; private final ArrayDeque breadCrumbs; private final Dispatcher dispatcher; private final ObservationDeck observationDeck; private final StoreRtcConnection rtcConnectionStore; private final StoreStream storeStream; private final HashMap> streamSpectators; private Map> streamSpectatorsSnapshot; private final StreamViewerTracker streamViewerTracker; private final HashMap streamsByUser; private Map streamsByUserSnapshot; private ModelApplicationStream targetStream; private final StoreUser userStore; private final StoreVoiceChannelSelected voiceChannelSelectedStore; /* compiled from: StoreApplicationStreaming.kt */ public static final class ActiveApplicationStream { private final State state; private final ModelApplicationStream stream; /* compiled from: StoreApplicationStreaming.kt */ public enum State { CONNECTING, ACTIVE, RECONNECTING, ENDED, PAUSED, DENIED_FULL; @Deprecated private static final List ACTIVE_STATES = n.listOf((Object[]) new State[]{CONNECTING, ACTIVE, RECONNECTING, PAUSED}); private static final Companion Companion = new Companion(null); /* compiled from: StoreApplicationStreaming.kt */ public static final class Companion { private Companion() { } public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } public final List getACTIVE_STATES() { return State.access$getACTIVE_STATES$cp(); } } public static final /* synthetic */ List access$getACTIVE_STATES$cp() { return ACTIVE_STATES; } public final boolean isStreamActive() { return ACTIVE_STATES.contains(this); } } public ActiveApplicationStream(State state, ModelApplicationStream modelApplicationStream) { m.checkNotNullParameter(state, "state"); m.checkNotNullParameter(modelApplicationStream, "stream"); this.state = state; this.stream = modelApplicationStream; } public static /* synthetic */ ActiveApplicationStream copy$default(ActiveApplicationStream activeApplicationStream, State state, ModelApplicationStream modelApplicationStream, int i, Object obj) { if ((i & 1) != 0) { state = activeApplicationStream.state; } if ((i & 2) != 0) { modelApplicationStream = activeApplicationStream.stream; } return activeApplicationStream.copy(state, modelApplicationStream); } public final State component1() { return this.state; } public final ModelApplicationStream component2() { return this.stream; } public final ActiveApplicationStream copy(State state, ModelApplicationStream modelApplicationStream) { m.checkNotNullParameter(state, "state"); m.checkNotNullParameter(modelApplicationStream, "stream"); return new ActiveApplicationStream(state, modelApplicationStream); } public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof ActiveApplicationStream)) { return false; } ActiveApplicationStream activeApplicationStream = (ActiveApplicationStream) obj; return m.areEqual(this.state, activeApplicationStream.state) && m.areEqual(this.stream, activeApplicationStream.stream); } public final State getState() { return this.state; } public final ModelApplicationStream getStream() { return this.stream; } public int hashCode() { State state = this.state; int i = 0; int hashCode = (state != null ? state.hashCode() : 0) * 31; ModelApplicationStream modelApplicationStream = this.stream; if (modelApplicationStream != null) { i = modelApplicationStream.hashCode(); } return hashCode + i; } public String toString() { StringBuilder L = a.L("ActiveApplicationStream(state="); L.append(this.state); L.append(", stream="); L.append(this.stream); L.append(")"); return L.toString(); } } /* compiled from: StoreApplicationStreaming.kt */ public static final class StreamViewerTracker { private final Map maxViewersByStream = new HashMap(); public final void clear() { this.maxViewersByStream.clear(); } public final Integer getMaxViewers(String str) { m.checkNotNullParameter(str, "streamKey"); return this.maxViewersByStream.get(str); } public final void onStreamUpdated(String str, int i) { m.checkNotNullParameter(str, "streamKey"); Integer num = this.maxViewersByStream.get(str); this.maxViewersByStream.put(str, Integer.valueOf(Math.max(num != null ? num.intValue() : 0, i))); } public final void remove(String str) { m.checkNotNullParameter(str, "streamKey"); this.maxViewersByStream.remove(str); } } public StoreApplicationStreaming(StoreStream storeStream, Dispatcher dispatcher, StoreUser storeUser, StoreVoiceChannelSelected storeVoiceChannelSelected, StoreRtcConnection storeRtcConnection, ObservationDeck observationDeck) { m.checkNotNullParameter(storeStream, "storeStream"); m.checkNotNullParameter(dispatcher, "dispatcher"); m.checkNotNullParameter(storeUser, "userStore"); m.checkNotNullParameter(storeVoiceChannelSelected, "voiceChannelSelectedStore"); m.checkNotNullParameter(storeRtcConnection, "rtcConnectionStore"); m.checkNotNullParameter(observationDeck, "observationDeck"); this.storeStream = storeStream; this.dispatcher = dispatcher; this.userStore = storeUser; this.voiceChannelSelectedStore = storeVoiceChannelSelected; this.rtcConnectionStore = storeRtcConnection; this.observationDeck = observationDeck; this.breadCrumbs = new ArrayDeque<>(); this.streamsByUser = new HashMap<>(); this.streamsByUserSnapshot = h0.emptyMap(); this.streamSpectators = new HashMap<>(); this.streamSpectatorsSnapshot = h0.emptyMap(); this.streamViewerTracker = new StreamViewerTracker(); addBreadCrumb("Initializing the store."); } /* JADX INFO: this call moved to the top of the method (can break code semantics) */ public /* synthetic */ StoreApplicationStreaming(StoreStream storeStream, Dispatcher dispatcher, StoreUser storeUser, StoreVoiceChannelSelected storeVoiceChannelSelected, StoreRtcConnection storeRtcConnection, ObservationDeck observationDeck, int i, DefaultConstructorMarker defaultConstructorMarker) { this(storeStream, dispatcher, storeUser, storeVoiceChannelSelected, storeRtcConnection, (i & 32) != 0 ? ObservationDeckProvider.get() : observationDeck); } public static final /* synthetic */ ActiveApplicationStream access$getActiveApplicationStream$p(StoreApplicationStreaming storeApplicationStreaming) { return storeApplicationStreaming.activeApplicationStream; } public static final /* synthetic */ StoreRtcConnection access$getRtcConnectionStore$p(StoreApplicationStreaming storeApplicationStreaming) { return storeApplicationStreaming.rtcConnectionStore; } public static final /* synthetic */ StoreStream access$getStoreStream$p(StoreApplicationStreaming storeApplicationStreaming) { return storeApplicationStreaming.storeStream; } public static final /* synthetic */ Map access$getStreamSpectatorsSnapshot$p(StoreApplicationStreaming storeApplicationStreaming) { return storeApplicationStreaming.streamSpectatorsSnapshot; } public static final /* synthetic */ Map access$getStreamsByUserSnapshot$p(StoreApplicationStreaming storeApplicationStreaming) { return storeApplicationStreaming.streamsByUserSnapshot; } public static final /* synthetic */ StoreUser access$getUserStore$p(StoreApplicationStreaming storeApplicationStreaming) { return storeApplicationStreaming.userStore; } public static final /* synthetic */ void access$setActiveApplicationStream$p(StoreApplicationStreaming storeApplicationStreaming, ActiveApplicationStream activeApplicationStream) { storeApplicationStreaming.activeApplicationStream = activeApplicationStream; } public static final /* synthetic */ void access$setStreamSpectatorsSnapshot$p(StoreApplicationStreaming storeApplicationStreaming, Map map) { storeApplicationStreaming.streamSpectatorsSnapshot = map; } public static final /* synthetic */ void access$setStreamsByUserSnapshot$p(StoreApplicationStreaming storeApplicationStreaming, Map map) { storeApplicationStreaming.streamsByUserSnapshot = map; } public static final /* synthetic */ void access$stopStreamInternal(StoreApplicationStreaming storeApplicationStreaming, String str) { storeApplicationStreaming.stopStreamInternal(str); } private final synchronized void addBreadCrumb(String str) { ArrayDeque arrayDeque = this.breadCrumbs; arrayDeque.addLast(str + ", on thread: " + Thread.currentThread()); ArrayDeque arrayDeque2 = this.breadCrumbs; if (arrayDeque2.size() > 50) { arrayDeque2.removeFirst(); } } public static /* synthetic */ void createStream$default(StoreApplicationStreaming storeApplicationStreaming, long j, Long l, String str, int i, Object obj) { if ((i & 4) != 0) { str = null; } storeApplicationStreaming.createStream(j, l, str); } private final synchronized void dumpBreadcrumbs() { for (String str : this.breadCrumbs) { AppLog.g.recordBreadcrumb(str, "StoreApplicationStreaming"); } } @StoreThread private final void handleStreamCreateOrUpdate(String str, boolean z2, List list) { updateActiveApplicationStream(new ActiveApplicationStream(z2 ? ActiveApplicationStream.State.PAUSED : ActiveApplicationStream.State.ACTIVE, ModelApplicationStream.Companion.decodeStreamKey(str))); this.streamSpectators.put(str, list); this.streamViewerTracker.onStreamUpdated(str, list.size()); markChanged(); } public static /* synthetic */ void handleVoiceStateUpdate$default(StoreApplicationStreaming storeApplicationStreaming, VoiceState voiceState, long j, int i, Object obj) { if ((i & 2) != 0) { j = voiceState.c(); } storeApplicationStreaming.handleVoiceStateUpdate(voiceState, j); } private final boolean isScreenSharing(ActiveApplicationStream activeApplicationStream) { return activeApplicationStream != null && activeApplicationStream.getStream().getOwnerId() == this.userStore.getMeInternal$app_productionBetaRelease().getId(); } private final void stopStreamInternal(String str) { this.storeStream.handleStreamDelete(new StreamDelete(str, StreamDelete.Reason.USER_REQUESTED, false), true); } public static /* synthetic */ void targetStream$default(StoreApplicationStreaming storeApplicationStreaming, String str, boolean z2, int i, Object obj) { if ((i & 2) != 0) { z2 = false; } storeApplicationStreaming.targetStream(str, z2); } @StoreThread private final void updateActiveApplicationStream(ActiveApplicationStream activeApplicationStream) { boolean isScreenSharing = isScreenSharing(this.activeApplicationStream); boolean isScreenSharing2 = isScreenSharing(activeApplicationStream); if (isScreenSharing != isScreenSharing2) { this.storeStream.handleIsScreenSharingChanged(isScreenSharing2); } this.activeApplicationStream = activeApplicationStream; } public final void createStream(long j, Long l, String str) { this.dispatcher.schedule(new StoreApplicationStreaming$createStream$1(this, j, l, str)); } @StoreThread public final String getActiveApplicationStreamKeyInternal$app_productionBetaRelease() { ModelApplicationStream stream; ActiveApplicationStream activeApplicationStream = this.activeApplicationStream; if (activeApplicationStream == null || (stream = activeApplicationStream.getStream()) == null) { return null; } return stream.getEncodedStreamKey(); } @StoreThread public final Integer getMaxViewersForStream(long j, long j2, Long l) { return this.streamViewerTracker.getMaxViewers((l != null ? new ModelApplicationStream.GuildStream(l.longValue(), j2, j) : new ModelApplicationStream.CallStream(j2, j)).getEncodedStreamKey()); } /* JADX DEBUG: Type inference failed for r0v0. Raw type applied. Possible types: java.util.Map, java.util.Map */ public final Map getStreamsByUser() { return this.streamsByUserSnapshot; } @StoreThread public final void handleConnectionOpen(ModelPayload modelPayload) { m.checkNotNullParameter(modelPayload, "payload"); addBreadCrumb("Connection open, clearing streams by user."); this.streamsByUser.clear(); this.streamViewerTracker.clear(); ActiveApplicationStream activeApplicationStream = this.activeApplicationStream; if (!(activeApplicationStream == null || activeApplicationStream.getState() == ActiveApplicationStream.State.ENDED)) { targetStream(activeApplicationStream.getStream().getEncodedStreamKey(), true); } List guilds = modelPayload.getGuilds(); m.checkNotNullExpressionValue(guilds, "payload.guilds"); for (Guild guild : guilds) { List O = guild.O(); if (O != null) { for (VoiceState voiceState : O) { handleVoiceStateUpdate(voiceState, guild.o()); } } } markChanged(); } @StoreThread public final void handleStreamCreate(StreamCreateOrUpdate streamCreateOrUpdate) { m.checkNotNullParameter(streamCreateOrUpdate, "streamCreate"); this.streamViewerTracker.remove(streamCreateOrUpdate.getStreamKey()); handleStreamCreateOrUpdate(streamCreateOrUpdate.getStreamKey(), streamCreateOrUpdate.getPaused(), streamCreateOrUpdate.getViewerIds()); } @StoreThread public final void handleStreamCreateRequest(String str) { m.checkNotNullParameter(str, "streamKey"); updateActiveApplicationStream(new ActiveApplicationStream(ActiveApplicationStream.State.CONNECTING, ModelApplicationStream.Companion.decodeStreamKey(str))); markChanged(); } @StoreThread public final void handleStreamDelete(StreamDelete streamDelete) { ModelApplicationStream modelApplicationStream; ActiveApplicationStream activeApplicationStream; m.checkNotNullParameter(streamDelete, "streamDelete"); this.streamSpectators.remove(streamDelete.getStreamKey()); if (streamDelete.getReason() == StreamDelete.Reason.STREAM_FULL) { updateActiveApplicationStream(new ActiveApplicationStream(ActiveApplicationStream.State.DENIED_FULL, ModelApplicationStream.Companion.decodeStreamKey(streamDelete.getStreamKey()))); markChanged(); return; } ActiveApplicationStream activeApplicationStream2 = this.activeApplicationStream; if (activeApplicationStream2 == null || (modelApplicationStream = activeApplicationStream2.getStream()) == null) { modelApplicationStream = this.targetStream; } ActiveApplicationStream activeApplicationStream3 = null; if (m.areEqual(modelApplicationStream != null ? modelApplicationStream.getEncodedStreamKey() : null, streamDelete.getStreamKey())) { if (streamDelete.getUnavailable()) { ActiveApplicationStream activeApplicationStream4 = this.activeApplicationStream; if (activeApplicationStream4 != null) { activeApplicationStream3 = ActiveApplicationStream.copy$default(activeApplicationStream4, ActiveApplicationStream.State.RECONNECTING, null, 2, null); } } else if (!(streamDelete.getReason() == StreamDelete.Reason.USER_REQUESTED || (activeApplicationStream = this.activeApplicationStream) == null)) { activeApplicationStream3 = ActiveApplicationStream.copy$default(activeApplicationStream, ActiveApplicationStream.State.ENDED, null, 2, null); } updateActiveApplicationStream(activeApplicationStream3); markChanged(); } } @StoreThread public final void handleStreamTargeted(String str) { m.checkNotNullParameter(str, "streamKey"); ModelApplicationStream decodeStreamKey = ModelApplicationStream.Companion.decodeStreamKey(str); ActiveApplicationStream activeApplicationStream = this.activeApplicationStream; if (m.areEqual(decodeStreamKey, activeApplicationStream != null ? activeApplicationStream.getStream() : null) && activeApplicationStream.getState().isStreamActive()) { return; } if (this.voiceChannelSelectedStore.getSelectedVoiceChannelId() == decodeStreamKey.getChannelId()) { this.storeStream.streamWatch(decodeStreamKey.getEncodedStreamKey()); this.targetStream = null; return; } this.targetStream = decodeStreamKey; } @StoreThread public final void handleStreamUpdate(StreamCreateOrUpdate streamCreateOrUpdate) { m.checkNotNullParameter(streamCreateOrUpdate, "streamUpdate"); handleStreamCreateOrUpdate(streamCreateOrUpdate.getStreamKey(), streamCreateOrUpdate.getPaused(), streamCreateOrUpdate.getViewerIds()); } @StoreThread public final void handleStreamWatch(String str) { m.checkNotNullParameter(str, "streamKey"); updateActiveApplicationStream(new ActiveApplicationStream(ActiveApplicationStream.State.CONNECTING, ModelApplicationStream.Companion.decodeStreamKey(str))); markChanged(); } @StoreThread public final void handleVoiceChannelSelected(long j) { ModelApplicationStream stream; ModelApplicationStream modelApplicationStream = this.targetStream; if (modelApplicationStream == null || j != modelApplicationStream.getChannelId()) { ActiveApplicationStream activeApplicationStream = this.activeApplicationStream; if (activeApplicationStream == null || (stream = activeApplicationStream.getStream()) == null || stream.getChannelId() != j) { updateActiveApplicationStream(null); this.targetStream = null; markChanged(); return; } return; } StoreStream storeStream = this.storeStream; ModelApplicationStream modelApplicationStream2 = this.targetStream; m.checkNotNull(modelApplicationStream2); storeStream.streamWatch(modelApplicationStream2.getEncodedStreamKey()); this.targetStream = null; } @StoreThread public final void handleVoiceStateUpdate(VoiceState voiceState, long j) { m.checkNotNullParameter(voiceState, "voiceState"); addBreadCrumb("Handling a voice state update for " + voiceState.m()); Long a = voiceState.a(); long m = voiceState.m(); boolean i = voiceState.i(); boolean z2 = true; boolean z3 = j != 0; if (a == null || a.longValue() == 0) { z2 = false; } if (i && z3 && z2) { HashMap hashMap = this.streamsByUser; Long valueOf = Long.valueOf(m); m.checkNotNull(a); hashMap.put(valueOf, new ModelApplicationStream.GuildStream(j, a.longValue(), m)); markChanged(); } else if (i && z2) { HashMap hashMap2 = this.streamsByUser; Long valueOf2 = Long.valueOf(m); m.checkNotNull(a); hashMap2.put(valueOf2, new ModelApplicationStream.CallStream(a.longValue(), m)); markChanged(); } else if (this.streamsByUser.containsKey(Long.valueOf(m))) { this.streamsByUser.remove(Long.valueOf(m)); markChanged(); } ActiveApplicationStream activeApplicationStream = this.activeApplicationStream; if (activeApplicationStream != null && m == this.userStore.getMeInternal$app_productionBetaRelease().getId()) { long channelId = activeApplicationStream.getStream().getChannelId(); Long a2 = voiceState.a(); if (a2 == null || channelId != a2.longValue()) { updateActiveApplicationStream(null); markChanged(); } } ModelApplicationStream modelApplicationStream = this.streamsByUser.get(Long.valueOf(m)); if (i && modelApplicationStream != null && activeApplicationStream != null && m.areEqual(modelApplicationStream.getEncodedStreamKey(), activeApplicationStream.getStream().getEncodedStreamKey()) && activeApplicationStream.getState() == ActiveApplicationStream.State.ENDED) { handleStreamTargeted(modelApplicationStream.getEncodedStreamKey()); } } @StoreThread public final boolean isScreenSharing() { return isScreenSharing(this.activeApplicationStream); } @StoreThread public final boolean isUserStreaming(long j) { addBreadCrumb(a.q("Asking if ", j, " is streaming")); return this.streamsByUser.containsKey(Long.valueOf(j)); } public final Observable observeActiveStream() { return ObservationDeck.connectRx$default(this.observationDeck, new ObservationDeck.UpdateSource[]{this}, false, null, null, new StoreApplicationStreaming$observeActiveStream$1(this), 14, null); } public final Observable>> observeStreamSpectators() { Observable>> r = ObservationDeck.connectRx$default(this.observationDeck, new ObservationDeck.UpdateSource[]{this}, false, null, null, new StoreApplicationStreaming$observeStreamSpectators$1(this), 14, null).r(); m.checkNotNullExpressionValue(r, "observationDeck\n … .distinctUntilChanged()"); return r; } public final Observable> observeStreamsByUser() { Observable> r = ObservationDeck.connectRx$default(this.observationDeck, new ObservationDeck.UpdateSource[]{this}, false, null, null, new StoreApplicationStreaming$observeStreamsByUser$1(this), 14, null).r(); m.checkNotNullExpressionValue(r, "observationDeck\n … .distinctUntilChanged()"); return r; } public final Observable> observeStreamsForGuild(long j) { Observable> r = ObservationDeck.connectRx$default(this.observationDeck, new ObservationDeck.UpdateSource[]{this}, false, null, null, new StoreApplicationStreaming$observeStreamsForGuild$1(this, j), 14, null).r(); m.checkNotNullExpressionValue(r, "observationDeck\n … .distinctUntilChanged()"); return r; } public final Observable observeStreamsForUser(long j) { Observable r = ObservationDeck.connectRx$default(this.observationDeck, new ObservationDeck.UpdateSource[]{this}, false, null, null, new StoreApplicationStreaming$observeStreamsForUser$1(this, j), 14, null).r(); m.checkNotNullExpressionValue(r, "observationDeck\n … .distinctUntilChanged()"); return r; } @Override // com.discord.stores.StoreV2 @StoreThread public void snapshotData() { super.snapshotData(); StringBuilder L = a.L("Snapshotting 'streamsByUser' of size, "); L.append(this.streamsByUser.size()); addBreadCrumb(L.toString()); try { this.streamsByUserSnapshot = new HashMap(this.streamsByUser); this.streamSpectatorsSnapshot = new HashMap(this.streamSpectators); } catch (Exception e) { dumpBreadcrumbs(); throw e; } } public final void stopStream(String str) { m.checkNotNullParameter(str, "streamKey"); this.dispatcher.schedule(new StoreApplicationStreaming$stopStream$1(this, str)); } public final void targetStream(String str, boolean z2) { m.checkNotNullParameter(str, "streamKey"); this.dispatcher.schedule(new StoreApplicationStreaming$targetStream$1(this, str, z2)); } }