package com.tencent.mm.audio.b;

import android.media.MediaRecorder;
import android.os.Looper;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.audio.d.c;
import com.tencent.mm.bf.c$1;
import com.tencent.mm.bf.d$a;
import com.tencent.mm.compatible.b.c.a;
import com.tencent.mm.compatible.util.g;
import com.tencent.mm.plugin.report.service.h;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.ak;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.ytcommon.BuildConfig;

public final class b {
    private static Object coV = new Object();
    private int coK = 0;
    private String coL = null;
    private int coM;
    private final Object coN = new Object();
    private c coO = null;
    private a coP;
    private com.tencent.mm.bf.c coQ = null;
    private d$a coR = null;
    private long coS = 0;
    private long coT = 0;
    private int coU = 0;
    private int coW = 16000;
    private boolean coX = false;
    public MediaRecorder coY;
    public c coZ = null;
    public a cpa;
    public b cpb;
    private volatile boolean cpc = false;
    private g.a cpd = new g.a();
    private int cpe = 0;
    private int cpf = 0;
    private c.a cpg = new 2(this);
    private boolean cph = false;
    private int mSampleRate = 8000;

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

    public b(a aVar) {
        AppMethodBeat.i(116481);
        ab.i("MicroMsg.MMAudioRecorder", "MMAudioRecorder recMode: ".concat(String.valueOf(aVar)));
        this.cpa = aVar;
        if (!com.tencent.mm.audio.b.g.b.ES()) {
            ab.i("MicroMsg.MMAudioRecorder", "can't use silk encode, force to use amr mode now");
            this.cpa = a.eoR;
        }
        if (this.cpa == a.eoR) {
            this.coM = 7;
            this.coY = new MediaRecorder();
        } else {
            EG();
            this.coM = 1;
        }
        this.cpc = false;
        AppMethodBeat.o(116481);
    }

    public final void a(a aVar) {
        AppMethodBeat.i(116482);
        if (this.cpa == a.eoR) {
            if (this.coY == null) {
                AppMethodBeat.o(116482);
                return;
            }
            this.coP = aVar;
            this.coY.setOnErrorListener(new 1(this));
            AppMethodBeat.o(116482);
        } else if (this.cpb == b.cpk) {
            this.coP = aVar;
            AppMethodBeat.o(116482);
        } else {
            ab.e("MicroMsg.MMAudioRecorder", "setOnErrorListener on wrong state");
            AppMethodBeat.o(116482);
        }
    }

    public final void setOutputFile(String str) {
        AppMethodBeat.i(116483);
        if (this.cpa == a.eoR) {
            if (this.coY == null) {
                AppMethodBeat.o(116483);
                return;
            }
            this.coY.setOutputFile(str);
            this.coL = str;
            AppMethodBeat.o(116483);
        } else if (this.cpb == b.cpk) {
            this.coL = str;
            AppMethodBeat.o(116483);
        } else {
            ab.e("MicroMsg.MMAudioRecorder", "set output path on wrong state");
            this.cpb = b.cpn;
            AppMethodBeat.o(116483);
        }
    }

    public final void setMaxDuration(int i) {
        AppMethodBeat.i(116484);
        if (this.cpa == a.eoR) {
            if (this.coY != null) {
                this.coY.setMaxDuration(i);
            }
            AppMethodBeat.o(116484);
            return;
        }
        this.coS = (long) i;
        AppMethodBeat.o(116484);
    }

    public final void ED() {
        AppMethodBeat.i(116485);
        if (this.cpa == a.eoR) {
            if (this.coY != null) {
                this.coY.setAudioEncoder(1);
            }
            AppMethodBeat.o(116485);
            return;
        }
        AppMethodBeat.o(116485);
    }

    public final void EE() {
        AppMethodBeat.i(116486);
        if (this.cpa != a.eoR) {
            AppMethodBeat.o(116486);
        } else if (this.coY == null) {
            AppMethodBeat.o(116486);
        } else {
            this.coY.setAudioSource(1);
            AppMethodBeat.o(116486);
        }
    }

    public final void EF() {
        AppMethodBeat.i(116487);
        if (this.cpa != a.eoR) {
            AppMethodBeat.o(116487);
        } else if (this.coY == null) {
            AppMethodBeat.o(116487);
        } else {
            this.coY.setOutputFormat(3);
            AppMethodBeat.o(116487);
        }
    }

