Capture from Multiple Sources - WinForms C#

This tutorial shows how to capture audio/video from multiple sources at once using the LEADTOOLS Multimedia SDK in a WinForms C# application.

Overview  
Summary This tutorial covers how to capture audio/video from multiple sources at once in WinForms C# application.
Completion Time 30 minutes
Project Download tutorial project (136 KB)
Platform Windows WinForms C# Application
IDE Visual Studio 2022
Runtime License Download LEADTOOLS

Required Knowledge

Before any functionality from the SDK can be leveraged, a valid runtime license will have to be set.

For instructions on how to obtain a runtime license refer to Obtaining a License.

This project requires the Multimedia DLL's to be registered. To accomplish this, first open Command Prompt or Windows Powershell and navigate to <INSTALL_DIR>\LEADTOOLS23\Bin\Common\Batch. Run the RegMMBin.bat file by using the command .\RegMMBin.bat register.

Note

Register the Multimedia DLLs either with Command Prompt or Windows Powershell. Both applications must run as administrator.

Create the Project

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. Add the following local DLL references:

The following DLLs are located at \LEADTOOLS23\Bin\Dotnet4\x64:

The following DLL is located at \LEADTOOLS23\Bin\CDLL\x64:

For a complete list of which DLL files are required for your application, refer to Multimedia Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in Add References and Set a License.

Create the Form Controls

With the project created, the references added, and the license set, coding can begin.

In Solution Explorer, double-click on Form1.cs to open the Designer.

First click on the main Form1 control, and in the properties window change the size to 897, 615.

Add the following controls to the form:

The form designer should now look like this

Form Designer

Initialize the Capture Control

In Solution Explorer, right-click on Form1.cs and select View Code to display the code-behind of the form. Add the following statements to the using block at the top.

C#
using Leadtools; 
using Leadtools.Multimedia; 
using LMH264EncoderLib; 
using LMAACEncoderLib; 
using LMMixCapSrcLib; 
using System; 
using System.Drawing; 
using System.IO; 
using System.Windows.Forms; 

Add the below global variable.

C#
internal CaptureCtrl captureCtrl; 

Create a new function InitCaptureControl(). This will be called in the Form1() constructor below the SetLicense() call. Add the following code to InitCaptureControl():

C#
void InitCaptureControl() 
{ 
   captureCtrl = new CaptureCtrl 
   { 
      Preview = false, 
      Dock = DockStyle.Fill 
   }; 
 
   panel1.Controls.Add(captureCtrl); 
   captureCtrl.VideoWindowSizeMode = SizeMode.Normal; 
 
   captureCtrl.VideoDevices["@device:sw:{E2B7DFB2-38C5-11D5-91F6-00104BDB8FF9}\\{E2B7DFB9-38C5-11D5-91F6-00104BDB8FF9}"].Selected = true; 
 
   var mixSrc = captureCtrl.GetSubObject(CaptureObject.VideoCaptureFilter) as ILMMixCapSrc; 
   mixSrc.ResetToDefaults(); 
   mixSrc.BackgroundColor = (uint)ColorTranslator.ToOle(Color.DarkRed); 
   mixSrc.BackgroundImage = @"background.jpg"; 
 
   mixSrc.Labels.AddLabel(); 
   ILMMixCapSrcLabel label = mixSrc.Labels.GetLabel(mixSrc.Labels.Count - 1); 
   label.Text = @"%c"; // Display current date and time, see https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strftime-wcsftime-strftime-l-wcsftime-l?view=msvc-170 for all the options 
   label.VerticalAlignment = LMMixCapSrcLabelVAlign.LMMixCapSrcLabelVAlign_Top; 
   label.HorizontalAlignment = LMMixCapSrcLabelHAlign.LMMixCapSrcLabelHAlign_Right; 
 
   stdole.IFontDisp font = label.Font; 
   font.Name = "Arial"; 
   font.Bold = true; 
   font.Size = 12; 
   label.Font = font; 
   label.BackgroundVisible = false; 
   label.Color = (uint)ColorTranslator.ToOle(Color.Gold); 
 
   mixSrc.TopMargin = 20; 
 
   mixSrc.Labels.AddLabel(); 
   label = mixSrc.Labels.GetLabel(mixSrc.Labels.Count - 1); 
   label.Text = @"LEAD Mixing Capture Source"; 
   label.VerticalAlignment = LMMixCapSrcLabelVAlign.LMMixCapSrcLabelVAlign_Bottom; 
   label.HorizontalAlignment = LMMixCapSrcLabelHAlign.LMMixCapSrcLabelHAlign_Center; 
 
   font = label.Font; 
   font.Name = "Arial"; 
   font.Bold = true; 
   font.Size = 12; 
   label.Font = font; 
   label.Color = (uint)ColorTranslator.ToOle(Color.Red); 
   label.BackgroundColor = (uint)ColorTranslator.ToOle(Color.Gold); 
 
   mixSrc.BottomMargin = 25; 
 
   mixSrc.Alignment = LMMixCapSrcAlignment.LMMixCapSrcAlignment_Horizontal; 
 
   // Add each source to the captureControl 
   AddStream(mixSrc, @"VIDEO NAME FOR DEVICE ONE", @"AUDIO NAME FOR DEVICE ONE", true, @"LABEL TEXT"); 
   AddStream(mixSrc, @"VIDEO NAME FOR DEVICE TWO", @"AUDIO NAME FOR DEVICE TWO", true, @"LABEL TEXT"); 
 
   captureCtrl.PreviewSource = CapturePreview.VideoAndAudio; // comment out this line if you do not want to preview audio 
   captureCtrl.Preview = true; 
} 

