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

import android.media.AudioDeviceInfo;
import android.media.AudioTrack;
import android.os.Build;
import android.os.Handler;
import androidx.annotation.GuardedBy;
import androidx.annotation.Nullable;
import java.nio.ByteBuffer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes2.dex */
public final class zl2 implements vk2 {

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public static final Object f19300l = new Object();

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public static int f19301m;

    @Nullable
    @GuardedBy("releaseExecutorLock")
    private static ScheduledExecutorService zzb;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final AudioTrack f19302a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final bl2 f19303b;

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final lt0 f19307f;

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

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

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

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

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

    @Nullable
    private rl2 zzf;

    @Nullable
    private final yl2 zzj;

    @Nullable
    private final dm2 zzr;

    public zl2(AudioTrack audioTrack, bl2 bl2Var, @Nullable dm2 dm2Var, sl0 sl0Var) {
        this.f19302a = audioTrack;
        this.f19303b = bl2Var;
        this.zzr = dm2Var;
        lt0 lt0Var = new lt0(Thread.currentThread());
        this.f19307f = lt0Var;
        lt0Var.f13864g = false;
        boolean zA = bb1.a(bl2Var.f9569a);
        this.f19305d = zA;
        if (zA) {
            this.f19306e = bb1.d(bl2Var.f9569a) * Integer.bitCount(bl2Var.f9571c);
        } else {
            this.f19306e = -1;
        }
        this.f19304c = new fm2(new sl2(this), sl0Var, audioTrack, bl2Var.f9569a, this.f19306e, bl2Var.f9572d);
        if (dm2Var != null) {
            this.zzf = new rl2(audioTrack, dm2Var);
        }
        this.zzj = c() ? new yl2(this) : null;
    }

    public static /* synthetic */ void d(AudioTrack audioTrack, Handler handler, final lt0 lt0Var) {
        try {
            audioTrack.flush();
            audioTrack.release();
            if (handler.getLooper().getThread().isAlive()) {
                final int i10 = 0;
                handler.post(new Runnable() { // from class: com.google.android.gms.internal.ads.ol2
                    @Override // java.lang.Runnable
                    public final /* synthetic */ void run() {
                        ml2 ml2Var = ml2.f14259a;
                        int i11 = i10;
                        lt0 lt0Var2 = lt0Var;
                        switch (i11) {
                            case 0:
                                lt0Var2.c(-1, ml2Var);
                                lt0Var2.d();
                                break;
                            default:
                                lt0Var2.c(-1, ml2Var);
                                lt0Var2.d();
                                break;
                        }
                    }
                });
            }
            synchronized (f19300l) {
                try {
                    int i11 = f19301m - 1;
                    f19301m = i11;
                    if (i11 == 0) {
                        ScheduledExecutorService scheduledExecutorService = zzb;
                        scheduledExecutorService.getClass();
                        scheduledExecutorService.shutdown();
                        zzb = null;
                    }
                } finally {
                }
            }
        } catch (Throwable th) {
            if (handler.getLooper().getThread().isAlive()) {
                final int i12 = 1;
                handler.post(new Runnable() { // from class: com.google.android.gms.internal.ads.ol2
                    @Override // java.lang.Runnable
                    public final /* synthetic */ void run() {
                        ml2 ml2Var = ml2.f14259a;
                        int i112 = i12;
                        lt0 lt0Var2 = lt0Var;
                        switch (i112) {
                            case 0:
                                lt0Var2.c(-1, ml2Var);
                                lt0Var2.d();
                                break;
                            default:
                                lt0Var2.c(-1, ml2Var);
                                lt0Var2.d();
                                break;
                        }
                    }
                });
            }
            synchronized (f19300l) {
                try {
                    int i13 = f19301m - 1;
                    f19301m = i13;
                    if (i13 == 0) {
                        ScheduledExecutorService scheduledExecutorService2 = zzb;
                        scheduledExecutorService2.getClass();
                        scheduledExecutorService2.shutdown();
                        zzb = null;
                    }
                    throw th;
                } finally {
                }
            }
        }
    }

    public final boolean a(int i10, ByteBuffer byteBuffer) throws zzqb {
        dm2 dm2Var;
        em2 em2Var;
        sk2 sk2Var;
        boolean z10 = this.f19305d;
        if (!z10 && this.f19311j == 0) {
            this.f19311j = qm2.c(this.f19303b.f9569a, byteBuffer);
        }
        e();
        AudioTrack audioTrack = this.f19302a;
        int underrunCount = audioTrack.getUnderrunCount();
        int i11 = this.f19312k;
        this.f19312k = underrunCount;
        if (underrunCount > i11) {
            nl2 nl2Var = nl2.f14664a;
            lt0 lt0Var = this.f19307f;
            lt0Var.c(-1, nl2Var);
            lt0Var.d();
        }
        int iRemaining = byteBuffer.remaining();
        int iWrite = audioTrack.write(byteBuffer, byteBuffer.remaining(), 1);
        if (iWrite >= 0) {
            z = iWrite == iRemaining;
            if (z10) {
                this.f19309h += (long) iWrite;
            } else if (z) {
                this.f19310i = (((long) this.f19311j) * ((long) i10)) + this.f19310i;
            }
            return z;
        }
        if (iWrite != -6 && iWrite != -32) {
            z = false;
        }
        if (z && (dm2Var = this.zzr) != null && (sk2Var = (em2Var = dm2Var.f10445a).f10860d) != null) {
            ok2 ok2Var = ok2.f15094c;
            em2Var.f10859c = ok2Var;
            sk2Var.f(ok2Var);
        }
        throw new zzqb(iWrite, z);
    }

    public final void b() {
        AudioTrack audioTrack = this.f19304c.f11291d;
        audioTrack.getClass();
        if (audioTrack.getPlayState() == 3) {
            this.f19302a.pause();
        }
        if (Build.VERSION.SDK_INT >= 29 && c()) {
            yl2 yl2Var = this.zzj;
            yl2Var.getClass();
            yl2Var.f18887c.f19302a.unregisterStreamEventCallback(yl2Var.f18886b);
            yl2Var.f18885a.removeCallbacksAndMessages(null);
        }
        rl2 rl2Var = this.zzf;
        if (rl2Var != null) {
            rl2Var.c();
            this.zzf = null;
        }
        AudioTrack audioTrack2 = this.f19302a;
        lt0 lt0Var = this.f19307f;
        Handler handlerZzc = bb1.zzc(null);
        synchronized (f19300l) {
            try {
                if (zzb == null) {
                    zzb = Executors.newSingleThreadScheduledExecutor(new x91());
                }
                f19301m++;
                zzb.schedule(new m1(audioTrack2, handlerZzc, lt0Var, 14), 20L, TimeUnit.MILLISECONDS);
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean c() {
        return Build.VERSION.SDK_INT >= 29 && this.f19302a.isOffloadedPlayback();
    }

    public final long e() {
        if (!this.f19305d) {
            return this.f19310i;
        }
        long j10 = this.f19309h;
        String str = bb1.f9474a;
        return ((j10 + r2) - 1) / this.f19306e;
    }

    @Override // com.google.android.gms.internal.ads.vk2
    public final void zzo(@Nullable AudioDeviceInfo audioDeviceInfo) {
        this.f19302a.setPreferredDevice(audioDeviceInfo);
    }
}
