Click here to Skip to main content
13,556,387 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 16 Nov 2011
Licenced CPOL

A Command Line Caller Example

, 16 Nov 2011
Rate this:
Please Sign up or sign in to vote.
A Command Line Caller example


I 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 university 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<phonelinestate> 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.	}  </phonelinestate>

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<callstate> e)
2.	{
3.	    Console.WriteLine("Call state has changed to {0}", e.Item);
4.	    if (e.Item == CallState.InCall)
5.	        mp3StreamPlayback.StartStreaming();
6.	}  </callstate>

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 check out this website, which I also used as the source of this article:


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


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionNo interesting! Pin
WuRunZhe24-Jun-14 1:19
memberWuRunZhe24-Jun-14 1:19 
GeneralMy vote of 4 Pin
Mic6-Aug-13 14:36
memberMic6-Aug-13 14:36 
Suggestion[My vote of 2] C++ or C# Pin
osy16-Nov-11 23:28
memberosy16-Nov-11 23:28 
GeneralRe: [My vote of 2] C++ or C# Pin
Nishant Sivakumar17-Nov-11 5:31
mvpNishant Sivakumar17-Nov-11 5:31 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180515.1 | Last Updated 16 Nov 2011
Article Copyright 2011 by gemma85
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid