转换一个RTP序列载荷中的一个.wav文件 [英] Convert a RTP sequence payload in a .wav file

查看:317
本文介绍了转换一个RTP序列载荷中的一个.wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有效载荷(十六进制)的有关VoIP通话的RTP数据包的文本文件,没有人知道如何将文本转换成一个文件。使用WAV音频C / C ++?

I have a text file with the payload (in hex) of about RTP packets of a VoIP conversation, does anyone know how to convert the text into a file. wav audio using c/c++?

PS:我使用的GNU / Linux

PS: I'm using GNU / Linux.

感谢

推荐答案

我做了Java的同样的事情。这是一类我使用的测试目的接收UDP的RTP包(ULAW),并将其保存为WAV文件。看到主方法如何使用它的一个例子。

I do that same thing with Java. Here is a class I use for testing purposes to receive UDP RTP packets (uLaw) and save them to a WAV file. See the main method for an example of how to use it.

public class UdpDataReceiver implements Runnable {

    private boolean isRunning = true;
    private int port = -1;
    private DatagramSocket socket;

    public UdpDataReceiver(int port) {
        this.port = port;
    }

    public void stop() {
        isRunning = false;
        socket.close();
    }

    @Override
    public void run() {
        try {
            socket = new DatagramSocket(port);
            System.out.println(" + Listening for UDP data on port " + port + ".");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Size of data buffer is insignificant; it just needs to be big enough to hold the UDP packet (or it will be truncated).
        byte[] data = new byte[512];
        DatagramPacket packet = new DatagramPacket(data, data.length);

        String file = null;
        FileOutputStream fos = null;
        try {
            // For now we'll just dump to a file.
            file = "C:\\tmp\\" + System.currentTimeMillis() + ".raw";
            fos = new FileOutputStream(file);

            while (isRunning) {
                try {
                    socket.receive(packet);
                } catch (SocketException se) {
                    // If we're just trying to tell the socket we're done, we'll get a SocketException.
                    if (isRunning) {
                        se.printStackTrace();
                    }
                }
                if (packet.getLength() > 12) {
                    // Strip off 12 bytes of header data.
                    fos.write(packet.getData(), packet.getOffset() + 12, packet.getLength() - 12);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                fos.close();
                convertULawFileToWav(file);
            } catch (Exception e) {
            }
            System.out.println(" + Closed port " + port + " and wrote UDP packets to file: " + file);
        }
    }

    public static void convertULawFileToWav(String filename) {
        File file = new File(filename);
        if (!file.exists())
            return;
        try {
            long fileSize = file.length();
            int frameSize = 160;
            long numFrames = fileSize / frameSize;
            AudioFormat audioFormat = new AudioFormat(Encoding.ULAW, 8000, 8, 1, frameSize, 50, true);
            AudioInputStream audioInputStream = new AudioInputStream(new FileInputStream(file), audioFormat, numFrames);
            AudioSystem.write(audioInputStream, Type.WAVE, new File(file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 3) + "wav"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // For testing...
    public static void main(String[] args) throws InterruptedException {
        UdpDataReceiver r1 = new UdpDataReceiver(65337);
        new Thread(r1).start();
        // Send data...
        Thread.sleep(30000);
        r1.stop();
    }

}

这篇关于转换一个RTP序列载荷中的一个.wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