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

import android.content.Context;
import android.media.AudioDeviceInfo;
import android.media.AudioFormat;
import android.media.AudioTrack;
import android.os.Build;
import android.os.Looper;
import android.util.Pair;
import androidx.annotation.Nullable;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.PlaybackException;
import java.math.RoundingMode;
import java.util.Objects;

/* JADX INFO: loaded from: classes2.dex */
public final class em2 implements cl2 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public lt0 f10857a;

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public sk2 f10860d;

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

    @Nullable
    private final Context zza;

    @Nullable
    private final dm2 zzb;

    @Nullable
    private Looper zzg;

    @Nullable
    private Context zzh;

    public /* synthetic */ em2(cm2 cm2Var) {
        this.zza = cm2Var.b();
        hm2 hm2Var = cm2Var.f9988a;
        hm2Var.getClass();
        this.f10861e = hm2Var;
        this.f10859c = cm2Var.c();
        this.zzb = cm2Var.b() == null ? null : new dm2(this);
        this.f10858b = sl0.f16474a;
    }

    private static String zzl(@Nullable Looper looper) {
        return looper == null ? "null" : looper.getThread().getName();
    }

    /* JADX WARN: Removed duplicated region for block: B:9:0x003f  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final zk2 a(xk2 xk2Var) {
        d(xk2Var);
        hm2 hm2Var = this.f10861e;
        zn2 zn2Var = xk2Var.f18464a;
        ud0 ud0Var = xk2Var.f18465b;
        tk2 tk2VarA = hm2Var.a(zn2Var, ud0Var);
        yk2 yk2Var = new yk2();
        int i10 = 0;
        if (Objects.equals(zn2Var.zzo, MimeTypes.AUDIO_RAW)) {
            int i11 = zn2Var.f19350w;
            if (bb1.a(i11)) {
                i10 = i11 != 2 ? 1 : 2;
            } else {
                com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b.q(new StringBuilder(String.valueOf(i11).length() + 22), "Invalid PCM encoding: ", i11);
            }
        } else if (this.f10859c.zzd(zn2Var, ud0Var) != null) {
        }
        yk2Var.f18877d = i10;
        yk2Var.f18874a = tk2VarA.f16927a;
        yk2Var.f18875b = tk2VarA.f16928b;
        yk2Var.f18876c = tk2VarA.f16929c;
        return yk2Var.a();
    }

    /* JADX WARN: Removed duplicated region for block: B:33:0x0096  */
    /* JADX WARN: Removed duplicated region for block: B:34:0x009d  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final bl2 b(xk2 xk2Var) throws zzqd {
        int iIntValue;
        int iIntValue2;
        char c6;
        int iD;
        ud0 ud0Var;
        int iMax;
        int i10;
        int iM;
        d(xk2Var);
        zn2 zn2Var = xk2Var.f18464a;
        String str = zn2Var.zzo;
        boolean zEquals = Objects.equals(str, MimeTypes.AUDIO_RAW);
        ud0 ud0Var2 = xk2Var.f18465b;
        if (zEquals) {
            int i11 = zn2Var.f19350w;
            ih1.x(bb1.a(i11));
            int i12 = zn2Var.f19348u;
            int iB = bb1.b(i12);
            iD = bb1.d(i11) * i12;
            iIntValue2 = iB;
            c6 = 0;
            iIntValue = i11;
        } else {
            tk2 tk2Var = tk2.f16926d;
            Pair pairZzd = this.f10859c.zzd(zn2Var, ud0Var2);
            if (pairZzd == null) {
                throw new zzqd((String) p0.g.checkNotNull("Unable to configure passthrough for: ".concat(String.valueOf(zn2Var))));
            }
            iIntValue = ((Integer) pairZzd.first).intValue();
            iIntValue2 = ((Integer) pairZzd.second).intValue();
            c6 = 2;
            iD = -1;
        }
        boolean zEquals2 = Objects.equals(str, MimeTypes.AUDIO_DTS_EXPRESS);
        int i13 = zn2Var.f19333f;
        if (zEquals2 && i13 == -1) {
            i13 = 768000;
        }
        int i14 = zn2Var.f19349v;
        int iMax2 = xk2Var.f18468e;
        if (iMax2 != -1) {
            ud0Var = ud0Var2;
        } else {
            int minBufferSize = AudioTrack.getMinBufferSize(i14, iIntValue2, iIntValue);
            ih1.b1(minBufferSize != -2);
            if (iD == -1) {
                iD = 1;
            }
            int i15 = 250000;
            if (c6 != 0) {
                if (c6 != 1) {
                    if (iIntValue == 5) {
                        i15 = 500000;
                    } else {
                        if (iIntValue == 8) {
                            i15 = PlaybackException.CUSTOM_ERROR_CODE_BASE;
                            i10 = 8;
                        }
                        if (i13 == -1) {
                            iM = ih1.M(i13, 8, RoundingMode.CEILING);
                        } else {
                            int iA = r2.a(i10);
                            ih1.b1(iA != -2147483647);
                            iM = iA;
                        }
                        iMax = za2.b((((long) i15) * ((long) iM)) / 1000000);
                    }
                    i10 = iIntValue;
                    if (i13 == -1) {
                    }
                    iMax = za2.b((((long) i15) * ((long) iM)) / 1000000);
                } else {
                    int iA2 = r2.a(iIntValue);
                    ih1.b1(iA2 != -2147483647);
                    iMax = za2.b((((long) iA2) * 50000000) / 1000000);
                }
                ud0Var = ud0Var2;
            } else {
                long j10 = i14;
                ud0Var = ud0Var2;
                long j11 = iD;
                int iB2 = za2.b(((((long) 250000) * j10) * j11) / 1000000);
                int iB3 = za2.b(((((long) 750000) * j10) * j11) / 1000000);
                String str2 = bb1.f9474a;
                iMax = Math.max(iB2, Math.min(minBufferSize * 4, iB3));
            }
            iMax2 = (((Math.max(minBufferSize, iMax) + iD) - 1) / iD) * iD;
        }
        al2 al2Var = new al2();
        ud0 ud0Var3 = ud0.f17210a;
        al2Var.f8736b = i14;
        al2Var.f8737c = iIntValue2;
        al2Var.f8735a = iIntValue;
        al2Var.f8738d = iMax2;
        al2Var.f8740f = xk2Var.f18466c;
        al2Var.f8739e = ud0Var;
        al2Var.f8741g = xk2Var.f18467d;
        return new bl2(al2Var);
    }

    public final zl2 c(bl2 bl2Var) throws zzqi {
        Context context;
        try {
            int i10 = bl2Var.f9574f;
            int i11 = bl2Var.f9575g;
            Context context2 = null;
            if (i11 != -1 && (context = this.zza) != null && Build.VERSION.SDK_INT >= 34) {
                Context context3 = this.zzh;
                if (context3 == null || context3.getDeviceId() != i11) {
                    this.zzh = context.createDeviceContext(i11);
                }
                context2 = this.zzh;
                i10 = 0;
            }
            AudioTrack.Builder sessionId = new AudioTrack.Builder().setAudioAttributes(bl2Var.f9573e.a()).setAudioFormat(new AudioFormat.Builder().setSampleRate(bl2Var.f9570b).setChannelMask(bl2Var.f9571c).setEncoding(bl2Var.f9569a).build()).setTransferMode(1).setBufferSizeInBytes(bl2Var.f9572d).setSessionId(i10);
            int i12 = Build.VERSION.SDK_INT;
            if (i12 >= 29) {
                sessionId.setOffloadedPlayback(false);
            }
            if (i12 >= 34 && context2 != null) {
                sessionId.setContext(context2);
            }
            AudioTrack audioTrackBuild = sessionId.build();
            if (audioTrackBuild.getState() == 1) {
                return new zl2(audioTrackBuild, bl2Var, this.zzb, this.f10858b);
            }
            try {
                audioTrackBuild.release();
            } catch (Exception unused) {
            }
            throw new zzqi();
        } catch (IllegalArgumentException e10) {
            e = e10;
            throw new zzqi(e);
        } catch (UnsupportedOperationException e11) {
            e = e11;
            throw new zzqi(e);
        }
    }

    public final void d(xk2 xk2Var) {
        Context context;
        e();
        sk2 sk2Var = this.f10860d;
        ud0 ud0Var = xk2Var.f18465b;
        if (sk2Var == null && (context = this.zza) != null) {
            sk2 sk2Var2 = new sk2(context, new bm2(this), ud0Var, xk2Var.zzc);
            this.f10860d = sk2Var2;
            this.f10859c = sk2Var2.b();
        } else if (sk2Var != null) {
            AudioDeviceInfo audioDeviceInfo = xk2Var.zzc;
            if (audioDeviceInfo != null) {
                sk2Var.zzc(audioDeviceInfo);
            }
            this.f10860d.a(ud0Var);
        }
        this.f10859c.getClass();
    }

    public final void e() {
        if (this.zza == null) {
            return;
        }
        Looper looperMyLooper = Looper.myLooper();
        Looper looper = this.zzg;
        boolean z10 = true;
        if (looper != null && looper != looperMyLooper) {
            z10 = false;
        }
        String strZzl = zzl(looper);
        String strZzl2 = zzl(looperMyLooper);
        if (!z10) {
            throw new IllegalStateException(ih1.B0("AudioTrackAudioOutputProvider accessed on multiple threads: %s and %s", strZzl, strZzl2));
        }
        this.zzg = looperMyLooper;
    }
}
