Stanley Wintergreen
Stanley Wintergreen

Reputation: 385

How to convert rtp packet payload bytes to any audio data?

I am making a project in java without using any third party libraries. I have successfully established an udp connection using the DatagramSocket. Then I started communicating using the sip protocol. I have successfully passed the registration and invitation stage. This is how I got the host and port to which the audio data stream will be transmitted. Then I successfully established a connection to the new address using DatagramSocket. And i began to receive data in the form of rtp packets. I managed to successfully get the following data from the package: Payload type (in my case 8 or PCMA), Timestamp, Sequence number and payload data (byte array). Now I want to process the received data so that I can use it in the future. That is, save to disk, convert to any other audio format at will, play audio, and so on. I can't figure out what exactly needs to be done with the byte array received from the packet.

Let's say for a start I want to save the received data to a file in the AudioFormat.Encoding.PCM_FLOAT 8000.0 Hz, 8 bit, mono, 160 bytes / frame format. What do I need to do for this?

Upvotes: 4

Views: 3447

Answers (1)

Stanley Wintergreen
Stanley Wintergreen

Reputation: 385

Using the example by Aaron Clauson I solved my problem. The byte array from the packet must be converted according to the proposed scheme. I made a working example of how this might look in Kotlin.

An example of how to get data from a raw rtp package.

If you glue the resulting arrays in the correct order (by sequenceNumber) and no duplicates, then you can, for example, write this to a WAV file using javax.sound.sampled.AudioInputStream, as in the example.

A simplified example of how you can play a stream of sound in real time on an Android. And a simplified example of how you can send sound from a microphone to a server.

Upvotes: 3

Related Questions