package a.c.d.i;

import a.a.a.b;
import a.c.d.i.f;
import a.c.d.l.c;
import android.graphics.SurfaceTexture;
import android.media.MediaCodec;
import android.media.MediaCrypto;
import android.media.MediaFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.SystemClock;
import android.view.Surface;
import java.nio.ByteBuffer;

public class f {
    public static final String A = "video/hevc";
    private static final float B = 15.0f;
    private static final String C = "VideoPlayerV2";
    private static final int D = 5;
    private static final int E = 100;
    private static final long F = 8589934592L;
    private static final String x = "vendor.qti-ext-dec-low-latency.enable";
    private static final String y = "vendor.qti-ext-dec-picture-order.enable";
    public static final String z = "video/avc";

    /* renamed from: a  reason: collision with root package name */
    private volatile boolean f157a = false;
    private volatile boolean b = false;
    private volatile boolean c = false;
    private volatile boolean d = false;
    private volatile boolean e = false;
    private long f = 0;
    private float g = 0.0f;
    private long h = 0;
    private MediaCodec i;
    private HandlerThread j;
    private Handler k;
    private g l;
    private final int m;
    private final int n;
    private Surface o;
    private SurfaceTexture p;
    private int q;
    private int r;
    private String s;
    private a.c.d.a t;
    private long u;
    private long v;
    private boolean w;

    public class a extends MediaCodec.Callback {
        public a() {
        }

        /* access modifiers changed from: private */
        public /* synthetic */ void a(MediaCodec mediaCodec, int i) {
            e eVar = null;
            while (true) {
                if (!f.this.c) {
                    break;
                }
                eVar = f.this.l.c();
                if (f.this.b) {
                    if (eVar != null && eVar.b()) {
                        f.this.b = false;
                        break;
                    } else if (eVar != null) {
                        f.this.l.a(eVar);
                    }
                } else if (eVar != null) {
                    break;
                }
            }
            if (eVar != null && eVar.a().remaining() > 0) {
                if (!f.this.w && f.this.v == -1) {
                    if (f.this.u != 0) {
                        int i2 = (eVar.c() > f.this.u ? 1 : (eVar.c() == f.this.u ? 0 : -1));
                        long c = eVar.c();
                        if (i2 <= 0) {
                        }
                    }
                    f fVar = f.this;
                    fVar.v = fVar.h * 5;
                }
                try {
                    ByteBuffer inputBuffer = mediaCodec.getInputBuffer(i);
                    inputBuffer.clear();
                    int remaining = eVar.a().remaining();
                    inputBuffer.limit(remaining);
                    inputBuffer.put(eVar.a());
                    mediaCodec.queueInputBuffer(i, 0, remaining, f.this.h * 5, 0);
                    f.f(f.this);
                } catch (IllegalStateException e) {
                    b.b(f.C, "get input buffer failed ", e);
                    f.this.e = true;
                } catch (Throwable th) {
                    f.this.l.a(eVar);
                    throw th;
                }
                f.this.l.a(eVar);
            }
        }

        public void onError(MediaCodec mediaCodec, MediaCodec.CodecException codecException) {
            b.b(f.C, "onError: when decode data. errorCode: " + codecException.getErrorCode() + ", isTransient: " + codecException.isTransient() + ", isRecoverable: " + codecException.isRecoverable() + ", message: " + codecException.getMessage());
            f.this.e = true;
        }

        public void onInputBufferAvailable(MediaCodec mediaCodec, int i) {
            if (i < 0) {
                b.b(f.C, " index " + i);
            } else if (f.this.c) {
                f.this.k.post(new Runnable(mediaCodec, i) {
                    /* class a.c.d.i.$$Lambda$f$a$76WSidJzdxx8l6AcncDlIvGyhQ8 */
                    public final /* synthetic */ MediaCodec f$1;
                    public final /* synthetic */ int f$2;

                    {
                        this.f$1 = r2;
                        this.f$2 = r3;
                    }

                    public final void run() {
                        f.a.this.a(this.f$1, this.f$2);
                    }
                });
            }
        }

        public void onOutputBufferAvailable(MediaCodec mediaCodec, int i, MediaCodec.BufferInfo bufferInfo) {
            int d = f.this.l.d();
            long elapsedRealtime = SystemClock.elapsedRealtime();
            boolean z = f.this.d;
            if (z && !f.this.w && f.this.v != -1 && f.this.v < bufferInfo.presentationTimeUs) {
                f.this.w = true;
                if (f.this.t != null) {
                    f.this.t.a();
                }
            } else if (z && d >= f.this.n && f.this.f != 0 && ((float) (elapsedRealtime - f.this.f)) < f.this.g) {
                z = false;
            }
            try {
                mediaCodec.releaseOutputBuffer(i, z);
            } catch (Exception e) {
                b.b(f.C, "release output buffer failed,", e);
                f.this.e = true;
            }
            if (z) {
                f.this.f = elapsedRealtime;
            }
        }

