package a0;

import a0.d;
import android.os.Handler;
import b0.AbstractC0399d;
import b0.C0395A;
import b0.InterfaceC0400e;

/* JADX INFO: loaded from: classes.dex */
public final class j implements d {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final Handler f1952a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final d.a f1953b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final InterfaceC0400e f1954c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final b0.y f1955d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private long f1956e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private long f1957f;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private int f1959h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private long f1960i;

    class a implements Runnable {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ int f1961b;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        final /* synthetic */ long f1962g;

        /* JADX INFO: renamed from: r, reason: collision with root package name */
        final /* synthetic */ long f1963r;

        a(int i2, long j2, long j3) {
            this.f1961b = i2;
            this.f1962g = j2;
            this.f1963r = j3;
        }

        @Override // java.lang.Runnable
        public void run() {
            j.this.f1953b.onBandwidthSample(this.f1961b, this.f1962g, this.f1963r);
        }
    }

    public j() {
        this(null, null);
    }

    private void f(int i2, long j2, long j3) {
        Handler handler = this.f1952a;
        if (handler == null || this.f1953b == null) {
            return;
        }
        handler.post(new a(i2, j2, j3));
    }

    @Override // a0.w
    public synchronized void a() {
        try {
            AbstractC0399d.e(this.f1959h > 0);
            long jA = this.f1954c.a();
            int i2 = (int) (jA - this.f1957f);
            if (i2 > 0) {
                long j2 = this.f1956e;
                long j3 = (8000 * j2) / ((long) i2);
                this.f1960i = j3;
                this.f1955d.a((int) Math.sqrt(j2), j3);
                float fD = this.f1955d.d(0.5f);
                long j4 = Float.isNaN(fD) ? -1L : (long) fD;
                this.f1958g = j4;
                f(i2, this.f1956e, j4);
            }
            int i3 = this.f1959h - 1;
            this.f1959h = i3;
            if (i3 > 0) {
                this.f1957f = jA;
            }
            this.f1956e = 0L;
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // a0.w
    public synchronized void b() {
        try {
            if (this.f1959h == 0) {
                this.f1957f = this.f1954c.a();
            }
            this.f1959h++;
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // a0.w
    public synchronized void c(int i2) {
        this.f1956e += (long) i2;
    }

    @Override // a0.d
    public synchronized long d() {
        return this.f1958g;
    }

    public j(Handler handler, d.a aVar) {
        this(handler, aVar, new C0395A());
    }

    public j(Handler handler, d.a aVar, InterfaceC0400e interfaceC0400e) {
        this(handler, aVar, interfaceC0400e, 2000);
    }

    public j(Handler handler, d.a aVar, InterfaceC0400e interfaceC0400e, int i2) {
        this.f1952a = handler;
        this.f1953b = aVar;
        this.f1954c = interfaceC0400e;
        this.f1955d = new b0.y(i2);
        this.f1958g = -1L;
    }
}
