NAudio + Network Audio + many clients

Feb 16, 2011 at 5:09 AM

Hello!
I apologize for my English, I write through the translator (Google), I'm Russian, and English is bad I know=(
I am writing an application to capture sound from the microphone and sending across the network - audio conference, I need to have a few people (about 20) can communicate simultaneously with each other.

Use this code here:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net.Sockets;
using System.Net;
using System.Threading;
using NAudio.Wave;
using NAudio.CoreAudioApi;
using System.IO;

namespace naudio_udp
{
  public partial class Form1 : Form
  {
    private Thread mListenThread;
    private IPEndPoint sIpEnd;
    private Socket server;
    private WaveIn wavein;
    private WaveOut waveout;
    private static BufferedWaveProvider wavProv;

    public Form1()
    {
      InitializeComponent();

      wavein = new WaveIn();
      wavein.WaveFormat = new WaveFormat(8000, 16, 1);

      wavein.DataAvailable += new EventHandler<WaveInEventArgs>(Recorded);
      waveout = new WaveOut();
      wavProv = new BufferedWaveProvider(new WaveFormat(8000, 16, 1));

      waveout.Init(wavProv);

      mListenThread = new Thread(new ParameterizedThreadStart(startListen));
      mListenThread.Start();
      server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

      wavein.StartRecording();
    }

    private void Recorded(object sender, WaveInEventArgs e)
    {
      try
      {
        IPEndPoint cIpEnd = new IPEndPoint(IPAddress.Parse("192.168.2.255"), 12131);
        server.SendTo(e.Buffer, cIpEnd);
      }
      catch { }
    }

    private void startListen(object sender)
    {
      IPEndPoint sIpEnd = new IPEndPoint(IPAddress.Any, 12131);
      Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
      sock.Bind(sIpEnd);

      IPEndPoint remoteIpEnd = new IPEndPoint(IPAddress.Any, 0);
      EndPoint Remote = (EndPoint)remoteIpEnd;

      waveout.Play();
      int offset = 0;
      while (true)
      {
        byte[] data = new byte[65535];
        int recv = sock.ReceiveFrom(data, ref Remote);
        wavProv.AddSamples(data, offset, recv);
      }
    }
  }
}

If two clients on the network all works fine, but if it connects the third, then the problem starts, one client called a mistake, another one could hear a long delay (about 10-15 seconds). Trying to do for each client a separate thread, with its WaveOut, but in this case there is no sound, buffer overflows, and looked to debug queue buffer is filled, but the reading of it is not happening.

private void startListen(object sender)
{
  IPEndPoint sIpEnd = new IPEndPoint(IPAddress.Any, 12131);
  Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  sock.Bind(sIpEnd);

  IPEndPoint remoteIpEnd = new IPEndPoint(IPAddress.Any, 0);
  EndPoint Remote = (EndPoint)remoteIpEnd;

  waveout = new WaveOut();
  wavProv = new BufferedWaveProvider(new WaveFormat(8000, 16, 1));
  waveout.Init(wavProv);
  waveout.Play();
  
  int offset = 0;
  while (true)
  {
    byte[] data = new byte[65535];
    int recv = sock.ReceiveFrom(data, ref Remote);
    wavProv.AddSamples(data, offset, recv);
  }
}

like this does not work

Tell me what could be the problem and how to make that many customers could communicate simultaneously.

Coordinator
Feb 21, 2011 at 9:37 AM

what code are you using to send the data across the network?

Feb 21, 2011 at 12:37 PM

private void Recorded(object sender, WaveInEventArgs e)
    {
      try
      {
        IPEndPoint cIpEnd = new IPEndPoint(IPAddress.Parse("192.168.2.255"), 12131);
        server.SendTo(e.Buffer, cIpEnd);
      }
      catch { }
    }

private void startListen(object sender)
{
  IPEndPoint sIpEnd = new IPEndPoint(IPAddress.Any, 12131);
  Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  sock.Bind(sIpEnd);

  IPEndPoint remoteIpEnd = new IPEndPoint(IPAddress.Any, 0);
  EndPoint Remote = (EndPoint)remoteIpEnd;

  waveout = new WaveOut();
  wavProv = new BufferedWaveProvider(new WaveFormat(8000, 16, 1));
  waveout.Init(wavProv);
  waveout.Play();
  
  int offset = 0;
  while (true)
  {
    byte[] data = new byte[65535];
    int recv = sock.ReceiveFrom(data, ref Remote);
    wavProv.AddSamples(data, offset, recv);
  }
}