package m4;

import I2.C0334s;
import com.google.android.gms.common.internal.ServiceSpecificExtraArgs;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import k4.C2389b;
import k4.C2408v;
import k4.C2410x;
import k4.InterfaceC2402o;
import r4.C2888a;

/* JADX INFO: loaded from: classes3.dex */
public class Q implements InterfaceC2547u {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public volatile boolean f20726a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public InterfaceC2553w f20727b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public InterfaceC2547u f20728c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public k4.r0 f20729d;

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public List f20730e = new ArrayList();

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public ArrayList f20734i = new ArrayList();

    @Override // m4.h2
    public final void a(boolean z6) {
        A4.g.t(this.f20727b != null, "May only be called after start");
        if (this.f20726a) {
            this.f20728c.a(z6);
        } else {
            o(new F(1, this, z6));
        }
    }

    @Override // m4.InterfaceC2547u
    public final C2389b b() {
        InterfaceC2547u interfaceC2547u;
        synchronized (this) {
            interfaceC2547u = this.f20728c;
        }
        return interfaceC2547u != null ? interfaceC2547u.b() : C2389b.f19841b;
    }

    @Override // m4.InterfaceC2547u
    public final void c(int i6) {
        A4.g.t(this.f20727b == null, "May only be called before start");
        this.f20734i.add(new N(this, i6, 2));
    }

    @Override // m4.h2
    public final void d(InterfaceC2402o interfaceC2402o) {
        A4.g.t(this.f20727b == null, "May only be called before start");
        this.f20734i.add(new E(6, this, interfaceC2402o));
    }

