如何将RTP数据包的有效载荷字节转换为任何音频数据?

huangapple go评论91阅读模式
英文:

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

问题

我正在使用Java制作一个项目,不使用任何第三方库。我已经成功使用DatagramSocket建立了UDP连接。然后我开始使用SIP协议进行通信。我已经成功完成了注册和邀请阶段。这是我获取主机和端口的方法,用于传输音频数据流。然后,我成功使用DatagramSocket建立了到新地址的连接。我开始以RTP数据包的形式接收数据。我成功地从数据包中获取了以下数据:有效载荷类型(在我这里是8或PCMA),时间戳,序列号以及有效载荷数据(字节数组)。现在,我想处理接收到的数据,以便将来使用。也就是说,保存到磁盘,随意转换为任何其他音频格式,播放音频等等。我无法弄清楚从数据包中接收到的字节数组需要做什么。

举个例子,作为一个开始,我想将接收到的数据保存到以AudioFormat.Encoding.PCM_FLOAT,8000.0赫兹,8位,单声道,每帧160字节的格式保存。为此,我需要做什么?

英文:

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?

答案1

得分: 2

使用Aaron Clauson示例,我解决了我的问题。数据包中的字节数组必须按照提议的方案进行转换。我制作了一个实际工作的Kotlin示例,演示了这个过程可能的样子。

一个获取原始RTP数据包中数据的示例

如果按照sequenceNumber的正确顺序将结果数组粘合在一起(没有重复项),那么你可以将其写入WAV文件,使用javax.sound.sampled.AudioInputStream,就像这个示例中所示。

一个简化的示例,演示了如何在Android上实时播放音频流。以及一个简化的示例,展示了如何将麦克风捕获的声音发送到服务器。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月20日 21:42:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63979598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定