package com.strmr.ps.ui.player;

import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.net.Uri;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.credentials.CredentialOption;
import androidx.media3.common.AudioAttributes;
import androidx.media3.common.C;
import androidx.media3.common.Format;
import androidx.media3.common.MediaItem;
import androidx.media3.common.PlaybackException;
import androidx.media3.common.PlaybackParameters;
import androidx.media3.common.Player;
import androidx.media3.common.TrackSelectionOverride;
import androidx.media3.common.Tracks;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.datasource.HttpDataSource;
import androidx.media3.exoplayer.DefaultLoadControl;
import androidx.media3.exoplayer.ExoPlaybackException;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.audio.AudioSink;
import androidx.media3.exoplayer.mediacodec.MediaCodecRenderer;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory;
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector;
import androidx.media3.exoplayer.trackselection.TrackSelector;
import androidx.media3.ui.PlayerView;
import androidx.media3.ui.SubtitleView;
import com.google.android.gms.common.internal.ImagesContract;
import com.strmr.ps.R;
import com.strmr.ps.data.model.ContentItem;
import com.strmr.ps.data.model.EpisodeFilter;
import com.strmr.ps.ui.player.VideoPlayerActivity;
import defpackage.cha;
import defpackage.cr4;
import defpackage.dp4;
import defpackage.fx4;
import defpackage.gha;
import defpackage.kq;
import defpackage.ky8;
import defpackage.lha;
import defpackage.n41;
import defpackage.n89;
import defpackage.nha;
import defpackage.q07;
import defpackage.qx;
import defpackage.rx4;
import defpackage.t7a;
import defpackage.vw2;
import defpackage.vx8;
import defpackage.wj0;
import defpackage.wo5;
import java.net.URLDecoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.concurrent.ExecutorService;
import kotlin.Metadata;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\b\n\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Lcom/strmr/ps/ui/player/u;", "Landroidx/media3/common/Player$Listener;", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class u implements Player.Listener {
    public final /* synthetic */ VideoPlayerActivity a;
    public final /* synthetic */ ExoPlayer b;

    public u(VideoPlayerActivity videoPlayerActivity, ExoPlayer exoPlayer) {
        this.a = videoPlayerActivity;
        this.b = exoPlayer;
    }

    @Override // androidx.media3.common.Player.Listener
    public final void onIsPlayingChanged(boolean z) {
        VideoPlayerActivity videoPlayerActivity = this.a;
        VideoPlayerActivity.x(videoPlayerActivity, z);
        if (z) {
            VideoPlayerActivity.g(videoPlayerActivity);
            VideoPlayerActivity.n(videoPlayerActivity);
        }
        if (!z && this.b.getPlaybackState() == 3) {
            VideoPlayerActivity.r(videoPlayerActivity);
            VideoPlayerActivity.q(videoPlayerActivity);
        } else if (z && videoPlayerActivity.P) {
            VideoPlayerActivity.t(videoPlayerActivity);
        }
    }

    @Override // androidx.media3.common.Player.Listener
    public final void onPlaybackStateChanged(int i) {
        if (i == 2) {
            FrameLayout frameLayout = this.a.t;
            if (frameLayout == null) {
                cr4.n("loadingOverlay");
                throw null;
            }
            if (frameLayout.getVisibility() != 0) {
                ImageView imageView = this.a.ab;
                if (imageView == null) {
                    cr4.n("bufferingIndicator");
                    throw null;
                }
                imageView.setVisibility(0);
                Animation animationLoadAnimation = AnimationUtils.loadAnimation(this.a, R.anim.rotate_spinner);
                ImageView imageView2 = this.a.ab;
                if (imageView2 != null) {
                    imageView2.startAnimation(animationLoadAnimation);
                    return;
                } else {
                    cr4.n("bufferingIndicator");
                    throw null;
                }
            }
            return;
        }
        if (i != 3) {
            if (i != 4) {
                return;
            }
            VideoPlayerActivity.o(this.a);
            return;
        }
        ImageView imageView3 = this.a.ab;
        if (imageView3 == null) {
            cr4.n("bufferingIndicator");
            throw null;
        }
        imageView3.clearAnimation();
        ImageView imageView4 = this.a.ab;
        if (imageView4 == null) {
            cr4.n("bufferingIndicator");
            throw null;
        }
        imageView4.setVisibility(8);
        VideoPlayerActivity.w(this.a);
        VideoPlayerActivity.s(this.a);
        VideoPlayerActivity videoPlayerActivity = this.a;
        if (videoPlayerActivity.am().getSkipIntroEnabled() && videoPlayerActivity.bx > 0 && videoPlayerActivity.bz > 0 && videoPlayerActivity.I.isEmpty()) {
            fx4 fx4Var = videoPlayerActivity.K;
            if (fx4Var != null) {
                ((rx4) fx4Var).h(null);
            }
            videoPlayerActivity.K = wj0.c(androidx.lifecycle.s.a(videoPlayerActivity), null, null, new t(videoPlayerActivity, null), 3);
        }
        VideoPlayerActivity videoPlayerActivity2 = this.a;
        if (!videoPlayerActivity2.cj) {
            videoPlayerActivity2.cj = true;
            long duration = this.b.getDuration();
            if (1 <= duration && duration < 30001) {
                this.a.ck = true;
                ExecutorService executorService = kq.a;
                kq.h("VideoPlayer", "Placeholder video detected (" + duration + "ms) — blocking scrobble/progress, trying next source");
                VideoPlayerActivity videoPlayerActivity3 = this.a;
                if (videoPlayerActivity3.cg < 5 && videoPlayerActivity3.ce.size() > 1) {
                    VideoPlayerActivity videoPlayerActivity4 = this.a;
                    videoPlayerActivity4.cg++;
                    if (VideoPlayerActivity.v(videoPlayerActivity4, 0L)) {
                        return;
                    }
                    VideoPlayerActivity videoPlayerActivity5 = this.a;
                    if (videoPlayerActivity5.cf + 1 < videoPlayerActivity5.ce.size()) {
                        List list = ky8.a;
                        ky8.c = this.a.cf + 1;
                        this.a.finish();
                        return;
                    }
                }
                this.a.bl("Content not cached\nThis torrent hasn't been downloaded by your debrid service yet. Try a different torrent or wait for it to finish caching.");
                return;
            }
        }
        VideoPlayerActivity videoPlayerActivity6 = this.a;
        if (!videoPlayerActivity6.cm && videoPlayerActivity6.bx > 0 && videoPlayerActivity6.bz > 0) {
            videoPlayerActivity6.cm = true;
            String str = videoPlayerActivity6.cl;
            if (str != null) {
                String lastPathSegment = Uri.parse(str).getLastPathSegment();
                String strDecode = lastPathSegment != null ? URLDecoder.decode(lastPathSegment, "UTF-8") : null;
                if (strDecode != null) {
                    EpisodeFilter episodeFilter = EpisodeFilter.INSTANCE;
                    VideoPlayerActivity videoPlayerActivity7 = this.a;
                    q07 q07VarMatchesEpisode$default = EpisodeFilter.matchesEpisode$default(episodeFilter, strDecode, videoPlayerActivity7.bx, videoPlayerActivity7.bz, null, 8, null);
                    boolean zBooleanValue = ((Boolean) q07VarMatchesEpisode$default.a).booleanValue();
                    String str2 = (String) q07VarMatchesEpisode$default.b;
                    if (!zBooleanValue) {
                        ExecutorService executorService2 = kq.a;
                        VideoPlayerActivity videoPlayerActivity8 = this.a;
                        int i2 = videoPlayerActivity8.bx;
                        int i3 = videoPlayerActivity8.bz;
                        StringBuilder sbV = n41.v("Episode mismatch: resolved '", i2, strDecode, "' but expected S", "E");
                        sbV.append(i3);
                        sbV.append(" (");
                        sbV.append(str2);
                        sbV.append(") — skipping");
                        kq.h("VideoPlayer", sbV.toString());
                        Toast.makeText(this.a, "Addon returned wrong episode — trying next source", 0).show();
                        VideoPlayerActivity videoPlayerActivity9 = this.a;
                        int i4 = videoPlayerActivity9.cg;
                        if (i4 < 5) {
                            videoPlayerActivity9.cg = i4 + 1;
                            if (VideoPlayerActivity.v(videoPlayerActivity9, 0L)) {
                                return;
                            }
                        }
                    }
                }
            }
        }
        VideoPlayerActivity.h(this.a);
        VideoPlayerActivity.g(this.a);
        VideoPlayerActivity videoPlayerActivity10 = this.a;
        if (!videoPlayerActivity10.cz) {
            VideoPlayerActivity.m(videoPlayerActivity10);
        }
        if (this.b.getPlayWhenReady()) {
            VideoPlayerActivity videoPlayerActivity11 = this.a;
            if (videoPlayerActivity11.P) {
                return;
            }
            VideoPlayerActivity.u(videoPlayerActivity11);
        }
    }

    /* JADX WARN: Can't wrap try/catch for region: R(20:179|(1:195)(3:183|(1:185)(3:186|(4:188|(1:190)|475|191)|192)|(1:513))|196|(1:198)(1:199)|200|(1:242)(15:204|(3:214|239|(1:241))(5:215|(4:218|(0)(3:225|(4:227|(2:229|(2:231|(2:233|(1:506)(1:236))(1:505))(1:504))|507|237)|503)|239|(0)(0))|217|238|(0))|(3:246|(1:250)|(3:(1:260)|261|(1:515))(2:256|(1:514)))|264|(4:266|(1:268)(1:269)|(1:274)(1:273)|(2:276|(4:278|473|279|516)))|283|(5:285|(1:287)(1:288)|(1:290)(1:291)|(1:296)(1:295)|(2:304|(6:306|471|307|308|310|311))(2:(1:302)(1:301)|(0)))|312|(1:357)(10:469|316|317|319|489|320|323|(1:325)(1:326)|(1:328)(1:329)|(4:338|(2:347|(1:349))|350|(1:508)(2:353|(2:355|356)))(6:331|(5:333|(0)(0)|(3:340|347|(0))|350|(0)(0))|337|(0)|350|(0)(0)))|467|358|362|(4:446|(1:452)(1:451)|453|(1:455)(2:456|(1:458)(1:459)))(2:364|(2:366|(2:368|(2:370|(2:372|SW:373)(1:416))(5:417|(1:419)(1:420)|(1:422)(1:423)|424|(1:443)(2:429|(1:431)(2:432|(1:434)(3:435|(1:439)|(1:441)(1:442))))))(1:444))(1:445))|460|461)|243|(2:246|(3:248|250|(5:252|254|(0)|261|(0))(0))(0))(0)|264|(0)|283|(0)|312|(2:314|357)(0)|467|358|362|(0)(0)|460|461) */
    /* JADX WARN: Code restructure failed: missing block: B:360:0x086b, code lost:
    
        r0 = move-exception;
     */
    /* JADX WARN: Code restructure failed: missing block: B:361:0x086c, code lost:
    
        defpackage.n41.ab("Error stopping player after error: ", r0.getMessage(), "VideoPlayerActivity");
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:214:0x056c  */
    /* JADX WARN: Removed duplicated region for block: B:241:0x05fb  */
    /* JADX WARN: Removed duplicated region for block: B:246:0x0604  */
    /* JADX WARN: Removed duplicated region for block: B:250:0x0610  */
    /* JADX WARN: Removed duplicated region for block: B:259:0x064c  */
    /* JADX WARN: Removed duplicated region for block: B:260:0x064e  */
    /* JADX WARN: Removed duplicated region for block: B:266:0x066b  */
    /* JADX WARN: Removed duplicated region for block: B:285:0x06ec  */
    /* JADX WARN: Removed duplicated region for block: B:304:0x0714  */
    /* JADX WARN: Removed duplicated region for block: B:338:0x07da  */
    /* JADX WARN: Removed duplicated region for block: B:340:0x07dd  */
    /* JADX WARN: Removed duplicated region for block: B:349:0x07f7  */
    /* JADX WARN: Removed duplicated region for block: B:353:0x0826  */
    /* JADX WARN: Removed duplicated region for block: B:357:0x085f  */
    /* JADX WARN: Removed duplicated region for block: B:364:0x087b  */
    /* JADX WARN: Removed duplicated region for block: B:446:0x0994  */
    /* JADX WARN: Removed duplicated region for block: B:508:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:510:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:515:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$UnknownArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    @Override // androidx.media3.common.Player.Listener
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void onPlayerError(PlaybackException playbackException) throws NoSuchAlgorithmException, KeyManagementException {
        long currentPosition;
        long duration;
        boolean z;
        boolean z2;
        boolean z3;
        Throwable cause;
        String message;
        VideoPlayerActivity videoPlayerActivity;
        String str;
        int i;
        String strI;
        String message2;
        long currentPosition2;
        String host;
        boolean z4;
        int i2;
        VideoPlayerActivity videoPlayerActivity2;
        long currentPosition3;
        boolean z5;
        boolean z6;
        long currentPosition4;
        boolean z7;
        String str2;
        int i3;
        Tracks.Group group;
        ContentItem.ItemType itemType;
        cr4.g(playbackException, "error");
        Log.e("VideoPlayerActivity", "Playback error: " + playbackException.getErrorCodeName(), playbackException);
        ExecutorService executorService = kq.a;
        q07 q07Var = new q07("errorCode", playbackException.getErrorCodeName());
        q07 q07Var2 = new q07("errorMessage", playbackException.getMessage());
        ContentItem contentItem = this.a.bw;
        q07 q07Var3 = new q07("title", contentItem != null ? contentItem.getTitle() : null);
        ContentItem contentItem2 = this.a.bw;
        q07 q07Var4 = new q07("tmdbId", contentItem2 != null ? Integer.valueOf(contentItem2.getTmdbId()) : null);
        ContentItem contentItem3 = this.a.bw;
        q07 q07Var5 = new q07(SessionDescription.ATTR_TYPE, (contentItem3 == null || (itemType = contentItem3.getItemType()) == null) ? null : itemType.name());
        q07 q07Var6 = new q07(ImagesContract.URL, n89.bb(100, this.a.bs));
        q07 q07Var7 = new q07("position", Long.valueOf(this.b.getCurrentPosition()));
        q07 q07Var8 = new q07("duration", Long.valueOf(this.b.getDuration()));
        Throwable cause2 = playbackException.getCause();
        kq.g("ERROR", wo5.j(q07Var, q07Var2, q07Var3, q07Var4, q07Var5, q07Var6, q07Var7, q07Var8, new q07("cause", cause2 != null ? cause2.getMessage() : null)));
        kq.a("VideoPlayer", "Playback error: " + playbackException.getErrorCodeName(), playbackException);
        try {
            currentPosition = this.b.getCurrentPosition();
        } catch (Exception unused) {
            currentPosition = 0;
        }
        try {
            duration = this.b.getDuration();
        } catch (Exception unused2) {
            duration = C.TIME_UNSET;
        }
        if (duration > 0 && currentPosition > 0 && currentPosition >= duration - ((long) 5000)) {
            ExecutorService executorService2 = kq.a;
            StringBuilder sbP = vw2.p(currentPosition, "Error near end of content (", "ms / ");
            sbP.append(duration);
            sbP.append("ms) — treating as completed");
            kq.h("VideoPlayer", sbP.toString());
            VideoPlayerActivity.o(this.a);
            return;
        }
        int i4 = 0;
        int i5 = 1;
        if (playbackException.errorCode == 4001) {
            Throwable cause3 = playbackException.getCause();
            if (cause3 instanceof MediaCodecRenderer.DecoderInitializationException) {
                String str3 = ((MediaCodecRenderer.DecoderInitializationException) cause3).mimeType;
                ExecutorService executorService3 = kq.a;
                kq.h("VideoPlayerActivity", "Decoder init failed for: " + str3 + ", attempting recovery");
                VideoPlayerActivity videoPlayerActivity3 = this.a;
                LinkedHashSet linkedHashSet = videoPlayerActivity3.y0;
                ExoPlayer exoPlayer = videoPlayerActivity3.bm;
                if (exoPlayer == null) {
                    exoPlayer = null;
                }
                if (exoPlayer != null) {
                    TrackSelector trackSelector = exoPlayer.getTrackSelector();
                    DefaultTrackSelector defaultTrackSelector = trackSelector instanceof DefaultTrackSelector ? (DefaultTrackSelector) trackSelector : null;
                    if (defaultTrackSelector != null) {
                        if (str3 == null) {
                            Log.w("VideoPlayerActivity", "Cannot recover: unknown audio format");
                        } else if (linkedHashSet.contains(str3)) {
                            Log.w("VideoPlayerActivity", "Already attempted recovery for: ".concat(str3));
                        } else {
                            linkedHashSet.add(str3);
                            kq.h("VideoPlayerActivity", "Attempting recovery from unsupported audio: ".concat(str3));
                            Tracks currentTracks = exoPlayer.getCurrentTracks();
                            cr4.f(currentTracks, "getCurrentTracks(...)");
                            t7a t7aVarListIterator = currentTracks.getGroups().listIterator(0);
                            cr4.f(t7aVarListIterator, "iterator(...)");
                            boolean z8 = false;
                            while (t7aVarListIterator.hasNext()) {
                                Tracks.Group group2 = (Tracks.Group) t7aVarListIterator.next();
                                if (group2.getType() == i5) {
                                    int i6 = group2.length;
                                    int i7 = i4;
                                    while (true) {
                                        if (i7 >= i6) {
                                            str2 = str3;
                                            break;
                                        }
                                        Format trackFormat = group2.getTrackFormat(i7);
                                        cr4.f(trackFormat, "getTrackFormat(...)");
                                        String str4 = trackFormat.sampleMimeType;
                                        if (str4 == null || str4.equalsIgnoreCase(str3) || linkedHashSet.contains(str4)) {
                                            str2 = str3;
                                        } else {
                                            MediaCodecInfo[] codecInfos = new MediaCodecList(i5).getCodecInfos();
                                            cr4.f(codecInfos, "getCodecInfos(...)");
                                            int length = codecInfos.length;
                                            str2 = str3;
                                            int i8 = 0;
                                            while (i8 < length) {
                                                MediaCodecInfo mediaCodecInfo = codecInfos[i8];
                                                if (mediaCodecInfo.isEncoder()) {
                                                    i3 = i8;
                                                    group = group2;
                                                } else {
                                                    i3 = i8;
                                                    String[] supportedTypes = mediaCodecInfo.getSupportedTypes();
                                                    group = group2;
                                                    cr4.f(supportedTypes, "getSupportedTypes(...)");
                                                    int length2 = supportedTypes.length;
                                                    int i9 = 0;
                                                    while (i9 < length2) {
                                                        int i10 = i9;
                                                        if (n89.u(supportedTypes[i10], str4)) {
                                                            ExecutorService executorService4 = kq.a;
                                                            kq.h("VideoPlayerActivity", "Found alternative audio track: ".concat(str4));
                                                            defaultTrackSelector.setParameters(defaultTrackSelector.buildUponParameters().addOverride(new TrackSelectionOverride(group.getMediaTrackGroup(), i7)).build());
                                                            z8 = true;
                                                            break;
                                                        }
                                                        i9 = i10 + 1;
                                                    }
                                                }
                                                i8 = i3 + 1;
                                                group2 = group;
                                            }
                                        }
                                        i7++;
                                        str3 = str2;
                                        group2 = group2;
                                        i5 = 1;
                                    }
                                    if (z8) {
                                        break;
                                    }
                                    str3 = str2;
                                    i4 = 0;
                                    i5 = 1;
                                }
                            }
                            if (z8) {
                                exoPlayer.seekTo(exoPlayer.getCurrentPosition());
                                exoPlayer.prepare();
                                i4 = 1;
                            } else {
                                Log.w("VideoPlayerActivity", "No compatible audio track found");
                                i4 = 0;
                            }
                        }
                    }
                }
                if (i4 != 0) {
                    return;
                }
            }
        }
        if (playbackException.errorCode == 5001) {
            Throwable cause4 = playbackException.getCause();
            if (cause4 instanceof AudioSink.InitializationException) {
                Format format = ((AudioSink.InitializationException) cause4).format;
                cr4.f(format, "format");
                String str5 = format.sampleMimeType;
                ExecutorService executorService5 = kq.a;
                kq.h("VideoPlayerActivity", "AudioTrack init failed for: " + str5 + " (channels: " + format.channelCount + "), attempting passthrough recovery");
                VideoPlayerActivity videoPlayerActivity4 = this.a;
                ExoPlayer exoPlayer2 = videoPlayerActivity4.bm;
                if (exoPlayer2 == null) {
                    exoPlayer2 = null;
                }
                if (exoPlayer2 == null) {
                    z7 = false;
                    if (z7) {
                    }
                } else if (videoPlayerActivity4.z0) {
                    Log.w("VideoPlayerActivity", "Already attempted passthrough recovery this session");
                    z7 = false;
                    if (z7) {
                        return;
                    }
                } else {
                    videoPlayerActivity4.z0 = true;
                    kq.h("VideoPlayerActivity", "Attempting audio recovery - forcing stereo output at sink level");
                    if (str5 != null) {
                        videoPlayerActivity4.y0.add(str5);
                    }
                    long currentPosition5 = exoPlayer2.getCurrentPosition();
                    kq.h("VideoPlayerActivity", "Saving position: " + currentPosition5 + "ms for recovery");
                    videoPlayerActivity4.G0 = true;
                    videoPlayerActivity4.u0 = false;
                    videoPlayerActivity4.v0 = false;
                    kq.h("VideoPlayerActivity", "Rebuilding player with stereo-only audio sink");
                    ExoPlayer exoPlayer3 = videoPlayerActivity4.bm;
                    if (exoPlayer3 != null) {
                        VideoPlayerActivity.c cVar = videoPlayerActivity4.g0;
                        if (cVar != null) {
                            exoPlayer3.removeListener(cVar);
                        }
                        try {
                            exoPlayer3.stop();
                            exoPlayer3.clearMediaItems();
                        } catch (Exception e) {
                            n41.ab("Error stopping player for recovery: ", e.getMessage(), "VideoPlayerActivity");
                        }
                        try {
                            exoPlayer3.release();
                        } catch (Exception e2) {
                            n41.ab("Error releasing player for recovery: ", e2.getMessage(), "VideoPlayerActivity");
                        }
                    }
                    videoPlayerActivity4.bm = null;
                    gha ghaVarAf = videoPlayerActivity4.af();
                    DefaultTrackSelector defaultTrackSelectorAg = videoPlayerActivity4.ag();
                    DefaultLoadControl defaultLoadControlAd = videoPlayerActivity4.ad();
                    DefaultMediaSourceFactory defaultMediaSourceFactoryAe = videoPlayerActivity4.ae();
                    videoPlayerActivity4.bp = defaultMediaSourceFactoryAe;
                    ExoPlayer exoPlayerBuild = new ExoPlayer.Builder(videoPlayerActivity4).setRenderersFactory(ghaVarAf).setTrackSelector(defaultTrackSelectorAg).setLoadControl(defaultLoadControlAd).setMediaSourceFactory(defaultMediaSourceFactoryAe).setAudioAttributes(new AudioAttributes.Builder().setUsage(1).setContentType(3).build(), false).setWakeMode(2).build();
                    cr4.f(exoPlayerBuild, "build(...)");
                    int i11 = videoPlayerActivity4.n0;
                    if (i11 != 0) {
                        exoPlayerBuild.setAudioSessionId(i11);
                    }
                    PlayerView playerView = videoPlayerActivity4.r;
                    if (playerView == null) {
                        cr4.n("playerView");
                        throw null;
                    }
                    playerView.setPlayer(exoPlayerBuild);
                    videoPlayerActivity4.bm = exoPlayerBuild;
                    if (videoPlayerActivity4.be != 1.0f) {
                        exoPlayerBuild.setPlaybackParameters(new PlaybackParameters(videoPlayerActivity4.be));
                    }
                    SubtitleView subtitleView = videoPlayerActivity4.s;
                    if (subtitleView == null) {
                        cr4.n("customSubtitleView");
                        throw null;
                    }
                    VideoPlayerActivity.c cVar2 = new VideoPlayerActivity.c(videoPlayerActivity4, subtitleView);
                    videoPlayerActivity4.g0 = cVar2;
                    exoPlayerBuild.addListener(cVar2);
                    exoPlayerBuild.addAnalyticsListener(new cha(videoPlayerActivity4, exoPlayerBuild));
                    exoPlayerBuild.addListener(new nha(videoPlayerActivity4));
                    MediaItem mediaItemBuild = new MediaItem.Builder().setUri(videoPlayerActivity4.an(videoPlayerActivity4.bs)).build();
                    cr4.f(mediaItemBuild, "build(...)");
                    exoPlayerBuild.setMediaItem(mediaItemBuild);
                    exoPlayerBuild.seekTo(currentPosition5);
                    exoPlayerBuild.prepare();
                    exoPlayerBuild.setPlayWhenReady(true);
                    ExecutorService executorService6 = kq.a;
                    kq.h("VideoPlayerActivity", "Stereo recovery complete, resuming from " + currentPosition5 + "ms");
                    z7 = true;
                    if (z7) {
                    }
                }
            }
        }
        int i12 = playbackException.errorCode;
        if (i12 == 4003 || i12 == 4001) {
            ExoPlaybackException exoPlaybackException = playbackException instanceof ExoPlaybackException ? (ExoPlaybackException) playbackException : null;
            if (exoPlaybackException != null && exoPlaybackException.type == 1) {
                String str6 = exoPlaybackException.rendererName;
                if (str6 == null) {
                    str6 = "";
                }
                if (n89.l(str6, "Audio", true)) {
                    VideoPlayerActivity videoPlayerActivity5 = this.a;
                    long currentPosition6 = this.b.getCurrentPosition();
                    if (videoPlayerActivity5.I0) {
                        Log.w("VideoPlayerActivity", "Already attempted FFmpeg audio recovery this session");
                        z = false;
                    } else {
                        videoPlayerActivity5.I0 = true;
                        videoPlayerActivity5.H0 = true;
                        Log.w("VideoPlayerActivity", "Audio renderer error at " + currentPosition6 + "ms — rebuilding with FFmpeg audio preferred");
                        ExoPlayer exoPlayer4 = videoPlayerActivity5.bm;
                        if (exoPlayer4 != null) {
                            VideoPlayerActivity.c cVar3 = videoPlayerActivity5.g0;
                            if (cVar3 != null) {
                                exoPlayer4.removeListener(cVar3);
                            }
                            try {
                                exoPlayer4.stop();
                                exoPlayer4.clearMediaItems();
                                exoPlayer4.release();
                            } catch (Exception unused3) {
                            }
                        }
                        videoPlayerActivity5.bm = null;
                        videoPlayerActivity5.bc(currentPosition6);
                        z = true;
                    }
                    if (z) {
                        return;
                    }
                }
            }
        }
        if (playbackException.errorCode == 5002 && VideoPlayerActivity.as(playbackException, AudioSink.UnexpectedDiscontinuityException.class)) {
            try {
                currentPosition4 = this.b.getCurrentPosition();
            } catch (Exception unused4) {
                currentPosition4 = -1;
            }
            ExecutorService executorService7 = kq.a;
            kq.h("VideoPlayer", "Audio discontinuity at " + currentPosition4 + "ms — resetting audio pipeline");
            try {
                this.b.seekTo(currentPosition4);
                return;
            } catch (Exception unused5) {
                return;
            }
        }
        if (playbackException.errorCode == 2000 && VideoPlayerActivity.as(playbackException, OutOfMemoryError.class)) {
            VideoPlayerActivity videoPlayerActivity6 = this.a;
            long currentPosition7 = this.b.getCurrentPosition();
            if (videoPlayerActivity6.A0) {
                Log.w("VideoPlayerActivity", "Already attempted OOM recovery this session");
                z6 = false;
            } else {
                videoPlayerActivity6.A0 = true;
                videoPlayerActivity6.F0 = true;
                Log.w("VideoPlayerActivity", "OOM detected at " + currentPosition7 + "ms — rebuilding with reduced buffers");
                ExoPlayer exoPlayer5 = videoPlayerActivity6.bm;
                if (exoPlayer5 != null) {
                    VideoPlayerActivity.c cVar4 = videoPlayerActivity6.g0;
                    if (cVar4 != null) {
                        exoPlayer5.removeListener(cVar4);
                    }
                    try {
                        exoPlayer5.stop();
                        exoPlayer5.clearMediaItems();
                        exoPlayer5.release();
                    } catch (Exception unused6) {
                    }
                }
                videoPlayerActivity6.bm = null;
                System.gc();
                videoPlayerActivity6.cq.postDelayed(new qx(videoPlayerActivity6, currentPosition7, 2), 100L);
                z6 = true;
            }
            if (z6) {
                return;
            }
        }
        int i13 = playbackException.errorCode;
        boolean z9 = i13 == 3001;
        if (i13 == 2000 && !VideoPlayerActivity.as(playbackException, OutOfMemoryError.class)) {
            String strAo = VideoPlayerActivity.ao(playbackException);
            if (n89.l(strAo, "varint", true) || n89.l(strAo, "Top bit not zero", true) || n89.l(strAo, "Invalid integer size", true) || n89.l(strAo, "CodecId missing", true) || n89.l(strAo, "TrackEntry", true)) {
                z2 = z9;
                z5 = true;
                z3 = z5;
            } else {
                if (VideoPlayerActivity.as(playbackException, ArrayIndexOutOfBoundsException.class)) {
                    if (!n89.l(strAo, "src.length=", false) && !n89.l(strAo, "dst.length=", false) && !n89.l(strAo, "srcPos=", false)) {
                        Throwable cause5 = playbackException;
                        loop4: while (cause5 != null) {
                            StackTraceElement[] stackTrace = cause5.getStackTrace();
                            cr4.f(stackTrace, "getStackTrace(...)");
                            int length3 = stackTrace.length;
                            int i14 = 0;
                            while (i14 < length3) {
                                StackTraceElement stackTraceElement = stackTrace[i14];
                                String className = stackTraceElement.getClassName();
                                Throwable th = cause5;
                                cr4.f(className, "getClassName(...)");
                                z2 = z9;
                                StackTraceElement[] stackTraceElementArr = stackTrace;
                                if (!n89.l(className, "extractor", true)) {
                                    String className2 = stackTraceElement.getClassName();
                                    cr4.f(className2, "getClassName(...)");
                                    if (!n89.l(className2, "MatroskaExtractor", true)) {
                                        String className3 = stackTraceElement.getClassName();
                                        cr4.f(className3, "getClassName(...)");
                                        if (!n89.l(className3, "DefaultExtractorInput", true)) {
                                            i14++;
                                            cause5 = th;
                                            z9 = z2;
                                            stackTrace = stackTraceElementArr;
                                        }
                                    }
                                }
                            }
                            cause5 = cause5.getCause();
                        }
                    }
                    z5 = true;
                    if (z5) {
                    }
                }
                z2 = z9;
                z5 = false;
                if (z5) {
                }
            }
            if (!z2 || z3) {
                cause = playbackException.getCause();
                if (cause != null || (message = cause.getMessage()) == null) {
                    message = "";
                }
                if (z2 || !(n89.l(message, "ContentCompAlgo", false) || n89.l(message, "not supported", false))) {
                    if (z3) {
                        ExecutorService executorService8 = kq.a;
                        kq.h("VideoPlayer", "Matroska parsing error via IO_UNSPECIFIED — attempting parser recovery");
                    }
                    if (VideoPlayerActivity.i(this.a, this.b.getCurrentPosition())) {
                        return;
                    }
                } else {
                    ExecutorService executorService9 = kq.a;
                    kq.h("VideoPlayer", "Unsupported container format: " + message + " — trying next source");
                    if (VideoPlayerActivity.v(this.a, this.b.getCurrentPosition())) {
                        return;
                    }
                }
            }
            if (playbackException.errorCode == 2004) {
                Throwable cause6 = playbackException.getCause();
                HttpDataSource.InvalidResponseCodeException invalidResponseCodeException = cause6 instanceof HttpDataSource.InvalidResponseCodeException ? (HttpDataSource.InvalidResponseCodeException) cause6 : null;
                if (invalidResponseCodeException != null && invalidResponseCodeException.responseCode == 416) {
                    VideoPlayerActivity videoPlayerActivity7 = this.a;
                    if (!videoPlayerActivity7.C0) {
                        videoPlayerActivity7.C0 = true;
                        ExecutorService executorService10 = kq.a;
                        kq.h("VideoPlayer", "HTTP 416 Range Not Satisfiable — retrying from position 0");
                        try {
                            this.b.stop();
                            this.b.clearMediaItems();
                            MediaItem.Builder builder = new MediaItem.Builder();
                            VideoPlayerActivity videoPlayerActivity8 = this.a;
                            MediaItem mediaItemBuild2 = builder.setUri(videoPlayerActivity8.an(videoPlayerActivity8.bs)).build();
                            cr4.f(mediaItemBuild2, "build(...)");
                            VideoPlayerActivity videoPlayerActivity9 = this.a;
                            this.b.setMediaSource(videoPlayerActivity9.ah(mediaItemBuild2, videoPlayerActivity9.bs));
                            this.b.seekTo(0L);
                            this.b.prepare();
                            this.b.setPlayWhenReady(true);
                            return;
                        } catch (Exception e3) {
                            ExecutorService executorService11 = kq.a;
                            kq.a("VideoPlayer", "416 retry failed", e3);
                            return;
                        }
                    }
                }
            }
            if (playbackException.errorCode == 2004) {
                Throwable cause7 = playbackException.getCause();
                HttpDataSource.InvalidResponseCodeException invalidResponseCodeException2 = cause7 instanceof HttpDataSource.InvalidResponseCodeException ? (HttpDataSource.InvalidResponseCodeException) cause7 : null;
                int i15 = invalidResponseCodeException2 != null ? invalidResponseCodeException2.responseCode : 0;
                if (500 <= i15 && i15 < 505) {
                    VideoPlayerActivity videoPlayerActivity10 = this.a;
                    int i16 = videoPlayerActivity10.D0;
                    if (i16 < videoPlayerActivity10.E0) {
                        int i17 = i16 + 1;
                        videoPlayerActivity10.D0 = i17;
                        long j = ((long) i17) * 1500;
                        ExecutorService executorService12 = kq.a;
                        StringBuilder sbR = vw2.r("HTTP ", i15, " — app-level retry #", " in ", i17);
                        sbR.append(j);
                        sbR.append("ms (same source)");
                        kq.h("VideoPlayer", sbR.toString());
                        try {
                            currentPosition3 = this.b.getCurrentPosition();
                        } catch (Exception unused7) {
                            currentPosition3 = 0;
                        }
                        VideoPlayerActivity videoPlayerActivity11 = this.a;
                        videoPlayerActivity11.cq.postDelayed(new lha(this.b, videoPlayerActivity11, currentPosition3, 0), j);
                        return;
                    }
                } else if (520 <= i15 && i15 < 523) {
                }
            }
            videoPlayerActivity = this.a;
            if (videoPlayerActivity.cg < 5 || videoPlayerActivity.ce.size() <= 1) {
                str = "";
            } else {
                try {
                    str = "";
                    currentPosition2 = this.b.getCurrentPosition();
                } catch (Exception unused8) {
                    str = "";
                    currentPosition2 = 0;
                }
                VideoPlayerActivity videoPlayerActivity12 = this.a;
                videoPlayerActivity12.cg++;
                try {
                    host = Uri.parse(videoPlayerActivity12.bs).getHost();
                } catch (Exception unused9) {
                    host = null;
                }
                Throwable cause8 = playbackException.getCause();
                HttpDataSource.InvalidResponseCodeException invalidResponseCodeException3 = cause8 instanceof HttpDataSource.InvalidResponseCodeException ? (HttpDataSource.InvalidResponseCodeException) cause8 : null;
                Integer numValueOf = invalidResponseCodeException3 != null ? Integer.valueOf(invalidResponseCodeException3.responseCode) : null;
                if (numValueOf == null) {
                    z4 = false;
                    if (!z4 && (((i2 = playbackException.errorCode) == 2004 || i2 == 2008 || i2 == 2001) && host != null)) {
                        videoPlayerActivity2 = this.a;
                        if (videoPlayerActivity2.ch.add(host)) {
                            ExecutorService executorService13 = kq.a;
                            kq.h("VideoPlayer", "Blacklisted host: " + host + " (failedHosts=" + videoPlayerActivity2.ch.size() + ")");
                        }
                    }
                    if (VideoPlayerActivity.v(this.a, currentPosition2)) {
                        return;
                    }
                    VideoPlayerActivity videoPlayerActivity13 = this.a;
                    if (videoPlayerActivity13.cf + 1 < videoPlayerActivity13.ce.size()) {
                        List list = ky8.a;
                        ky8.c = this.a.cf + 1;
                        ExecutorService executorService14 = kq.a;
                        kq.h("VideoPlayer", "No direct sources left — requesting SourcesActivity retry from index " + (this.a.cf + 1));
                        this.a.finish();
                        return;
                    }
                } else {
                    if (numValueOf.intValue() != 429) {
                        Integer num = numValueOf;
                        if (new dp4(CredentialOption.PRIORITY_OIDC_OR_SIMILAR, 504, 1).d(num.intValue()) || new dp4(520, 522, 1).d(num.intValue())) {
                        }
                        if (!z4) {
                            videoPlayerActivity2 = this.a;
                            if (videoPlayerActivity2.ch.add(host)) {
                            }
                        }
                        if (VideoPlayerActivity.v(this.a, currentPosition2)) {
                        }
                    }
                    z4 = true;
                    if (!z4) {
                    }
                    if (VideoPlayerActivity.v(this.a, currentPosition2)) {
                    }
                }
            }
            this.b.stop();
            this.b.clearMediaItems();
            i = playbackException.errorCode;
            if (i == 1003) {
                Throwable cause9 = playbackException.getCause();
                String str7 = (cause9 == null || (message2 = cause9.getMessage()) == null) ? str : message2;
                strI = n89.l(str7, "release timed out", true) ? "Player timed out\nThe player failed to initialize. Try selecting this source again." : n89.l(str7, "Stuck", true) ? "Playback stalled\nThe stream stopped responding. Try a different source." : "Connection timed out\nThe source didn't respond. Check your network or try a different source.";
            } else if (i == 2008) {
                strI = "Stream seek error\nCouldn't resume at this position. Try restarting playback.";
            } else if (i == 5001) {
                strI = "Audio output error\nCouldn't initialize audio. Check your audio output settings.";
            } else if (i == 2004) {
                boolean z10 = false;
                Throwable cause10 = playbackException.getCause();
                HttpDataSource.InvalidResponseCodeException invalidResponseCodeException4 = cause10 instanceof HttpDataSource.InvalidResponseCodeException ? (HttpDataSource.InvalidResponseCodeException) cause10 : null;
                int i18 = invalidResponseCodeException4 != null ? invalidResponseCodeException4.responseCode : 0;
                if (i18 == 401 || i18 == 403) {
                    strI = n41.i(i18, "Link expired (", ")\nThe debrid link is no longer valid. Go back and select a new source.");
                } else if (i18 == 404) {
                    strI = "Source not found (404)\nThis file was removed or the link expired. Try a different source.";
                } else if (i18 == 429) {
                    strI = "Rate limited (429)\nToo many requests to this source. Wait a moment and try again.";
                } else {
                    if (500 <= i18 && i18 < 600) {
                        z10 = true;
                    }
                    strI = z10 ? n41.i(i18, "Server error (", ")\nThe source server is having issues. Try a different source or provider.") : n41.i(i18, "Source unavailable (", ")\nThis link may have expired. Try a different source.");
                }
            } else if (i != 2005) {
                switch (i) {
                    case 2000:
                        String strAo2 = VideoPlayerActivity.ao(playbackException);
                        strI = !VideoPlayerActivity.as(playbackException, OutOfMemoryError.class) ? (!n89.l(strAo2, "varint", false) && !n89.l(strAo2, "Top bit not zero", false) && !n89.l(strAo2, "Invalid integer size", false) && !n89.l(strAo2, "srcPos=-", false)) ? !n89.l(strAo2, "timeout", true) ? "Stream error\nThe source stopped working mid-playback. Try a different source." : "Stream timed out\nThe connection to the source was lost. Try again or pick a different source." : "Corrupted stream\nThe video data is damaged. Try a different source." : "Out of memory\nThis stream requires too much memory. Try a lower quality source.";
                        break;
                    case PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED /* 2001 */:
                        String strAo3 = VideoPlayerActivity.ao(playbackException);
                        strI = !n89.l(strAo3, "Unable to resolve host", true) ? (!n89.l(strAo3, "SocketTimeout", true) && !n89.l(strAo3, "timeout", true)) ? !n89.l(strAo3, "Connection refused", true) ? !n89.l(strAo3, "Connection reset", true) ? "Network error\nCheck your connection or try a different source." : "Connection dropped\nThe server closed the connection. Try a different source." : "Connection refused\nThe source server is down. Try a different source." : "Connection timed out\nThe source server is slow or unreachable. Try a different source." : "DNS lookup failed\nCouldn't reach the server. Check your internet connection.";
                        break;
                    case PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT /* 2002 */:
                        break;
                    default:
                        switch (i) {
                            case 3001:
                                strI = "Corrupted file\nThis video file is damaged or incomplete. Try a different source.";
                                break;
                            case 3002:
                                strI = "Invalid manifest\nThe stream metadata is corrupt. Try a different source.";
                                break;
                            case 3003:
                                strI = "Unsupported format\nThis video container format isn't supported. Try a different source.";
                                break;
                            default:
                                switch (i) {
                                    case PlaybackException.ERROR_CODE_DECODER_INIT_FAILED /* 4001 */:
                                        strI = "Unsupported codec\nYour device can't decode this video format. Try a different source.";
                                        break;
                                    case PlaybackException.ERROR_CODE_DECODER_QUERY_FAILED /* 4002 */:
                                        strI = "Decoder error\nYour device couldn't initialize the video decoder. Try restarting the app.";
                                        break;
                                    case PlaybackException.ERROR_CODE_DECODING_FAILED /* 4003 */:
                                        strI = "Decoding failed\nThe video decoder crashed. Try a different source or lower quality.";
                                        break;
                                    default:
                                        strI = vw2.ab("Playback failed (", playbackException.getErrorCodeName(), ")\nTry a different source.");
                                        break;
                                }
                                break;
                        }
                        break;
                }
            } else {
                strI = "Source not found\nThis link has expired or the file was removed. Try a different source.";
            }
            this.a.bl(strI);
        }
        z2 = z9;
        if (!z2) {
            cause = playbackException.getCause();
            if (cause != null) {
                message = "";
                if (z2) {
                    if (z3) {
                    }
                    if (VideoPlayerActivity.i(this.a, this.b.getCurrentPosition())) {
                    }
                }
            }
        }
        if (playbackException.errorCode == 2004) {
        }
        if (playbackException.errorCode == 2004) {
        }
        videoPlayerActivity = this.a;
        if (videoPlayerActivity.cg < 5) {
            str = "";
        }
        this.b.stop();
        this.b.clearMediaItems();
        i = playbackException.errorCode;
        if (i == 1003) {
        }
        this.a.bl(strI);
    }

    @Override // androidx.media3.common.Player.Listener
    public final void onTracksChanged(Tracks tracks) {
        cr4.g(tracks, "tracks");
        VideoPlayerActivity videoPlayerActivity = this.a;
        VideoPlayerActivity.y(videoPlayerActivity, tracks);
        VideoPlayerActivity.f(videoPlayerActivity, tracks);
    }
}
