package com.google.android.gms.internal.ads;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.net.Uri;
import android.view.Surface;
import android.view.TextureView;
import androidx.annotation.Nullable;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Iterator;

/* JADX INFO: loaded from: classes2.dex */
public final class zzcgw extends zzcfk implements TextureView.SurfaceTextureListener, u30 {
    public final boolean Q;
    public boolean R;
    public boolean S;
    public int T;
    public int U;
    public float V;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final c40 f19599c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final d40 f19600d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final b40 f19601e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public q30 f19602f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public Surface f19603g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public v30 f19604h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public String f19605i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public String[] f19606j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public boolean f19607k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public int f19608l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public a40 f19609m;

    @Nullable
    private final cr0 zzf;

    public zzcgw(Context context, d40 d40Var, c40 c40Var, boolean z10, boolean z11, b40 b40Var, @Nullable cr0 cr0Var) {
        super(context);
        this.f19608l = 1;
        this.f19599c = c40Var;
        this.f19600d = d40Var;
        this.Q = z10;
        this.f19601e = b40Var;
        d40Var.a(this);
        this.zzf = cr0Var;
    }

    public static String F(String str, Exception exc) {
        String canonicalName = exc.getClass().getCanonicalName();
        String message = exc.getMessage();
        int length = String.valueOf(canonicalName).length();
        StringBuilder sb = new StringBuilder(str.length() + 1 + length + 1 + String.valueOf(message).length());
        u.a.h(sb, str, "/", canonicalName, ":");
        sb.append(message);
        return sb.toString();
    }