        public void onOutputFormatChanged(MediaCodec mediaCodec, MediaFormat mediaFormat) {
            b.e(f.C, "onOutputFormatChanged: " + mediaFormat);
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:4:0x0047, code lost:
        if (r5.equals(r7) != false) goto L_0x003e;
     */
    public f(g gVar, int i2, String str) {
        this.m = i2;
        float f2 = (float) i2;
        this.g = 1000.0f / f2;
        this.n = (int) (f2 * 0.1f);
        this.l = gVar;
        HandlerThread handlerThread = new HandlerThread("VideoSinkInputThread");
        this.j = handlerThread;
        handlerThread.start();
        String str2 = z;
        if (!str2.equals(str)) {
            str2 = A;
        }
        this.s = str2;
        Looper looper = this.j.getLooper();
        if (looper != null) {
            this.k = new Handler(looper);
            try {
                b.a(C, "createDecoderByType " + str);
                this.i = MediaCodec.createDecoderByType(str);
            } catch (Exception e2) {
                b.b(C, "createDecoderByType failed", e2);
            }
        } else {
            throw new RuntimeException("Failed to start wait input thread!");
        }
    }

    private void a() {
        if (this.p == null || this.o == null) {
            b();
            SurfaceTexture surfaceTexture = new SurfaceTexture(0);
            this.p = surfaceTexture;
            surfaceTexture.setDefaultBufferSize(this.q, this.r);
            this.o = new Surface(this.p);
        }
    }

    private void b() {
        SurfaceTexture surfaceTexture = this.p;
        if (surfaceTexture != null) {
            try {
                surfaceTexture.release();
            } catch (Exception e2) {
                b.b(C, "dummy texture release failed", e2);
            }
            this.p = null;
        }
        Surface surface = this.o;
        if (surface != null) {
            try {
                surface.release();
            } catch (Exception e3) {
                b.b(C, "dummy surface release failed", e3);
            }
            this.o = null;
        }
    }

    public static /* synthetic */ long f(f fVar) {
        long j2 = fVar.h;
        fVar.h = 1 + j2;
        return j2;
    }

    public synchronized void a(Surface surface, int i2, int i3, boolean z2, long j2, a.c.d.a aVar) throws IllegalStateException, IllegalArgumentException {
        String str;
        if (surface == null || i2 <= 0 || i3 <= 0) {
            b.b(C, "failed to init");
            return;
        }
        if (!this.f157a) {
            b.a(C, "init, configureDecoder");
            MediaFormat createVideoFormat = MediaFormat.createVideoFormat(this.s, i2, i3);
            if (z2) {
                if (c.a()) {
                    createVideoFormat.setInteger(x, 1);
                    str = y;
                } else if (Build.VERSION.SDK_INT >= 30) {
                    str = "low-latency";
                }
                createVideoFormat.setInteger(str, 1);
            }
            try {
                this.i.configure(createVideoFormat, surface, (MediaCrypto) null, 0);
                Bundle bundle = new Bundle();
                bundle.putFloat("operating-rate", ((float) this.m) + B);
                this.i.setParameters(bundle);
                this.f157a = true;
                this.b = true;
                this.l.b();
            } catch (IllegalArgumentException | IllegalStateException e2) {
                b.b(C, "codec configured failed", e2);
            }
        } else {
            b.c(C, "init, mHasConfigured is true, reset output surface");
            try {
                this.i.setOutputSurface(surface);
            } catch (IllegalStateException e3) {
                b.b(C, "init: setOutputSurface failed", e3);
                this.e = true;
            }
        }
        this.q = i2;
        this.r = i3;
        this.u = j2;
        this.v = -1;
        this.w = false;
        this.t = aVar;
        b.a(C, "init done");
    }

    public void a(boolean z2) {
        b.c(C, "enableRender " + z2);
        this.d = z2;
    }

    public synchronized boolean c() {
        return this.e;
    }

    public synchronized boolean d() {
        return this.f157a;
    }

    public synchronized boolean e() {
        return this.c;
    }

    public synchronized void f() {
        b.a(C, "pause");
        a();
        try {
            this.i.setOutputSurface(this.o);
        } catch (IllegalStateException e2) {
            b.b(C, "pause: setOutputSurface failed", e2);
            this.e = true;
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:18:0x0041, code lost:
        if (r0 != null) goto L_0x0052;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:25:0x0050, code lost:
        if (r0 == null) goto L_0x0057;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:26:0x0052, code lost:
        r0.release();
        r4.i = null;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:27:0x0057, code lost:
        b();
     */
    public synchronized void g() {
        MediaCodec mediaCodec;
        b.a(C, "release");
        this.k.removeCallbacksAndMessages(null);
        this.c = false;
        try {
            HandlerThread handlerThread = this.j;
            if (handlerThread != null) {
                handlerThread.quit();
                this.j.interrupt();
                this.j.join();
                this.j = null;
            }
            g gVar = this.l;
            if (gVar != null) {
                gVar.a();
                this.l = null;
            }
        } catch (IllegalStateException | InterruptedException e2) {
            b.b(C, "release video player exception", e2);
        }
        try {
            MediaCodec mediaCodec2 = this.i;
            if (mediaCodec2 != null) {
                mediaCodec2.stop();
            }
            mediaCodec = this.i;
        } catch (IllegalStateException e3) {
            b.b(C, "release video player exception", e3);
            mediaCodec = this.i;
        } catch (Throwable th) {
            MediaCodec mediaCodec3 = this.i;
            if (mediaCodec3 != null) {
                mediaCodec3.release();
                this.i = null;
            }
            throw th;
        }
    }

    public synchronized void h() {
        b.a(C, "startCodec");
        if (!this.c) {
            this.i.setCallback(new a());
            this.i.start();
            this.c = true;
        }
    }
}
