Tel.: 00 36 52 532 731

How to send audio data from microphone into a SIP voice call


This example demonstrates how to get microphone using c#, how to connect media handlers and attach microphone to call, how to send microphone audio stream into a call in c#, using Ozeki VoIP SIP SDK.
To fully understand this example, you might have to study the How to register to a SIP PBX chapter and the How to ring a SIP extension chapter first.
To use this example, you need to have Ozeki VoIP SIP SDK installed, and a reference to ozeki voipsipsdk.dll should be added to your visual studio project.

Figure 1 - when the call has been established, the softphone is able to send voice stream into the call

What is microphone used for during a SIP voice call? How does it work?

A microphone is an acoustic-to-electric transducer or sensor that converts sound in air into an electrical signal. Microphones are used in many applications such as telephones, hearing aids, live and recorded audio engineering, voice recording, speech recognition, VoIP and much more.

Softphones are using microphones as audio sender devices for the purpose to capture microphone input and send the microphone audio stream to the other party (exspecially, to the other party's receiver device). To be able to do this, an analog-to-digital converter device converts the continuous physical quantity to digital numbers, that represent the quantity's amplitude, and can be processed by the c# softphone.

Pulse-code modulation (PCM) is a method, used to digitally represent sampled analog signals. In this example, PCM means the uncompressed form of digital audio data. This example is using the G.711 waveform codec, which is an ITU-T standard for audio companding, and it is primarily used in telephony and to communicate over IP networks. It is a lossless, encoded data (to about 50%), which has got 64 kbit/s transfer rate.

The Real-time Transport Protocol (RTP) defines a standardized packet format for delivering audio and video over IP networks. RTP is used extensively in communication and entertainment systems that involve streaming media, such as telephony, video teleconference applications, television services and web-based push-to-talk features.
RTP is designed for end-to-end, real-time transfer of stream data, and is able to transfer the data to multiple destinations. The RTP package construction:

You can learn more about RTP from the How to work with RTP in VoIP SIP calls article.

How to access microphone and send audio stream using c#?

Ozeki VoIP SIP SDK provides c# Microphone class for the purpose to create microphone object, which is able to handle the microphone amplitude, microphone input level, microphone volume etc. With this c# virtual microphone, users are able to access the microphone and get the microphone stream, can control microphone values, and also able to attach it to a call with the correct media sender object, to send the captured microphone input to the other party.

Connect microphone to call example in C#

using System;
using System.Threading;
using Ozeki.Media;
using Ozeki.Media.MediaHandlers;
using Ozeki.Network.Nat;
using Ozeki.VoIP;
using Ozeki.VoIP.SDK;

namespace SDK_Microphone
    class Program
        static ISoftPhone mySoftphone;
        static IPhoneLine phoneLine;
        static IPhoneCall call;
        static Microphone microphone;
        static MediaConnector connector;
        static PhoneCallAudioSender mediaSender;

        private static void Main(string[] args)
            mySoftphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000, 5060);

            var registrationRequired = true;
            var userName = "sipusername";
            var displayName = "sipdisplayname";
            var authenticationId = "authenticationid";
            var registerPassword = "password";
            var domainHost = "";
            var domainPort = 5060;

            mySoftphone_Register(mySoftphone, registrationRequired, displayName, userName, authenticationId, registerPassword,
                             domainHost, domainPort);

            microphone = Microphone.GetDefaultDevice();
            mediaSender = new PhoneCallAudioSender();
            connector = new MediaConnector();

            while (true) Thread.Sleep(10);

        static void mySoftphone_Register(ISoftPhone softphone, bool registrationRequired, string displayName, string userName,
        string authenticationId, string registerPassword, string domainHost, int domainPort)
                var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);
                var natConfiguration = new NatConfiguration(NatTraversalMethod.None);
                phoneLine = softphone.CreatePhoneLine(account, natConfiguration);
                phoneLine.PhoneLineStateChanged += mySoftphone_PhoneLineStateChanged;
            catch (Exception ex)
                Console.WriteLine("Error during SIP registration: " + ex.ToString());

        static void mySoftphone_PhoneLineStateChanged(object sender, VoIPEventArgs<PhoneLineState> e)
            if (e.Item == PhoneLineState.RegistrationTimedOut || e.Item == PhoneLineState.RegistrationFailed)
                Console.WriteLine("Registration failed!");

            if (e.Item == PhoneLineState.RegistrationSucceeded || e.Item == PhoneLineState.NoRegNeeded)
                Console.WriteLine("Registration succeeded - Online!");

        static void CreateCall()
            var numberToDial = "1002";

            call = mySoftphone.CreateCallObject(phoneLine, numberToDial);
            call.CallStateChanged += call_CallStateChanged;
            call.CallErrorOccured += call_CallErrorOccured;

        static void SetupMicrophone()
            connector.Connect(microphone, mediaSender);	// connects the microphone to the mediaSender object
            mediaSender.AttachToCall(call);	// attaches the mediaSender object to the call
            microphone.Start();	// starts the microphone
            Console.WriteLine("The microphone is functioning.");	// indicates that the microphone is ready to be used

        static void call_CallStateChanged(object sender, VoIPEventArgs<CallState> e)
            Console.WriteLine("Call state: {0}.", e.Item);

            if (e.Item == CallState.Answered)
                SetupMicrophone();	// calls the proper method to set up the microphone

        static void call_CallErrorOccured(object sender, VoIPEventArgs<CallError> e)
            Console.WriteLine("Error occured during the call: {0}.", e.Item);

Communication through the network

When the call has been accepted by the called party, the softphone is ready to send the voice stream to the destination through the UDP network as RTP packages. This example also writes to the console that the microphone has been successfully started.

Step 1: the application notifies the user about the success of starting the microphone

The microphone is functioning.

Step 2: RTP packages are being sent to the destination (a sample package)

[Stream setup by SDP (fram 28)]
10.. .... = Version: RFC 1889 Version (2)
..0. .... = Padding: False
...0 .... = Extension: False
.... 0000 = Contributing source code identifiers count: 0
1... .... = Marker: True
Payload type: ITU-T G.711 PCMU (0)
Sequence number: 7133
[Extended sequence number: 72669]
Timestamp: 85000
Synchronization Source identifier: 0x712f7356 (1898935126)
Payload: d55555545657545756565455575051575650515557515050...

Training guides, simple examples

This article is a simple example as a solution to introduce the implementation of this feature, if you would like to visit the detailed developer documentations, you can learn much more from the Ozeki VoIP SIP SDK Training chapter.

Related Pages

If you have any questions or need assistance, contact us at