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

Media stream to file and file to media stream conversion with different file formats and format conversions (using wav, mp3, avi, mpg, raw audio, video)

Explanation

Prerequisities

Download: AudioConversion.zip

This article is a brief introduction about media file conversions in relation with Ozeki VoIP SIP SDK. After reading through this page you will be fully familiar with all the essential terms concerning media file conversion technologies and what you will need for creating your own solution using Ozeki VoIP SIP SDK.

Introduction

VoIP communication solutions basically operate with digitized data streams, but they are usually capable for using media files playing into the communication channels. For this purpose it is essential that the concrete file types are supported by the VoIP programs.


Figure 1 - VoIP audio conversions

The VoIP communication itself is made by playing audio and video streams into the output devices. These streams can be directly for the digitized voice or images captured by the microphone and the camera or they can be created from a pre-recorded media file.

Ozeki VoIP SIP SDK has all the support needed for a VoIP software in order to capture voice and video, save them to media files and play them back into a SIP call.

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.

Ozeki VoIP SIP SDK supports .wav and .mp3 audio formats. It can play .wav and .mp3 files and record into .wav format. It also provides the possibility of human voice input from a microphone and sound output through a speaker. This example program shows, how you can use these input and output possibilities for audio conversion.

The GUI of the program shown in Figure 1 is really simple, it only contains two groupboxes, one for the input and one for the output tools. You can choose the input and output tools in any variations you want, the only important thing is to have at least one input and one output tool selected.


Figure 2 - The user interface for the audio conversion program

The program itself has a really simple concept. You get all the input tools and mix all the input data together in the output tool(s). The implementation is a bit more difficult as there are a lot of logical checks, you need to do.

First of all, lets see the tools you need to use. Basically, you will only need some MediaHandlers and a MediaConnector object (Code 1). The connector will connect the right handlers together in the right order and the rest will be done by calling some start methods.

Microphone microphone = Microphone.GetDefaultDevice();
Speaker speaker = Speaker.GetDefaultDevice();

WaveStreamPlayback waveStreamPlayback;
WaveStreamRecorder waveStreamRecorder;
MP3StreamPlayback mp3StreamPlayback;

MediaConnector connector = new MediaConnector();
AudioMixerMediaHandler mixer = new AudioMixerMediaHandler();

Code 1 - The tools you need for the conversion

When a checkbox is checked or unchecked on the GUI it invokes an event that needs to be handled in the case of the file specification checkboxes. Code 2 shows the event handler method for the input .wav file selection. The input .mp3 and output .wav selection handling is implemented similarly. The Open and Save buttons are enabled only when the checkboxes are checked. In this case you can specify the input and the output files.

In the case of the input file, the textboxes of the file names are always in read only mode. In this case you cannot specify the input file name by typing, but you neither can mistype it. The output file name can be typed in. In that case the recorded file will be saved in the folder where the .exe file is stored, but you can specify the file name by pressing the Save button too. In that case you can browse for the output folder.

private void inputWavCB_CheckedChanged(object sender, EventArgs e)
{
    wavOpen.Enabled = inputWavCB.Checked;
    if (!inputWavCB.Checked)
    {
        wavInputName.Text = string.Empty;
        waveStreamPlayback = null;
    }
}

Code 2 - Watch for the checkbox changes

When you press one of the Open buttons, an event handler method will run. In the case of the .wav open button this method is shown in Code 3. The .mp3 file opening is similar to this, only the file filter is different. The pop-up Open dialog window is a standard open window in Microsoft Windows system, so you will find it familiar.

private void wavOpen_Click(object sender, EventArgs e)
{
    wavInputName.Text = string.Empty;
    using (OpenFileDialog ofd = new OpenFileDialog() { Multiselect = false, Filter = "Wave Files (*.wav)|*.wav" })
    {
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             wavInputName.Text = ofd.FileName;
    }
}

Code 3 - Handling the Open buttons

Code 4 shows the Save button's event handler method. It works similar to the open methods, the only difference is that it opens a save dialog window that is also a standard Windows tool.

private void wavSave_Click(object sender, EventArgs e)
{
    wavOutputName.Text = string.Empty;
    using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Wave Files (*.wav)|*.wav", FileName = wavOutputName.Text })
    {
        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            wavOutputName.Text = sfd.FileName;
    }
}

Code 4 - Handling the Save button

The actual conversion is done by pressing the start conversion button. When you do that, a slightly difficult method will run. Code 5 shows the first part of this method that handles those cases when the conversion cannot start because there is no input and/or output tools selected. In these cases the conversion will not start, but an error message box will pop up that specifies the problem.

private void StartConversion_Click(object sender, EventArgs e)
{
    conversionInProgress = !conversionInProgress;

    if (conversionInProgress)
    {
        //no output tool selected
        if (!(speakerCB.Checked || outputWavCB.Checked))
        {
            MessageBox.Show("Please choose at least one output tool!", "Missing output", MessageBoxButtons.OK, MessageBoxIcon.Error);
            conversionInProgress = false;
            StartConversion.Text = "Start Conversion";
            return;
        }
        //output .wav file not specified though checked
        if (outputWavCB.Checked && wavOutputName.Text == string.Empty)
        {
            MessageBox.Show("You did not specify the output file name.", "Missing output file name", MessageBoxButtons.OK, MessageBoxIcon.Error);
            conversionInProgress = false;
            StartConversion.Text = "Start Conversion";
            return;
        }

        //no input tool selected
        if (!(microphoneCB.Checked || inputWavCB.Checked || inputMp3CB.Checked))
        {
            MessageBox.Show("Please choose at least one input tool!", "Missing input", MessageBoxButtons.OK, MessageBoxIcon.Error);
            conversionInProgress = false;
            StartConversion.Text = "Start Conversion";
            return;
        }
        ...

Code 5 - Pressing the Start button without input and/or output tool specified

If you check any of the input file checkboxes, you need to specify the files before the conversion. If you do not do so, pressing the Start Conversion button will force you to do so by making the open dialog windows pop up. If you do not specify the file names even in this case, the conversion will be cancelled.

//no input .wav file specified
if (inputWavCB.Checked && wavInputName.Text == string.Empty)
{
    MessageBox.Show("You did not specify the input .wav file name.", "Missing input .wav file name", MessageBoxButtons.OK, MessageBoxIcon.Error);
    wavOpen.PerformClick();
    if (wavInputName.Text == string.Empty)
    {
        conversionInProgress = false;
        return;
    }
}
//no input .mp3 file specified
if (inputMp3CB.Checked && mp3Name.Text == string.Empty)
{
    MessageBox.Show("You did not specify the input .mp3 file name.", "Missing input .mp3 file name", MessageBoxButtons.OK, MessageBoxIcon.Error);
    mp3Open.PerformClick();
    if (mp3Name.Text == string.Empty)
    {
        conversionInProgress = false;
        return;
    }
}

Code 6 - Missing input file(s)

When the conversion actually starts, the program does not allow modifying the input and/or output files, therefore the Open and Save buttons will be disabled during the conversion.

StartConversion.Text = "Stop Conversion";

//disable the open and save buttons
wavOpen.Enabled = false;
wavSave.Enabled = false;
mp3Open.Enabled = false;

Code 7 - You cannot change the input and output during the conversion

The next section of the StartConversion_Click method (Code 8) contains the input file player initialization(s) if they are necessary. If the input file name is not valid, it will throw an exception. The method is similar for both the .wav and .mp3 playback objects.

//trying to initialize input .wav player
if (inputWavCB.Checked && wavInputName.Text != string.Empty)
{
    try
    {
        waveStreamPlayback = new WaveStreamPlayback(wavInputName.Text);
        waveStreamPlayback.Stopped += WavPlaybackStopped;
    }
    catch (Exception ex)
    {
        MessageBox.Show("The input .wav file does not exist", "Problem with input data", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
//trying to initialize input .mp3 player
if (inputMp3CB.Checked && mp3Name.Text != string.Empty)
{
    try
    {
        mp3StreamPlayback = new MP3StreamPlayback(mp3Name.Text);
        mp3StreamPlayback.Stopped += Mp3PlaybackStopped;
    }
    catch (Exception ex)
    {
        MessageBox.Show("The input .mp3 file does not exist", "Problem with input data", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Code 8 - Input audio player initialization (if necessary)

If you specified an output file for the conversion, the code snippet in Code 9 will initialize the recorder object for the file.

//initializing output file
if (wavOutputName.Text != string.Empty)
{
    waveStreamRecorder = new WaveStreamRecorder(wavOutputName.Text);
    waveStreamRecorder.Stopped += WaveRecordStopped;
}

Code 9 - Initializing the output file (if necessary)

When all the selected input and output file handlers are initialized, it is time to make the proper Mediaandler connections for the conversion (Code 10). You need to connect all the valid input handlers (Microphone, WaveStreamPlayback, MP3Playback) to the AudioMixerMediaHandler object that is the middleware MediaHandler for the conversion.

You also need to connect the AudioMixerMediaHandler to the chosen output tools (speaker and/or WaveStreamRecorder). You need to start the tools with the Start() or StartStreaming() method calls, and this starts the conversion.

//there is a valid speaker selected
if (speaker != null && speakerCB.Checked)
{
    connector.Connect(mixer, speaker);
    speaker.Start();
}
//there is a valid microphone selected
if (microphone != null && microphoneCB.Checked)
{
    microphone.Start();
    connector.Connect(microphone, mixer);
}
//there is a valid WaveStreamPlayback
if (waveStreamPlayback != null)
{
    connector.Connect(waveStreamPlayback, mixer);
    waveStreamPlayback.StartStreaming();
    waveStreamPlayback.IsStreaming = true;
}
//there is a valid MP3Playback
if (mp3StreamPlayback != null)
{
    connector.Connect(mp3StreamPlayback, mixer);
    mp3StreamPlayback.StartStreaming();
    mp3StreamPlayback.IsStreaming = true;
}
//there is a specified output file
if (waveStreamRecorder != null)
{
    connector.Connect(mixer, waveStreamRecorder);
    waveStreamRecorder.StartStreaming();
    waveStreamRecorder.IsStreaming = true;
}

Code 10 - Making media handler connections

When the conversion is in progress, the Start Conversion button changes to Stop Conversion. This means that you need to press the button agan for making the conversion stop.

Code 11 shows the code section of the StartConversion_Click event handler method that handles the conversion stopping. As you can see all the Open and Save buttons will be set back to enabled or disabled according to the checkboxes. All the started MediaHandler objects will be stopped and disconnected. The output file will be closed by calling the Dispose method of the WaveStreamRecorder, and all the file handlers will be set to null.

StartConversion.Text = "Start Conversion";

//enable open and save buttons if necessary
wavOpen.Enabled = inputWavCB.Checked;
wavSave.Enabled = outputWavCB.Checked;
mp3Open.Enabled = inputMp3CB.Checked;

//empty all the file names
wavInputName.Text = string.Empty;
wavOutputName.Text = string.Empty;
mp3Name.Text = string.Empty;

//disconnections
//speaker
if (speakerCB.Checked)
{
    speaker.Stop();
    connector.Disconnect(mixer, speaker);
}
//microphone
if (microphoneCB.Checked)
{
    microphone.Stop();
    connector.Disconnect(microphone, mixer);
}
//WaveStreamPlayback
if (inputWavCB.Checked)
{
    waveStreamPlayback.StopStreaming();
    waveStreamPlayback.IsStreaming = false;
    connector.Disconnect(waveStreamPlayback, mixer);
    waveStreamPlayback = null;
}
//MP3Playback
if (inputMp3CB.Checked)
{
    mp3StreamPlayback.StopStreaming();
    mp3StreamPlayback.IsStreaming = false;
    connector.Disconnect(mp3StreamPlayback, mixer);
    mp3StreamPlayback = null;
}
//WaveStreamRecorder
if (outputWavCB.Checked)
{
    waveStreamRecorder.StopStreaming();
    waveStreamRecorder.IsStreaming = false;
    connector.Connect(mixer, waveStreamRecorder);
    waveStreamRecorder.Dispose();
    waveStreamRecorder = null;
}

Code 11 - Stopping the conversion

This example program needed more attention as there are so many cases when the conversion could go wrong, but the functionality is now perfectly working. At this point you can see how you can use the MediaHandlers for audio conversion and you can use these in a similar way in a softphone solution too.

This article introduced you the basic knowledge about media file conversion and showed how Ozeki VoIP SIP SDK can help you to fulfill your wishes about this topic. If you have read through this page carefully, you already have all the knowledge you need to start on your own solution.

As you are now familiar with all the terms concerning this topic, now it is time to take a step further and explore what other extraordinary solution Ozeki VoIP SIP SDK can provide to you.

If you have any questions 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 licensing page

Related Pages

Operating system: Windows 8, Windows 7, Vista, 200x, XP
System memory: 512 MB+
Free disk space: 100 MB+
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