如何将 rtp 数据包有效载荷字节转换为任何音频数据? [英] How to convert rtp packet payload bytes to any audio data?
问题描述
我正在用 Java 制作一个项目,而不使用任何第三方库.我已经使用 DatagramSocket
成功建立了 udp 连接.然后我开始使用 sip 协议进行通信.我已成功通过注册和邀请阶段.这就是我获得音频数据流将传输到的主机和端口的方式.然后我使用 DatagramSocket
成功建立了到新地址的连接.我开始以 rtp 数据包的形式接收数据.我成功地从包中获取了以下数据:有效载荷类型(在我的情况下为 8 或 PCMA)、时间戳、序列号和有效载荷数据(字节数组).现在我想处理接收到的数据,以便我将来可以使用它.即保存到磁盘,随意转换为任何其他音频格式,播放音频等等.我无法弄清楚从数据包中收到的字节数组到底需要做什么.
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.
假设首先我想将接收到的数据保存到 AudioFormat.Encoding.PCM_FLOAT
8000.0 Hz、8 位、单声道、160 字节/帧格式的文件中.我需要为此做什么?
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?
推荐答案
使用 示例 来自 Aaron Clauson 我解决了我的问题.数据包中的字节数组必须根据建议的方案进行转换.我做了一个有效的示例,说明这在 Kotlin 中的表现.
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.
关于如何从原始 rtp 包获取数据的示例.
An example of how to get data from a raw rtp package.
如果您以正确的顺序(按 sequenceNumber
)粘合结果数组并且没有重复,那么您可以,例如,使用 javax.sound.sampled 将其写入 WAV 文件.AudioInputStream
,如示例.
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.
这篇关于如何将 rtp 数据包有效载荷字节转换为任何音频数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!