2

首先,我要道歉。很久以前,我曾经修补过 VB5,并且已经离开程序员多年了——我仍在重新学习基础知识,最近开始学习 C#/.NET。我也是这个网站的新手,请耐心等待并给予指导。我的背景故事已经够多了。

将此包装器用于 Opus,其中我将包装器项目添加到我自己的解决方案中,并且 NAudio 我相信我已将其设置为主动从我的设备(声卡)中获取音频并利用示例编码器代码将编码的音频放入_playBuffer。

我的下一个任务是从 RDP 获取编码数据并使用它发送它,以便可以在另一台机器上的客户端应用程序中发送它以进行解码,在那里它将被解码并从他们的声音设备中播放出来。

我是否正确理解 _playBuffer 中的数据是现成的编码数据?或者这是否需要对 RTP 数据包进行不同的拆分?(我在这里看到了一个 uLAW 示例,但我不确定我是否可以适应我的需要。因为下载的源代码是用似乎是德语的注释 - 但我几乎不会说和写英语作为第一语言 - 即使那些也不是非常有帮助。)

(我什至使用了正确的术语吗?)截至目前,您看到的股票代码通过 WaveOut 将 _playBuffer 数据放回,就像他的示例一样 - 我在这里忽略了它并留下来解释我的(可能缺乏)理解。(如果它是“可播放的”,它就是“可发送的”。)

另一个问题是我的意图是在互联网上为点对点多播流 - 尽管我不确定多播是我想要的。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using NAudio;
    using NAudio.CoreAudioApi;
    using NAudio.Wave;
    using FragLabs.Audio.Codecs;

    namespace VUmeterappStereo
    {
        public partial class Form1 : Form
        {private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < WaveIn.DeviceCount; i++)
            {
                comboBox1.Items.Add(WaveIn.GetCapabilities(i).ProductName);
            }
            if (WaveIn.DeviceCount > 0)
                comboBox1.SelectedIndex = 0;
            for (int i = 0; i < WaveOut.DeviceCount; i++)
            {
                comboBox2.Items.Add(WaveOut.GetCapabilities(i).ProductName);
            }
            if (WaveOut.DeviceCount > 0)
                comboBox2.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button2.Enabled = true;
            button1.Enabled = false;
            StartEncoding();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.Enabled = true;
            button2.Enabled = false;
            StopEncoding();
        }

        WaveIn _waveIn;
        WaveOut _waveOut;
        BufferedWaveProvider _playBuffer;
        OpusEncoder _encoder;
        OpusDecoder _decoder;
        int _segmentFrames;
        int _bytesPerSegment;
        ulong _bytesSent;
        DateTime _startTime;
        Timer _timer = null;

        void StartEncoding()
        {
            _startTime = DateTime.Now;
            _bytesSent = 0;
            _segmentFrames = 960;
            _encoder = OpusEncoder.Create(48000, 1, FragLabs.Audio.Codecs.Opus.Application.Voip);
            _encoder.Bitrate = 8192;
            _decoder = OpusDecoder.Create(48000, 1);
            _bytesPerSegment = _encoder.FrameByteCount(_segmentFrames);

            _waveIn = new WaveIn(WaveCallbackInfo.FunctionCallback());
            _waveIn.BufferMilliseconds = 50;
            _waveIn.DeviceNumber = comboBox1.SelectedIndex;
            _waveIn.DataAvailable += _waveIn_DataAvailable;
            _waveIn.WaveFormat = new WaveFormat(48000, 16, 1);

            _playBuffer = new BufferedWaveProvider(new WaveFormat(48000, 16, 1));

            _waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());
            _waveOut.DeviceNumber = comboBox2.SelectedIndex;
            _waveOut.Init(_playBuffer);

            _waveOut.Play();
            _waveIn.StartRecording();

            if (_timer == null)
            {
                _timer = new Timer();
                _timer.Interval = 1000;
                _timer.Tick += _timer_Tick;
            }
            _timer.Start();
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            var timeDiff = DateTime.Now - _startTime;
            var bytesPerSecond = _bytesSent / timeDiff.TotalSeconds;
            Console.WriteLine("{0} Bps", bytesPerSecond);
        }

        byte[] _notEncodedBuffer = new byte[0];
        void _waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            byte[] soundBuffer = new byte[e.BytesRecorded + _notEncodedBuffer.Length];
            for (int i = 0; i < _notEncodedBuffer.Length; i++)
                soundBuffer[i] = _notEncodedBuffer[i];
            for (int i = 0; i < e.BytesRecorded; i++)
                soundBuffer[i + _notEncodedBuffer.Length] = e.Buffer[i];

            int byteCap = _bytesPerSegment;
            int segmentCount = (int)Math.Floor((decimal)soundBuffer.Length / byteCap);
            int segmentsEnd = segmentCount * byteCap;
            int notEncodedCount = soundBuffer.Length - segmentsEnd;
            _notEncodedBuffer = new byte[notEncodedCount];
            for (int i = 0; i < notEncodedCount; i++)
            {
                _notEncodedBuffer[i] = soundBuffer[segmentsEnd + i];
            }

            for (int i = 0; i < segmentCount; i++)
            {
                byte[] segment = new byte[byteCap];
                for (int j = 0; j < segment.Length; j++)
                    segment[j] = soundBuffer[(i * byteCap) + j];
                int len;
                byte[] buff = _encoder.Encode(segment, segment.Length, out len);
                _bytesSent += (ulong)len;
                buff = _decoder.Decode(buff, len, out len);
                _playBuffer.AddSamples(buff, 0, len);
            }
        }

        void StopEncoding()
        {
            _timer.Stop();
            _waveIn.StopRecording();
            _waveIn.Dispose();
            _waveIn = null;
            _waveOut.Stop();
            _waveOut.Dispose();
            _waveOut = null;
            _playBuffer = null;
            _encoder.Dispose();
            _encoder = null;
            _decoder.Dispose();
            _decoder = null;

        }



        private void timer1_Tick(object sender, EventArgs e)
        {
            MMDeviceEnumerator de = new MMDeviceEnumerator();
            MMDevice device = de.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
            //float volume = (float)device.AudioMeterInformation.MasterPeakValue * 100;
            float volLeft = (float)device.AudioMeterInformation.PeakValues[0] * 100;
            float volRight = (float)device.AudioMeterInformation.PeakValues[1] * 100;
            progressBar1.Value = (int)volLeft;
            progressBar2.Value = (int)volRight;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {

        }
    }
}

