Docs/NetworkChatDemo.md
The Network Chat panel in NAudioDemo is a small two-way voice-chat sample. It captures
your microphone, encodes it with a codec of your choice, sends it over the network, and plays
back whatever it receives from a peer. It is a good starting point if you want to build any kind
of live audio streaming on top of NAudio.
This page explains how to run it (on one machine or across two) and how the pieces fit together.
Launch NAudioDemo, choose Network Chat from the demo list, then fill in:
| Field | Meaning |
|---|---|
| Remote host | The machine to send your audio to – an IP address (192.168.1.50) or a host name. |
| Remote port | The UDP port the remote machine is listening on. |
| Listen port | The port this instance listens on for incoming audio. |
| Input device | The microphone to capture from. |
| Codec | How the audio is compressed before sending (see below). |
Press Start Streaming to begin and Stop to end. Audio is sent over UDP (see Why UDP below).
Run the demo on both PCs. On each one, set Remote host to the other machine and use the same port for remote and listen on both sides:
192.168.1.10): Remote host 192.168.1.20, Remote port 7080, Listen port 7080192.168.1.20): Remote host 192.168.1.10, Remote port 7080, Listen port 7080Make sure any firewall allows the chosen UDP port. Because there is no connection to establish, the two ends can be started in any order.
You can experiment with just one PC by running two copies of the demo and swapping the ports so each instance listens on the port the other sends to:
127.0.0.1, Remote port 7081, Listen port 7080127.0.0.1, Remote port 7080, Listen port 7081(If you point Remote host straight back at yourself with matching ports, you simply hear your own voice echoed back – handy for a quick loopback test.)
Note: because this is full-duplex and plays out of your speakers, using it on one machine with a live microphone can produce acoustic feedback. Use headphones, or a virtual input.
The demo ships several codecs, discovered automatically by reflection (each implements
INetworkChatCodec). They fall into two groups:
AcmStream and
built-in codec support. The ACM-based ones are Windows-only and are hidden automatically if the
codec is not installed (INetworkChatCodec.IsAvailable).For anything new, prefer Opus.
The demo separates capture/playback from transport so you can reuse either piece:
microphone ─▶ WasapiRecorder ─▶ codec.Encode ─▶ IAudioSender ──network──▶ IAudioReceiver ─▶ codec.Decode ─▶ BufferedWaveProvider ─▶ WasapiPlayer ─▶ speakers
NetworkAudioSender captures with WasapiRecorder (NAudio 3's
recommended capture device), encodes each ~50 ms buffer and hands the bytes to an IAudioSender.
WASAPI shared mode converts the device mix format to the codec's record format, so any codec
sample rate works without manual resampling.NetworkAudioPlayer receives encoded bytes from an IAudioReceiver, decodes them, and feeds
a BufferedWaveProvider (acting as a small jitter buffer) that a WasapiPlayer
plays. The buffer is capped at ~500 ms with DiscardOnBufferOverflow = true, so latency stays
bounded if packets arrive in bursts.IAudioSender / IAudioReceiver (a seam you can swap out):
UdpAudioSender / UdpAudioReceiver send one datagram per encoded chunk.The receiver binds to IPAddress.Any, so audio from other machines is received – not just
loopback traffic. (An earlier version of this demo bound to IPAddress.Loopback, which is why it
only ever worked between two instances on the same PC.)
This demo streams over UDP only, because UDP is the right transport for real-time audio:
TCP, by contrast, guarantees delivery and ordering – which sounds appealing but is the wrong trade-off here. A single lost segment causes head-of-line blocking: every packet behind it waits for a retransmit, adding latency that keeps growing and never recovers. That is why real voice/video systems build on UDP (RTP, WebRTC, SRT, QUIC) rather than raw TCP. If you genuinely need to traverse a network that blocks UDP, reach for one of those protocols rather than streaming audio down a TCP socket.
To stream audio in your own app, the smallest version of this is:
WasapiRecorder (via WasapiRecorderBuilder) and subscribe to DataAvailable.UdpClient.BufferedWaveProvider with
DiscardOnBufferOverflow = true, played by a WasapiPlayer.The NetworkChatDemo source under samples/NAudioDemo/NetworkChatDemo is a complete, working
reference for each of those steps.