1. 程式人生 > >使用MediaCodec,進行音訊AAC硬編

使用MediaCodec,進行音訊AAC硬編

與我們熟知的MP3格式一樣,AAC是一種音訊編碼格式,對比MP3格式,AAC在縮小30%的前題下可以提供更好的音質。這篇部落格的主要內容就是通過AudioRecorder錄製PCM音訊,再通過MediaCodec將PCM資料硬編碼為AAC格式的音訊。 通常我們使用MediaCodec的流程如下:

MediaCodec的使用流程:

  • createEncoderByType/createDecoderByType
  • configure
  • start
  • while(1) {
  • dequeueInputBuffer
  • queueInputBuffer
  • dequeueOutputBuffer
  • releaseOutputBuffer
  • }
  • stop
  • release

編解碼器一個比較經典的工作原理圖如下: 在這裡插入圖片描述 圖中的Client一般就是我們開發者,解釋一下就是:我們從Codec中拿到拿到空的input buffer,然後填充上我們需要進行編碼的資料,再輸送給Codec,Codec對資料進行編解碼,編解碼完成後,Codec將處理好的資料放進output buffer,我們取出後再清空返還給Codec,形成一個環形結構。可以看作一個生產者-消費者模式。

下面我們的編碼流程也基本遵守上面的這個流程。為了便於大家理解,我畫個流程圖講一下我整個程式碼的邏輯: 在這裡插入圖片描述

