package com.tencent.liteav.videodecoder;

import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.os.Message;
import android.view.Surface;
import com.tencent.liteav.basic.d.a;
import com.tencent.liteav.basic.log.TXCLog;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.smtt.sdk.TbsMediaPlayer$TbsMediaPlayerListener;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;

public class b implements a, d {
    boolean a = true;
    boolean b = true;
    boolean c = false;
    boolean d = false;
    Surface e;
    d f;
    private int g;
    private ByteBuffer h;
    private ByteBuffer i;
    private long j;
    private boolean k = false;
    private ArrayList<com.tencent.liteav.basic.g.b> l = new ArrayList();
    private a m;
    private WeakReference<a> n;

    public void onNotifyEvent(int i, Bundle bundle) {
        AppMethodBeat.i(67768);
        com.tencent.liteav.basic.util.b.a(this.n, this.j, i, bundle);
        AppMethodBeat.o(67768);
    }

    public void a(long j) {
        this.j = j;
    }

    public b() {
        AppMethodBeat.i(67769);
        AppMethodBeat.o(67769);
    }

    public void a(d dVar) {
        this.f = dVar;
    }

    public boolean a() {
        return this.b;
    }

    public void a(a aVar) {
        AppMethodBeat.i(67770);
        this.n = new WeakReference(aVar);
        AppMethodBeat.o(67770);
    }

    public int a(SurfaceTexture surfaceTexture, ByteBuffer byteBuffer, ByteBuffer byteBuffer2, boolean z) {
        AppMethodBeat.i(67771);
        int a = a(new Surface(surfaceTexture), byteBuffer, byteBuffer2, z);
        AppMethodBeat.o(67771);
        return a;
    }

    public int a(Surface surface, ByteBuffer byteBuffer, ByteBuffer byteBuffer2, boolean z) {
        this.e = surface;
        this.h = byteBuffer;
        this.i = byteBuffer2;
        this.a = z;
        return 0;
    }

    public void a(boolean z) {
        this.b = z;
    }

    private void b(com.tencent.liteav.basic.g.b bVar) {
        AppMethodBeat.i(67772);
        boolean z = bVar.b == 0;
        Bundle bundle = new Bundle();
        bundle.putBoolean("iframe", z);
        bundle.putByteArray("nal", bVar.a);
        bundle.putLong("pts", bVar.g);
        bundle.putLong("dts", bVar.h);
        bundle.putInt("codecId", bVar.i);
        Message message = new Message();
        message.what = TbsMediaPlayer$TbsMediaPlayerListener.MEDIA_EXTRA_SUBTITLE_COUNT;
        message.setData(bundle);
        a aVar = this.m;
        if (aVar != null) {
            aVar.sendMessage(message);
        }
        this.g++;
        AppMethodBeat.o(67772);
    }

    public void a(com.tencent.liteav.basic.g.b bVar) {
        AppMethodBeat.i(67773);
        Object obj = bVar.b == 0 ? 1 : null;
        if (this.d || obj != null) {
            try {
                if (!(this.d || obj == null)) {
                    TXCLog.w("TXCVideoDecoder", "play:decode: push first i frame");
                    this.d = true;
                }
                if (!(this.k || bVar.i != 1 || this.b)) {
                    TXCLog.w("TXCVideoDecoder", "play:decode: hevc decode error  ");
                    com.tencent.liteav.basic.util.b.a(this.n, -2304, "h265解码失败");
                    this.k = true;
                }
                if (this.m != null) {
                    if (!this.l.isEmpty()) {
                        Iterator it = this.l.iterator();
                        while (it.hasNext()) {
                            b((com.tencent.liteav.basic.g.b) it.next());
                        }
                    }
                    this.l.clear();
                    b(bVar);
                    AppMethodBeat.o(67773);
                    return;
                }
                if (!(obj == null || this.l.isEmpty())) {
                    this.l.clear();
                }
                this.l.add(bVar);
                if (!this.k) {
                    b();
                }
                AppMethodBeat.o(67773);
                return;
            } catch (Exception e) {
                AppMethodBeat.o(67773);
                return;
            }
        }
        TXCLog.i("TXCVideoDecoder", "play:decode: push nal ignore p frame when not got i frame");
        AppMethodBeat.o(67773);
    }

