问题
I'm developing a voip server-client app using NAUDIO and Sockets.
I've read the naudio's documentation and I have tried alot to get the data from the microphone then send it to the client, the thing that you can get the data, but you have to save it to a byte array first then send it which is almost like sending a file using TCP.
How can I get data from naudio and send it at the same time "Stream it" to the client using UDP protocol.
Thanks in advance.
回答1:
NAudio has a Network Chat Demo within the Examples if you download the source code, which does a good job of showing how to implement a very simple Chat application.
Basically though what you want the client to do is this:
void Initialize()
{
waveIn = new WaveIn();
waveIn.BufferMilliseconds = 50;
waveIn.DeviceNumber = inputDeviceNumber;
waveIn.WaveFormat = codec.RecordFormat;
waveIn.DataAvailable += waveIn_DataAvailable;
waveIn.StartRecording();
...
}
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
//Encode and send e.Buffer
}
With this you get a byte array every 50 ms (or however long you set you buffer to) and send it to the server. You'll need to encode it however, since sending the sound un-encoded will take up too much bandwidth. NAudio has codecs of its own, so that shouldn't be much of a problem. See here for NAudio's network chat demo.
Another thing to consider, if you plan to implement a client to client voip (either via p2p or streamed through the server itself) is a good networking library to handle all the communications. I've used Lidgren on a similar project which worked pretty well. It's open source, but can easily be set up to fit your needs.
回答2:
There is a demo in the NAudioDemo app called "Network Chat", which records from the microphone, compresses the audio with a codec, and sends it out via UDP. It also receives audio from UDP, decompresses it and plays it. So looking at that code should point you in the right direction. What it doesn't show is the use of any protocol on top of UDP, so just the raw compressed audio is being sent over the network with no timestamps or indications of what codec is being used.
来源:https://stackoverflow.com/questions/19723988/how-to-voip-using-naudio