    public final int getMaxAmplitude() {
        AppMethodBeat.i(116488);
        if (this.cpa == a.eoR) {
            if (this.coY == null) {
                AppMethodBeat.o(116488);
                return 0;
            }
            int maxAmplitude = this.coY.getMaxAmplitude();
            AppMethodBeat.o(116488);
            return maxAmplitude;
        } else if (this.cpb == b.cpm) {
            int i = this.coK;
            this.coK = 0;
            AppMethodBeat.o(116488);
            return i;
        } else {
            AppMethodBeat.o(116488);
            return 0;
        }
    }

    public final void EG() {
        AppMethodBeat.i(116489);
        this.coR = d$a.ajX();
        if (this.coR != null) {
            this.coX = this.coR.ajW();
        }
        if (this.cpa == a.eoS) {
            this.mSampleRate = bo.getInt(((com.tencent.mm.plugin.zero.b.a) com.tencent.mm.kernel.g.K(com.tencent.mm.plugin.zero.b.a.class)).Nu().getValue("VoiceSamplingRate"), 16000);
            this.coW = bo.getInt(((com.tencent.mm.plugin.zero.b.a) com.tencent.mm.kernel.g.K(com.tencent.mm.plugin.zero.b.a.class)).Nu().getValue("VoiceRate"), 16000);
            ab.i("MicroMsg.MMAudioRecorder", "initMediaRecorder dynamicSample: %s sampleRate: %d dynamicEncoding: %s audioEncoding: %d", new Object[]{r1, Integer.valueOf(this.mSampleRate), r0, Integer.valueOf(this.coW)});
        } else {
            if (this.coX) {
                this.mSampleRate = 16000;
            } else {
                this.mSampleRate = 8000;
            }
            com.tencent.mm.kernel.g.RQ();
            int a = bo.a((Integer) com.tencent.mm.kernel.g.RP().eJH.get(27), 0);
            ab.i("MicroMsg.MMAudioRecorder", "sampleRate: " + this.mSampleRate + " notSupp16K: " + a);
            if (a == 1) {
                this.mSampleRate = 8000;
            }
        }
        this.coK = 0;
        this.coL = null;
        synchronized (this.coN) {
            try {
                this.coQ = null;
                this.coO = null;
            } finally {
                while (true) {
                }
                AppMethodBeat.o(116489);
            }
        }
        this.coU = 0;
        try {
            synchronized (coV) {
                this.coZ = new c(this.mSampleRate, 1, 0);
                this.coZ.bq(true);
                this.coZ.gG(120);
                this.coZ.cpM = this.cpg;
            }
            this.cpb = b.cpk;
        } catch (Exception e) {
            if (e.getMessage() != null) {
                ab.e("MicroMsg.MMAudioRecorder", e.getMessage());
            } else {
                ab.e("MicroMsg.MMAudioRecorder", "Unknown error occured while initializing recording");
            }
            this.cpb = b.cpn;
            AppMethodBeat.o(116489);
        } catch (Throwable th) {
            while (true) {
                AppMethodBeat.o(116489);
            }
        }
    }

    public final void start() {
        AppMethodBeat.i(116490);
        if (this.cpa == a.eoR) {
            if (this.coY != null) {
                this.coY.start();
            }
            AppMethodBeat.o(116490);
            return;
        }
        ab.i("MicroMsg.MMAudioRecorder", "Start record now state: " + this.cpb + " recMode: " + this.cpa);
        if (this.cpb == b.cpl) {
            this.coT = System.currentTimeMillis();
            this.coU = 0;
            this.cpb = b.cpm;
            synchronized (coV) {
                try {
                    this.coZ.EL();
                } catch (Throwable th) {
                    while (true) {
                        AppMethodBeat.o(116490);
                    }
                }
            }
        } else {
            com.tencent.mm.kernel.g.RQ();
            com.tencent.mm.kernel.g.RP().eJH.set(27, Integer.valueOf(1));
            ab.e("MicroMsg.MMAudioRecorder", "start() called on illegal state");
            this.cpb = b.cpn;
        }
        this.cpc = false;
        AppMethodBeat.o(116490);
    }

    public final void prepare() {
        AppMethodBeat.i(116491);
        if (this.cpa == a.eoR) {
            if (this.coY != null) {
                this.coY.prepare();
            }
            AppMethodBeat.o(116491);
        } else if (this.cpb != b.cpk || this.coL == null) {
            this.cpb = b.cpn;
            release();
            AppMethodBeat.o(116491);
        } else {
            this.cpb = b.cpl;
            AppMethodBeat.o(116491);
        }
    }

