.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

A Command Line Caller example

Posted By:gemma85       Posted Date: November 14, 2011    Points: 200    Category: .Net Framework    URL: http://www.dotnetspark.com  

An article that contains an exciting coding sample to make a command line caller example.

A Command Line Caller example

I've first started to study command line calling more seriously, when I chose it to be the subject of my PhD dissertation. I go to a computer uni in Sweden, and I always find VoIP-related lessons the most interesting. Then Command Line Calling came into my sight.

In this short article (I have to finish my paper, too) I try to present some bits of the basic programming knowledge about the subject. My aim is to help those who share this interest with me (and maybe also work on a dissertation). In the meantime, I don't want this to be a very insider article, so I will also try to sum up some general knowledge about VoIP. Let's start with this.

VoIP is short for "Voice over Internet Protocol". As the name says, it breaks up with the traditional phoning system (or "Plain Old Telephone System" - shortly: POTS), and uses Internet connection for telephoning instead. This can have many advantages from personal and from business aspects, too. But it is not this article's duty to gather all these things. Let's move along on the technical line.

To communicate through VoIP, you need to use a service. There are a lot of VoIP service providers out there. You have to choose the one (or more) that fits the best to your communication needs and wants. But how is this thing working exactly? How can your voice go from one system to another?

The essence of VoIP technology is to use an IP network, through an ADSL or other Internet connection, to make or receive phone calls to/from POTS landline networks. This means, you can even talk to people with a landline phone number through your computer.

VoIP solutions are for free in most cases (mainly between two computers, but frequently between a computer and a landline or mobile phone, too). But even if it's not (depending on the service provider), it's still cheaper than the traditional way. Studies have shown that, compared to using a POTS line, using VoIP can potentially make you save up to 40% on local calls, and up to 90% on international calls.

The easiest way of developing your own VoIP application is using an SDK made for this purpose. I have found my SDK after a long-long search, but you need to do this, if you want the right thing. Do your own research to find the perfect match for your requirements. I needed an SDK for studying closely the source code of the Command Line Caller.

The Command Line Caller of the described SDK is a sample program written in C#. Phone calls can be placed right from this program without the help of any rich interface. The calls are made through the command prompt where you need to provide the argument of the exe file. The argument of the exe file is the phone number that is to be called. This Command Line Caller does not use the local microphone for sending audio data via the RTP packets. This program uses an mp3 file and plays this file for the called party. That's what I've been looking for.

Now, we have finally arrived to the most important part: coding. I would like to demonstrate the greatness of command line calling through a coding example. This code shows how you attach the MP3 playback to your phone call.

Initializing the default values:

  1. static void Main(string[] args)  
  2.       {  
  3.     displayName = "CommandLineCaller";  
  4.     domainServerPort = 5060;  
  5.     registerPassword = string.Empty;  
  6.     registrationRequired = false;  
  7.     localAddress = SoftPhoneFactory.GetLocalIP().ToString();  
  8.     mp3FilePath = "..\\..\\Resources\\test.mp3";  
  10.             . . .  

Parsing the command line arguments:

  1. if(!ParseArgs(args))  
  2. {  
  3.     PrintHelp();  
  4.     Console.ReadLine();  
  5.     return;  
  6. }  

(the ParseArgs() method returns true if the arguments are valid, and returns false if arguments are missing or they are misconfigured)

Opening the .mp3 file.

  1.       {  
  2.           mp3StreamPlayback = new MP3StreamPlayback(mp3FilePath);  
  3.       }  
  4.       catch (Exception ex)  
  5.       {  
  6.           Console.WriteLine(ex.Message);  
  7.           Console.ReadLine();  
  8.           return;  
  9.       }  

As a result of this, a new instance of the phoneCallMediaSender and the MediaConnector class is created and the mp3StreamPlayback is connected to the phoneCallMediaSender via the Connect() method. Looks like this:

  1. phoneCallMediaSender = new PhoneCallMediaSender();  
  2. mediaConnector = new MediaConnector();  
  3. mediaConnector.Connect(mp3StreamPlayback, phoneCallMediaSender);  

A softPhone and a phoneLine are created based on the predefined values and the command line arguments:

  1. softPhone = SoftPhoneFactory.CreateSoftPhone(localAddress, 5000, 5100, 5060);  
  2. phoneLine = softPhone.CreatePhoneLine(new SIPAccount(registrationRequired, displayName, userName, registerName,  
  3.                                                          registerPassword, domainServerHost, domainServerPort));  

(These lines create a new event handler for the event when the state of the phone line is changed. This event handler is the phoneLine_PhoneLineStateChanged.)

 The phone line is being registered:

  1. phoneLine.PhoneLineStateChanged += phoneLine_PhoneLineStateChanged;  
  2. softPhone.RegisterPhoneLine(phoneLine);  

If no registration is needed or the phone line is successfully registered, the CreateCall() method is invoked:

  1. static void phoneLine_PhoneLineStateChanged(object sender, VoIPEventArgs e)  
  2. {  
  3.     Console.WriteLine("Phone line state has changed to {0}.", e.Item);  
  4.     if (e.Item == PhoneLineState.NoRegNeeded || e.Item == PhoneLineState.RegistrationSucceded)  
  5.         CreateCall();  
  6. }  

Finally, the calling:

  1. static void CreateCall()  
  2. {  
  3.     if (phoneCall != null)  
  4.         return;  
  5.     Console.WriteLine("Creating new call ...");  
  6.     phoneCall = softPhone.CreateCallObject(phoneLine, dialNumber);  
  7.     phoneCall.CallErrorOccured += phoneCall_CallErrorOccured;  
  8.     phoneCall.CallStateChanged += phoneCall_CallStateChanged;  
  9.     phoneCallMediaSender.AttachToCall(phoneCall);  
  10.     phoneCall.Start();  
  11. }  

If the callstate is InCall, the MP3 file is played for the called party by invoking the mp3StreamPlayback.StartStreaming() method.

  1. static void phoneCall_CallStateChanged(object sender, VoIPEventArgs e)  
  2. {  
  3.     Console.WriteLine("Call state has changed to {0}", e.Item);  
  4.     if (e.Item == CallState.InCall)  
  5.         mp3StreamPlayback.StartStreaming();  
  6. }  

See? It's not that complicated (for someone who's familiar with programming). I hope you've found my article interesting, and that more and more people will dive in the beauty of VoIP technology.

If you would like to learn more, I suggest you to check out this website, which I also used as the source of this article:


 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply

    Read also another Resources from the same Author

Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend