package a.c.d.j;

import a.a.a.b;
import a.c.d.i.d;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.audiofx.AcousticEchoCanceler;
import com.ucar.vehiclesdk.recorder.AudioConfig;
import javax.annotation.Nullable;

public class a implements Runnable {
    private static final String h = "CarAudioRecorder";
    private static final long i = 10;
    public static final int j = 15;

    /* renamed from: a  reason: collision with root package name */
    private AudioConfig f160a;
    private AudioRecord b;
    private AbstractC0020a c;
    private AcousticEchoCanceler d;
    private boolean e = true;
    private int f = 0;
    private volatile boolean g = false;

    /* renamed from: a.c.d.j.a$a  reason: collision with other inner class name */
    public interface AbstractC0020a {
        void a(short[] sArr, int i);
    }

    public a(AudioConfig audioConfig, boolean z, AbstractC0020a aVar) {
        this.c = aVar;
        this.f160a = audioConfig;
        this.e = z;
    }

    private AudioRecord.Builder a() {
        int i2;
        AudioFormat.Builder builder;
        AudioFormat.Builder builder2 = new AudioFormat.Builder();
        if (12 == this.f160a.getChannel()) {
            builder = builder2.setSampleRate(this.e ? this.f160a.getSampleRate() : this.f160a.getSampleRate() * 2);
            if (!this.e) {
                i2 = 16;
                builder.setChannelMask(i2);
                return new AudioRecord.Builder().setAudioFormat(builder2.setEncoding(this.f160a.getFormat()).build()).setAudioSource(this.f160a.getSource()).setBufferSizeInBytes(this.f);
            }
        } else {
            builder = builder2.setSampleRate(this.f160a.getSampleRate());
        }
        i2 = this.f160a.getChannel();
        builder.setChannelMask(i2);
        return new AudioRecord.Builder().setAudioFormat(builder2.setEncoding(this.f160a.getFormat()).build()).setAudioSource(this.f160a.getSource()).setBufferSizeInBytes(this.f);
    }

    private void a(@Nullable AudioRecord.Builder builder, int i2) {
        b.a(h, "setSessionId, id: " + i2);
        AudioRecord.Builder.class.getDeclaredMethod("setSessionId", Integer.TYPE).invoke(builder, Integer.valueOf(i2));
    }

    /* JADX WARNING: Removed duplicated region for block: B:13:0x0042  */
    /* JADX WARNING: Removed duplicated region for block: B:14:0x0044  */
    /* JADX WARNING: Removed duplicated region for block: B:16:0x0047  */
    private boolean b() {
        int i2;
        int i3;
        boolean z;
        if (12 == this.f160a.getChannel()) {
            i3 = this.e ? this.f160a.getSampleRate() : this.f160a.getSampleRate() * 2;
            if (!this.e) {
                i2 = 16;
                this.f = AudioRecord.getMinBufferSize(i3, i2, this.f160a.getFormat());
                z = this.f == -2;
                if (!z) {
                    b.b(h, "initBuffer error!");
                }
                return z;
            }
        } else {
            i3 = this.f160a.getSampleRate();
        }
        i2 = this.f160a.getChannel();
        this.f = AudioRecord.getMinBufferSize(i3, i2, this.f160a.getFormat());
        if (this.f == -2) {
        }
        if (!z) {
        }
        return z;
    }

    private boolean c() {
        boolean z;
        if (this.b == null) {
            AudioRecord.Builder a2 = a();
            a(a2, d.c());
            try {
                this.b = a2.build();
            } catch (UnsupportedOperationException unused) {
                try {
                    this.b = a().build();
                } catch (UnsupportedOperationException e2) {
                    b.e(h, "initRecorder failed", e2);
                    z = false;
                }
            }
        }
        z = true;
        if (z) {
            a(this.b.getAudioSessionId());
        }
        return z;
    }

    public boolean a(int i2) {
        String str;
        if (this.d != null) {
            return false;
        }
        if (!AcousticEchoCanceler.isAvailable()) {
            str = "AcousticEchoCanceler is not available";
        } else {
            AcousticEchoCanceler create = AcousticEchoCanceler.create(i2);
            this.d = create;
            if (create != null) {
                create.setEnabled(true);
                return this.d.getEnabled();
            }
            str = "AcousticEchoCanceler create failed";
        }
        b.c(h, str);
        return false;
    }

    public boolean d() {
        return this.g;
    }

    public void e() {
        b.c(h, "startRecording, supportStereo: " + this.e);
        if (b() && c()) {
            new Thread(this).start();
        }
    }

    public void f() {
        this.g = false;
    }

    public void run() {
        AbstractC0020a aVar;
        if (this.b.getState() != 0) {
            b.c(h, "car audio recorder thread start");
            int i2 = 15;
            this.g = false;
            while (true) {
                int i3 = i2 - 1;
                if (i2 <= 0) {
                    break;
                }
                this.b.startRecording();
                if (this.b.getRecordingState() == 3) {
                    this.g = true;
                    break;
                }
                try {
                    Thread.sleep(i);
                } catch (IllegalArgumentException | InterruptedException e2) {
                    b.b(h, "sleep duration start recording error.", e2);
                }
                i2 = i3;
            }
            if (!this.g) {
                b.b(h, "mic is occupied, exit audio record thread");
            }
            int i4 = this.f;
            short[] sArr = new short[i4];
            while (this.g) {
                int read = this.b.read(sArr, 0, i4);
                if (read > 0 && (aVar = this.c) != null) {
                    aVar.a(sArr, read);
                }
            }
            this.b.stop();
            this.b.release();
            AcousticEchoCanceler acousticEchoCanceler = this.d;
            if (acousticEchoCanceler != null) {
                acousticEchoCanceler.release();
                this.d = null;
            }
        }
    }
}