    public final void release() {
        AppMethodBeat.i(116492);
        if (this.cpa == a.eoR) {
            if (this.coY != null) {
                this.coY.release();
            }
            AppMethodBeat.o(116492);
            return;
        }
        if (this.cpb == b.cpm) {
            EH();
        } else {
            b bVar = b.cpl;
        }
        synchronized (coV) {
            try {
                if (this.coZ != null) {
                    this.coZ.EB();
                    this.coZ.cpM = null;
                    this.coZ = null;
                }
            } finally {
                while (true) {
                }
                AppMethodBeat.o(116492);
            }
        }
    }

    public final boolean EH() {
        AppMethodBeat.i(116493);
        if (this.cpa == a.eoR) {
            ab.i("MicroMsg.MMAudioRecorder", "stop sysMediaRecorder: %s", new Object[]{this.coY});
            if (this.coY != null) {
                this.coY.stop();
                this.coY.release();
                this.coY = null;
            }
            AppMethodBeat.o(116493);
            return true;
        }
        g.a aVar = new g.a();
        ab.i("MicroMsg.MMAudioRecorder", "stop now state: " + this.cpb);
        if (this.cpb == b.cpk) {
            h.pYm.a(357, 53, 1, true);
            if (!this.cph) {
                h.pYm.a(357, 54, 1, true);
            }
            this.cph = true;
        }
        if (this.cpb != b.cpm) {
            ab.e("MicroMsg.MMAudioRecorder", "stop() called on illegal state");
            this.cpb = b.cpn;
            AppMethodBeat.o(116493);
            return true;
        }
        synchronized (coV) {
            try {
                if (this.coZ != null) {
                    this.cpc = true;
                } else {
                    this.cpc = false;
                }
            } catch (Throwable th) {
                AppMethodBeat.o(116493);
            }
        }
        long Mr = aVar.Mr();
        this.cpb = b.cpo;
        long Mr2 = aVar.Mr();
        if (this.cpc) {
            ab.i("MicroMsg.MMAudioRecorder", "start to wait pcmrecorder stop, markStop: %s", new Object[]{Boolean.valueOf(this.cpc)});
            int i = 0;
            while (this.cpc) {
                i++;
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    ab.printErrStackTrace("MicroMsg.MMAudioRecorder", e, BuildConfig.FLAVOR, new Object[0]);
                }
                if (i >= 25) {
                    synchronized (coV) {
                        try {
                            ab.i("MicroMsg.MMAudioRecorder", "wait pcmrecorder stop, reach maximum count!, mPcmRecorder: %s", new Object[]{this.coZ});
                            if (this.coZ != null) {
                                this.coZ.EB();
                                this.coZ.cpM = null;
                                this.coZ = null;
                            }
                        } catch (Exception e2) {
                            ab.printErrStackTrace("MicroMsg.MMAudioRecorder", e2, BuildConfig.FLAVOR, new Object[0]);
                        } catch (Throwable th2) {
                            AppMethodBeat.o(116493);
                        }
                    }
                    ab.i("MicroMsg.MMAudioRecorder", "finish to wait pcmrecorder stop, markStop: %s, count: %s", new Object[]{Boolean.valueOf(this.cpc), Integer.valueOf(i)});
                }
            }
            ab.i("MicroMsg.MMAudioRecorder", "finish to wait pcmrecorder stop, markStop: %s, count: %s", new Object[]{Boolean.valueOf(this.cpc), Integer.valueOf(i)});
        }
        synchronized (this.coN) {
            try {
                if (this.coO != null) {
                    this.coO.Fe();
                }
                if (this.coQ != null) {
                    com.tencent.mm.bf.c cVar = this.coQ;
                    ab.i("MicroMsg.SpeexEncoderWorker", "stop ");
                    new ak(Looper.getMainLooper()).post(new c$1(cVar));
                }
            } catch (Throwable th3) {
                while (true) {
                    AppMethodBeat.o(116493);
                }
            }
        }
        long gb = bo.gb(this.coT);
        ab.i("MicroMsg.MMAudioRecorder", "toNow " + gb + " startTickCnt: " + this.coT + " pcmDataReadedCnt: " + this.coU);
        if (gb > 2000 && this.coU == 0) {
            com.tencent.mm.kernel.g.RQ();
            com.tencent.mm.kernel.g.RP().eJH.set(27, Integer.valueOf(1));
            ab.i("MicroMsg.MMAudioRecorder", "16k not suppourt");
        }
        ab.i("MicroMsg.MMAudioRecorder", "Wait Stop Time Media:" + Mr + " Read:" + Mr2 + " Thr:" + aVar.Mr());
        AppMethodBeat.o(116493);
        return false;
    }
}