    /* JADX WARNING: Missing block: B:15:0x0091, code skipped:
            r1 = new android.os.Bundle();
            r1.putInt("EVT_ID", 2008);
            r1.putLong("EVT_TIME", com.tencent.liteav.basic.util.TXCTimeUtil.getTimeTick());
            r2 = "EVT_MSG";
     */
    /* JADX WARNING: Missing block: B:16:0x00ab, code skipped:
            if (r11.b == false) goto L_0x00d1;
     */
    /* JADX WARNING: Missing block: B:17:0x00ad, code skipped:
            r0 = "启动硬解";
     */
    /* JADX WARNING: Missing block: B:18:0x00b0, code skipped:
            r1.putCharSequence(r2, r0);
            r2 = "EVT_PARAM1";
     */
    /* JADX WARNING: Missing block: B:19:0x00b8, code skipped:
            if (r11.b == false) goto L_0x00d5;
     */
    /* JADX WARNING: Missing block: B:20:0x00ba, code skipped:
            r0 = 1;
     */
    /* JADX WARNING: Missing block: B:21:0x00bb, code skipped:
            r1.putInt(r2, r0);
            com.tencent.liteav.basic.util.b.a(r11.n, r11.j, 2008, r1);
            com.tencent.matrix.trace.core.AppMethodBeat.o(67774);
     */
    /* JADX WARNING: Missing block: B:26:0x00d1, code skipped:
            r0 = "启动软解";
     */
    /* JADX WARNING: Missing block: B:27:0x00d5, code skipped:
            r0 = 2;
     */
    /* JADX WARNING: Missing block: B:33:?, code skipped:
            return 0;
     */
    public int b() {
        /*
        r11 = this;
        r10 = 2008; // 0x7d8 float:2.814E-42 double:9.92E-321;
        r0 = -1;
        r8 = 0;
        r9 = 67774; // 0x108be float:9.4972E-41 double:3.3485E-319;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r9);
        r1 = r11.b;
        if (r1 == 0) goto L_0x001f;
    L_0x000e:
        r1 = r11.e;
        if (r1 != 0) goto L_0x001f;
    L_0x0012:
        r1 = "TXCVideoDecoder";
        r2 = "play:decode: start decoder error when not setup surface";
        com.tencent.liteav.basic.log.TXCLog.i(r1, r2);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r9);
    L_0x001e:
        return r0;
    L_0x001f:
        monitor-enter(r11);
        r1 = r11.m;	 Catch:{ all -> 0x00cb }
        if (r1 == 0) goto L_0x0032;
    L_0x0024:
        r1 = "TXCVideoDecoder";
        r2 = "play:decode: start decoder error when decoder is started";
        com.tencent.liteav.basic.log.TXCLog.e(r1, r2);	 Catch:{ all -> 0x00cb }
        monitor-exit(r11);	 Catch:{ all -> 0x00cb }
        com.tencent.matrix.trace.core.AppMethodBeat.o(r9);
        goto L_0x001e;
    L_0x0032:
        r0 = 0;
        r11.g = r0;	 Catch:{ all -> 0x00cb }
        r0 = 0;
        r11.k = r0;	 Catch:{ all -> 0x00cb }
        r1 = new android.os.HandlerThread;	 Catch:{ all -> 0x00cb }
        r0 = "VDecoder";
        r1.<init>(r0);	 Catch:{ all -> 0x00cb }
        r1.start();	 Catch:{ all -> 0x00cb }
        r0 = new java.lang.StringBuilder;	 Catch:{ all -> 0x00cb }
        r2 = "VDecoder";
        r0.<init>(r2);	 Catch:{ all -> 0x00cb }
        r2 = r1.getId();	 Catch:{ all -> 0x00cb }
        r0 = r0.append(r2);	 Catch:{ all -> 0x00cb }
        r0 = r0.toString();	 Catch:{ all -> 0x00cb }
        r1.setName(r0);	 Catch:{ all -> 0x00cb }
        r0 = new com.tencent.liteav.videodecoder.b$a;	 Catch:{ all -> 0x00cb }
        r1 = r1.getLooper();	 Catch:{ all -> 0x00cb }
        r0.<init>(r1);	 Catch:{ all -> 0x00cb }
        r1 = r11.c;	 Catch:{ all -> 0x00cb }
        r2 = r11.b;	 Catch:{ all -> 0x00cb }
        r3 = r11.e;	 Catch:{ all -> 0x00cb }
        r4 = r11.h;	 Catch:{ all -> 0x00cb }
        r5 = r11.i;	 Catch:{ all -> 0x00cb }
        r6 = r11;
        r7 = r11;
        r0.a(r1, r2, r3, r4, r5, r6, r7);	 Catch:{ all -> 0x00cb }
        r1 = "TXCVideoDecoder";
        r2 = "play:decode: start decode thread";
        com.tencent.liteav.basic.log.TXCLog.w(r1, r2);	 Catch:{ all -> 0x00cb }
        r1 = android.os.Message.obtain();	 Catch:{ all -> 0x00cb }
        r2 = 100;
        r1.what = r2;	 Catch:{ all -> 0x00cb }
        r2 = r11.a;	 Catch:{ all -> 0x00cb }
        r2 = java.lang.Boolean.valueOf(r2);	 Catch:{ all -> 0x00cb }
        r1.obj = r2;	 Catch:{ all -> 0x00cb }
        r0.sendMessage(r1);	 Catch:{ all -> 0x00cb }
        r11.m = r0;	 Catch:{ all -> 0x00cb }
        monitor-exit(r11);	 Catch:{ all -> 0x00cb }
        r1 = new android.os.Bundle;
        r1.<init>();
        r0 = "EVT_ID";
        r1.putInt(r0, r10);
        r0 = "EVT_TIME";
        r2 = com.tencent.liteav.basic.util.TXCTimeUtil.getTimeTick();
        r1.putLong(r0, r2);
        r2 = "EVT_MSG";
        r0 = r11.b;
        if (r0 == 0) goto L_0x00d1;
    L_0x00ad:
        r0 = "启动硬解";
    L_0x00b0:
        r1.putCharSequence(r2, r0);
        r2 = "EVT_PARAM1";
        r0 = r11.b;
        if (r0 == 0) goto L_0x00d5;
    L_0x00ba:
        r0 = 1;
    L_0x00bb:
        r1.putInt(r2, r0);
        r0 = r11.n;
        r2 = r11.j;
        com.tencent.liteav.basic.util.b.a(r0, r2, r10, r1);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r9);
        r0 = r8;
        goto L_0x001e;
    L_0x00cb:
        r0 = move-exception;
        monitor-exit(r11);	 Catch:{ all -> 0x00cb }
        com.tencent.matrix.trace.core.AppMethodBeat.o(r9);
        throw r0;
    L_0x00d1:
        r0 = "启动软解";
        goto L_0x00b0;
    L_0x00d5:
        r0 = 2;
        goto L_0x00bb;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.liteav.videodecoder.b.b():int");
    }

    public void c() {
        AppMethodBeat.i(67775);
        synchronized (this) {
            try {
                if (this.m != null) {
                    this.m.sendEmptyMessage(TbsMediaPlayer$TbsMediaPlayerListener.MEDIA_EXTRA_SUBTITLE_INDEX);
                }
                this.m = null;
            } finally {
                while (true) {
                }
                AppMethodBeat.o(67775);
            }
        }
        this.l.clear();
        this.d = false;
        this.g = 0;
    }

    public void b(boolean z) {
        int i = 1;
        AppMethodBeat.i(67776);
        synchronized (this) {
            try {
                this.b = z;
                this.l.clear();
                this.d = false;
                this.g = 0;
                Message obtain = Message.obtain();
                obtain.what = TbsMediaPlayer$TbsMediaPlayerListener.MEDIA_EXTRA_AUDIOTRACK_TITLES;
                obtain.arg1 = this.b ? 1 : 0;
                if (!this.a) {
                    i = 0;
                }
                obtain.arg2 = i;
                if (this.m != null) {
                    this.m.sendMessage(obtain);
                }
            } finally {
                while (true) {
                }
                AppMethodBeat.o(67776);
            }
        }
    }

    public void a(byte[] bArr, long j, int i) {
        AppMethodBeat.i(67777);
        a aVar = this.m;
        if (!(aVar == null || aVar.d || aVar.a == null)) {
            ((TXCVideoFfmpegDecoder) aVar.a).loadNativeData(bArr, j, i);
        }
        AppMethodBeat.o(67777);
    }

    public int d() {
        AppMethodBeat.i(67778);
        int size = this.g + this.l.size();
        AppMethodBeat.o(67778);
        return size;
    }

    public void a(SurfaceTexture surfaceTexture, int i, int i2, long j, long j2) {
        AppMethodBeat.i(67779);
        if (this.f != null) {
            this.f.a(surfaceTexture, i, i2, j, j2);
        }
        if (this.g > 0) {
            this.g--;
        }
        AppMethodBeat.o(67779);
    }

    public void a(long j, int i, int i2, long j2, long j3) {
        AppMethodBeat.i(67780);
        if (this.f != null) {
            this.f.a(j, i, i2, j2, j3);
        }
        if (this.g > 0) {
            this.g--;
        }
        AppMethodBeat.o(67780);
    }

    public void a(int i, int i2) {
        AppMethodBeat.i(67781);
        if (this.f != null) {
            this.f.a(i, i2);
        }
        AppMethodBeat.o(67781);
    }

    public boolean e() {
        AppMethodBeat.i(67782);
        a aVar = this.m;
        if (aVar != null) {
            boolean a = aVar.a();
            AppMethodBeat.o(67782);
            return a;
        }
        AppMethodBeat.o(67782);
        return false;
    }
}
