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:
- static void Main(string[] args)
- {
- displayName = "CommandLineCaller";
- domainServerPort = 5060;
- registerPassword = string.Empty;
- registrationRequired = false;
- localAddress = SoftPhoneFactory.GetLocalIP().ToString();
- mp3FilePath = "..\\..\\Resources\\test.mp3";
-
- . . .
-
Parsing the command line
arguments:
- if(!ParseArgs(args))
- {
- PrintHelp();
- Console.ReadLine();
- return;
- }
(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.
- {
- mp3StreamPlayback = new MP3StreamPlayback(mp3FilePath);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- Console.ReadLine();
- return;
- }
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:
- phoneCallMediaSender = new PhoneCallMediaSender();
- mediaConnector = new MediaConnector();
- mediaConnector.Connect(mp3StreamPlayback, phoneCallMediaSender);
A softPhone and a phoneLine are
created based on the predefined values and the command line arguments:
- softPhone = SoftPhoneFactory.CreateSoftPhone(localAddress, 5000, 5100, 5060);
- phoneLine = softPhone.CreatePhoneLine(new SIPAccount(registrationRequired, displayName, userName, registerName,
- 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:
- phoneLine.PhoneLineStateChanged += phoneLine_PhoneLineStateChanged;
- softPhone.RegisterPhoneLine(phoneLine);
If no registration is needed or
the phone line is successfully registered, the CreateCall() method is
invoked:
- static void phoneLine_PhoneLineStateChanged(object sender, VoIPEventArgs e)
- {
- Console.WriteLine("Phone line state has changed to {0}.", e.Item);
- if (e.Item == PhoneLineState.NoRegNeeded || e.Item == PhoneLineState.RegistrationSucceded)
- CreateCall();
- }
Finally, the calling:
- static void CreateCall()
- {
- if (phoneCall != null)
- return;
- Console.WriteLine("Creating new call ...");
- phoneCall = softPhone.CreateCallObject(phoneLine, dialNumber);
- phoneCall.CallErrorOccured += phoneCall_CallErrorOccured;
- phoneCall.CallStateChanged += phoneCall_CallStateChanged;
- phoneCallMediaSender.AttachToCall(phoneCall);
- phoneCall.Start();
- }
If the callstate is InCall,
the MP3 file is played for the called party by invoking the mp3StreamPlayback.StartStreaming()
method.
- static void phoneCall_CallStateChanged(object sender, VoIPEventArgs e)
- {
- Console.WriteLine("Call state has changed to {0}", e.Item);
- if (e.Item == CallState.InCall)
- mp3StreamPlayback.StartStreaming();
- }
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:
http://www.voip-sip-sdk.com/p_251-how-to-use-the-command-line-caller-example-with-ozeki-voip-sip-sdk-voip.html