    private final void zzV(boolean z10, @Nullable Integer num) {
        v30 v30Var = this.f19604h;
        if (v30Var != null && !z10) {
            v30Var.zzn(num);
            return;
        }
        if (this.f19605i == null || this.f19603g == null) {
            return;
        }
        if (z10) {
            if (!A()) {
                q7.j.d("No valid ExoPlayerAdapter exists when switch source.");
                return;
            } else {
                v30Var.A();
                C();
            }
        }
        if (this.f19605i.startsWith("cache:")) {
            w40 w40VarZzr = this.f19599c.zzr(this.f19605i);
            if (w40VarZzr instanceof d50) {
                d50 d50Var = (d50) w40VarZzr;
                synchronized (d50Var) {
                    d50Var.f10205g = true;
                    d50Var.notify();
                }
                n50 n50Var = d50Var.f10202d;
                n50Var.f14448k = null;
                d50Var.f10202d = null;
                this.f19604h = n50Var;
                n50Var.zzn(num);
                if (!this.f19604h.n()) {
                    q7.j.d("Precached video player has been released.");
                    return;
                }
            } else {
                if (!(w40VarZzr instanceof c50)) {
                    q7.j.d("Stream cache miss: ".concat(String.valueOf(this.f19605i)));
                    return;
                }
                c50 c50Var = (c50) w40VarZzr;
                p7.r0 r0Var = m7.n.C.f30464c;
                c40 c40Var = this.f19599c;
                r0Var.A(c40Var.getContext(), c40Var.zzs().f32212a);
                synchronized (c50Var.f9739k) {
                    try {
                        ByteBuffer byteBuffer = c50Var.f9737i;
                        if (byteBuffer != null && !c50Var.f9738j) {
                            byteBuffer.flip();
                            c50Var.f9738j = true;
                        }
                        c50Var.f9734f = true;
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                ByteBuffer byteBuffer2 = c50Var.f9737i;
                boolean z11 = c50Var.Q;
                String str = c50Var.f9732d;
                if (str == null) {
                    q7.j.d("Stream cache URL is null.");
                    return;
                } else {
                    v30 v30VarZzE = zzE(num);
                    this.f19604h = v30VarZzE;
                    v30VarZzE.w(new Uri[]{Uri.parse(str)}, byteBuffer2, z11);
                }
            }
        } else {
            this.f19604h = zzE(num);
            p7.r0 r0Var2 = m7.n.C.f30464c;
            c40 c40Var2 = this.f19599c;
            r0Var2.A(c40Var2.getContext(), c40Var2.zzs().f32212a);
            Uri[] uriArr = new Uri[this.f19606j.length];
            int i10 = 0;
            while (true) {
                String[] strArr = this.f19606j;
                if (i10 >= strArr.length) {
                    break;
                }
                uriArr[i10] = Uri.parse(strArr[i10]);
                i10++;
            }
            n50 n50Var2 = (n50) this.f19604h;
            n50Var2.getClass();
            n50Var2.w(uriArr, ByteBuffer.allocate(0), false);
        }
        ((n50) this.f19604h).f14448k = this;
        D(this.f19603g, false);
        if (this.f19604h.n()) {
            int iO = this.f19604h.o();
            this.f19608l = iO;
            if (iO == 3) {
                E();
            }
        }
    }

    public final boolean A() {
        v30 v30Var = this.f19604h;
        return (v30Var == null || !v30Var.n() || this.f19607k) ? false : true;
    }

    public final boolean B() {
        return A() && this.f19608l != 1;
    }

    public final void C() {
        if (this.f19604h != null) {
            D(null, true);
            v30 v30Var = this.f19604h;
            if (v30Var != null) {
                ((n50) v30Var).f14448k = null;
                v30Var.x();
                this.f19604h = null;
            }
            this.f19608l = 1;
            this.f19607k = false;
            this.R = false;
            this.S = false;
        }
    }

    public final void D(Surface surface, boolean z10) {
        v30 v30Var = this.f19604h;
        if (v30Var == null) {
            q7.j.d("Trying to set surface before player is initialized.");
        } else {
            try {
                v30Var.y(surface);
            } catch (IOException unused) {
            }
        }
    }

    public final void E() {
        if (this.R) {
            return;
        }
        this.R = true;
        p7.r0.f31763l.post(new g40(this, 0));
        zzq();
        this.f19600d.b();
        if (this.S) {
            l();
        }
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void a(int i10) {
        v30 v30Var = this.f19604h;
        if (v30Var != null) {
            g50 g50Var = ((n50) v30Var).f14441d;
            synchronized (g50Var) {
                g50Var.f11488d = ((long) i10) * 1000;
            }
        }
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void b(int i10) {
        v30 v30Var = this.f19604h;
        if (v30Var != null) {
            g50 g50Var = ((n50) v30Var).f14441d;
            synchronized (g50Var) {
                g50Var.f11489e = ((long) i10) * 1000;
            }
        }
    }

    @Override // com.google.android.gms.internal.ads.u30
    public final void c() {
        p7.r0.f31763l.post(new g40(this, 1));
    }

    @Override // com.google.android.gms.internal.ads.u30
    public final void d(long j10, boolean z10) {
        if (this.f19599c != null) {
            b30.f9364f.execute(new i40(this, z10, j10, 0));
        }
    }

    @Override // com.google.android.gms.internal.ads.u30
    public final void e(int i10, int i11) {
        this.T = i10;
        this.U = i11;
        float f10 = i11 > 0 ? i10 / i11 : 1.0f;
        if (this.V != f10) {
            this.V = f10;
            requestLayout();
        }
    }

    @Override // com.google.android.gms.internal.ads.u30
    public final void f(String str, Exception exc) {
        v30 v30Var;
        String strF = F(str, exc);
        q7.j.d("ExoPlayerAdapter error: ".concat(strF));
        this.f19607k = true;
        if (this.f19601e.f9382a && (v30Var = this.f19604h) != null) {
            v30Var.v(false);
        }
        p7.r0.f31763l.post(new h40(this, strF, 1));
        m7.n.C.f30469h.e("AdExoPlayerView.onError", exc);
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void g(int i10) {
        v30 v30Var = this.f19604h;
        if (v30Var != null) {
            Iterator it = ((n50) v30Var).V.iterator();
            while (it.hasNext()) {
                f50 f50Var = (f50) ((WeakReference) it.next()).get();
                if (f50Var != null) {
                    f50Var.f11039q = i10;
                    for (Socket socket : f50Var.f11040r) {
                        if (!socket.isClosed()) {
                            try {
                                socket.setReceiveBufferSize(f50Var.f11039q);
                            } catch (SocketException unused) {
                            }
                        }
                    }
                }
            }
        }
    }

    @Override // com.google.android.gms.internal.ads.u30
    public final void h(Exception exc) {
        String strF = F("onLoadException", exc);
        q7.j.d("ExoPlayerAdapter exception: ".concat(strF));
        m7.n.C.f30469h.e("AdExoPlayerView.onException", exc);
        p7.r0.f31763l.post(new h40(this, strF, 0));
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final String i() {
        return "ExoPlayer/2".concat(true != this.Q ? "" : " spherical");
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void j(q30 q30Var) {
        this.f19602f = q30Var;
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void k() {
        if (A()) {
            this.f19604h.A();
            C();
        }
        d40 d40Var = this.f19600d;
        d40Var.f10192k = false;
        f40 f40Var = this.f19585b;
        f40Var.f11016d = false;
        f40Var.a();
        d40Var.c();
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void l() {
        v30 v30Var;
        if (!B()) {
            this.S = true;
            return;
        }
        if (this.f19601e.f9382a && (v30Var = this.f19604h) != null) {
            v30Var.v(true);
        }
        this.f19604h.q(true);
        this.f19600d.e();
        f40 f40Var = this.f19585b;
        f40Var.f11016d = true;
        f40Var.a();
        this.f19584a.f17856c = true;
        p7.r0.f31763l.post(new g40(this, 3));
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void m() {
        v30 v30Var;
        if (B()) {
            if (this.f19601e.f9382a && (v30Var = this.f19604h) != null) {
                v30Var.v(false);
            }
            this.f19604h.q(false);
            this.f19600d.f10192k = false;
            f40 f40Var = this.f19585b;
            f40Var.f11016d = false;
            f40Var.a();
            p7.r0.f31763l.post(new g40(this, 4));
        }
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final int n() {
        if (B()) {
            return (int) this.f19604h.r();
        }
        return 0;
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final int o() {
        if (B()) {
            return (int) this.f19604h.p();
        }
        return 0;
    }

    @Override // android.view.TextureView, android.view.View
    public final void onAttachedToWindow() {
        super.onAttachedToWindow();
        setSurfaceTextureListener(this);
    }

    @Override // android.view.View
    public final void onMeasure(int i10, int i11) {
        super.onMeasure(i10, i11);
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        float f10 = this.V;
        if (f10 != 0.0f && this.f19609m == null) {
            float f11 = measuredWidth;
            float f12 = f11 / measuredHeight;
            if (f10 > f12) {
                measuredHeight = (int) (f11 / f10);
            }
            if (f10 < f12) {
                measuredWidth = (int) (measuredHeight * f10);
            }
        }
        setMeasuredDimension(measuredWidth, measuredHeight);
        a40 a40Var = this.f19609m;
        if (a40Var != null) {
            a40Var.a(measuredWidth, measuredHeight);
        }
    }

    @Override // android.view.TextureView.SurfaceTextureListener
    public final void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i10, int i11) {
        v30 v30Var;
        float f10;
        int i12;
        cr0 cr0Var;
        if (this.Q) {
            if (((Boolean) n7.y.f31093e.f31096c.b(ao.f9175ye)).booleanValue() && (cr0Var = this.zzf) != null) {
                br0 br0VarA = cr0Var.a();
                br0VarA.zzc("action", "svp_aepv");
                br0VarA.b();
            }
            a40 a40Var = new a40(getContext());
            this.f19609m = a40Var;
            a40Var.f8493m = i10;
            a40Var.f8492l = i11;
            a40Var.R = surfaceTexture;
            a40Var.start();
            SurfaceTexture surfaceTextureZze = a40Var.zze();
            if (surfaceTextureZze != null) {
                surfaceTexture = surfaceTextureZze;
            } else {
                this.f19609m.b();
                this.f19609m = null;
            }
        }
        Surface surface = new Surface(surfaceTexture);
        this.f19603g = surface;
        if (this.f19604h == null) {
            zzV(false, null);
        } else {
            D(surface, true);
            if (!this.f19601e.f9382a && (v30Var = this.f19604h) != null) {
                v30Var.v(true);
            }
        }
        int i13 = this.T;
        if (i13 == 0 || (i12 = this.U) == 0) {
            f10 = i11 > 0 ? i10 / i11 : 1.0f;
            if (this.V != f10) {
                this.V = f10;
                requestLayout();
            }
        } else {
            f10 = i12 > 0 ? i13 / i12 : 1.0f;
            if (this.V != f10) {
                this.V = f10;
                requestLayout();
            }
        }
        p7.r0.f31763l.post(new g40(this, 5));
    }

    @Override // android.view.TextureView.SurfaceTextureListener
    public final boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        m();
        a40 a40Var = this.f19609m;
        if (a40Var != null) {
            a40Var.b();
            this.f19609m = null;
        }
        v30 v30Var = this.f19604h;
        if (v30Var != null) {
            if (v30Var != null) {
                v30Var.v(false);
            }
            Surface surface = this.f19603g;
            if (surface != null) {
                surface.release();
            }
            this.f19603g = null;
            D(null, true);
        }
        p7.r0.f31763l.post(new g40(this, 6));
        return true;
    }

    @Override // android.view.TextureView.SurfaceTextureListener
    public final void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i10, int i11) {
        a40 a40Var = this.f19609m;
        if (a40Var != null) {
            a40Var.a(i10, i11);
        }
        p7.r0.f31763l.post(new p30(this, i10, i11));
    }

    @Override // android.view.TextureView.SurfaceTextureListener
    public final void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
        this.f19600d.d(this);
        this.f19584a.a(surfaceTexture, this.f19602f);
    }

    @Override // android.view.View
    public final void onWindowVisibilityChanged(int i10) {
        StringBuilder sb = new StringBuilder(String.valueOf(i10).length() + 46);
        sb.append("AdExoPlayerView3 window visibility changed to ");
        sb.append(i10);
        p7.l0.g(sb.toString());
        p7.r0.f31763l.post(new lh(i10, 4, this));
        super.onWindowVisibilityChanged(i10);
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void p(int i10) {
        if (B()) {
            this.f19604h.B(i10);
        }
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void q(float f10, float f11) {
        a40 a40Var = this.f19609m;
        if (a40Var != null) {
            a40Var.c(f10, f11);
        }
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final int r() {
        return this.T;
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final int s() {
        return this.U;
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final long t() {
        v30 v30Var = this.f19604h;
        if (v30Var != null) {
            return v30Var.s();
        }
        return -1L;
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final long u() {
        v30 v30Var = this.f19604h;
        if (v30Var != null) {
            return v30Var.t();
        }
        return -1L;
    }

    @Override // com.google.android.gms.internal.ads.u30
    public final void v(int i10) {
        v30 v30Var;
        if (this.f19608l != i10) {
            this.f19608l = i10;
            if (i10 == 3) {
                E();
                return;
            }
            if (i10 != 4) {
                return;
            }
            if (this.f19601e.f9382a && (v30Var = this.f19604h) != null) {
                v30Var.v(false);
            }
            this.f19600d.f10192k = false;
            f40 f40Var = this.f19585b;
            f40Var.f11016d = false;
            f40Var.a();
            p7.r0.f31763l.post(new g40(this, 2));
        }
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final long w() {
        v30 v30Var = this.f19604h;
        if (v30Var != null) {
            return v30Var.u();
        }
        return -1L;
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final int x() {
        v30 v30Var = this.f19604h;
        if (v30Var != null) {
            return ((n50) v30Var).f14450m;
        }
        return -1;
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void y(int i10) {
        v30 v30Var = this.f19604h;
        if (v30Var != null) {
            g50 g50Var = ((n50) v30Var).f14441d;
            synchronized (g50Var) {
                g50Var.f11486b = ((long) i10) * 1000;
            }
        }
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void z(int i10) {
        v30 v30Var = this.f19604h;
        if (v30Var != null) {
            g50 g50Var = ((n50) v30Var).f14441d;
            synchronized (g50Var) {
                g50Var.f11487c = ((long) i10) * 1000;
            }
        }
    }

    public final v30 zzE(@Nullable Integer num) {
        c40 c40Var = this.f19599c;
        n50 n50Var = new n50(c40Var.getContext(), this.f19601e, c40Var, num);
        q7.j.c("ExoPlayerAdapter initialized.");
        return n50Var;
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void zzc(@Nullable String str) {
        if (str != null) {
            zzx(str, null, null);
        }
    }

    @Override // com.google.android.gms.internal.ads.e40
    public final void zzq() {
        p7.r0.f31763l.post(new g40(this, 7));
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    @Nullable
    public final Integer zzw() {
        v30 v30Var = this.f19604h;
        if (v30Var != null) {
            return v30Var.zzj();
        }
        return null;
    }

    @Override // com.google.android.gms.internal.ads.zzcfk
    public final void zzx(@Nullable String str, @Nullable String[] strArr, @Nullable Integer num) {
        if (str == null) {
            return;
        }
        if (strArr == null) {
            this.f19606j = new String[]{str};
        } else {
            this.f19606j = (String[]) Arrays.copyOf(strArr, strArr.length);
        }
        String str2 = this.f19605i;
        boolean z10 = false;
        if (this.f19601e.f9392k && str2 != null && !str.equals(str2) && this.f19608l == 4) {
            z10 = true;
        }
        this.f19605i = str;
        zzV(z10, num);
    }
}
