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

import android.content.Context;
import android.media.MediaCodec;
import android.media.MediaCrypto;
import android.os.Build;
import android.os.HandlerThread;
import android.os.Trace;
import android.view.Surface;
import androidx.annotation.Nullable;
import androidx.media3.common.MimeTypes;
import java.io.IOException;

/* JADX INFO: loaded from: classes2.dex */
public final class ln2 implements pn2 {

    @Nullable
    private final Context zza;

    public ln2(Context context, @Nullable uq1 uq1Var, @Nullable uq1 uq1Var2) {
        this.zza = context;
    }

    /* JADX WARN: Removed duplicated region for block: B:66:0x010c  */
    @Override // com.google.android.gms.internal.ads.pn2
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final rn2 b(on2 on2Var) throws Exception {
        String str;
        MediaCodec mediaCodecCreateByCodecName;
        Context context;
        int i10 = Build.VERSION.SDK_INT;
        int i11 = 8;
        fn2 fn2Var = null;
        mediaCodec = null;
        MediaCodec mediaCodec = null;
        fn2Var = null;
        if (i10 < 31 && ((context = this.zza) == null || i10 < 28 || !context.getPackageManager().hasSystemFeature("com.amazon.hardware.tv_screen"))) {
            try {
                tn2 tn2Var = on2Var.f15117a;
                String str2 = tn2Var.f16954a;
                Trace.beginSection("createCodec:".concat(str2));
                MediaCodec mediaCodecCreateByCodecName2 = MediaCodec.createByCodecName(str2);
                Trace.endSection();
                try {
                    Trace.beginSection("configureCodec");
                    Surface surface = on2Var.zzd;
                    if (surface != null || !tn2Var.f16960g || i10 < 35) {
                        i11 = 0;
                    }
                    mediaCodecCreateByCodecName2.configure(on2Var.f15118b, surface, (MediaCrypto) null, i11);
                    Trace.endSection();
                    Trace.beginSection("startCodec");
                    mediaCodecCreateByCodecName2.start();
                    Trace.endSection();
                    return new eo2(mediaCodecCreateByCodecName2, on2Var.zzf);
                } catch (IOException e10) {
                    e = e10;
                    mediaCodec = mediaCodecCreateByCodecName2;
                    if (mediaCodec != null) {
                        mediaCodec.release();
                    }
                    throw e;
                } catch (RuntimeException e11) {
                    e = e11;
                    mediaCodec = mediaCodecCreateByCodecName2;
                    if (mediaCodec != null) {
                    }
                    throw e;
                }
            } catch (IOException e12) {
                e = e12;
            } catch (RuntimeException e13) {
                e = e13;
            }
        } else {
            int iZzf = hc.zzf(on2Var.f15119c.zzo);
            switch (iZzf) {
                case -2:
                    str = "none";
                    break;
                case -1:
                    str = "unknown";
                    break;
                case 0:
                    str = "default";
                    break;
                case 1:
                    str = "audio";
                    break;
                case 2:
                    str = "video";
                    break;
                case 3:
                    str = MimeTypes.BASE_TYPE_TEXT;
                    break;
                case 4:
                    str = MimeTypes.BASE_TYPE_IMAGE;
                    break;
                case 5:
                    str = "metadata";
                    break;
                default:
                    str = "camera motion";
                    break;
            }
            vt0.b("Creating an asynchronous MediaCodec adapter for track type ".concat(str));
            uz0 uz0Var = new uz0(iZzf);
            tn2 tn2Var2 = on2Var.f15117a;
            String str3 = tn2Var2.f16954a;
            try {
                StringBuilder sb = new StringBuilder(str3.length() + 12);
                sb.append("createCodec:");
                sb.append(str3);
                Trace.beginSection(sb.toString());
                mediaCodecCreateByCodecName = MediaCodec.createByCodecName(str3);
                try {
                    fn2 fn2Var2 = new fn2(mediaCodecCreateByCodecName, (HandlerThread) ((uq1) uz0Var.f17461c).zza(), new hn2(mediaCodecCreateByCodecName, (HandlerThread) ((uq1) uz0Var.f17460b).zza()), on2Var.zzf);
                    try {
                        Trace.endSection();
                        Surface surface2 = on2Var.zzd;
                        if (surface2 != null || !tn2Var2.f16960g || i10 < 35) {
                            i11 = 0;
                        }
                        fn2Var2.l(on2Var.f15118b, surface2, i11);
                        return fn2Var2;
                    } catch (Exception e14) {
                        e = e14;
                        fn2Var = fn2Var2;
                        if (fn2Var != null) {
                            fn2Var.zzl();
                        } else if (mediaCodecCreateByCodecName != null) {
                            mediaCodecCreateByCodecName.release();
                        }
                        throw e;
                    }
                } catch (Exception e15) {
                    e = e15;
                }
            } catch (Exception e16) {
                e = e16;
                mediaCodecCreateByCodecName = null;
            }
        }
    }
}
