High performance VoIP SDK for .Net developers

VoIP SIP SDK

How to make a video call (and play an avi)

Explanation

Prerequisities

Download: Download:14_VideoPhone.zip

This article is a detailed guide about making video calls and playing .avi files in relation with Ozeki VoIP SIP SDK. After reading through this page you will be fully familiar with all the essential terms concerning video calls and what you will need for creating your own solution using Ozeki VoIP SIP SDK.

Introduction

VoIP communication has extended functions that are not usually part of the ordinary telephoning system. One of these is the video phoning function that can be implemented in a VoIP application (Figure 1).

Video phoning uses the basis of the voice communication and extends it with some extra codec support and display methods. In case of a video communication not only audio but video data is also sent in a digitized and segmented form through the established phone line between the two VoIP clients. If both clients support video phoning, the users can talk to each other like they were in the same room.


Figure 1 - Making video calls

The following program code uses the background support of Ozeki VoIP SIP SDK, therefore you will need to download and install the SDK on your computer before starting to use the program code. You will also need to have Visual Studio 2010 or compatible IDE and .NET Framework installed on your system, as the program code below is written in C# language.

The Basic Changes for Video Support

The MediaHandler classes contain new media handlers for supporting video handling. As for the audio, the video support also has receiver and sender objects (Code 1).

PhoneCallVideoSender videoSender;
PhoneCallVideoReceiver videoReceiver;

Code 1 - Basic MediaHandlers for the video support

The basic peripheral device for the video is, of course, the web cam that also has its own model class in Ozeki VoIP SIP SDK. Code 2 shows the definition of a WebCamera object.

public WebCamera WebCamera { get; private set; }

Code 2 - Defining a WebCamera object

Ther are two other MediaHandler objects that are needed for the video communication. These are ImageProviders that are needed for the video displays' proper work (Code 3).

public ImageProvider<Image> LocalImageProvider { get; private set; }
public ImageProvider<Image> RemoteImageProvider { get; private set; }

Code 3 - The definition of the ImageProvider objects

The Graphical User Interface also needs some special elements that are defined in Ozeki VoIP SIP SDK. The SDK provides a subclass of System.Windows.Forms.Control named VideoViewerWF. This class is in the Ozeki.Media.Video.Controls namespace and it is used for displaying the video data.

The display objects are needed to be put on simple panels on the GUI. You will need two displays - one for the local and another for the remote video. Code 4 shows the definition of these GUI elements.

private Ozeki.Media.Video.Controls.VideoViewerWF remoteVideoViewer;
private System.Windows.Forms.Panel pRemoteVideo;
private System.Windows.Forms.Panel pLocalVideo;
private Ozeki.Media.Video.Controls.VideoViewerWF localVideoViewer;

Code 4 - The definition of the video displays

After defining the basic tools, you need to know how you can use them for making video calls. It is really similar to the voice calls.

System logic for the video calls

The sample program has two basic classes the MediaHandlers and the PhoneMain. The MediaHandlers class collects all the functionalities that are connected to the media handler objects.

Code 5 shows the initialization of the media handler objects that are used in the video telephone application. They are written in the constructor of the MediaHandlers class.

The code separates the audio and the video handles in order to have all these collected and be better to understand.

connector = new MediaConnector();

// audio
microphone = Microphone.GetDefaultDevice();
if (microphone == null)
throw new Exception("No microphone was found.");

speaker = Speaker.GetDefaultDevice();
if (speaker == null)
throw new Exception("No speaker was found.");

audioSender = new PhoneCallAudioSender();
audioReceiver = new PhoneCallAudioReceiver();

connector.Connect(microphone, audioSender);
connector.Connect(audioReceiver, speaker);

// video
WebCamera = WebCamera.GetDefaultDevice();
if (WebCamera == null)
throw new Exception("No camera was found.");

videoSender = new PhoneCallVideoSender();
videoReceiver = new PhoneCallVideoReceiver();
LocalImageProvider = new DrawingImageProvider();
RemoteImageProvider = new DrawingImageProvider();

connector.Connect(WebCamera, videoSender);
connector.Connect(videoReceiver, RemoteImageProvider);

Code 5 - The initialization of the media handler objects

The MediaHandlers class contains the basic methods for these handlers, like start, stop, attach to call or detach. These methods are collections of method calls that collect the given functionalities of the handlers into one method call. Code 6 shows the AttachToCall method. The other methods are similar to this one, they are just different in the exact method calls.

public void AttachToCall(IPhoneCall call)
{
    audioSender.AttachToCall(call);
    audioReceiver.AttachToCall(call);
    videoSender.AttachToCall(call);
    videoReceiver.AttachToCall(call);
}

