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

import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Handler;
import android.os.HandlerThread;
import androidx.annotation.GuardedBy;
import androidx.annotation.Nullable;
import java.util.ArrayDeque;

/* JADX INFO: loaded from: classes2.dex */
public final class jn2 extends MediaCodec.Callback {

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

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

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

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

    @Nullable
    @GuardedBy("lock")
    private MediaFormat zzh;

    @Nullable
    @GuardedBy("lock")
    private MediaFormat zzi;

    @Nullable
    @GuardedBy("lock")
    private MediaCodec.CodecException zzj;

    @Nullable
    @GuardedBy("lock")
    private MediaCodec.CryptoException zzk;

    @Nullable
    @GuardedBy("lock")
    private IllegalStateException zzn;

    @Nullable
    @GuardedBy("lock")
    private qn2 zzo;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final androidx.collection.h f12938d = new androidx.collection.h();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final androidx.collection.h f12939e = new androidx.collection.h();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final ArrayDeque f12940f = new ArrayDeque();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final ArrayDeque f12941g = new ArrayDeque();

    public jn2(HandlerThread handlerThread) {
        this.f12936b = handlerThread;
    }

    public final int a(MediaCodec.BufferInfo bufferInfo) {
        synchronized (this.f12935a) {
            try {
                f();
                if (this.f12942h > 0 || this.f12943i) {
                    return -1;
                }
                androidx.collection.h hVar = this.f12939e;
                if (hVar.f937b == hVar.f938c) {
                    return -1;
                }
                int iB = hVar.b();
                if (iB >= 0) {
                    this.zzh.getClass();
                    MediaCodec.BufferInfo bufferInfo2 = (MediaCodec.BufferInfo) this.f12940f.remove();
                    bufferInfo.set(bufferInfo2.offset, bufferInfo2.size, bufferInfo2.presentationTimeUs, bufferInfo2.flags);
                } else if (iB == -2) {
                    this.zzh = (MediaFormat) this.f12941g.remove();
                    iB = -2;
                }
                return iB;
            } finally {
            }
        }
    }

    public final MediaFormat b() {
        MediaFormat mediaFormat;
        synchronized (this.f12935a) {
            try {
                mediaFormat = this.zzh;
                if (mediaFormat == null) {
                    throw new IllegalStateException();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return mediaFormat;
    }

    public final void c(un2 un2Var) {
        synchronized (this.f12935a) {
            this.zzo = un2Var;
        }
    }

    public final /* synthetic */ void d() {
        Object obj = this.f12935a;
        synchronized (obj) {
            try {
                if (this.f12943i) {
                    return;
                }
                long j10 = this.f12942h - 1;
                this.f12942h = j10;
                if (j10 > 0) {
                    return;
                }
                if (j10 >= 0) {
                    e();
                    return;
                }
                IllegalStateException illegalStateException = new IllegalStateException();
                synchronized (obj) {
                    this.zzn = illegalStateException;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void e() {
        ArrayDeque arrayDeque = this.f12941g;
        if (!arrayDeque.isEmpty()) {
            this.zzi = (MediaFormat) arrayDeque.getLast();
        }
        androidx.collection.h hVar = this.f12938d;
        hVar.f938c = hVar.f937b;
        androidx.collection.h hVar2 = this.f12939e;
        hVar2.f938c = hVar2.f937b;
        this.f12940f.clear();
        arrayDeque.clear();
    }

    public final void f() {
        IllegalStateException illegalStateException = this.zzn;
        if (illegalStateException != null) {
            this.zzn = null;
            throw illegalStateException;
        }
        MediaCodec.CodecException codecException = this.zzj;
        if (codecException != null) {
            this.zzj = null;
            throw codecException;
        }
        MediaCodec.CryptoException cryptoException = this.zzk;
        if (cryptoException == null) {
            return;
        }
        this.zzk = null;
        throw cryptoException;
    }

    @Override // android.media.MediaCodec.Callback
    public final void onCryptoError(MediaCodec mediaCodec, MediaCodec.CryptoException cryptoException) {
        synchronized (this.f12935a) {
            this.zzk = cryptoException;
        }
    }

    @Override // android.media.MediaCodec.Callback
    public final void onError(MediaCodec mediaCodec, MediaCodec.CodecException codecException) {
        synchronized (this.f12935a) {
            this.zzj = codecException;
        }
    }

    @Override // android.media.MediaCodec.Callback
    public final void onInputBufferAvailable(MediaCodec mediaCodec, int i10) {
        dj2 dj2Var;
        synchronized (this.f12935a) {
            try {
                this.f12938d.a(i10);
                qn2 qn2Var = this.zzo;
                if (qn2Var != null && (dj2Var = ((un2) qn2Var).f17345a.f18126c0) != null) {
                    ((vh2) dj2Var).a();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // android.media.MediaCodec.Callback
    public final void onOutputBufferAvailable(MediaCodec mediaCodec, int i10, MediaCodec.BufferInfo bufferInfo) {
        dj2 dj2Var;
        synchronized (this.f12935a) {
            try {
                MediaFormat mediaFormat = this.zzi;
                if (mediaFormat != null) {
                    this.f12939e.a(-2);
                    this.f12941g.add(mediaFormat);
                    this.zzi = null;
                }
                this.f12939e.a(i10);
                this.f12940f.add(bufferInfo);
                qn2 qn2Var = this.zzo;
                if (qn2Var != null && (dj2Var = ((un2) qn2Var).f17345a.f18126c0) != null) {
                    ((vh2) dj2Var).a();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // android.media.MediaCodec.Callback
    public final void onOutputFormatChanged(MediaCodec mediaCodec, MediaFormat mediaFormat) {
        synchronized (this.f12935a) {
            this.f12939e.a(-2);
            this.f12941g.add(mediaFormat);
            this.zzi = null;
        }
    }
}
