This tutorial shows live video from a capture device with MFLive: an SDI or HDMI card, an NDI source or a
webcam. It uses the project from MFormats 1: Playing a file; the frame loop is the same,
only the source changes.
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
A capture object works in three steps: select the device, then its input line, then the video format of the input.
private readonly MFLiveClass m_live = new MFLiveClass();
private void FillDevices()
{
m_live.DeviceGetCount(eMFDeviceType.eMFDT_Video, out int count);
for (int i = 0; i < count; i++)
{
m_live.DeviceGetByIndex(eMFDeviceType.eMFDT_Video, i, out string name, out int busy);
deviceBox.Items.Add(busy != 0 ? name + " (busy)" : name);
}
}
private void DeviceBox_SelectedIndexChanged(object sender, EventArgs e)
{
m_live.DeviceSet(eMFDeviceType.eMFDT_Video, deviceBox.SelectedIndex, "");
// Input lines of the device, for example SDI or HDMI
inputBox.Items.Clear();
m_live.PropsOptionGetCount("line-in", out int lines);
for (int i = 0; i < lines; i++)
{
m_live.PropsOptionGetByIndex("line-in", i, out string line, out string help);
inputBox.Items.Add(line);
}
// Video formats of the input
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.PropsOptionSetByIndex("line-in", inputBox.SelectedIndex);
}
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);
}
A device that is used by another application is reported as busy. Many devices detect the input format themselves; set the format when the device cannot or when you need a specific one.
Show the video
The loop is the loop of the file tutorial with m_live as the source. A live source gives frames at the rate
of the signal, so SourceFrameGet waits for each new frame.
private void CaptureLoop(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
m_live.SourceFrameGet(-1, out MFFrame frame, "");
try
{
m_preview.ReceiverFramePut(frame, -1, "");
}
finally
{
Marshal.ReleaseComObject(frame);
}
}
}
Start the loop in a worker thread from startButton, as in the file tutorial. When the form closes, stop the
loop, then call m_live.DeviceClose() to free the device for other applications, and release
m_live with Marshal.ReleaseComObject.
Next steps
- Send the captured video to an output: MFormats 3: Sending video to a device.
- Device settings (captions, ANC data, timecode) are described in the articles about the supported hardware.
- The SDK sample
Tutorial Live Inputcontains this code as a complete application.
Still have questions?
Tell us what didn’t work or what you’d like clarified.