Code 6 - The method that makes all the media handlers attach to the call object

When you export the media handler functions into a separate class, the exact softphone code will be a bit more sophisticated. The methods are basically the same as in the case of a voice call softphone, with some small changes, mostly additions.

Code 7 shows a part of the call_CallStateChanged event handler method. This is the code segment that runs when a call is in established (InCall) state.

case CallState.InCall:
    mediaHandlers.AttachToCall(call);
    mediaHandlers.Start();
    localVideoViewer.Start();
    remoteVideoViewer.Start();
    break;

Code 7 - The code segment for the InCall call state

The softphone initialization is basically the same as in case of a voice call softphone. In this case there are two extra lines for the video display settings (Code 8).

softPhone = SoftPhoneFactory.CreateSoftPhone(SoftPhoneFactory.GetLocalIP(), 5700, 5750, 5700);
softPhone.IncomingCall += new EventHandler<VoIPEventArgs<IPhoneCall>>(softPhone_IncomingCall);
phoneLine = softPhone.CreatePhoneLine(new SIPAccount(true, "850", "850", "850", "850", "192.168.112.101", 5060));
phoneLine.PhoneLineStateChanged += new EventHandler<VoIPEventArgs<PhoneLineState>>(phoneLine_PhoneLineInformation);

softPhone.RegisterPhoneLine(phoneLine);

mediaHandlers = new MediaHandlers();
localVideoViewer.SetImageProvider(mediaHandlers.LocalImageProvider);
remoteVideoViewer.SetImageProvider(mediaHandlers.RemoteImageProvider);

Code 8 - The softphone initialization

Note that the PBX information like the SIP account and the IP address is set for example purposes. You need to add your own PBX registration data in the CreatePhoneLine method call in order to have your softphone work properly.

The GUI elements are needed to have the proper event handlers that are similar to the ones for a simple voice softphone. In this case only the video related button and the checkbox needs special treatment as they are missing from the other softphones.

Code 9 shows the handler for the checkbox that indicates if the local video is needed to be displayed.

private void chkbLocalVideo_CheckedChanged(object sender, EventArgs e)
{
    if (chkbLocalVideo.Checked)
    {
        mediaHandlers.EnableVideoLocalImage();
        localVideoViewer.Start();
    }
    else
    {
        mediaHandlers.DisableVideoLocalImage();
        localVideoViewer.Stop();
    }
}

Code 9 - The event handler method for the video display checkbox

The call accept and start are done similar to the original softphones. In this case you can use the CallType class to identify if the call is actually a video or an audio call. It is possible to change the CallType during the call too.

Specifying a CallType means that you can set a single method for picking up a call that can be called from the event handlers of the PickUpAudio and PickUPVideo buttons with the proper parameter (Code 10).

An incoming call can be accepted with a specified CallType parameter, so you do not need to bother with the actual call type. You can also modify the call type in case of an established call with the call.ModifyCallType(callType); method call.

 private void PickUp(CallType callType)
{
    if (inComingCall)
    {
        inComingCall = false;
        call.Accept(callType);
        return;
    }

    if (call != null)
    {
        call.ModifyCallType(callType);
        return;
    }

    if (string.IsNullOrEmpty(labelDialingNumber.Text))
        return;

    if (phoneLineInformation != PhoneLineState.RegistrationSucceeded && phoneLineInformation != PhoneLineState.NoRegNeeded)
    {
        MessageBox.Show("Phone line state is not valid!");
        return;
    }

    call = softPhone.CreateCallObject(phoneLine, labelDialingNumber.Text, callType);
    WireUpCallEvents();
    call.Start();
}

Code 10 - The PickUp method that is parameterized with the call type

This sample program showed the basic concepts of a video softphone that can be created by using Ozeki VoIP SIP SDK. This program can be modified for your own purposes but it is a good start to understand the concept of video phoning.

If you have any question or need assistance, please contact us at info@voip-sip-sdk.com

You can select a suitable Ozeki VoIP SIP SDK license for your project on Pricing and licensing information page

Related Pages

Operating system: Windows 8, Windows 7, Vista, 200x, XP
Development environment: Visual Studio 2010 (Recommended), Visual Studio 2008, Visual Studio 2005
Programming language: C#.NET
Supported .NET framework: .NET Framework 4.5, .NET Framework 4.0, .NET Framework 3.5 SP1
Software development kit: OZEKI VoIP SIP SDK (Download)
VoIP connection: 1 SIP account
System memory: 512 MB+
Free disk space: 100 MB+