package com.tencent.mm.media.d;

import a.f.a.b;
import a.f.b.j;
import a.l;
import a.y;
import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.os.HandlerThread;
import android.view.Surface;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.media.b.c;
import com.tencent.mm.sdk.g.d;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.ak;
import com.tencent.mm.sdk.platformtools.bo;
import java.nio.ByteBuffer;

@l(dWo = {1, 1, 13}, dWp = {"\u0000T\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0002\u0010\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0000\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\f\u0018\u0000 $2\u00020\u0001:\u0001$B4\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\b\b\u0002\u0010\u0004\u001a\u00020\u0005\u0012\u001b\b\u0002\u0010\u0006\u001a\u0015\u0012\u0004\u0012\u00020\u0001\u0012\u0004\u0012\u00020\b\u0018\u00010\u0007¢\u0006\u0002\b\t¢\u0006\u0002\u0010\nJ\b\u0010\u001d\u001a\u00020\bH\u0002J\u0010\u0010\u001e\u001a\u00020\b2\u0006\u0010\u001f\u001a\u00020\u000eH\u0016J\b\u0010 \u001a\u00020\bH\u0016J\b\u0010!\u001a\u00020\bH\u0002J\b\u0010\"\u001a\u00020\u000eH\u0016J\b\u0010#\u001a\u00020\bH\u0016R\u000e\u0010\u000b\u001a\u00020\fXD¢\u0006\u0002\n\u0000R\u0010\u0010\r\u001a\u00020\u000eXD¢\u0006\u0004\n\u0002\b\u000fR\u000e\u0010\u0010\u001a\u00020\u0011X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0012\u001a\u00020\u0013X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0014\u001a\u00020\u0015X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0016\u001a\u00020\u0017X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0018\u001a\u00020\u0019X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u001a\u001a\u00020\u0005X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u001b\u001a\u00020\u0005X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u001c\u001a\u00020\u000eX\u000e¢\u0006\u0002\n\u0000¨\u0006%"}, dWq = {"Lcom/tencent/mm/media/encoder/MediaCodecTransEncoder;", "Lcom/tencent/mm/media/encoder/IMediaCodecTransEncoder;", "videoCodecConfig", "Lcom/tencent/mm/media/config/VideoCodecConfig;", "highPriority", "", "init", "Lkotlin/Function1;", "", "Lkotlin/ExtensionFunctionType;", "(Lcom/tencent/mm/media/config/VideoCodecConfig;ZLkotlin/jvm/functions/Function1;)V", "TAG", "", "TIMEOUT", "", "TIMEOUT$1", "bufferInfo", "Landroid/media/MediaCodec$BufferInfo;", "encodeHandler", "Lcom/tencent/mm/sdk/platformtools/MMHandler;", "encodeThread", "Landroid/os/HandlerThread;", "encoder", "Landroid/media/MediaCodec;", "encoderLock", "Ljava/lang/Object;", "hasFinished", "isEnd", "startTime", "drainEncoder", "encodeFrame", "pts", "finishEncode", "finishWithEncodeLastFrame", "getRecordTimes", "release", "Companion", "plugin-mediaeditor_release"})
public final class e extends b {
    public static final a eVM = new a();
    private final String TAG;
    private HandlerThread eSP;
    private ak eSQ;
    private boolean eTf;
    private final BufferInfo eTy;
    private final long eVJ;
    private boolean eVK;
    private final Object eVL;
    private MediaCodec eVq;
    private long startTime;

    @l(dWo = {1, 1, 13}, dWp = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\t\n\u0000\b\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u000e\u0010\u0003\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000¨\u0006\u0005"}, dWq = {"Lcom/tencent/mm/media/encoder/MediaCodecTransEncoder$Companion;", "", "()V", "TIMEOUT", "", "plugin-mediaeditor_release"})
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(byte b) {
            this();
        }
    }

    static {
        AppMethodBeat.i(12951);
        AppMethodBeat.o(12951);
    }

    public /* synthetic */ e(c cVar, b bVar) {
        this(cVar, false, bVar);
    }

    public e(c cVar, boolean z, b<? super b, y> bVar) {
        j.p(cVar, "videoCodecConfig");
        super(cVar, bVar);
        AppMethodBeat.i(12950);
        this.TAG = "MicroMsg.MediaCodecTransEncoder";
        this.eVJ = 10000;
        this.eTy = new BufferInfo();
        HandlerThread ek = d.ek("MediaCodecTransEncoder_encodeThread", z ? -2 : 0);
        j.o(ek, "ThreadPool.newFreeHandle….THREAD_PRIORITY_DEFAULT)");
        this.eSP = ek;
        this.eVL = new Object();
        MediaCodec createByCodecName = MediaCodec.createByCodecName(cVar.name);
        j.o(createByCodecName, "MediaCodec.createByCodec…me(videoCodecConfig.name)");
        this.eVq = createByCodecName;
        this.eVq.configure(cVar.Ux(), null, null, 1);
        Surface createInputSurface = this.eVq.createInputSurface();
        j.o(createInputSurface, "encoder.createInputSurface()");
        e(createInputSurface);
        this.eVq.start();
        this.eSP.start();
        this.eSQ = new ak(this.eSP.getLooper());
        if (bVar != null) {
            bVar.am(this);
            AppMethodBeat.o(12950);
            return;
        }
        AppMethodBeat.o(12950);
    }