感谢您为帮助我了解如何通过 RTP 流获取数据而做出的任何贡献。

哦,是的,这首先开始于我从一个教程示例中重新创建一个 VU 表 - 因此命名空间名称和额外代码,它确实起作用。

4

1 回答 1

1

该代码示例对音频进行编码而不是解码。您需要将 Buff 中包含的字节发送到网络。

上面示例中的这部分代码是从声卡接收音频。

    byte[] _notEncodedBuffer = new byte[0];
    void _waveIn_DataAvailable(object sender, WaveInEventArgs e)
    {
        byte[] soundBuffer = new byte[e.BytesRecorded + _notEncodedBuffer.Length];
        for (int i = 0; i < _notEncodedBuffer.Length; i++)
            soundBuffer[i] = _notEncodedBuffer[i];
        for (int i = 0; i < e.BytesRecorded; i++)
            soundBuffer[i + _notEncodedBuffer.Length] = e.Buffer[i];

        int byteCap = _bytesPerSegment;
        int segmentCount = (int)Math.Floor((decimal)soundBuffer.Length / byteCap);
        int segmentsEnd = segmentCount * byteCap;
        int notEncodedCount = soundBuffer.Length - segmentsEnd;
        _notEncodedBuffer = new byte[notEncodedCount];
        for (int i = 0; i < notEncodedCount; i++)
        {
            _notEncodedBuffer[i] = soundBuffer[segmentsEnd + i];
        }

        for (int i = 0; i < segmentCount; i++)
        {
            byte[] segment = new byte[byteCap];
            for (int j = 0; j < segment.Length; j++)
                segment[j] = soundBuffer[(i * byteCap) + j];
            int len;
            byte[] buff = _encoder.Encode(segment, segment.Length, out len);
            _bytesSent += (ulong)len;
            buff = _decoder.Decode(buff, len, out len);
            _playBuffer.AddSamples(buff, 0, len);
        }
    }

在这一行

byte[] buff = _encoder.Encode(segment, segment.Length, out len);

正是在这一点上,您创建了您的 RTP 数据包

https://www.rfc-editor.org/rfc/rfc3550

然后用C#发到网络上

通常为 UDP

在 C# 中发送 UDP 数据包

在从 RTP 数据包中提取 Buff 之后,其余代码属于接收应用程序。

buff = _decoder.Decode(buff, len, out len);
            _playBuffer.AddSamples(buff, 0, len);
于 2017-06-14T02:44:46.653 回答