package com.tencent.mm.audio.b;

import android.media.AudioRecord;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.sdk.g.d;
import com.tencent.mm.sdk.platformtools.ab;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.locks.ReentrantLock;

public final class e extends f {
    AudioRecord cpL;
    int cpX;
    boolean cpx;
    private int cqa = 12800;
    b cqb = new b();
    com.tencent.mm.compatible.b.b cqc = null;
    com.tencent.mm.audio.b.c.a cqd;
    final Object cqe = new Object();
    final byte[] cqf = new byte[0];
    final Object cqg = new Object();
    Timer cqh = null;
    boolean cqi = false;
    private boolean cqj = false;
    boolean mIsMute = false;
    int mStatus = 0;

    class a extends TimerTask {
        private int cql = (e.this.cpX * 2);
        private int cqm = e.this.cpX;
        byte[] cqn = new byte[this.cql];

        a() {
            AppMethodBeat.i(55750);
            AppMethodBeat.o(55750);
        }

        public final void run() {
            AppMethodBeat.i(55751);
            if (!e.this.mIsPause) {
                int EP = (int) (0.8d * ((double) e.this.EP()));
                if (EP < e.this.cpX * 8) {
                    EP = e.this.cpX * 8;
                }
                if (e.this.EQ() > EP) {
                    EP = 8;
                } else {
                    EP = 1;
                }
                for (int i = 0; i < EP; i++) {
                    int x = e.this.x(this.cqn, this.cqm);
                    if (e.this.cqd != null && x == 0) {
                        e.this.cqd.u(this.cqn, this.cqm);
                    }
                }
            }
            AppMethodBeat.o(55751);
        }
    }

    final class b implements Runnable {
        volatile c cqo;

        b() {
        }

