如何解码 RTP 数据包并将其保存为 .wav 文件 [英] How to decode RTP packets and save it has .wav file

查看:244
本文介绍了如何解码 RTP 数据包并将其保存为 .wav 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个应用程序,其中建立了 sip 呼叫,然后我正在捕获 rtp 音频数据包.由于它们已编码,因此我需要对其进行解码并将其保存为 .wav 文件.尝试使用 NAudio 但没有奏效.有没有使用 NAudio 或任何其他来源的解决方案来解决这个问题...

I am trying to develop an application in which a sip call is established and then i am capturing rtp audio packets. As they are encoded so i need to decode them and save it has .wav file. Tried using NAudio but didnt worked. Is there any solution using NAudio or any other source to solve this problem...

我使用的代码如下.data是rtp包数据所在的字节数组.

the code i used is as follow. data is the byte array in which rtp packet data is.

System.IO.MemoryStream stream = new System.IO.MemoryStream(data);

RawSourceWaveStream rsws = new RawSourceWaveStream(stream, WaveFormat.CreateMuLawFormat(8000,1));

WaveStream conversionStream = WaveFormatConversionStream.CreatePcmStream(rsws);

WaveStream blockAlignedStream = new BlockAlignReductionStream(conversionStream);

byte[] buffer = new byte[udpHeader.Data.Length];
blockAlignedStream.Read(buffer, 0, udpHeader.Data.Length);
writer.WriteData(buffer, 0, buffer.Length);

提前致谢.

推荐答案

迟到总比不到好!

使用 Mark Heath 的以下助手类从使用 NAudio 解码 mu-law 音频

Use the following helper classs by Mark Heath from Using NAudio to decode mu-law audio

    public class RawSourceWaveStream : WaveStream
    {
        private Stream sourceStream;
        private WaveFormat waveFormat;

        public RawSourceWaveStream(Stream sourceStream, WaveFormat waveFormat)
        {
            this.sourceStream = sourceStream;
            this.waveFormat = waveFormat;
        }

        public override WaveFormat WaveFormat
        {
            get { return this.waveFormat; }
        }

        public override long Length
        {
            get { return this.sourceStream.Length; }
        }

        public override long Position
        {
            get
            {
                return this.sourceStream.Position;
            }
            set
            {
                this.sourceStream.Position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return sourceStream.Read(buffer, offset, count);
        }
    }

获取最新的 NAudio.dll 和 NAudio.WindowsMediaFormat.dll

Get the latest NAudio.dll and NAudio.WindowsMediaFormat.dll

来自

http://naudio.codeplex.com/

然后执行以下操作将 U-Law 或 Mu-Law 转换为 Wav:

Then do the following to convert from U-Law or Mu-Law to Wav:

Stream tmpMemStream = new FileStream(Server.MapPath("/input/") + "input.voc", FileMode.Open, FileAccess.Read);
var waveFormat = WaveFormat.CreateMuLawFormat(8000, 1);  // Feel free to tweak this number
var reader = new RawSourceWaveStream(tmpMemStream, waveFormat);
using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
{
    WaveFileWriter.CreateWaveFile(Server.MapPath("/output/") + "output.wav", convertedStream);
}
tmpMemStream.Close();

请记住尊重这些音频文件所有者的隐私!

Just remember to respect the privacy of the owners of those audio files!

这篇关于如何解码 RTP 数据包并将其保存为 .wav 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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