package com.dowse.base.audio;

import android.annotation.SuppressLint;
import android.media.AudioRecord;
import android.os.SystemClock;
import android.util.Log;

/* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/audio/AudioCapturer.class */
public class AudioCapturer {
    private static final String TAG = "AudioCapturer";
    private static final int DEFAULT_SOURCE = 1;
    private static final int DEFAULT_SAMPLE_RATE = 8000;
    private static final int DEFAULT_CHANNEL_CONFIG = 16;
    private static final int DEFAULT_AUDIO_FORMAT = 2;
    private AudioRecord mAudioRecord;
    private Thread mCaptureThread;
    private OnAudioFrameCapturedListener mAudioFrameCapturedListener;
    private int mMinBufferSize = 0;
    private boolean mIsCaptureStarted = false;
    private volatile boolean mIsLoopExit = false;

    /* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/audio/AudioCapturer$OnAudioFrameCapturedListener.class */
    public interface OnAudioFrameCapturedListener {
        void onAudioFrameCaptured(byte[] bArr);
    }

    public boolean isCaptureStarted() {
        return this.mIsCaptureStarted;
    }

    public void setOnAudioFrameCapturedListener(OnAudioFrameCapturedListener listener) {
        this.mAudioFrameCapturedListener = listener;
    }

    public boolean startCapture() {
        return startCapture(1, DEFAULT_SAMPLE_RATE, DEFAULT_CHANNEL_CONFIG, 2);
    }

    @SuppressLint({"MissingPermission"})
    public boolean startCapture(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat) {
        if (this.mIsCaptureStarted) {
            Log.e(TAG, "Capture already started !");
            return false;
        }
        this.mMinBufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
        if (this.mMinBufferSize == -2) {
            Log.e(TAG, "Invalid parameter !");
            return false;
        }
        Log.d(TAG, "getMinBufferSize = " + this.mMinBufferSize + " bytes !");
        this.mAudioRecord = new AudioRecord(audioSource, sampleRateInHz, channelConfig, audioFormat, this.mMinBufferSize);
        if (this.mAudioRecord.getState() == 0) {
            Log.e(TAG, "AudioRecord initialize fail !");
            return false;
        }
        this.mAudioRecord.startRecording();
        this.mIsLoopExit = false;
        this.mCaptureThread = new Thread(new AudioCaptureRunnable());
        this.mCaptureThread.start();
        this.mIsCaptureStarted = true;
        Log.d(TAG, "Start audio capture success !");
        return true;
    }

    public boolean stopCapture() {
        if (!this.mIsCaptureStarted) {
            return false;
        }
        this.mIsLoopExit = true;
        try {
            this.mCaptureThread.interrupt();
            this.mCaptureThread.join(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (this.mAudioRecord.getRecordingState() == 3) {
            this.mAudioRecord.stop();
        }
        this.mAudioRecord.release();
        this.mIsCaptureStarted = false;
        Log.d(TAG, "Stop audio capture success !");
        return true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/audio/AudioCapturer$AudioCaptureRunnable.class */
    public class AudioCaptureRunnable implements Runnable {
        private AudioCaptureRunnable() {
        }

        @Override // java.lang.Runnable
        public void run() {
            while (!AudioCapturer.this.mIsLoopExit) {
                byte[] buffer = new byte[AudioCapturer.this.mMinBufferSize];
                int ret = AudioCapturer.this.mAudioRecord.read(buffer, 0, AudioCapturer.this.mMinBufferSize);
                if (ret == -3) {
                    Log.e(AudioCapturer.TAG, "Error ERROR_INVALID_OPERATION");
                } else if (ret != -2) {
                    if (AudioCapturer.this.mAudioFrameCapturedListener != null) {
                        AudioCapturer.this.mAudioFrameCapturedListener.onAudioFrameCaptured(buffer);
                    }
                } else {
                    Log.e(AudioCapturer.TAG, "Error ERROR_BAD_VALUE");
                }
                SystemClock.sleep(10L);
            }
        }
    }
}
