info@voip-sip-sdk.com Tel.: 00 36 52 532 731

How to play an mp3 file into a voice call using C#?

Download: Play_MP3_Into_Call.zip

This example demonstrates how to create an mp3 player, which is able to play an mp3 file into the call as voice. Before you start to study how to stream mp3 file into a call, you might need 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 start streaming the mp3 file

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

Streaming media is multimedia that is constantly received by and presented to an end-user while being delivered by a provider. An mp3 media player can begin playing the mp3 file's voice data before the entire file has been transmitted.

In the case of softphones, mp3 streamers are used to play mp3 files into calls, for example in the cases of IVRs (Interactive Voice Response), voicemails, auto-answerers etc. Basically, mp3 stream players work almost the same way as microphones, but the stream of audio data is being sent from an mp3 file instead from a microphone, which means mp3 streamers can be attached to calls with the correct mediasender object.

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 play mp3 file into call using c#?

Ozeki VoIP SIP SDK provides the MP3StreamPlayback class for the purpose to create mp3 streamer objects, which are receiving the mp3 file's path and name as parameter, and are able to be attached to the call with the correct media sender object. The user only have to call the StartStreaming() method of the object, so the mp3 streamer can begin the streaming.
Please note that, there are much more media handlers provided by the SDK, for example you can even record calls to mp3 files, or play - and record - wav files as well.

Mp3 file streaming into 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 Play_MP3_Into_Call
{
    class Program
    {
        static ISoftPhone mySoftphone;
        static IPhoneLine phoneLine;
        static IPhoneCall call;
        static MediaConnector connector;
        static PhoneCallAudioSender mediaSender;
        static MP3StreamPlayback mp3Player;	// the mp3Player object is able to play mp3 files

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

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

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

            mp3Player = new MP3StreamPlayback("helloworld.mp3");	// sets the mp3Player to play the file, given as parameter
            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)
        {
            try
            {
                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;
                softphone.RegisterPhoneLine(phoneLine);
            }
            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!");
                CreateCall();
            }
        }

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

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

        static void SetupMp3Player()
        {
            connector.Connect(mp3Player, mediaSender);	// connects the mp3Player object to the mediaSender
            mediaSender.AttachToCall(call);	// attaches the mediaSender object to the call
            mp3Player.StartStreaming();	// the mp3Player object starts to stream the mp3 file into the call
            Console.WriteLine("The mp3 player is streaming.");	// indicates that the mp3Player object has started to stream
        }

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

            if (e.Item == CallState.Answered)
                SetupMp3Player();
        }

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

Training guides, simple examples

If you would like to visit the detailed developer documentations, you can learn much more from the Ozeki VoIP SIP SDK Training chapter.

Communication throught the network

When the call has been accepted by the called party, the softphone is ready to stream the given mp3 file into the call through the UDP network as RTP packages. This example also writes to the console that the mp3 streamer has started to stream the file.

Step1: the application notifies the user about the beginning of the streaming

The mp3 player is streaming.

Step 2: the mp3 is being sent to the destination as RTP packages (a sample package)

[Stream setup by SDP (frame 28)]
10.. .... = Version: RFC 1889 Version (2)
..0. .... = Padding: False
...0 .... = Extension: False
.... 0000 = Contributing source 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...

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

Related Pages