        /* JADX WARNING: Removed duplicated region for block: B:134:0x00a1 A:{SYNTHETIC} */
        /* JADX WARNING: Removed duplicated region for block: B:89:0x01a0  */
        /* JADX WARNING: Removed duplicated region for block: B:29:0x00c4  */
        /* JADX WARNING: Removed duplicated region for block: B:72:0x0174  */
        /* JADX WARNING: Removed duplicated region for block: B:119:0x0265  */
        /* JADX WARNING: Removed duplicated region for block: B:75:0x017b  */
        /* JADX WARNING: Missing block: B:13:0x009b, code skipped:
            r0 = new byte[r13.cqk.cpX];
     */
        /* JADX WARNING: Missing block: B:15:0x00a5, code skipped:
            if (2 != r13.cqk.mStatus) goto L_0x00c0;
     */
        /* JADX WARNING: Missing block: B:16:0x00a7, code skipped:
            r1 = r13.cqk.cqf;
     */
        /* JADX WARNING: Missing block: B:17:0x00ab, code skipped:
            monitor-enter(r1);
     */
        /* JADX WARNING: Missing block: B:20:0x00b0, code skipped:
            if (r13.cqk.mIsPause == false) goto L_0x00dc;
     */
        /* JADX WARNING: Missing block: B:22:?, code skipped:
            r13.cqk.cqf.wait();
     */
        public final void run() {
            /*
            r13 = this;
            r2 = -1;
            r12 = 1;
            r11 = 55752; // 0xd9c8 float:7.8125E-41 double:2.7545E-319;
            r10 = 2;
            r3 = 0;
            com.tencent.matrix.trace.core.AppMethodBeat.i(r11);
            r0 = "MicroMsg.RecordModeAsyncRead";
            r1 = "RecordThread started. frameSize:%d";
            r4 = new java.lang.Object[r12];
            r5 = com.tencent.mm.audio.b.e.this;
            r5 = r5.cpX;
            r5 = java.lang.Integer.valueOf(r5);
            r4[r3] = r5;
            com.tencent.mm.sdk.platformtools.ab.i(r0, r1, r4);
            r0 = -123456789; // 0xfffffffff8a432eb float:-2.6642794E34 double:NaN;
            r1 = com.tencent.mm.audio.b.e.this;
            r1 = r1.cpB;
            if (r0 == r1) goto L_0x0049;
        L_0x0028:
            r0 = "MicroMsg.RecordModeAsyncRead";
            r1 = new java.lang.StringBuilder;
            r4 = "set priority to ";
            r1.<init>(r4);
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.cpB;
            r1 = r1.append(r4);
            r1 = r1.toString();
            com.tencent.mm.sdk.platformtools.ab.i(r0, r1);
            r0 = com.tencent.mm.audio.b.e.this;
            r0 = r0.cpB;
            android.os.Process.setThreadPriority(r0);
        L_0x0049:
            r0 = com.tencent.mm.audio.b.e.this;
            r1 = r0.cqg;
            monitor-enter(r1);
            r0 = "MicroMsg.RecordModeAsyncRead";
            r4 = "RecordRunnable#run lock[%s] mStatusLock[%s]";
            r5 = 2;
            r5 = new java.lang.Object[r5];	 Catch:{ all -> 0x00d6 }
            r6 = 0;
            r7 = java.lang.Thread.currentThread();	 Catch:{ all -> 0x00d6 }
            r8 = r7.getId();	 Catch:{ all -> 0x00d6 }
            r7 = java.lang.Long.valueOf(r8);	 Catch:{ all -> 0x00d6 }
            r5[r6] = r7;	 Catch:{ all -> 0x00d6 }
            r6 = 1;
            r7 = com.tencent.mm.audio.b.e.this;	 Catch:{ all -> 0x00d6 }
            r7 = r7.cqg;	 Catch:{ all -> 0x00d6 }
            r5[r6] = r7;	 Catch:{ all -> 0x00d6 }
            com.tencent.mm.sdk.platformtools.ab.i(r0, r4, r5);	 Catch:{ all -> 0x00d6 }
            r0 = com.tencent.mm.audio.b.e.this;	 Catch:{ all -> 0x00d6 }
            r0 = r0.mStatus;	 Catch:{ all -> 0x00d6 }
            if (r12 == r0) goto L_0x0095;
        L_0x0076:
            r0 = "MicroMsg.RecordModeAsyncRead";
            r2 = new java.lang.StringBuilder;	 Catch:{ all -> 0x00d6 }
            r3 = "status is not inited, now status: ";
            r2.<init>(r3);	 Catch:{ all -> 0x00d6 }
            r3 = com.tencent.mm.audio.b.e.this;	 Catch:{ all -> 0x00d6 }
            r3 = r3.mStatus;	 Catch:{ all -> 0x00d6 }
            r2 = r2.append(r3);	 Catch:{ all -> 0x00d6 }
            r2 = r2.toString();	 Catch:{ all -> 0x00d6 }
            com.tencent.mm.sdk.platformtools.ab.e(r0, r2);	 Catch:{ all -> 0x00d6 }
            monitor-exit(r1);	 Catch:{ all -> 0x00d6 }
            com.tencent.matrix.trace.core.AppMethodBeat.o(r11);
        L_0x0094:
            return;
        L_0x0095:
            r0 = com.tencent.mm.audio.b.e.this;	 Catch:{ all -> 0x00d6 }
            r4 = 2;
            r0.mStatus = r4;	 Catch:{ all -> 0x00d6 }
            monitor-exit(r1);	 Catch:{ all -> 0x00d6 }
            r0 = com.tencent.mm.audio.b.e.this;
            r0 = r0.cpX;
            r0 = new byte[r0];
        L_0x00a1:
            r1 = com.tencent.mm.audio.b.e.this;
            r1 = r1.mStatus;
            if (r10 != r1) goto L_0x00c0;
        L_0x00a7:
            r1 = com.tencent.mm.audio.b.e.this;
            r1 = r1.cqf;
            monitor-enter(r1);
            r4 = com.tencent.mm.audio.b.e.this;	 Catch:{ all -> 0x00fe }
            r4 = r4.mIsPause;	 Catch:{ all -> 0x00fe }
            if (r4 == 0) goto L_0x00dc;
        L_0x00b2:
            r4 = com.tencent.mm.audio.b.e.this;	 Catch:{ InterruptedException -> 0x0280 }
            r4 = r4.cqf;	 Catch:{ InterruptedException -> 0x0280 }
            r4.wait();	 Catch:{ InterruptedException -> 0x0280 }
        L_0x00b9:
            r4 = com.tencent.mm.audio.b.e.this;	 Catch:{ all -> 0x00fe }
            r4 = r4.mStatus;	 Catch:{ all -> 0x00fe }
            if (r10 == r4) goto L_0x00dc;
        L_0x00bf:
            monitor-exit(r1);	 Catch:{ all -> 0x00fe }
        L_0x00c0:
            r0 = r13.cqo;
            if (r0 == 0) goto L_0x00c9;
        L_0x00c4:
            r0 = r13.cqo;
            r0.finish();
        L_0x00c9:
            r0 = "MicroMsg.RecordModeAsyncRead";
            r1 = "RecordThread exited.";
            com.tencent.mm.sdk.platformtools.ab.i(r0, r1);
            com.tencent.matrix.trace.core.AppMethodBeat.o(r11);
            goto L_0x0094;
        L_0x00d6:
            r0 = move-exception;
            monitor-exit(r1);	 Catch:{ all -> 0x00d6 }
            com.tencent.matrix.trace.core.AppMethodBeat.o(r11);
            throw r0;
        L_0x00dc:
            monitor-exit(r1);	 Catch:{ all -> 0x00fe }
            r1 = com.tencent.mm.audio.b.e.this;
            r1 = r1.cpL;
            if (r1 != 0) goto L_0x0104;
        L_0x00e3:
            r0 = "MicroMsg.RecordModeAsyncRead";
            r1 = "mAudioRecord is null, so stop record.";
            com.tencent.mm.sdk.platformtools.ab.i(r0, r1);
            r0 = com.tencent.mm.audio.b.e.this;
            r1 = r0.cqg;
            monitor-enter(r1);
            r0 = com.tencent.mm.audio.b.e.this;	 Catch:{ all -> 0x00f8 }
            r2 = 3;
            r0.mStatus = r2;	 Catch:{ all -> 0x00f8 }
            monitor-exit(r1);	 Catch:{ all -> 0x00f8 }
            goto L_0x00c0;
        L_0x00f8:
            r0 = move-exception;
            monitor-exit(r1);	 Catch:{ all -> 0x00f8 }
            com.tencent.matrix.trace.core.AppMethodBeat.o(r11);
            throw r0;
        L_0x00fe:
            r0 = move-exception;
            monitor-exit(r1);	 Catch:{ all -> 0x00fe }
            com.tencent.matrix.trace.core.AppMethodBeat.o(r11);
            throw r0;
        L_0x0104:
            r1 = com.tencent.mm.audio.b.e.this;
            r1 = r1.cpx;
            if (r1 == 0) goto L_0x0110;
        L_0x010a:
            r0 = com.tencent.mm.audio.b.e.this;
            r0 = r0.cpX;
            r0 = new byte[r0];
        L_0x0110:
            r1 = com.tencent.mm.audio.b.e.this;
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.cqp;
            r4 = r4 + 1;
            r1.cqp = r4;
            r1 = new com.tencent.mm.compatible.util.g$a;
            r1.<init>();
            r1 = com.tencent.mm.audio.b.e.this;
            r1 = r1.cpL;
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.cpX;
            r1 = r1.read(r0, r3, r4);
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.mStatus;
            if (r10 != r4) goto L_0x00c0;
        L_0x0131:
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.cpT;
            if (r4 == 0) goto L_0x013e;
        L_0x0137:
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.cpT;
            r4.d(r1, r0);
        L_0x013e:
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.cpX;
            if (r4 == r1) goto L_0x0155;
        L_0x0144:
            r4 = "MicroMsg.RecordModeAsyncRead";
            r5 = "read len ";
            r6 = java.lang.String.valueOf(r1);
            r5 = r5.concat(r6);
            com.tencent.mm.sdk.platformtools.ab.i(r4, r5);
        L_0x0155:
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.cpX;
            if (r1 >= r4) goto L_0x0169;
        L_0x015b:
            r4 = "MicroMsg.RecordModeAsyncRead";
            r5 = "read too fast? sleep 10 ms";
            com.tencent.mm.sdk.platformtools.ab.i(r4, r5);
            r4 = 10;
            java.lang.Thread.sleep(r4);	 Catch:{ InterruptedException -> 0x027d }
        L_0x0169:
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.cqd;
            if (r4 == 0) goto L_0x00a1;
        L_0x016f:
            if (r1 <= 0) goto L_0x00a1;
        L_0x0171:
            r4 = r0.length;
            if (r1 <= r4) goto L_0x0175;
        L_0x0174:
            r1 = r0.length;
        L_0x0175:
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.cqc;
            if (r4 == 0) goto L_0x0265;
        L_0x017b:
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.mIsMute;
            if (r4 == 0) goto L_0x0184;
        L_0x0181:
            java.util.Arrays.fill(r0, r3, r1, r3);
        L_0x0184:
            r4 = com.tencent.mm.audio.b.e.this;
            r5 = r4.cqc;
            if (r1 <= 0) goto L_0x019d;
        L_0x018a:
            r4 = r5.eoO;
            if (r4 == 0) goto L_0x0193;
        L_0x018e:
            r4 = r5.eoP;
            r4.lock();
        L_0x0193:
            r4 = r5.eoM;
            r6 = r5.eoN;
            if (r4 != r6) goto L_0x01c1;
        L_0x0199:
            r4 = r5.eoJ;
        L_0x019b:
            if (r1 <= r4) goto L_0x01fb;
        L_0x019d:
            r1 = r2;
        L_0x019e:
            if (r1 == 0) goto L_0x00a1;
        L_0x01a0:
            r4 = "MicroMsg.RecordModeAsyncRead";
            r5 = "WriteToBuffer Failed, ret:%d AudioBuffer length: %d";
            r6 = new java.lang.Object[r10];
            r1 = java.lang.Integer.valueOf(r1);
            r6[r3] = r1;
            r1 = com.tencent.mm.audio.b.e.this;
            r1 = r1.cqc;
            r1 = r1.KH();
            r1 = java.lang.Integer.valueOf(r1);
            r6[r12] = r1;
            com.tencent.mm.sdk.platformtools.ab.e(r4, r5, r6);
            goto L_0x00a1;
        L_0x01c1:
            r4 = r5.eoN;
            r4 = r4 + 1;
            r6 = r5.eoJ;
            r4 = r4 % r6;
            r6 = r5.eoM;
            if (r4 != r6) goto L_0x01ce;
        L_0x01cc:
            r4 = r3;
            goto L_0x019b;
        L_0x01ce:
            r4 = r5.eoM;
            r6 = r5.eoN;
            if (r4 >= r6) goto L_0x01ea;
        L_0x01d4:
            r4 = r5.eoN;
            r6 = r5.eoM;
            r4 = r4 - r6;
            r5.eoK = r4;
        L_0x01db:
            r4 = r5.eoO;
            if (r4 == 0) goto L_0x01e4;
        L_0x01df:
            r4 = r5.eoP;
            r4.unlock();
        L_0x01e4:
            r4 = r5.eoJ;
            r6 = r5.eoK;
            r4 = r4 - r6;
            goto L_0x019b;
        L_0x01ea:
            r4 = r5.eoM;
            r6 = r5.eoN;
            if (r4 <= r6) goto L_0x01db;
        L_0x01f0:
            r4 = r5.eoN;
            r6 = r5.eoJ;
            r4 = r4 + r6;
            r6 = r5.eoM;
            r4 = r4 - r6;
            r5.eoK = r4;
            goto L_0x01db;
        L_0x01fb:
            r4 = r5.eoN;
            r4 = r4 + r1;
            r6 = r5.eoJ;
            r4 = r4 % r6;
            r6 = r5.eoM;
            if (r4 != r6) goto L_0x0207;
        L_0x0205:
            r1 = r2;
            goto L_0x019e;
        L_0x0207:
            r4 = r5.eoO;
            if (r4 == 0) goto L_0x0210;
        L_0x020b:
            r4 = r5.eoP;
            r4.lock();
        L_0x0210:
            r4 = r5.eoM;
            r6 = r5.eoN;
            if (r4 >= r6) goto L_0x0255;
        L_0x0216:
            r4 = r5.eoJ;
            r6 = r5.eoN;
            r4 = r4 - r6;
            if (r1 <= r4) goto L_0x0255;
        L_0x021d:
            r4 = r5.eoL;
            r6 = r5.eoN;
            r7 = r5.eoJ;
            r8 = r5.eoN;
            r7 = r7 - r8;
            java.lang.System.arraycopy(r0, r3, r4, r6, r7);
            r4 = r5.eoJ;
            r6 = r5.eoN;
            r4 = r4 - r6;
            r6 = r5.eoL;
            r7 = r5.eoJ;
            r8 = r5.eoN;
            r7 = r7 - r8;
            r7 = r1 - r7;
            java.lang.System.arraycopy(r0, r4, r6, r3, r7);
            r4 = r5.eoJ;
            r6 = r5.eoN;
            r4 = r4 - r6;
            r1 = r1 - r4;
            r5.eoN = r1;
            r1 = r5.eoN;
            r4 = r5.eoJ;
            r1 = r1 % r4;
            r5.eoN = r1;
        L_0x0249:
            r1 = r5.eoO;
            if (r1 == 0) goto L_0x0252;
        L_0x024d:
            r1 = r5.eoP;
            r1.unlock();
        L_0x0252:
            r1 = r3;
            goto L_0x019e;
        L_0x0255:
            r4 = r5.eoL;
            r6 = r5.eoN;
            java.lang.System.arraycopy(r0, r3, r4, r6, r1);
            r4 = r5.eoN;
            r1 = r1 + r4;
            r4 = r5.eoJ;
            r1 = r1 % r4;
            r5.eoN = r1;
            goto L_0x0249;
        L_0x0265:
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.cqd;
            if (r4 == 0) goto L_0x00a1;
        L_0x026b:
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.mIsMute;
            if (r4 == 0) goto L_0x0274;
        L_0x0271:
            java.util.Arrays.fill(r0, r3, r1, r3);
        L_0x0274:
            r4 = com.tencent.mm.audio.b.e.this;
            r4 = r4.cqd;
            r4.u(r0, r1);
            goto L_0x00a1;
        L_0x027d:
            r4 = move-exception;
            goto L_0x0169;
        L_0x0280:
            r4 = move-exception;
            goto L_0x00b9;
            */
            throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.audio.b.e$b.run():void");
        }
    }