錄音和編碼分別在兩個執行緒中進行,兩個執行緒通過一個ArrayBlockQueue(這是一個執行緒安全的佇列,想了解更多自己動手)佇列共享資料,錄音執行緒中的AudioRecorder通過read()將一幀資料put()到隊尾,編碼執行緒中的MediaCodec再通過take()取出隊首的一幀資料進行編碼。 首先看錄音執行緒,為了使程式碼更加簡潔易懂,我會省略掉一些程式碼,完整程式碼會在文末貼出。

    /**
     * 錄音執行緒
     */
    public class AudioRecorder extends Thread {

        private AudioRecord mAudioRecord;
        private boolean isRecording;
        private int minBufferSize;

        public AudioRecorder() {
            isRecording = true;
            initRecorder();
        }

        @Override
        public void run() {
            super.run();
            startRecording();
        }

        /**
         * 初始化錄音
         */
        public void initRecorder(){
            minBufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
            mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, sampleRateInHz, channelConfig, audioFormat, minBufferSize);
            if (mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
                isRecording = false;
                return;
            }
        }

        /**
         * 開始錄音
         */
        public void startRecording(){
            if (mAudioRecord == null){
                return;
            }

            mAudioRecord.startRecording();
            while (isRecording) {
                //自定義的一個類,用來儲存一幀pcm資料,即byte[],下面給出具體定義,很簡單
                AudioDate audioDate = new AudioDate();
                audioDate.buffer = ByteBuffer.allocateDirect(minBufferSize);
                audioDate.size = mAudioRecord.read(audioDate.buffer, minBufferSize);
                try {
                    if (queue != null) {
                        queue.put(audioDate);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            release();
        }
    }

錄音執行緒比較簡單,主要是先初始化錄音器在initRecorder()中,然後通過AudioRecorder的read方法,獲取到一幀資料,通過queue.put放入隊尾。 然後是編碼執行緒。

/**
 * 音訊編碼執行緒
  */
public class AudioEncorder extends Thread {

    private MediaCodec mEncorder;
 private Boolean isEncording = false;
 private int minBufferSize;

 private OutputStream mFileStream;

 public AudioEncorder() {
        isEncording = true;
  initEncorder();
  }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
  public void run() {
        super.run();
  startEncording();
  }

    /**
 * 初始化編碼器
  */
  private void initEncorder(){
        minBufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
 try {
            mEncorder = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC);
  } catch (IOException e) {
            e.printStackTrace();
  }
        MediaFormat format = MediaFormat.createAudioFormat(MediaFormat.MIMETYPE_AUDIO_AAC, sampleRateInHz, channelConfig);
  format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AAC);
  format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
  format.setInteger(MediaFormat.KEY_BIT_RATE, 96000);
  format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, minBufferSize * 2);
  mEncorder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
  }

    /**
 * 開始編碼
  */
  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void startEncording(){
        if (mEncorder == null){
            return;
  }

        mEncorder.start();
 try {
            mFileStream = new FileOutputStream(getSDPath() + "/aac_encode.aac");
  MediaCodec.BufferInfo mBufferInfo = new MediaCodec.BufferInfo();
  AudioDate audioDate;
 while (isEncording) {
                // 從佇列中取出錄音的一幀音訊資料
  audioDate = getAudioDate();

 if (audioDate == null) {
                    continue;
  }

                // 取出InputBuffer,填充音訊資料,然後輸送到編碼器進行編碼
  int inputBufferIndex = mEncorder.dequeueInputBuffer(0);
 if (inputBufferIndex >= 0) {
                    ByteBuffer inputBuffer = mEncorder.getInputBuffer(inputBufferIndex);
  inputBuffer.clear();
  inputBuffer.put(audioDate.buffer);
  mEncorder.queueInputBuffer(inputBufferIndex, 0, audioDate.size, System.nanoTime(), 0);
  }

                // 取出編碼好的一幀音訊資料,然後給這一幀新增ADTS頭
  int outputBufferIndex = mEncorder.dequeueOutputBuffer(mBufferInfo, 0);
 while (outputBufferIndex >= 0) {
                    int outBitsSize = mBufferInfo.size;
 int outPacketSize = outBitsSize + 7; // ADTS頭部是7個位元組
  ByteBuffer outputBuffer = mEncorder.getOutputBuffer(outputBufferIndex);
  outputBuffer.position(mBufferInfo.offset);
  outputBuffer.limit(mBufferInfo.offset + outBitsSize);

 byte[] outData = new byte[outPacketSize];
  addADTStoPacket(outData, outPacketSize);

  outputBuffer.get(outData, 7, outBitsSize);
  outputBuffer.position(mBufferInfo.offset);
  mFileStream.write(outData);
  mEncorder.releaseOutputBuffer(outputBufferIndex, false);
  outputBufferIndex = mEncorder.dequeueOutputBuffer(mBufferInfo, 0);
  }
            }
            release();
  } catch (FileNotFoundException e) {
            e.printStackTrace();
  } catch (IOException e) {
            e.printStackTrace();
  }
    }

    /**
 * 新增ADTS頭
  * @param packet
  * @param packetLen
  */
  private void addADTStoPacket(byte[] packet, int packetLen) {
        int profile = 2; //AAC LC
  int freqIdx = 4; //44100 根據不同的取樣率修改這個值
  int chanCfg = 2; //CPE
  packet[0] = (byte) 0xFF;
  packet[1] = (byte) 0xF9;
  packet[2] = (byte) (((profile - 1) << 6) + (freqIdx << 2) + (chanCfg >> 2));
  packet[3] = (byte) (((chanCfg & 3) << 6) + (packetLen >> 11));
  packet[4] = (byte) ((packetLen & 0x7FF) >> 3);
  packet[5] = (byte) (((packetLen & 7) << 5) + 0x1F);
  packet[6] = (byte) 0xFC;
  }
}

和錄音執行緒一樣,先進行編碼器的初始化在initEncorder()中,這裡的引數MediaFormat比較關鍵,針對具體的編碼格式有不同的編碼格式有不同的引數,其他的編碼方式自行參閱官方文件,這裡的AAC編碼器引數我是經過測試可行的。 然後是最關鍵的一步–編碼。在startEncording()方法中,所有的流程完全對應上面我列出的MediaCodec的使用流程,可以對照上面的流程來閱讀程式碼。 編碼流程中非常重要的一步就是為每一幀AAC音訊新增ADTS頭,和WAV格式的音訊不同,AAC為每一幀音訊都添加了一個ADTS頭,使得解碼器可以從任意一幀開始解碼,有時我們遇到無法編碼的AAC檔案無法播放,可能就是因為我們沒有為其新增ADTS頭。通過上面的addADTStoPacket()方法便可以未每一幀AAC音訊新增ADTS頭了。 至此,我就列出了我認為在AAC編碼的過程中所需要注意的所有點。在完成這個功能的時候,我也參照了一些別人的做法,以及我自己的一些理解與實踐,也遇到了一些令人頭大的錯誤(例如:同樣的PCM資料,放進佇列中再取出來寫入檔案,就出現了很大的雜音)如有錯誤的地方,還請留言指正。 下面是完整程式碼:

package com.example.sisyphus.audiovideolearning;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * Created by sisyphus on 2018/8/8. * 學習 MediaCodec API,完成音訊 AAC 硬編、硬解
  * 
  * MediaCodec的使用流程:
  * - createEncoderByType/createDecoderByType
 * - configure * - start * - while(1) { * - dequeueInputBuffer * - queueInputBuffer * - dequeueOutputBuffer * - releaseOutputBuffer * } * - stop * - release */public class AACCodecActivity extends Activity {

    private final int sampleRateInHz = 44100;
 private final int channelConfig = 1;
 private final int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

 private class AudioDate {
        private ByteBuffer buffer;
 private int size;
  }

    public static String[] MICROPHONE = {Manifest.permission.RECORD_AUDIO};
 public static String[] STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

 private AudioRecorder mAudioRecorder;
 private AudioEncorder mAudioEncorder;

 private ArrayBlockingQueue queue;

 private Button btnStartRecording;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_aac_codec);
  btnStartRecording = findViewById(R.id.btn_start_aac_encode);
  queue = new ArrayBlockingQueue<>(1024);
  mAudioRecorder = new AudioRecorder();
  mAudioEncorder = new AudioEncorder();
  btnStartRecording.setOnTouchListener(new View.OnTouchListener() {
            @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {
                int action = motionEvent.getAction();
 if (action == MotionEvent.ACTION_DOWN) {
                    checkRecordPermission();
  btnStartRecording.setText("正在錄製");
  startRecord();
  } else if (action == MotionEvent.ACTION_UP) {
                    btnStartRecording.setText("錄音");
  stopRecord();
  }
                return false;
  }
        });
  }

    private void startRecord() {
        mAudioRecorder.start();
  mAudioEncorder.start();
  }

    private void stopRecord() {
        mAudioRecorder.stopRecording();
  mAudioEncorder.stopEncording();
  }

    /**
 * 錄音執行緒
  */
  public class AudioRecorder extends Thread {

        private AudioRecord mAudioRecord;
 private boolean isRecording;
 private int minBufferSize;

 public AudioRecorder() {
            isRecording = true;
  initRecorder();
  }

        @Override
  public void run() {
            super.run();
  startRecording();
  }

        /**
 * 初始化錄音
  */
  public void initRecorder(){
            minBufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
  mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, sampleRateInHz, channelConfig, audioFormat, minBufferSize);
 if (mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
                isRecording = false;
 return;  }
        }

        /**
 * 釋放資源
  */
  public void release() {
            if (mAudioRecord != null && mAudioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
                mAudioRecord.stop();
  }
        }

        /**
 * 開始錄音
  */
  public void startRecording(){
            if (mAudioRecord == null){
                return;
  }

            mAudioRecord.startRecording();
 while (isRecording) {
                AudioDate audioDate = new AudioDate();
  audioDate.buffer = ByteBuffer.allocateDirect(minBufferSize);
  audioDate.size = mAudioRecord.read(audioDate.buffer, minBufferSize);
 try {
                    if (queue != null) {
                        queue.put(audioDate);
  }
                } catch (InterruptedException e) {
                    e.printStackTrace();
  }
            }
            release();
  }

        /**
 * 結束錄音
  */
  public void stopRecording() {
            isRecording = false;
  }
    }

    /**
 * 音訊編碼執行緒
  */
  public class AudioEncorder extends Thread {

        private MediaCodec mEncorder;
 private Boolean isEncording = false;
 private int minBufferSize;

 private OutputStream mFileStream;

 public AudioEncorder() {
            isEncording = true;
  initEncorder();
  }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
  public void run() {
            super.run();
  startEncording();
  }

        /**
 * 初始化編碼器
  */
  private void initEncorder(){
            minBufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
 try {
                mEncorder = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC);
  } catch (IOException e) {
                e.printStackTrace();
  }
            MediaFormat format = MediaFormat.createAudioFormat(MediaFormat.MIMETYPE_AUDIO_AAC, sampleRateInHz, channelConfig);
  format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AAC);
  format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
  format.setInteger(MediaFormat.KEY_BIT_RATE, 96000);
  format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, minBufferSize * 2);
  mEncorder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
  }

        /**
 * 開始編碼
  */
  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public void startEncording(){
            if (mEncorder == null){
                return;
  }

            mEncorder.start();
 try {
                mFileStream = new FileOutputStream(getSDPath() + "/aac_encode.aac");
  MediaCodec.BufferInfo mBufferInfo = new MediaCodec.BufferInfo();
  AudioDate audioDate;
 while (isEncording) {
                    // 從佇列中取出錄音的一幀音訊資料
  audioDate = getAudioDate();

 if (audioDate == null) {
                        continue;
  }

                    // 取出InputBuffer,填充音訊資料,然後輸送到編碼器進行編碼
  int inputBufferIndex = mEncorder.dequeueInputBuffer(0);
 if (inputBufferIndex >= 0) {
                        ByteBuffer inputBuffer = mEncorder.getInputBuffer(inputBufferIndex);
  inputBuffer.clear();
  inputBuffer.put(audioDate.buffer);
  mEncorder.queueInputBuffer(inputBufferIndex, 0, audioDate.size, System.nanoTime(), 0);
  }

                    // 取出編碼好的一幀音訊資料,然後給這一幀新增ADTS頭
  int outputBufferIndex = mEncorder.dequeueOutputBuffer(mBufferInfo, 0);
 while (outputBufferIndex >= 0) {
                        int outBitsSize = mBufferInfo.size;
 int outPacketSize = outBitsSize + 7; // ADTS頭部是7個位元組
  ByteBuffer outputBuffer = mEncorder.getOutputBuffer(outputBufferIndex);
  outputBuffer.position(mBufferInfo.offset);
  outputBuffer.limit(mBufferInfo.offset + outBitsSize);

 byte[] outData = new byte[outPacketSize];
  addADTStoPacket(outData, outPacketSize);

  outputBuffer.get(outData, 7, outBitsSize);
  outputBuffer.position(mBufferInfo.offset);
  mFileStream.write(outData);
  mEncorder.releaseOutputBuffer(outputBufferIndex, false);
  outputBufferIndex = mEncorder.dequeueOutputBuffer(mBufferInfo, 0);
  }
                }
                release();
  } catch (FileNotFoundException e) {
                e.printStackTrace();
  } catch (IOException e) {
                e.printStackTrace();
  }
        }

        /**
 * 停止編碼
  */
  public void stopEncording() {
            isEncording = false;
  }

        /**
 * 從佇列中取出一幀待編碼的音訊資料
  * @return
  */
  public AudioDate getAudioDate(){
            if (queue != null){
                try {
                    return queue.take();
  } catch (InterruptedException e) {
                    e.printStackTrace();
  }
            }
            return null;
  }

 /**
 * 新增ADTS頭
  * @param packet
  * @param packetLen
  */
  private void addADTStoPacket(byte[] packet, int packetLen) {
            int profile = 2; //AAC LC
  int freqIdx = 4; //44100 根據不同的取樣率修改這個值
  int chanCfg = 2; //CPE
  packet[0] = (byte) 0xFF;
  packet[1] = (byte) 0xF9;
  packet[2] = (byte) (((profile - 1) << 6) + (freqIdx << 2) + (chanCfg >> 2));
  packet[3] = (byte) (((chanCfg & 3) << 6) + (packetLen >> 11));
  packet[4] = (byte) ((packetLen & 0x7FF) >> 3);
  packet[5] = (byte) (((packetLen & 7) << 5) + 0x1F);
  packet[6] = (byte) 0xFC;
  }

 /**
 * 釋放資源
  */
  public void release() {
            if (mFileStream != null) {
                try {
                    mFileStream.flush();
  mFileStream.close();
  } catch (IOException e) {
                    e.printStackTrace();
  }
            }
            if (mEncorder != null) {
                mEncorder.stop();
  }
        }
    }

    public String getSDPath() {
        // 判斷是否掛載
  if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();
  }
        return Environment.getRootDirectory().getAbsolutePath();
  }

    public void checkRecordPermission() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, MICROPHONE, 1);
 return;  }
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, STORAGE, 1);
 return;  }
    }

}