    public static final /* synthetic */ void d(e eVar) {
        AppMethodBeat.i(12952);
        eVar.UR();
        AppMethodBeat.o(12952);
    }

    public final void UP() {
        AppMethodBeat.i(12944);
        if (0 == this.startTime) {
            this.startTime = bo.yz();
        }
        this.eSQ.post((Runnable) new b(this));
        AppMethodBeat.o(12944);
    }

    /* JADX WARNING: Missing block: B:11:0x0021, code skipped:
            com.tencent.matrix.trace.core.AppMethodBeat.o(12945);
     */
    /* JADX WARNING: Missing block: B:26:?, code skipped:
            return;
     */
    public final void UO() {
        /*
        r9 = this;
        r8 = 12945; // 0x3291 float:1.814E-41 double:6.3957E-320;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r8);
        r2 = r9.TAG;
        r3 = "finishEncode ";
        com.tencent.mm.sdk.platformtools.ab.i(r2, r3);
        r4 = r9.eVL;
        monitor-enter(r4);
        r2 = r9.eTf;	 Catch:{ all -> 0x0059 }
        if (r2 != 0) goto L_0x0020;
    L_0x0014:
        r2 = r9.eVK;	 Catch:{ all -> 0x0059 }
        if (r2 != 0) goto L_0x0020;
    L_0x0018:
        r2 = 0;
        r6 = r9.startTime;	 Catch:{ all -> 0x0059 }
        r2 = (r2 > r6 ? 1 : (r2 == r6 ? 0 : -1));
        if (r2 != 0) goto L_0x0025;
    L_0x0020:
        monitor-exit(r4);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r8);
    L_0x0024:
        return;
    L_0x0025:
        r2 = 1;
        r9.eTf = r2;	 Catch:{ Exception -> 0x0037 }
        r2 = r9.eVq;	 Catch:{ Exception -> 0x0037 }
        r2.signalEndOfInputStream();	 Catch:{ Exception -> 0x0037 }
    L_0x002d:
        r9.UR();	 Catch:{ all -> 0x0059 }
        r2 = a.y.AUy;	 Catch:{ all -> 0x0059 }
        monitor-exit(r4);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r8);
        goto L_0x0024;
    L_0x0037:
        r3 = move-exception;
        r5 = r9.TAG;	 Catch:{ all -> 0x0059 }
        r0 = r3;
        r0 = (java.lang.Throwable) r0;	 Catch:{ all -> 0x0059 }
        r2 = r0;
        r6 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0059 }
        r7 = "finishEncode signalEndOfInputStream error:";
        r6.<init>(r7);	 Catch:{ all -> 0x0059 }
        r3 = r3.getMessage();	 Catch:{ all -> 0x0059 }
        r3 = r6.append(r3);	 Catch:{ all -> 0x0059 }
        r3 = r3.toString();	 Catch:{ all -> 0x0059 }
        r6 = 0;
        r6 = new java.lang.Object[r6];	 Catch:{ all -> 0x0059 }
        com.tencent.mm.sdk.platformtools.ab.printErrStackTrace(r5, r2, r3, r6);	 Catch:{ all -> 0x0059 }
        goto L_0x002d;
    L_0x0059:
        r2 = move-exception;
        monitor-exit(r4);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r8);
        throw r2;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.media.d.e.UO():void");
    }

    /* JADX WARNING: Missing block: B:13:0x0044, code skipped:
            com.tencent.matrix.trace.core.AppMethodBeat.o(12946);
     */
    /* JADX WARNING: Missing block: B:17:?, code skipped:
            return;
     */
    private final void US() {
        /*
        r4 = this;
        r3 = 12946; // 0x3292 float:1.8141E-41 double:6.396E-320;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r3);
        r0 = r4.TAG;
        r1 = new java.lang.StringBuilder;
        r2 = "finishWithEncodeLastFrame  ,isEnd:";
        r1.<init>(r2);
        r2 = r4.eTf;
        r1 = r1.append(r2);
        r2 = "   ,hasFinished: ";
        r1 = r1.append(r2);
        r2 = r4.eVK;
        r1 = r1.append(r2);
        r1 = r1.toString();
        com.tencent.mm.sdk.platformtools.ab.d(r0, r1);
        r1 = r4.eVL;
        monitor-enter(r1);
        r0 = r4.eTf;	 Catch:{ all -> 0x0048 }
        if (r0 == 0) goto L_0x0034;
    L_0x0030:
        r0 = r4.eVK;	 Catch:{ all -> 0x0048 }
        if (r0 == 0) goto L_0x0039;
    L_0x0034:
        monitor-exit(r1);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r3);
    L_0x0038:
        return;
    L_0x0039:
        r4.release();	 Catch:{ all -> 0x0048 }
        r0 = r4.eVz;	 Catch:{ all -> 0x0048 }
        if (r0 == 0) goto L_0x0043;
    L_0x0040:
        r0.invoke();	 Catch:{ all -> 0x0048 }
    L_0x0043:
        monitor-exit(r1);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r3);
        goto L_0x0038;
    L_0x0048:
        r0 = move-exception;
        monitor-exit(r1);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r3);
        throw r0;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.media.d.e.US():void");
    }

    public final void release() {
        AppMethodBeat.i(12947);
        synchronized (this.eVL) {
            try {
                if (this.eVK) {
                    ab.i(this.TAG, "release, already finished");
                    AppMethodBeat.o(12947);
                    return;
                }
                this.eVK = true;
                this.eVq.stop();
                this.eVq.release();
                this.eSQ.removeCallbacksAndMessages(null);
                this.eSP.quitSafely();
                AppMethodBeat.o(12947);
            } catch (Exception e) {
                ab.e(this.TAG, "release encoder error " + e.getMessage());
            } catch (Throwable th) {
                AppMethodBeat.o(12947);
            }
        }
    }

    private final void UR() {
        AppMethodBeat.i(12948);
        try {
            ab.i(this.TAG, "drainEncoder");
            int dequeueOutputBuffer = this.eVq.dequeueOutputBuffer(this.eTy, this.eVJ);
            while (dequeueOutputBuffer != -1) {
                if (dequeueOutputBuffer == -2) {
                    ab.d(this.TAG, "encoder output format changed " + this.eVq.getOutputFormat());
                } else if (dequeueOutputBuffer != -3) {
                    if (dequeueOutputBuffer < 0) {
                        ab.e(this.TAG, "outputBufferIndex " + dequeueOutputBuffer + ", ignore");
                    } else {
                        ab.d(this.TAG, "outputBuffer index: ".concat(String.valueOf(dequeueOutputBuffer)));
                        ByteBuffer[] outputBuffers = this.eVq.getOutputBuffers();
                        ab.d(this.TAG, "getOutputBuffers");
                        ByteBuffer byteBuffer = outputBuffers[dequeueOutputBuffer];
                        if (byteBuffer == null) {
                            Throwable runtimeException = new RuntimeException("encoderOutputBuffer " + dequeueOutputBuffer + " was null");
                            AppMethodBeat.o(12948);
                            throw runtimeException;
                        }
                        ab.d(this.TAG, "outputBuffers[outputBufferIndex]");
                        if ((this.eTy.flags & 2) != 0) {
                            ab.i(this.TAG, "codec config!");
                        }
                        ab.i(this.TAG, "bufferInfo.flags : " + (this.eTy.flags & 4));
                        if (this.eTy.size != 0) {
                            byteBuffer.position(this.eTy.offset);
                            byteBuffer.limit(this.eTy.offset + this.eTy.size);
                            f(byteBuffer, this.eTy);
                            this.eVq.releaseOutputBuffer(dequeueOutputBuffer, false);
                        } else if ((this.eTy.flags & 4) != 0) {
                            ab.i(this.TAG, "encode end of stream");
                            US();
                            break;
                        } else {
                            ab.d(this.TAG, "bufferInfo.size == 0");
                        }
                    }
                }
                ab.d(this.TAG, "dequeueOutputBuffer start");
                int dequeueOutputBuffer2 = this.eVq.dequeueOutputBuffer(this.eTy, this.eVJ);
                if (dequeueOutputBuffer2 < 0) {
                    break;
                }
                dequeueOutputBuffer = dequeueOutputBuffer2;
            }
            ab.d(this.TAG, "outputBuffer try again later");
        } catch (Exception e) {
            ab.e(this.TAG, "drainEncoder error");
            if (this.eTO) {
                com.tencent.mm.media.i.c cVar = com.tencent.mm.media.i.c.faj;
                com.tencent.mm.media.i.c.VX();
            }
        }
        US();
        AppMethodBeat.o(12948);
    }

    public final long UQ() {
        AppMethodBeat.i(12949);
        long yz = bo.yz() - this.startTime;
        AppMethodBeat.o(12949);
        return yz;
    }
}
