1. 程式人生 > >Android手機間語音通話使用webrtc消除迴音

Android手機間語音通話使用webrtc消除迴音

       公司的產品智慧門鈴當與人通話過程中會產生迴音,因此想用webrtc的迴音消除模組來消除,所以讓我寫一個android間語音通話的demo來驗證webrtc迴音消除模組的效果,下面就是我實現這個demo的整個過程。

       實現步驟:

              (1)用socket讓手機間建立連線

              (2)開啟手機錄音和播放功能

              (4)通過socket的流傳輸語音資料

              (3)加入webrtc迴音消除模組進行迴音消除,delay值不同的手機不同,需要自己除錯,否則迴音消除沒有效果,我測試的小米3,delay值大概是190

下面是實現的具體程式碼:

package com.ljc.userotherso;

import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.webrtc.audio.MobileAEC;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_ip_str;
    private CheckBox cb_is_acem;
    private Button bt_start_double_talk;
    private ServerSocket serverSocket;
    private Socket client;
    private OutputStream clientOut;
    private InputStream clientIs;
    private OutputStream serverOut;
    private InputStream serverIs;
    private EditText et_ip_address;
    private Button bt_start_connection;
    private Button bt_finish;
    private boolean isServer = true;
    private boolean isClear = false;
    private Button bt_record_and_play;
    //錄音
    private AudioRecord audioRecord;
    //播放
    private AudioTrack audioTrack;
    //取樣率
    private static final int frequency = 16000;
    private static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    private static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
    private int recBufSize;
    private int playBufSize;
    private boolean isRecording = false;
    private EditText et_delay;
    private static final int port = 7788;
    private static final int buffSize = 320;
    private static final int delay = 190;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_ip_str = (TextView) findViewById(R.id.tv_ip_str);
        cb_is_acem = (CheckBox) findViewById(R.id.cb_is_acem);
        bt_start_double_talk = (Button) findViewById(R.id.bt_start_double_talk);
        et_ip_address = (EditText) findViewById(R.id.et_ip_address);
        bt_start_connection = (Button) findViewById(R.id.bt_start_connection);
        bt_finish = (Button) findViewById(R.id.bt_finish);

        bt_finish.setOnClickListener(this);
        bt_start_double_talk.setOnClickListener(this);
        bt_start_connection.setOnClickListener(this);

        String ip = getPhoneIp();
        if(ip == null){

            tv_ip_str.setText("wifi未連線,請連線wifi");
        }else{

            tv_ip_str.setText("本機ip:"+ip);
        }

        cb_is_acem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                isClear = isChecked;
            }
        });
        
        //服務端監聽
        listen();

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case  R.id.bt_start_connection://開始連線

                startConnection();

                break;

            case R.id.bt_start_double_talk://開始對講

                startTalk();

                break;
            case R.id.bt_finish://結束對講

                stopTalk();
                break;
            default:
                break;
        }
    }

    private void startConnection(){

        String ip = et_ip_address.getText().toString();
        if(TextUtils.isEmpty(ip)){

            Toast.makeText(this,"未輸入ip無法連線!",Toast.LENGTH_SHORT).show();
            return;
        }

        connection(ip);

        isServer = false;
    }

    private void startTalk(){

        bt_start_double_talk.setEnabled(false);

        new Thread(new Runnable() {
            @Override
            public void run() {

                try {

                    openAudioRecordAndAudioTrack();
                    isRecording = true;

                    MobileAEC aecm = new MobileAEC(null);
                    aecm.setAecmMode(MobileAEC.AggressiveMode.MOST_AGGRESSIVE).prepare();

                    byte[] buff = new byte[buffSize];

                    while(isRecording){

                        int len = audioRecord.read(buff, 0, buffSize);

                        if(isClear){

                            buff = acem(aecm,buff);

                        }

                        if(isServer){

                            clientOut.write(buff,0,buffSize);
                        }else{

                            serverOut.write(buff,0,buffSize);
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 消除迴音
     * @param aecm
     * @param buff
     * @return
     * @throws Exception
     */
    private byte[] acem(MobileAEC aecm,byte[] buff) throws Exception {

        short[] aecTmpIn = new short[buffSize / 2];
        short[] aecTmpOut = new short[buffSize / 2];

        ByteBuffer.wrap(buff).order(ByteOrder.LITTLE_ENDIAN)
                .asShortBuffer().get(aecTmpIn);

        aecm.farendBuffer(aecTmpIn, buffSize / 2);

        aecm.echoCancellation(aecTmpIn, null, aecTmpOut,
                (short) (buffSize / 2), (short)delay);

        byte[] aecBuf = new byte[buffSize];
        ByteBuffer.wrap(aecBuf).order(ByteOrder.LITTLE_ENDIAN)
                .asShortBuffer().put(aecTmpOut);

        return aecBuf;
    }

    /**
     * 結束對話
     */
    private void stopTalk(){
        try {
            audioTrack.stop();
            audioRecord.stop();
            isRecording =false;
            bt_start_double_talk.setEnabled(true);
            bt_start_connection.setEnabled(true);

            if(isServer){

                clientOut.close();
                clientIs.close();
                serverSocket.close();
                client.close();

            }else{
                serverOut.close();
                serverIs.close();
                client.close();
            }

            listen();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * 監聽連線
     */
    private void listen() {

        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    serverSocket = new ServerSocket(port);
                    client = serverSocket.accept();

                    clientOut = client.getOutputStream();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            bt_start_connection.setEnabled(false);
                            Toast.makeText(getApplicationContext(), "連線成功",Toast.LENGTH_SHORT).show();
                        }
                    });

                    clientIs = client.getInputStream();
                    byte[] buff = new byte[buffSize];
                    int len = 0;

                    while(client.isConnected()){

                        if(((len = clientIs.read(buff)) != -1) && audioTrack != null){

                            audioTrack.write(buff,0,len);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 連線服務端
     * @param ip
     */
    private void connection(final String ip){

        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    client = new Socket(ip,port);

                    if(client.isConnected()){

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bt_start_connection.setEnabled(false);
                            }
                        });
                        serverOut = client.getOutputStream();
                        serverIs = client.getInputStream();

                        byte[] buff = new byte[buffSize];
                        int len = 0;

                        while(client.isConnected()){

                            if(((len = serverIs.read(buff)) != -1) && audioTrack != null){

                                audioTrack.write(buff,0,len);
                            }
                        }
                    }else{
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                Toast.makeText(getApplicationContext(), "連線失敗,ip有誤",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 開啟錄音器和播音器
     */
    private void openAudioRecordAndAudioTrack(){

        recBufSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
        playBufSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, recBufSize);
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency, channelConfiguration, audioEncoding, playBufSize, AudioTrack.MODE_STREAM);
        audioTrack.setStereoVolume(1.0f, 1.0f);
        audioRecord.startRecording();
        audioTrack.play();
    }
    /**
     * 獲得手機ip地址
     *
     * @return
     */
    private String getPhoneIp() {

        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        String ipAddress;
        if(wifiManager.isWifiEnabled()){

            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            ipAddress = intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址
        }else{

            ipAddress = null;
        }

        return ipAddress;
    }

    /**
     * 將得到的int型別的IP轉換為String型別
     *
     * @param ip
     * @return
     */
    public static String intIP2StringIP(int ip) {
        return (ip & 0xFF) + "." +
                ((ip >> 8) & 0xFF) + "." +
                ((ip >> 16) & 0xFF) + "." +
                (ip >> 24 & 0xFF);
    }
}
效果圖: