Introduction
In this article, I will discuss a simple voice chat application. We will grab the audio from the microphone using DirectSound and transmit it in UDP packets. To make things interesting, I used G711 vocoder to compress the data before transmission. I won't discuss here how to capture the audio from microphone and play it back using DirectSound or how G711 is implemented. For all these things you can look into the references given at the end of this article. Before we go further, let's discuss the architecture of the application. We will use the following call messages between the parties.
| Invite |
| --------------------------------> |
| OK |
| <-------------------------------- |
| |
| --------------------------------> |
| Audio flow |
| <-------------------------------- |
| Bye |
| --------------------------------> |
A B
Using the code
Here are the commands for interaction between the two parties:
enum Command
{
Invite,
Bye,
Busy,
OK,
Null,
}
When the user wants to make a call, we send an Invite message and wait for an OK response. When we receive an OK, we start receiving/sending audio captured from the microphone. If the remote party rejects the call then a Busy response is sent. To drop a call, we simply send a Bye message. The application will asynchronously receive/send call messages on port 1450 and synchronously receive/send audio data on port 1550. In other words, the application listens on two ports: one for call messages and the other for audio data. I will now walk you through some code. When the application starts, we start listening for call messages on port 1450:
clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
EndPoint ourEP = new IPEndPoint(IPAddress.Any, 1450);
clientSocket.Bind(ourEP);
EndPoint remoteEP = (EndPoint)(new IPEndPoint(IPAddress.Any, 0));
byteData = new byte[1024];
clientSocket.BeginReceiveFrom(byteData,
0, byteData.Length,
SocketFlags.None,
ref remoteEP,
new AsyncCallback(OnReceive),
null);
When we receive a message, we process it to see what type of message it is and act accordingly. Please see the OnReceive handler for it in the attached project files. To receive/send audio from a microphone, we start two threads so that the synchronous send/receive doesn't block our UI. This is done as follows, noting that the audio is received/sent on port 1550:
private void InitializeCall()
{
try
{
udpClient = new UdpClient(1550);
Thread senderThread = new Thread(new ThreadStart(Send));
Thread receiverThread = new Thread(new ThreadStart(Receive));
bIsCallActive = true;
receiverThread.Start();
senderThread.Start();
btnCall.Enabled = false;
btnEndCall.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
"VoiceChat-InitializeCall ()", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
The Send and Receive methods can be seen in the attached project; understanding them is easy.
References
History
- 5 July, 2007 -- Original version posted