Click here to Skip to main content
15,884,472 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 717.1K   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

 
BugProblem! Pin
Júnior Pacheco17-Apr-12 3:59
professionalJúnior Pacheco17-Apr-12 3:59 
Questiononly one usage of each socket address (protocol/network address/port) is normally permitted Pin
Member 84226775-Apr-12 1:34
Member 84226775-Apr-12 1:34 
Questionis it possible to encrypt a voice in your application? Pin
Thendral.N27-Mar-12 1:02
Thendral.N27-Mar-12 1:02 
QuestionVoice chat application doesnt connect with another machine Pin
Thendral.N24-Jan-12 1:00
Thendral.N24-Jan-12 1:00 
Questiononly one usage of each socket address protocol network address port is normally permitted Pin
Thendral.N23-Jan-12 23:19
Thendral.N23-Jan-12 23:19 
QuestionSend message issue Pin
danm3612-Jan-12 10:12
danm3612-Jan-12 10:12 
Questionhi sir how can I add Microsoft.directx and Microsoft.directx .DirectSound and voicechat.vhost.exe.manifest refferance files tell me plz Pin
bahidab4-Jan-12 0:17
bahidab4-Jan-12 0:17 
QuestionQuerry.. Pin
Tarun Khatana14-Dec-11 8:45
Tarun Khatana14-Dec-11 8:45 
where the message will be displayed on the remote computer

and where it will as ask for ok reply ......
Generalno sound while lost focus Pin
Akeeq14-Jun-11 1:39
Akeeq14-Jun-11 1:39 
GeneralRe: no sound while lost focus Pin
Akeeq16-Jun-11 20:40
Akeeq16-Jun-11 20:40 
GeneralRe: no sound while lost focus Pin
harshad3654-Dec-12 17:43
harshad3654-Dec-12 17:43 
GeneralRe: no sound while lost focus Pin
Akeeq11-Dec-12 0:52
Akeeq11-Dec-12 0:52 
GeneralSound Quality Pin
Oblivion1707-Jun-11 5:51
Oblivion1707-Jun-11 5:51 
GeneralRe: Sound Quality Pin
bubifengyun7-Jan-13 16:36
bubifengyun7-Jan-13 16:36 
Questionhi Pin
maktumpashal22-May-11 2:15
maktumpashal22-May-11 2:15 
GeneralMDA LoaderLock Error; Error in Application Pin
Akeeq13-May-11 19:36
Akeeq13-May-11 19:36 
GeneralRe: MDA LoaderLock Error; Error in Application Pin
Akeeq16-Jun-11 20:44
Akeeq16-Jun-11 20:44 
GeneralMy vote of 5 Pin
Member 446500731-Mar-11 14:29
Member 446500731-Mar-11 14:29 
GeneralHi Pin
Kiya161-Mar-11 7:33
Kiya161-Mar-11 7:33 
Generali want to make it over internet Pin
crysis8928-Feb-11 11:28
crysis8928-Feb-11 11:28 
Generalerror Pin
samathu22-Feb-11 20:11
samathu22-Feb-11 20:11 
Generalerror Pin
samathu21-Feb-11 4:19
samathu21-Feb-11 4:19 
GeneralSome question Pin
Mohammad Reza Valadkhani19-Jan-11 2:27
professionalMohammad Reza Valadkhani19-Jan-11 2:27 
GeneralMy vote of 3 Pin
lxxlxx4-Jan-11 2:09
lxxlxx4-Jan-11 2:09 
GeneralMuLawEncoder Vs ALawEncoder What's the difference Pin
jibin.mn30-Oct-10 0:09
jibin.mn30-Oct-10 0:09 

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.