Creating a LEADTOOLS Multimedia MPEG-2 Streaming Application

Perform the following steps to create and run a multimedia MPEG2 Streaming application using the LEADTOOLS Multimedia ConvertCtrl and PlayCtrl controls.

  1. Start Visual Studio.

  2. Choose File->New->Project... from the menu.

  3. 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.

  4. 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.

  5. 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:

    • LEAD MPEG-2 Transport Multiplexer Library
    • LEAD MPEG-2 Transport Demultiplexer Library

    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.

  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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:

    vb 
    Imports Leadtools.Multimedia 
    Imports System.Runtime.InteropServices 
    Imports LMMpg2MxTLib 
    Imports LMMpgDmxTLib 
    C#
    using Leadtools.Multimedia; 
    using System.Runtime.InteropServices; 
    using LMMpg2MxTLib; 
    using LMMpgDmxTLib; 

  12. Declare the following private variable:

    vb 
    Private Const SLIDER_MAX As Integer = 10000 
    Private _streaming As Boolean = False 
    Private _firstPTS As Double 
    Private _lastPTS As Double 
    C#
    Private const int SLIDER_MAX = 10000; 
    Private bool _streaming = false; 
    Private double _firstPTS; 
    Private double _lastPTS; 

  13. Add an event handler to the _convertctrl Started event and code it as follows:

    vb 
    Private Sub _convertCtrl_Started(ByVal sender As Object, ByVal e As EventArgs) Handles _convertCtrl.Started 
       _streaming = True 
       _buttonBrowse.Enabled = False 
       If _playctrl.State = PlayState.Stopped OrElse _playctrl.State = PlayState.NotReady Then 
          _playctrl.SourceFile = _convertCtrl.TargetFile 
          _playctrl.Run() 
       End If 
    End Sub 
    C#
    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(); 
       } 
    } 

  14. Add an event handler to the _convertctrl Complete event and code it as follows:

    vb 
    Private Sub _convertCtrl_Complete(ByVal sender As Object, ByVal e As EventArgs) Handles _convertCtrl.Complete 
       If _streaming = True Then 
          _convertCtrl.StartConvert() 
       Else 
          _buttonStartStop.Text = "Start" 
          _buttonBrowse.Enabled = True 
       End If 
    End Sub 
    C#
    void _convertCtrl_Complete(object sender, EventArgs e) 
    { 
       if (_streaming == true) 
          _convertCtrl.StartConvert(); 
       else 
       { 
          _buttonStartStop.Text = "Start"; 
          _buttonBrowse.Enabled = true; 
       } 
    } 

  15. Add an event handler to the _playctrl TrackingPositionChanged event and code it as follows:

    vb 
    Private Sub _playctrl_TrackingPositionChanged(ByVal sender As Object, ByVal e As TrackingPositionChangedEventArgs) Handles _playctrl.TrackingPositionChanged 
       Dim demux As LMMpgDmxT = TryCast(_playctrl.GetSubObject(PlayObject.Splitter), LMMpgDmxT) 
       If Not demux Is Nothing Then 
          demux.RefreshPosition(0) 
                     
          _firstPTS = demux.FirstStreamPTS 
          _lastPTS = demux.LastStreamPTS 
                     
          _track.Value = PTSToSliderPosition(demux.CurrentStreamPTS) 
          Marshal.ReleaseComObject(demux) 
       End If 
    End Sub 
    C#
    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); 
       } 
    } 

  16. Add an event handler to the _track Scroll event and code it as follows:

    vb 
    Private Sub _track_Scroll(ByVal sender As Object, ByVal e As EventArgs) Handles _track.Scroll 
       Dim demux As LMMpgDmxT = TryCast(_playctrl.GetSubObject(PlayObject.Splitter), LMMpgDmxT) 
       If Not demux Is Nothing Then 
          demux.RefreshPosition(0) 
          demux.CurrentStreamPTS = SliderPositionToPTS(_track.Value) 
                     
          Marshal.ReleaseComObject(demux) 
       End If 
    End Sub 
    C#
    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); 
       } 
    } 

  17. Add the following helper methods for use by the _track control's scroll and _playctrl track positioning handlers:

    vb 
    Private Function PTSToSliderPosition(ByVal curPTS As Double) As Integer 
       Dim retVal As Integer = 0 
       If curPTS <= _firstPTS Then 
          retVal = 0 
       ElseIf curPTS >= _lastPTS Then 
          retVal = SLIDER_MAX 
       Else 
          retVal = CInt(((curPTS - _firstPTS) * SLIDER_MAX) / (_lastPTS - _firstPTS) + 0.5) 
       End If 
                     
       Return retVal 
    End Function 
                     
    Private Function SliderPositionToPTS(ByVal nPos As Integer) As Double 
       Return _firstPTS + CDbl(nPos) * (_lastPTS - _firstPTS) / CDbl(SLIDER_MAX) 
    End Function 
    C#
    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; 
    } 

  18. Add an event handler to the _buttonBrowse Click event and code it as follows:

    vb 
    Private Sub _btnBrowse_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _buttonBrowse.Click 
       Dim ofn As OpenFileDialog = New OpenFileDialog() 
       ofn.FileName = _txtSrcFile.Text 
       ofn.FilterIndex = 0 
                     
       If _txtSrcFile.Text <> String.Empty Then 
          ofn.InitialDirectory = Path.GetDirectoryName(_txtSrcFile.Text) 
       Else 
          ofn.InitialDirectory = Directory.GetCurrentDirectory() 
       End If 
                     
       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 Then 
          _txtSrcFile.Text = ofn.FileName 
       End If 
    End Sub 
    C#
    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; 
       } 
    } 

  19. Add an event handler to the _buttonStartStop Click event and code it as follows:

    vb 
    Private Sub _btnStartStop_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _buttonStartStop.Click 
       Dim srcFile As String = _txtSrcFile.Text 
                     
       If _streaming = False Then 
          If srcFile <> String.Empty AndAlso File.Exists(srcFile) Then 
             _convertCtrl.SourceFile = srcFile 
             _convertCtrl.AllowedStreams = StreamFormatType.AudioVideoCC 
             _convertCtrl.PreferredMPEG2Splitter = Leadtools.Multimedia.Constants.Filter_MPEG2_Transport_Demux 
             _convertCtrl.TargetFormat = TargetFormatType.MPEG2Transport 
             _convertCtrl.Preview = True 
             _convertCtrl.TargetFile = "udp://127.0.0.1:9005" 
                     
             Dim mux As LMMpg2MxT = TryCast(_convertCtrl.GetSubObject(ConvertObject.TargetFilter), LMMpg2MxT) 
             If Not mux Is Nothing Then 
                mux.OutputType = CInt(LMMpg2MxTLib.Mpg2MxT_OutputType.Mpg2MxT_OutputType_Broadcast) 
                mux.ResyncInterval = 10 
                     
                Marshal.ReleaseComObject(mux) 
             End If 
                     
             Try 
                _convertCtrl.StartConvert() 
                _buttonStartStop.Text = "Stop" 
                _buttonBrowse.Enabled = False 
             Catch e1 As Exception 
             End Try 
          End If 
       Else 
          If _streaming = True Then 
             _convertCtrl.StopConvert() 
             _buttonStartStop.Text = "Start" 
             _buttonBrowse.Enabled = True 
          End If 
       End If 
    End Sub 
    C#
    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; 
          } 
       } 
    } 

  20. Add an event handler to the _buttonPauseResume Click event and code it as follows:

    vb 
    Private Sub _buttonPauseResume_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _buttonPauseResume.Click 
       If _playctrl.State = PlayState.Running Then 
          _playctrl.Pause() 
          _buttonPauseResume.Text = "Resume" 
       ElseIf _playctrl.State = PlayState.Paused Then 
          _playctrl.Run() 
          _buttonPauseResume.Text = "Pause" 
       End If 
    End Sub 
    C#
    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"; 
       } 
    } 

  21. 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.

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Multimedia
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.