KB1RCC
KB1RCC

Reputation: 23

Choppy Audio with Naudio and TCP Stream. Buffer Full Exception

I am attempting to stream audio using Naudio over a TCP connection. The problem is the audio sounds choppy. I believe this is because I am getting an exception saying the buffer is full when I try to add samples to the bufferedwaveprovider.

I have tried increasing the buffer size however the result remains unchanged.

-----CLIENT CODE-----

    public TcpClient client;
    public WaveOut waveplayer = new WaveOut();
    public BufferedWaveProvider bwp = new BufferedWaveProvider(new WaveFormat(8000, 16, 1));
    public byte[] buffer = new byte[1024 * 16];

    public Form1()
    {
        bwp.BufferLength = 1024 * 16;
        waveplayer.Init(bwp);
        waveplayer.Play();
    }

    public void audio()
    {
        try
        {
            client = new TcpClient(textBox1.Text.ToString(), 8001);
            NetworkStream ns = client.GetStream();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }

        while (true)
        {
            try
            {
                ns.Read(buffer, 0, buffer.Length);
                bwp.AddSamples(buffer, 0, buffer.Length);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }

-----SERVER CODE------

    public NAudio.Wave.WaveInEvent sourcestream = null;
    public TcpListener listener = new TcpListener(IPAddress.Any, 8001);
    public TcpClient client;
    public NetworkStream ns;

    public Form1()
    {
        InitializeComponent();
        sourcestream = new NAudio.Wave.WaveInEvent();
        sourcestream.DeviceNumber = 0;            
        sourcestream.WaveFormat = new NAudio.Wave.WaveFormat(8000, 16, 1);
        sourcestream.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>(audioDataAvailable);
        sourcestream.StartRecording();
    }

    public void acceptclients()
    {
        listener = new TcpListener(IPAddress.Any, 8001);
        listener.Start();
        client = listener.AcceptTcpClient();
        ns = client.GetStream();
    }

    void audioDataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
    {
        try
        {
            if (client.Connected)
            {
                ns.Write(e.Buffer, 0, e.Buffer.Length);
                ns.Flush();
            }
        }
        catch(Exception ex)
        {            

Here is the exact error I recieve

"System.InvalidOperationException: Buffer full at NAudio.Wave.BufferedWaveProvider.AddSamples(Byte[] buffer, Int32 offset, Int32 count

Upvotes: 2

Views: 1649

Answers (1)

Mark Heath
Mark Heath

Reputation: 49482

If you're getting a buffer full exception, that means audio is arriving faster than you are playing it. You either need a larger buffer size, or to throttle audio before downloading it.

You also should use e.BytesRecorded, not e.Buffer.Length on the server side. That might also account for the issue you are seeing.

Another bug is that you should examine the number of bytes read from ns.Read and use that number when calling bwp.AddSamples

Upvotes: 2

Related Questions