javaandroidrtp

Capturing RTP packets from sockets in Android


I am using RTP stream to stream pcm (16kHz) data from remote server to android device. Using audiotrack seems to be right approach to play PCM audio. I am just looking for a way to capture the RTP packets from the server and can write it in audiotrack.write(). This is what I have got so far.

int bufsize = AudioTrack.getMinBufferSize(16000,
AudioFormat.CHANNEL_OUT_STEREO,
AudioFormat.ENCODING_PCM_16BIT);

AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                                                    16000,
                          AudioFormat.CHANNEL_OUT_STEREO,
                          AudioFormat.ENCODING_PCM_16BIT,
                          bufsize,
                          AudioTrack.MODE_STREAM);
audioTrack.play();

But writing data is where I am bit confused about. How can I feed the incoming RTP packets from sockets?

audioTrack.write(byte[], int, int);

I got stuck here. How can I approach this?


Solution

  • Once you read the data from the udp socket.

    Remove the 12 Bytes RTP header (rfc 3550), you will get the payload (PCM data),

    you can pass the pcm data to audio track.