package I0;

import B0.RunnableC0014a;
import N1.C0224z;
import android.media.LoudnessCodecController;
import android.media.MediaCodec;
import android.media.MediaCrypto;
import android.media.MediaFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Trace;
import android.view.Surface;
import java.nio.ByteBuffer;
import java.util.HashSet;
import x0.AbstractC1709a;

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

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final MediaCodec f3191q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final h f3192r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final o f3193s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final A1.y f3194t;

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

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public int f3196v = 0;

    public d(MediaCodec mediaCodec, HandlerThread handlerThread, o oVar, A1.y yVar) {
        this.f3191q = mediaCodec;
        this.f3192r = new h(handlerThread);
        this.f3193s = oVar;
        this.f3194t = yVar;
    }

    public static void b(d dVar, MediaFormat mediaFormat, Surface surface, MediaCrypto mediaCrypto, int i) {
        A1.y yVar;
        LoudnessCodecController loudnessCodecController;
        h hVar = dVar.f3192r;
        AbstractC1709a.j(hVar.f3214c == null);
        HandlerThread handlerThread = hVar.f3213b;
        handlerThread.start();
        Handler handler = new Handler(handlerThread.getLooper());
        MediaCodec mediaCodec = dVar.f3191q;
        mediaCodec.setCallback(hVar, handler);
        hVar.f3214c = handler;
        Trace.beginSection("configureCodec");
        mediaCodec.configure(mediaFormat, surface, mediaCrypto, i);
        Trace.endSection();
        dVar.f3193s.start();
        Trace.beginSection("startCodec");
        mediaCodec.start();
        Trace.endSection();
        if (x0.v.f18553a >= 35 && (yVar = dVar.f3194t) != null && ((loudnessCodecController = (LoudnessCodecController) yVar.f365t) == null || loudnessCodecController.addMediaCodec(mediaCodec))) {
            AbstractC1709a.j(((HashSet) yVar.f363r).add(mediaCodec));
        }
        dVar.f3196v = 1;
    }

    public static String e(int i, String str) {
        StringBuilder sb = new StringBuilder(str);
        if (i == 1) {
            sb.append("Audio");
        } else if (i == 2) {
            sb.append("Video");
        } else {
            sb.append("Unknown(");
            sb.append(i);
            sb.append(")");
        }
        return sb.toString();
    }

    @Override // I0.n
    public final MediaFormat C() {
        MediaFormat mediaFormat;
        h hVar = this.f3192r;
        synchronized (hVar.f3212a) {
            try {
                mediaFormat = hVar.f3219h;
                if (mediaFormat == null) {
                    throw new IllegalStateException();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return mediaFormat;
    }

    @Override // I0.n
    public final void a() {
        A1.y yVar;
        A1.y yVar2;
        try {
            if (this.f3196v == 1) {
                this.f3193s.shutdown();
                h hVar = this.f3192r;
                synchronized (hVar.f3212a) {
                    hVar.f3223m = true;
                    hVar.f3213b.quit();
                    hVar.a();
                }
            }
            this.f3196v = 2;
            if (this.f3195u) {
                return;
            }
            try {
                int i = x0.v.f18553a;
                if (i >= 30 && i < 33) {
                    this.f3191q.stop();
                }
                if (i >= 35 && (yVar2 = this.f3194t) != null) {
                    yVar2.z(this.f3191q);
                }
                this.f3191q.release();
                this.f3195u = true;
            } finally {
            }
        } catch (Throwable th) {
            if (!this.f3195u) {
                try {
                    int i5 = x0.v.f18553a;
                    if (i5 >= 30 && i5 < 33) {
                        this.f3191q.stop();
                    }
                    if (i5 >= 35 && (yVar = this.f3194t) != null) {
                        yVar.z(this.f3191q);
                    }
                    this.f3191q.release();
                    this.f3195u = true;
                } finally {
                }
            }
            throw th;
        }
    }

    @Override // I0.n
    public final void c(Bundle bundle) {
        this.f3193s.c(bundle);
    }

    @Override // I0.n
    public final void d(int i, A0.d dVar, long j8, int i5) {
        this.f3193s.d(i, dVar, j8, i5);
    }

    @Override // I0.n
    public final void f(int i, int i5, long j8, int i8) {
        this.f3193s.f(i, i5, j8, i8);
    }

    @Override // I0.n
    public final void flush() {
        this.f3193s.flush();
        this.f3191q.flush();
        h hVar = this.f3192r;
        synchronized (hVar.f3212a) {
            hVar.f3222l++;
            Handler handler = hVar.f3214c;
            int i = x0.v.f18553a;
            handler.post(new RunnableC0014a(12, hVar));
        }
        this.f3191q.start();
    }

    @Override // I0.n
    public final int g(MediaCodec.BufferInfo bufferInfo) {
        this.f3193s.a();
        h hVar = this.f3192r;
        synchronized (hVar.f3212a) {
            try {
                IllegalStateException illegalStateException = hVar.f3224n;
                if (illegalStateException != null) {
                    hVar.f3224n = null;
                    throw illegalStateException;
                }
                MediaCodec.CodecException codecException = hVar.f3220j;
                if (codecException != null) {
                    hVar.f3220j = null;
                    throw codecException;
                }
                MediaCodec.CryptoException cryptoException = hVar.f3221k;
                if (cryptoException != null) {
                    hVar.f3221k = null;
                    throw cryptoException;
                }
                if (hVar.f3222l > 0 || hVar.f3223m) {
                    return -1;
                }
                C0224z c0224z = hVar.f3216e;
                int i = c0224z.f4726b;
                int i5 = c0224z.f4727c;
                if (i == i5) {
                    return -1;
                }
                if (i == i5) {
                    throw new ArrayIndexOutOfBoundsException();
                }
                int i8 = c0224z.f4725a[i];
                c0224z.f4726b = c0224z.f4728d & (i + 1);
                if (i8 >= 0) {
                    AbstractC1709a.k(hVar.f3219h);
                    MediaCodec.BufferInfo bufferInfo2 = (MediaCodec.BufferInfo) hVar.f3217f.remove();
                    bufferInfo.set(bufferInfo2.offset, bufferInfo2.size, bufferInfo2.presentationTimeUs, bufferInfo2.flags);
                } else if (i8 == -2) {
                    hVar.f3219h = (MediaFormat) hVar.f3218g.remove();
                }
                return i8;
            } finally {
            }
        }
    }

    @Override // I0.n
    public final void j(long j8, int i) {
        this.f3191q.releaseOutputBuffer(i, j8);
    }

    @Override // I0.n
    public final void k() {
        this.f3191q.detachOutputSurface();
    }

    @Override // I0.n
    public final ByteBuffer l(int i) {
        return this.f3191q.getInputBuffer(i);
    }

    @Override // I0.n
    public final void m(Surface surface) {
        this.f3191q.setOutputSurface(surface);
    }

    @Override // I0.n
    public final void n(int i) {
        this.f3191q.releaseOutputBuffer(i, false);
    }

    @Override // I0.n
    public final ByteBuffer q(int i) {
        return this.f3191q.getOutputBuffer(i);
    }

    @Override // I0.n
    public final void r(R0.i iVar, Handler handler) {
        this.f3191q.setOnFrameRenderedListener(new b(this, iVar, 0), handler);
    }

    @Override // I0.n
    public final boolean t(K4.c cVar) {
        h hVar = this.f3192r;
        synchronized (hVar.f3212a) {
            hVar.f3225o = cVar;
        }
        return true;
    }

    @Override // I0.n
    public final int y() {
        this.f3193s.a();
        h hVar = this.f3192r;
        synchronized (hVar.f3212a) {
            try {
                IllegalStateException illegalStateException = hVar.f3224n;
                if (illegalStateException != null) {
                    hVar.f3224n = null;
                    throw illegalStateException;
                }
                MediaCodec.CodecException codecException = hVar.f3220j;
                if (codecException != null) {
                    hVar.f3220j = null;
                    throw codecException;
                }
                MediaCodec.CryptoException cryptoException = hVar.f3221k;
                if (cryptoException != null) {
                    hVar.f3221k = null;
                    throw cryptoException;
                }
                int i = -1;
                if (hVar.f3222l > 0 || hVar.f3223m) {
                    return -1;
                }
                C0224z c0224z = hVar.f3215d;
                int i5 = c0224z.f4726b;
                int i8 = c0224z.f4727c;
                if (!(i5 == i8)) {
                    if (i5 == i8) {
                        throw new ArrayIndexOutOfBoundsException();
                    }
                    i = c0224z.f4725a[i5];
                    c0224z.f4726b = (i5 + 1) & c0224z.f4728d;
                }
                return i;
            } finally {
            }
        }
    }

    @Override // I0.n
    public final void z(int i) {
        this.f3191q.setVideoScalingMode(i);
    }
}