    public e(AudioRecord audioRecord, boolean z, int i, com.tencent.mm.audio.b.c.a aVar, boolean z2) {
        AppMethodBeat.i(55753);
        this.cpL = audioRecord;
        this.cpx = z;
        this.cpX = i;
        this.cqd = aVar;
        this.cqj = z2;
        AppMethodBeat.o(55753);
    }

    public final boolean EL() {
        boolean z = true;
        AppMethodBeat.i(55754);
        ab.i("MicroMsg.RecordModeAsyncRead", "startRecord");
        synchronized (this.cqg) {
            try {
                this.mStatus = 1;
            } finally {
                while (true) {
                }
                AppMethodBeat.o(55754);
            }
        }
        ab.i("MicroMsg.RecordModeAsyncRead", "[startRecord] dumpRunningTask:%s", new Object[]{d.xDG.dqb()});
        d.f(this.cqb, "RecordModeAsyncRead_record");
        if (true == this.cqj) {
            boolean z2;
            this.cqc = new com.tencent.mm.compatible.b.b();
            if (this.cqc == null) {
                ab.e("MicroMsg.RecordModeAsyncRead", "new m_audioBuffer error ");
                z2 = false;
            } else {
                this.cqa = this.cpX * 20;
                ab.i("MicroMsg.RecordModeAsyncRead", "audioBuffer init mAudioBufferSize: " + this.cqa);
                com.tencent.mm.compatible.b.b bVar = this.cqc;
                int i = this.cqa;
                if (i <= 0) {
                    z2 = true;
                } else {
                    bVar.eoL = new byte[i];
                    if (bVar.eoL == null) {
                        z2 = true;
                    } else {
                        bVar.eoJ = i;
                        if (bVar.eoO) {
                            bVar.eoP = new ReentrantLock();
                        }
                        z2 = false;
                    }
                }
                if (z2) {
                    ab.e("MicroMsg.RecordModeAsyncRead", "audioBuffer init failed, error code = -1");
                    z2 = false;
                } else {
                    z2 = true;
                }
            }
            if (z2) {
                if (this.cqi || this.cqh != null) {
                    ab.w("MicroMsg.RecordModeAsyncRead", "Timer has been created or, timer has been started, " + this.cqi);
                } else {
                    this.cqh = new Timer();
                    if (this.cqh != null) {
                        z = false;
                    }
                }
                if (z) {
                    ab.e("MicroMsg.RecordModeAsyncRead", "InitAudioRecTimer failed, error code = -1");
                    AppMethodBeat.o(55754);
                    return false;
                }
                this.cqh.scheduleAtFixedRate(new a(), 60, 20);
                this.cqi = true;
            } else {
                ab.e("MicroMsg.RecordModeAsyncRead", "initAudioBuffer failed");
                return false;
            }
        }
        AppMethodBeat.o(55754);
        return true;
    }