    @Override // m4.InterfaceC2547u
    public void e(C0334s c0334s) {
        synchronized (this) {
            try {
                if (this.f20727b == null) {
                    return;
                }
                if (this.f20728c != null) {
                    c0334s.g(Long.valueOf(this.f20733h - this.f20732g), "buffered_nanos");
                    this.f20728c.e(c0334s);
                } else {
                    c0334s.g(Long.valueOf(System.nanoTime() - this.f20732g), "buffered_nanos");
                    c0334s.f2658b.add("waiting_for_connection");
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // m4.h2
    public final void f(C2888a c2888a) {
        A4.g.t(this.f20727b != null, "May only be called after start");
        if (this.f20726a) {
            this.f20728c.f(c2888a);
        } else {
            o(new E(9, this, c2888a));
        }
    }

    @Override // m4.h2
    public final void flush() {
        A4.g.t(this.f20727b != null, "May only be called after start");
        if (this.f20726a) {
            this.f20728c.flush();
        } else {
            o(new O(this, 2));
        }
    }

    @Override // m4.InterfaceC2547u
    public void g(k4.r0 r0Var) {
        boolean z6 = false;
        boolean z7 = true;
        A4.g.t(this.f20727b != null, "May only be called after start");
        A4.g.o(r0Var, "reason");
        synchronized (this) {
            try {
                InterfaceC2547u interfaceC2547u = this.f20728c;
                if (interfaceC2547u == null) {
                    C2511h1 c2511h1 = C2511h1.f21047a;
                    if (interfaceC2547u != null) {
                        z7 = false;
                    }
                    A4.g.r(interfaceC2547u, "realStream already set to %s", z7);
                    this.f20728c = c2511h1;
                    this.f20733h = System.nanoTime();
                    this.f20729d = r0Var;
                } else {
                    z6 = true;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        if (z6) {
            o(new E(10, this, r0Var));
            return;
        }
        p();
        r(r0Var);
        this.f20727b.b(r0Var, EnumC2550v.f21201a, new k4.g0());
    }

    @Override // m4.InterfaceC2547u
    public final void h() {
        A4.g.t(this.f20727b != null, "May only be called after start");
        o(new O(this, 3));
    }

    @Override // m4.h2
    public final void i(int i6) {
        A4.g.t(this.f20727b != null, "May only be called after start");
        if (this.f20726a) {
            this.f20728c.i(i6);
        } else {
            o(new N(this, i6, 0));
        }
    }

    @Override // m4.h2
    public final boolean isReady() {
        if (this.f20726a) {
            return this.f20728c.isReady();
        }
        return false;
    }

    @Override // m4.InterfaceC2547u
    public final void j(int i6) {
        A4.g.t(this.f20727b == null, "May only be called before start");
        this.f20734i.add(new N(this, i6, 1));
    }

    @Override // m4.InterfaceC2547u
    public final void k(C2410x c2410x) {
        A4.g.t(this.f20727b == null, "May only be called before start");
        A4.g.o(c2410x, "decompressorRegistry");
        this.f20734i.add(new E(7, this, c2410x));
    }

    @Override // m4.InterfaceC2547u
    public final void l(InterfaceC2553w interfaceC2553w) {
        k4.r0 r0Var;
        boolean z6;
        A4.g.o(interfaceC2553w, ServiceSpecificExtraArgs.CastExtraArgs.LISTENER);
        A4.g.t(this.f20727b == null, "already started");
        synchronized (this) {
            try {
                r0Var = this.f20729d;
                z6 = this.f20726a;
                if (!z6) {
                    P p6 = new P(interfaceC2553w);
                    this.f20731f = p6;
                    interfaceC2553w = p6;
                }
                this.f20727b = interfaceC2553w;
                this.f20732g = System.nanoTime();
            } catch (Throwable th) {
                throw th;
            }
        }
        if (r0Var != null) {
            interfaceC2553w.b(r0Var, EnumC2550v.f21201a, new k4.g0());
        } else if (z6) {
            q(interfaceC2553w);
        }
    }

    @Override // m4.h2
    public final void m() {
        A4.g.t(this.f20727b == null, "May only be called before start");
        this.f20734i.add(new O(this, 0));
    }

    @Override // m4.InterfaceC2547u
    public final void n(C2408v c2408v) {
        A4.g.t(this.f20727b == null, "May only be called before start");
        this.f20734i.add(new E(8, this, c2408v));
    }

    public final void o(Runnable runnable) {
        A4.g.t(this.f20727b != null, "May only be called after start");
        synchronized (this) {
            try {
                if (this.f20726a) {
                    runnable.run();
                } else {
                    this.f20730e.add(runnable);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:32:0x0057, code lost:
    
        r0 = r1.iterator();
     */
    /* JADX WARN: Code restructure failed: missing block: B:34:0x005f, code lost:
    
        if (r0.hasNext() == false) goto L44;
     */
    /* JADX WARN: Code restructure failed: missing block: B:35:0x0061, code lost:
    
        ((java.lang.Runnable) r0.next()).run();
     */
    /* JADX WARN: Removed duplicated region for block: B:27:0x004f A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:9:0x0019  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void p() {
        P p6;
        List list;
        List list2;
        List arrayList = new ArrayList();
        while (true) {
            synchronized (this) {
                if (this.f20730e.isEmpty()) {
                    break;
                }
                list2 = this.f20730e;
                this.f20730e = arrayList;
            }
            if (p6 != null) {
                return;
            }
            List arrayList2 = new ArrayList();
            while (true) {
                synchronized (p6) {
                    try {
                        if (p6.f20717c.isEmpty()) {
                            p6.f20717c = null;
                            p6.f20716b = true;
                            return;
                        } else {
                            list = p6.f20717c;
                            p6.f20717c = arrayList2;
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                Iterator it = list.iterator();
                while (it.hasNext()) {
                    ((Runnable) it.next()).run();
                }
                list.clear();
                arrayList2 = list;
            }
            list2.clear();
            arrayList = list2;
        }
        this.f20730e = null;
        this.f20726a = true;
        p6 = this.f20731f;
        if (p6 != null) {
        }
    }

    public final void q(InterfaceC2553w interfaceC2553w) {
        Iterator it = this.f20734i.iterator();
        while (it.hasNext()) {
            ((Runnable) it.next()).run();
        }
        this.f20734i = null;
        this.f20728c.l(interfaceC2553w);
    }

    public final O s(InterfaceC2547u interfaceC2547u) {
        synchronized (this) {
            try {
                if (this.f20728c != null) {
                    return null;
                }
                A4.g.o(interfaceC2547u, "stream");
                InterfaceC2547u interfaceC2547u2 = this.f20728c;
                A4.g.r(interfaceC2547u2, "realStream already set to %s", interfaceC2547u2 == null);
                this.f20728c = interfaceC2547u;
                this.f20733h = System.nanoTime();
                InterfaceC2553w interfaceC2553w = this.f20727b;
                if (interfaceC2553w == null) {
                    this.f20730e = null;
                    this.f20726a = true;
                }
                if (interfaceC2553w == null) {
                    return null;
                }
                q(interfaceC2553w);
                return new O(this, 1);
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public void r(k4.r0 r0Var) {
    }
}
