Perform the following steps to create and run a multimedia MPEG2 Streaming application using the LEADTOOLS Multimedia ConvertCtrl and PlayCtrl controls.
Start Visual Studio.
Choose File->New->Project... from the menu.
In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
Type the project name as "Multimedia MPEG2 Streaming" in the Project Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click OK.
In the "Solution Explorer" window, right-click the "References" folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and select Leadtools.Multimedia and click OK. Also, right-click the "References" folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the "COM" tab and select:
Note: The above COM objects must be registered; in case they were not; using regsvr32. For more information, refer to DirectShow Registry-free Activation.
Next click OK.
Make sure Form1 is in design view. Go to the toolbox (View->Toolbox) and drag a ConvertCtrl control and a PlayCtrl control on the form. NOTE: If you do not have ConvertCtrl or PlayCtrl controls in your toolbox, select Tools->Choose Toolbox Items from the menu. Click Browse and then select Leadtools.Multimedia.dll from "<LEADTOOLS_INSTALLDIR>\Bin\Dotnet4\Win32" and then click Open and then click OK. After adding these controls to the form, set the following properties:
Property | Value |
---|---|
Name | _convertctrl |
Anchor | Top, Left |
BackColor | Black |
Name | _playctrl |
Anchor | Top, Bottom, Left, Right |
AutoStart | False |
Go to the toolbox (View->Toolbox) and drag a Text control on the form (below the convert control) and set the following properties:
Property | Value |
---|---|
Name | _txtSrcFile |
Anchor | Bottom, Left |
Locked | True |
Go to the toolbox (View->Toolbox) and drag a Button control to the right of the text control and set the following properties:
Property | Value |
---|---|
Name | _buttonBrowse |
Text | ... |
Anchor | Bottom, Left |
Go to the toolbox (View->Toolbox) and drag a TrackBar control on the form (below the play control) and set the following properties:
Property | Value |
---|---|
Name | _track |
Anchor | Bottom, Left, Right |
Maximum | 10000 |
Go to the toolbox (View->Toolbox) and drag two Button controls to the bottom of the form (one below the convert control and one below the play control) and set the following properties:
Property | Value |
---|---|
Name | _buttonStartStop |
Text | Start |
Anchor | Bottom, Left |
Name | _buttonPauseResume |
Text | Pause |
Anchor | Bottom, Right |
Switch Form1 to code view (right-click Form1 in the Solution Explorer then choose View Code) and add the following lines to the beginning of the file:
using Leadtools.Multimedia;
using System.Runtime.InteropServices;
using LMMpg2MxTLib;
using LMMpgDmxTLib;
Declare the following private variable:
Private const int SLIDER_MAX = 10000;
Private bool _streaming = false;
Private double _firstPTS;
Private double _lastPTS;
Add an event handler to the _convertctrl Started event and code it as follows:
void _convertCtrl_Started(object sender, EventArgs e)
{
_streaming = true;
_buttonBrowse.Enabled = false;
if (_playctrl.State == PlayState.Stopped || _playctrl.State == PlayState.NotReady)
{
_playctrl.SourceFile = _convertCtrl.TargetFile;
_playctrl.Run();
}
}
Add an event handler to the _convertctrl Complete event and code it as follows:
void _convertCtrl_Complete(object sender, EventArgs e)
{
if (_streaming == true)
_convertCtrl.StartConvert();
else
{
_buttonStartStop.Text = "Start";
_buttonBrowse.Enabled = true;
}
}
Add an event handler to the _playctrl TrackingPositionChanged event and code it as follows:
void _playctrl_TrackingPositionChanged(object sender, TrackingPositionChangedEventArgs e)
{
LMMpgDmxT demux = _playctrl.GetSubObject(PlayObject.Splitter) as LMMpgDmxT;
if (demux != null)
{
demux.RefreshPosition(0);
_firstPTS = demux.FirstStreamPTS;
_lastPTS = demux.LastStreamPTS;
_track.Value = PTSToSliderPosition(demux.CurrentStreamPTS);
Marshal.ReleaseComObject(demux);
}
}
Add an event handler to the _track Scroll event and code it as follows:
private void _track_Scroll(object sender, EventArgs e)
{
LMMpgDmxT demux = _playctrl.GetSubObject(PlayObject.Splitter) as LMMpgDmxT;
if (demux != null)
{
demux.RefreshPosition(0);
demux.CurrentStreamPTS = SliderPositionToPTS(_track.Value);
Marshal.ReleaseComObject(demux);
}
}
Add the following helper methods for use by the _track control's scroll and _playctrl track positioning handlers:
private int PTSToSliderPosition(double curPTS)
{
int retVal = 0;
if (curPTS <= _firstPTS)
retVal = 0;
else if (curPTS >= _lastPTS)
retVal = SLIDER_MAX;
else
retVal = (int)(((curPTS - _firstPTS) * SLIDER_MAX) / (_lastPTS - _firstPTS) + 0.5);
return retVal;
}
private double SliderPositionToPTS(int nPos)
{
return _firstPTS + (double)nPos * (_lastPTS - _firstPTS) / (double)SLIDER_MAX;
}
Add an event handler to the _buttonBrowse Click event and code it as follows:
private void _btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog ofn = new OpenFileDialog();
ofn.FileName = _txtSrcFile.Text;
ofn.FilterIndex = 0;
if (_txtSrcFile.Text != string.Empty)
ofn.InitialDirectory = Path.GetDirectoryName(_txtSrcFile.Text);
else
ofn.InitialDirectory = Directory.GetCurrentDirectory();
ofn.Multiselect = false;
ofn.Title = "Select Source Video";
ofn.CheckFileExists = true;
ofn.Filter = "MPEG2 Video Files (*.mpg)|*.mpg | All files (*.*)|*.*";
if (ofn.ShowDialog() == DialogResult.OK)
{
_txtSrcFile.Text = ofn.FileName;
}
}
Add an event handler to the _buttonStartStop Click event and code it as follows:
private void _btnStartStop_Click(object sender, EventArgs e)
{
string srcFile = _txtSrcFile.Text;
if (_streaming == false)
{
if (srcFile != string.Empty && File.Exists(srcFile))
{
_convertCtrl.SourceFile = srcFile;
_convertCtrl.AllowedStreams = StreamFormatType.AudioVideoCC;
_convertCtrl.PreferredMPEG2Splitter = Constants.Filter_MPEG2_Transport_Demux;
_convertCtrl.TargetFormat = TargetFormatType.MPEG2Transport;
_convertCtrl.Preview = true;
_convertCtrl.TargetFile = @"udp://127.0.0.1:9005";
LMMpg2MxT mux = _convertCtrl.GetSubObject(ConvertObject.TargetFilter) as LMMpg2MxT;
if (mux != null)
{
mux.OutputType = (int)LMMpg2MxTLib.Mpg2MxT_OutputType.Mpg2MxT_OutputType_Broadcast;
mux.ResyncInterval = 10;
Marshal.ReleaseComObject(mux);
}
try
{
_convertCtrl.StartConvert();
_buttonStartStop.Text = "Stop";
_buttonBrowse.Enabled = false;
}
catch (Exception) { }
}
}
else
{
if (_streaming == true)
{
_convertCtrl.StopConvert();
_buttonStartStop.Text = "Start";
_buttonBrowse.Enabled = true;
}
}
}
Add an event handler to the _buttonPauseResume Click event and code it as follows:
private void _buttonPauseResume_Click(object sender, EventArgs e)
{
if (_playctrl.State == PlayState.Running)
{
_playctrl.Pause();
_buttonPauseResume.Text = "Resume";
}
else if (_playctrl.State == PlayState.Paused)
{
_playctrl.Run();
_buttonPauseResume.Text = "Pause";
}
}
Build, and Run the program to test it. Click the [...] browse button to locate a video file for the convert / streaming output. Then click the Start button to begin streaming. Finally, click the Pause/Resume button to test pause and resume on the play control OR drag the track bar control to reposition the stream playback.