    public final void stopRecord() {
        AppMethodBeat.i(55755);
        ab.i("MicroMsg.RecordModeAsyncRead", "stopRecord");
        synchronized (this.cqg) {
            try {
                this.mStatus = 3;
                this.cqb.cqo = new 1(this);
            } finally {
                while (true) {
                }
                AppMethodBeat.o(55755);
            }
        }
        synchronized (this.cqf) {
            try {
                this.cqf.notify();
            } catch (Throwable th) {
                while (true) {
                    AppMethodBeat.o(55755);
                }
            }
        }
    }

    public final int EP() {
        synchronized (this.cqe) {
            if (this.cqc != null) {
                int i = this.cqc.eoJ;
                return i;
            }
            return -1;
        }
    }

    public final int EQ() {
        int KH;
        AppMethodBeat.i(55756);
        synchronized (this.cqe) {
            try {
                if (this.cqc != null) {
                    KH = this.cqc.KH();
                } else {
                    AppMethodBeat.o(55756);
                    return -1;
                }
            } finally {
                while (true) {
                }
                AppMethodBeat.o(55756);
            }
        }
        return KH;
    }

    public final int x(byte[] bArr, int i) {
        int i2 = -1;
        AppMethodBeat.i(55757);
        synchronized (this.cqe) {
            try {
                if (this.cqc != null) {
                    com.tencent.mm.compatible.b.b bVar = this.cqc;
                    if (i > 0 && i <= bVar.KH() && bArr != null) {
                        if (bVar.eoM != bVar.eoN) {
                            if (bVar.eoO) {
                                bVar.eoP.lock();
                            }
                            if (bVar.eoM < bVar.eoN) {
                                System.arraycopy(bVar.eoL, bVar.eoM, bArr, 0, i);
                                bVar.eoM += i;
                            } else if (i <= bVar.eoJ - bVar.eoM) {
                                System.arraycopy(bVar.eoL, bVar.eoM, bArr, 0, i);
                                bVar.eoM += i;
                            } else {
                                System.arraycopy(bVar.eoL, bVar.eoM, bArr, 0, bVar.eoJ - bVar.eoM);
                                System.arraycopy(bVar.eoL, 0, bArr, bVar.eoJ - bVar.eoM, i - (bVar.eoJ - bVar.eoM));
                                bVar.eoM = i - (bVar.eoJ - bVar.eoM);
                            }
                            if (bVar.eoO) {
                                bVar.eoP.unlock();
                            }
                            i2 = 0;
                        }
                    }
                } else {
                    AppMethodBeat.o(55757);
                }
            } finally {
                AppMethodBeat.o(55757);
            }
        }
        return i2;
    }

    public final void bs(boolean z) {
        this.mIsMute = z;
    }
}
