Click here to Skip to main content
Licence CPOL
First Posted 5 Jul 2007
Views 238,173
Downloads 6,994
Bookmarked 175 times

A Voice Chat Application in C#

By Hitesh Sharma | 5 Jul 2007
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.
1 vote, 3.1%
1
3 votes, 9.4%
2
3 votes, 9.4%
3
4 votes, 12.5%
4
21 votes, 65.6%
5
4.17/5 - 32 votes
1 removed
μ 3.89, σa 2.08 [?]

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:

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:

//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:

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)

About the Author

Hitesh Sharma



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionVoice chat application doesnt connect with another machine PinmemberThendral.N2:00 24 Jan '12  
Questiononly one usage of each socket address protocol network address port is normally permitted PinmemberThendral.N0:19 24 Jan '12  
QuestionSend message issue Pinmemberdanm3611:12 12 Jan '12  
Questionhi sir how can I add Microsoft.directx and Microsoft.directx .DirectSound and voicechat.vhost.exe.manifest refferance files tell me plz [modified] Pinmemberbahidab1:17 4 Jan '12  
QuestionQuerry.. PinmemberTarun Khatana9:45 14 Dec '11  
Generalno sound while lost focus PinmemberAkeeq2:39 14 Jun '11  
GeneralRe: no sound while lost focus PinmemberAkeeq21:40 16 Jun '11  
GeneralSound Quality PinmemberOblivion1706:51 7 Jun '11  
Questionhi Pinmembermaktumpashal3:15 22 May '11  
GeneralMDA LoaderLock Error; Error in Application PinmemberAkeeq20:36 13 May '11  
GeneralRe: MDA LoaderLock Error; Error in Application PinmemberAkeeq21:44 16 Jun '11  
GeneralMy vote of 5 PinmemberMember 446500715:29 31 Mar '11  
GeneralHi PinmemberKiya168:33 1 Mar '11  
Generali want to make it over internet Pinmembercrysis8912:28 28 Feb '11  
Generalerror Pinmembersamathu21:11 22 Feb '11  
Generalerror Pinmembersamathu5:19 21 Feb '11  
GeneralSome question PinmemberRezabest3:27 19 Jan '11  
GeneralMy vote of 3 Pinmemberlxxlxx3:09 4 Jan '11  
GeneralMuLawEncoder Vs ALawEncoder What's the difference Pinmemberjibin.mn1:09 30 Oct '10  
Generalerrors regarding the project "A Voice Chat Application in C#". Pinmemberhemendrapsingh10:17 28 Feb '10  
GeneralMy vote of 2 Pinmembersomireddy23:04 25 Feb '10  
GeneralCompact Framework Pinmembersonia108220:20 11 Jan '10  
QuestionError while calling on second time [modified] Pinmemberhiran53622:08 12 Oct '09  
Generalnot working in studio 2008 Pinmembersurpatel783:21 5 Oct '09  
AnswerRe: not working in studio 2008 PinmemberSmacky3114:44 25 May '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120206.1 | Last Updated 5 Jul 2007
Article Copyright 2007 by Hitesh Sharma
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid