Reputation: 1616
I'm writing a UDP local area network video chat system and have got the video and audio streams working. However I'm experiencing a little latency (about half a second) in the audio and was wondering what codecs would provide the least latency. I'm using NAudio (http://naudio.codeplex.com/) which provides me access to the following codecs for streaming;
I've tried them out and I'm not noticing much difference. Is there any others that I should download and try to reduce latency? I'm only going to be sending voice over the connection but I'm not really worried about quality or background noises too much.
UPDATE
I'm sending the audio in blocks like so;
waveIn = new WaveIn();
waveIn.BufferMilliseconds = 50;
waveIn.DeviceNumber = inputDeviceNumber;
waveIn.WaveFormat = codec.RecordFormat;
waveIn.DataAvailable += waveIn_DataAvailable;
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
if (connected)
{
byte[] encoded = codec.Encode(e.Buffer, 0, e.BytesRecorded);
udpSender.Send(encoded, encoded.Length);
}
}
Upvotes: 2
Views: 1992
Reputation: 26
Your latency issue is probably not coming from the codec you pick but from your audio API choice. WinMM and DirectSound APIs are quite old and as pointed at by Mark Heath, the creator of the NAudio Libray :
WinMM - these are the APIs that have been around for ages (e.g. the waveOut... and waveIn... functions). Their main limitation is poor latency (its hard to go much below 50ms without dropouts).
DirectSound - has its uses, particularly for game development, but it appears Microsoft is phasing this out too.
What's up with WASAPI? - Mark Heath
If you use Window 7 or higher version, I recommand you tu use WASAPI instead. This API is already included in NAudio like WinMM and DirectSound and it will help you to achieve low latency audio transmission.
Also, Asio4All can be a very good solution too. However, in my experience, Asio4All will not fit if you want to redirect specific audio stream to specific output devices on your system programmatically (in your code each devices appears like one and you configure the audio stream repartition later in the Asio4All GUI).
You will find a interesting recap here : NAudio Output Devices - Mark Heath
Upvotes: 1
Reputation: 1616
Oddly enough the latency was appearing on the client side. After trying various combinations of settings I eventually decided to give DirectSoundOut
ago instead of WaveOut
. I was shocked to find that the latency shot way down. Guess I'll be using that in the future.
dso= new DirectSoundOut(); //Direct Sound Removed nearly all latency
//WaveOut waveOut = new WaveOut();
waveProvider = new BufferedWaveProvider(codec.RecordFormat);
//WaveOut.Init(waveProvider);
//WaveOut.Play();
dso.Init(waveProvider);
dso.Play();
Upvotes: 0