/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xvideo;

import android.media.AudioFormat;
import android.media.AudioRecord;

/**
 * 音频录制
 *
 * @author xuexiang
 * @since 2018/5/30 下午8:18
 */
public class AudioRecorder extends Thread {

    private AudioRecord mAudioRecord = null;
    /**
     * 采样率
     */
    private int mSampleRate = 44100;
    private IMediaRecorder mMediaRecorder;

    public AudioRecorder(IMediaRecorder mediaRecorder) {
        this.mMediaRecorder = mediaRecorder;
    }

    /**
     * 设置采样率
     */
    public void setSampleRate(int sampleRate) {
        this.mSampleRate = sampleRate;
    }

    @Override
    public void run() {
        if (mSampleRate != 8000 && mSampleRate != 16000 && mSampleRate != 22050 && mSampleRate != 44100) {
            mMediaRecorder.onAudioError(MediaRecorderBase.AUDIO_RECORD_ERROR_SAMPLERATE_NOT_SUPPORT, "sampleRate not support.");
            return;
        }

        final int mMinBufferSize = AudioRecord.getMinBufferSize(mSampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);

        if (AudioRecord.ERROR_BAD_VALUE == mMinBufferSize) {
            mMediaRecorder.onAudioError(MediaRecorderBase.AUDIO_RECORD_ERROR_GET_MIN_BUFFER_SIZE_NOT_SUPPORT, "parameters are not supported by the hardware.");
            return;
        }

        mAudioRecord = new AudioRecord(android.media.MediaRecorder.AudioSource.MIC, mSampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, mMinBufferSize);
        if (null == mAudioRecord) {
            mMediaRecorder.onAudioError(MediaRecorderBase.AUDIO_RECORD_ERROR_CREATE_FAILED, "new AudioRecord failed.");
            return;
        }
        try {
            mAudioRecord.startRecording();
        } catch (IllegalStateException e) {
            mMediaRecorder.onAudioError(MediaRecorderBase.AUDIO_RECORD_ERROR_UNKNOWN, "startRecording failed.");
            return;
        }

        byte[] sampleBuffer = new byte[2048];

        try {
            while (!Thread.currentThread().isInterrupted()) {

                int result = mAudioRecord.read(sampleBuffer, 0, 2048);
                if (result > 0) {
                    mMediaRecorder.receiveAudioData(sampleBuffer, result);
                }
            }
        } catch (Exception e) {
            mMediaRecorder.onAudioError(MediaRecorderBase.AUDIO_RECORD_ERROR_UNKNOWN, e.getMessage());
        }

        mAudioRecord.release();
        mAudioRecord = null;
    }
}
