如何使用DirectShowLib捕获elgato视频流窗口?

时间:2015-06-20 12:39:20

标签: c# .net winforms directshow

我有elgato捕获设备连接到我的电脑,我正试图捕捉并观看elgato捕获设备的窗口。

我在谷歌搜索并找到了这个答案:

Can you use Elgato's HDMIComponent Game Capture HD as a video-in device in C#?

这是代码:

IFilterGraph2 graph;
ICaptureGraphBuilder2 captureGraph;
IBaseFilter elgatoFilter;
IBaseFilter smartTeeFilter;
IBaseFilter videoRendererFilter;
Size videoSize;

//Set the video size to use for capture and recording
videoSize = new Size(1280, 720);

//Initialize filter graph and capture graph
graph = (IFilterGraph2)new FilterGraph();
captureGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
captureGraph.SetFiltergraph(graph);
rot = new DsROTEntry(graph);

//Create filter for Elgato
Guid elgatoGuid = new Guid("39F50F4C-99E1-464A-B6F9-D605B4FB5918");
Type comType = Type.GetTypeFromCLSID(elgatoGuid);
elgatoFilter = (IBaseFilter)Activator.CreateInstance(comType);
graph.AddFilter(elgatoFilter, "Elgato Video Capture Filter");

//Create smart tee filter, add to graph, connect Elgato's video out to smart tee in
smartTeeFilter = (IBaseFilter)new SmartTee();
graph.AddFilter(smartTeeFilter, "Smart Tee");
IPin outPin = GetPin(PinDirection.Output, "Video", elgatoFilter);
IPin inPin = GetPin(PinDirection.Input, smartTeeFilter);
graph.Connect(outPin, inPin);

//Create video renderer filter, add it to graph, connect smartTee Preview pin to video renderer's input pin
videoRendererFilter = (IBaseFilter)new VideoRenderer();
graph.AddFilter(videoRendererFilter, "Video Renderer");
outPin = GetPin(PinDirection.Output, "Preview", smartTeeFilter);
inPin = GetPin(PinDirection.Input, videoRendererFilter);
graph.Connect(outPin, inPin);

//Render stream from video renderer
captureGraph.RenderStream(PinCategory.Preview, MediaType.Video, videoRendererFilter, null, null);

//Set the video preview to be the videoFeed panel
IVideoWindow vw = (IVideoWindow)graph;
vw.put_Owner(videoFeed.Handle);
vw.put_MessageDrain(this.Handle);
vw.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipSiblings | WindowStyle.ClipChildren);
vw.SetWindowPosition(0, 0, 1280, 720);

//Start the preview
mediaControl = graph as IMediaControl;
mediaControl.Run();

我在项目中创建了一个新表单并添加了DirectShowLib-2005 dll 然后我在新表格的顶部添加:

using DirectShowLib;

在构造函数添加全局变量之前:

IFilterGraph2 graph;
ICaptureGraphBuilder2 captureGraph;
IBaseFilter elgatoFilter;
IBaseFilter smartTeeFilter;
IBaseFilter videoRendererFilter;
Size videoSize;

然后在构造函数中添加了剩下的代码。 我现在收到的错误很少:

在这一行上,变量rot不存在:

rot = new DsROTEntry(graph);

在使用方法GetPin的四行中,GetPin方法不存在:

IPin outPin = GetPin(PinDirection.Output, "Video", elgatoFilter);
IPin inPin = GetPin(PinDirection.Input, smartTeeFilter);
outPin = GetPin(PinDirection.Output, "Preview", smartTeeFilter);
inPin = GetPin(PinDirection.Input, videoRendererFilter);

在这一行:

vw.put_Owner(videoFeed.Handle);

变量videoFeed不存在。

最后这两行:

mediaControl = graph as IMediaControl;
mediaControl.Run();

mediaControl不存在。

我缺少什么?

1 个答案:

答案 0 :(得分:0)

secure是您控制的主机视频。

videoFeed将是mediaControl类型的变量:

IMediaControl

IMediaControl mediaControl = graph as IMediaControl; mediaControl.Run(); this或类似的; GetPin不是强制性的,而是helps inspect filter graph externally使用GraphEdit或类似工具。