This tutorial shows live video from a capture device with MLive: an SDI or HDMI card, an NDI source or a
webcam. It uses the project from MPlatform 1: Playing a file. In MPlatform devices,
inputs and other choices are selected by name: DeviceGetByIndex lists the names, DeviceSet
selects one.
On the form, place a Panel named previewPanel, three ComboBox controls
(deviceBox, inputBox, formatBox) and a Button named
startButton.
Choose the device, the input and the format
private readonly MLiveClass m_live = new MLiveClass();
// "video" lists the video devices, "video::line-in" the inputs of the selected device
private static void FillNames(MLiveClass live, string type, ComboBox box)
{
box.Items.Clear();
live.DeviceGetCount(0, type, out int count);
for (int i = 0; i < count; i++)
{
live.DeviceGetByIndex(0, type, i, out string name, out string description);
box.Items.Add(name);
}
}
private void FillDevices()
{
FillNames(m_live, "video", deviceBox);
}
private void DeviceBox_SelectedIndexChanged(object sender, EventArgs e)
{
m_live.DeviceSet("video", (string)deviceBox.SelectedItem, "");
FillNames(m_live, "video::line-in", inputBox);
formatBox.Items.Clear();
m_live.FormatVideoGetCount(eMFormatType.eMFT_Input, out int formats);
for (int i = 0; i < formats; i++)
{
m_live.FormatVideoGetByIndex(eMFormatType.eMFT_Input, i, out M_VID_PROPS props, out string formatName);
formatBox.Items.Add(formatName);
}
}
private void InputBox_SelectedIndexChanged(object sender, EventArgs e)
{
m_live.DeviceSet("video::line-in", (string)inputBox.SelectedItem, "");
}
private void FormatBox_SelectedIndexChanged(object sender, EventArgs e)
{
m_live.FormatVideoGetByIndex(eMFormatType.eMFT_Input, formatBox.SelectedIndex, out M_VID_PROPS props, out string formatName);
m_live.FormatVideoSet(eMFormatType.eMFT_Input, ref props);
}
An audio device is chosen the same way with the type "audio".
Start the capture
private void StartButton_Click(object sender, EventArgs e)
{
m_live.PreviewWindowSet("", previewPanel.Handle.ToInt64());
m_live.PreviewEnable("", 1, 1);
m_live.ObjectStart(null); // a live source starts without a source of its own
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
m_live.ObjectClose(); // stops the capture and frees the device
Marshal.ReleaseComObject(m_live);
base.OnFormClosed(e);
}
Next steps
- Send the captured video to an output: MPlatform 3: Sending video to a device.
- The SDK sample
Live Source Samplecontains this code with audio devices, delay and an output.
Still have questions?
Tell us what didn’t work or what you’d like clarified.