使用android.net.rtp工具时RTP媒体的大延迟

时间:2015-07-03 16:58:46

标签: android media voip rtp

我正在为Android构建一个VOIP应用程序,并使用内置的RTP工具android.net.rtp。除了以下问题外,一切正常:

  • 在传入的RTP流中存在很大的延迟(大约0.5-1秒,直到我听到另一方) - 传出流立即到达另一方的通知
  • 在传出的RTP流中存在小的中断。

请记住,对于AudioStream,我使用的是PCMU编解码器,远程用户也是如此。

以下是一些关键代码:

初始化音频设施(一次):

public class SoundManager implements AudioManager.OnAudioFocusChangeListener {
    Context appContext;
    AudioManager audio;
    AudioStream audioStream;
    AudioGroup audioGroup;
    InetAddress localAddress;
    private static final String TAG = "SoundManager";

    public SoundManager(Context appContext, String ip){
        this.appContext = appContext;
        audio = (AudioManager) appContext.getSystemService(Context.AUDIO_SERVICE);
        try {
            localAddress = InetAddress.getByName(ip);
            audioGroup = new AudioGroup();
            audioGroup.setMode(AudioGroup.MODE_ECHO_SUPPRESSION);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

初始化AudioSession(每次通话开始前一次)

public int setupAudioStream() {
    Log.i(TAG, "Setting up Audio Stream");
    try {
        audioStream = new AudioStream(localAddress);
        audioStream.setCodec(AudioCodec.PCMU);
        audioStream.setMode(RtpStream.MODE_NORMAL);
    }
    catch (SocketException e) {
        e.printStackTrace();
    }

    return audioStream.getLocalPort();
}

开始播放(每次通话开始时一次)

public void startStreaming(int remoteRtpPort, String remoteIp) {
    audio.setMode(AudioManager.MODE_IN_COMMUNICATION);

    // Request audio focus for playback
    int result = audio.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);

    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        try {
            audioStream.associate(
                    InetAddress.getByName(remoteIp),
                    remoteRtpPort);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }

        try {
            audioStream.join(audioGroup);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }
    else {
        Log.e(TAG, "Cannot receive audio focus; media stream not setup");
    }
}

停止播放(每次通话结束时一次)

public void stopStreaming() {
    try {
        audioStream.join(null);
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }

    audioGroup.clear();
    if (audioStream.isBusy()) {
        Log.i(TAG, "AudioStream is busy");
    }
    audioStream = null;
    audio.setMode(AudioManager.MODE_NORMAL);

    // Abandon audio focus when playback complete
    audio.abandonAudioFocus(this);
}

最后,请注意在Android控制台(logcat)中我看到了这些:

D/AudioGroup﹕ latency: output 400, input 64'
D/AudioGroup﹕ stream[56] is configured as PCMU 8kHz 20ms mode 0
D/AudioGroup﹕ stream[67] is configured as RAW 8kHz 32ms mode 0

似乎其中一个流有很大的延迟,加上其中一个似乎配置为RAW。不确定我是否正确解释它们

有没有人见过类似的行为?

祝你好运, 安东尼

0 个答案:

没有答案