Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C#
Article

A Voice Chat Application in C#

Rate me:
Please Sign up or sign in to vote.
4.38/5 (64 votes)
5 Jul 2007CPOL2 min read 716.3K   43K   248   155
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.

Screenshot - VoiceChat.jpg

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:

C#
enum Command
{
    Invite, //Make a call.
    Bye,    //End a call.
    Busy,   //User busy.
    OK,     //Response to an invite message. OK is sent to 
            //indicate that call is accepted.
    Null,   //No command.
}

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:

C#
//Using UDP sockets
clientSocket = new Socket(AddressFamily.InterNetwork, 
    SocketType.Dgram, 
    ProtocolType.Udp);
EndPoint ourEP = new IPEndPoint(IPAddress.Any, 1450);

//Listen asynchronously on port 1450 for 
//coming messages (Invite, Bye, etc).
clientSocket.Bind(ourEP);

//Receive data from any IP.
EndPoint remoteEP = (EndPoint)(new IPEndPoint(IPAddress.Any, 0));
byteData = new byte[1024];

//Receive data asynchornously.
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:

C#
private void InitializeCall()
{
    try
    {
        //Start listening on port 1500.
        udpClient = new UdpClient(1550);
        Thread senderThread = new Thread(new ThreadStart(Send));
        Thread receiverThread = new Thread(new ThreadStart(Receive));
        bIsCallActive = true;

        //Start the receiver and sender thread.
        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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: How to test it? Pin
Hitesh Sharma18-Feb-08 4:54
Hitesh Sharma18-Feb-08 4:54 
GeneralRe: How to test it? Pin
escrinser11-Mar-08 6:58
escrinser11-Mar-08 6:58 
GeneralRe: How to test it? Pin
Hitesh Sharma13-Mar-08 4:48
Hitesh Sharma13-Mar-08 4:48 
GeneralRe: How to test it? Pin
albin_t@yahoo.com4-Jun-08 6:13
albin_t@yahoo.com4-Jun-08 6:13 
Questionthe references to directx broke Pin
Member 454227215-Feb-08 2:48
Member 454227215-Feb-08 2:48 
GeneralRe: the references to directx broke Pin
Hitesh Sharma15-Feb-08 21:25
Hitesh Sharma15-Feb-08 21:25 
GeneralSource File Corrupted Pin
abady_sniper21-Dec-07 10:55
abady_sniper21-Dec-07 10:55 
GeneralRe: Source File Corrupted Pin
Hitesh Sharma6-Feb-08 22:27
Hitesh Sharma6-Feb-08 22:27 
Generalvery nice Pin
onebitcuongdv9-Sep-07 20:57
onebitcuongdv9-Sep-07 20:57 
GeneralRe: very nice Pin
Hitesh Sharma10-Sep-07 2:33
Hitesh Sharma10-Sep-07 2:33 
GeneralDirectX Pin
tempsh21-Aug-07 23:41
tempsh21-Aug-07 23:41 
GeneralRe: DirectX Pin
Hitesh Sharma22-Aug-07 0:27
Hitesh Sharma22-Aug-07 0:27 
GeneralChat Conference Pin
tempsh21-Aug-07 10:56
tempsh21-Aug-07 10:56 
GeneralRe: Chat Conference Pin
Hitesh Sharma21-Aug-07 11:25
Hitesh Sharma21-Aug-07 11:25 
GeneralRe: Chat Conference Pin
tempsh21-Aug-07 19:32
tempsh21-Aug-07 19:32 
GeneralArticle title misleading Pin
Elias Bachaalany17-Jul-07 5:58
Elias Bachaalany17-Jul-07 5:58 
Hello

First, let me thank you for your efforts and work.

When I saw the article, I thought you're going to talk about how to capture audio, send audio, and discuss some networking issues.

I was disappointed to learn that you only covered the networking part, which is obvious than the audio part.

You could have written the article in a better way.

--
Elias
GeneralRe: Article title misleading Pin
Hitesh Sharma17-Jul-07 7:45
Hitesh Sharma17-Jul-07 7:45 
GeneralRe: Article title misleading Pin
Jason Barrera25-Jul-07 18:57
Jason Barrera25-Jul-07 18:57 
GeneralRe: Article title misleading Pin
Hitesh Sharma21-Aug-07 11:18
Hitesh Sharma21-Aug-07 11:18 
QuestionJust a little tick in audio? Pin
jerommeke8312-Jul-07 3:24
jerommeke8312-Jul-07 3:24 
AnswerRe: Just a little tick in audio? [modified] Pin
Hitesh Sharma13-Jul-07 5:11
Hitesh Sharma13-Jul-07 5:11 
GeneralUDP unreliable Pin
mav.northwind5-Jul-07 9:49
mav.northwind5-Jul-07 9:49 
GeneralRe: UDP unreliable Pin
Hitesh Sharma5-Jul-07 9:55
Hitesh Sharma5-Jul-07 9:55 
GeneralRe: UDP unreliable Pin
Steve Hansen5-Jul-07 22:27
Steve Hansen5-Jul-07 22:27 
GeneralRe: UDP unreliable Pin
OscarTV9-Jul-07 20:57
OscarTV9-Jul-07 20:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.