Add Streams to Capture Control

Create two new functions:

// 1. AddStream function 
AddStream(ILMMixCapSrc mixSrc, string videoName, string audioName = null, bool useFriendlyName = false, string labelText = null) 
 
// 2. FindDeviceName function 
FindDeviceName(ILMMixCapSrcDevices devices, string name) 

These two functions will be used together in order to add the specified sources to the captureCtrl object.

C#
private void AddStream(ILMMixCapSrc mixSrc, string videoName, string audioName = null, bool useFriendlyName = false, string labelText = null) 
{ 
   var count = mixSrc.StreamCount; 
   mixSrc.AddStream(); 
   ILMMixCapSrcStream stream = mixSrc.GetStream(count); 
 
   if (videoName != null) 
   { 
      ILMMixCapSrcDevices devices = stream.VideoDevices; 
      if (useFriendlyName) 
         videoName = FindDeviceName(devices, videoName); 
      var index = devices.Find(videoName); 
      devices.Selection = index; 
      if (labelText != null) 
      { 
         stream.Labels.AddLabel(); 
         ILMMixCapSrcLabel label = stream.Labels.GetLabel(stream.Labels.Count - 1); 
         label.Text = labelText; 
 
         stdole.IFontDisp font = label.Font; 
         font.Name = "Arial"; 
         font.Bold = true; 
         font.Size = 8; 
         label.Font = font; 
 
         label.Color = (uint)ColorTranslator.ToOle(Color.White); 
         label.BackgroundColor = (uint)ColorTranslator.ToOle(Color.Blue); 
      } 
   } 
   if (audioName != null) 
   { 
      ILMMixCapSrcDevices devices = stream.AudioDevices; 
      if (useFriendlyName) 
         audioName = FindDeviceName(devices, audioName); 
      var index = devices.Find(audioName); 
      devices.Selection = index; 
   } 
} 
 
private string FindDeviceName(ILMMixCapSrcDevices devices, string name) 
{ 
   for (int i = 0; i < devices.Count; i++) 
   { 
      ILMMixCapSrcDevice device = devices.Item(i); 
      if (String.Compare(device.FriendlyName, name, StringComparison.OrdinalIgnoreCase) == 0) 
         return device.Name; 
 
   } 
   return ""; 
} 

Add the Capture Start and Stop Code

Navigate back to the Form1.cs Designer. Double-click on the button1 object to create a new event function button1_Click(). Add the following code to button1_Click() to start and stop the capture.

C#
private void button1_Click(object sender, EventArgs e) 
{ 
   // If the capture is running, stop it which will trigger the file to be saved and created 
   if (captureCtrl.State == CaptureState.Running) 
   { 
      captureCtrl.StopCapture(); 
      button1.Text = "Start Capture"; 
   } 
   else //Otherwise, open the save file dialog and let the user select the path and setup the compression options 
   { 
      SaveFileDialog saveFileDialog = new SaveFileDialog(); 
      saveFileDialog.Filter = "MP4 | *.mp4"; 
      if (saveFileDialog.ShowDialog() == DialogResult.OK) 
      { 
         captureCtrl.EnterEdit(); 
         captureCtrl.TargetFile = saveFileDialog.FileName; 
         captureCtrl.TargetFormat = TargetFormatType.ISO; 
         captureCtrl.AudioCompressors.AAC.Selected = true; 
         captureCtrl.VideoCompressors.H264.Selected = true; 
 
         ILMAACEncoder encoderAudio = captureCtrl.GetSubObject(CaptureObject.AudioCompressor) as ILMAACEncoder; 
         encoderAudio.ResetToDefaults(); 
 
         // Contains the video quality factor. 
         // A quality factor of 1 gives the highest quality and the largest video file size. 
         // A quality factor of 49 gives the lowest quality and smallest file size. 
         // This property is valid only if the EnableRateControl property is FALSE. 
         ILMH264Encoder encoderVideo = captureCtrl.GetSubObject(CaptureObject.VideoCompressor) as ILMH264Encoder; 
         encoderVideo.ResetToDefaults(eH264APILEVEL.H264_APILEVEL_1); 
         encoderVideo.EnableRateControl = false; 
         encoderVideo.QualityFactor = 28; // medium quality 
         // encoderVideo.QualityFactor = 1; // high quality 
         // encoderVideo.QualityFactor = 40; // low quality 
 
         captureCtrl.LeaveEdit(); 
         captureCtrl.StartCapture(CaptureMode.VideoAndAudio); 
         button1.Text = "Stop Capture"; 
      } 
   } 
} 

Run the Project

Ensure the Target Platform is set to x64. Run the project by pressing F5.

If the steps were followed correctly, the application runs and displays the sources to the user in the window. Upon pressing Start Capture, a dialog will appear prompting the user to select a name and location for the captured file. The application will capture until the Stop Capture button is clicked, upon clicking the button the capture will be stopped and saved to the specified file and location.

Preview showing two different sources

Wrap-up

This tutorial showed how to capture audio/video from multiple sources at once using the LEADTOOLS Multimedia SDK in a WinForms C# application.

See Also

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

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