Adding the same filter multiple times to a DirectShow Graph
the problem I have is a bit difficult to explain without first explaining what I'm trying to do so I will start with that. I'm trying to grab samples from multiple video streams using the Sample Grabber + Null Renderer filter combination in Directshow. The input sources can be anything from a webcam to a video file to a URL. I know how to do this for a single input source, get the IBaseFilter of the input source and then use CoCreateInstance() to get the IBaseFilter pointers for a sample grabber and null renderer :
HRESULT hr = CoCreateInstance(CLSID_Sam开发者_开发技巧pleGrabber,
NULL,
CLSCTX_INPROC,
IID_IBaseFilter,
(void **)&sample_grabber_filter);
if(FAILED(hr)) {
printf("(Fatal) Error setting up Sample Grabber.\n");
return hr;
}
hr = CoCreateInstance(CLSID_NullRenderer,
NULL,
CLSCTX_INPROC,
IID_IBaseFilter,
(void **)&null_renderer);
if(FAILED(hr)) {
printf("(Fatal) Error seeting up Null Renderer.\n");
return hr;
}
I can then use the FilterGraph::AddFilter() function to add all 3 filters and use another interface like CaptureGraphBuilder2 to render the stream. But what happens when I want to render from multiple sources simultaneously? I can add all the source filters to the graph but what about the Sample Grabber and Null Renderer filters to complete the graph for each video stream? Can I do something like:
IGraphBuilder *graph_builder;
ICaptureGraphBuilder2 *cap_graph;
IMediaControl *media_control;
// ... set up graph_builder and cap_graph and media_control
cap_graph->AddFilterGraph(graph_builder);
IBaseFilter *new_source;
wchar_t *source_name; // Allocate some memory
while(ScanForSource(&new_source, &source_name)) {
graph_builder->AddFilter(new_source, source_name);
graph_builder->AddFilter(sample_grabber_filter, new_sg_name);
graph_builder->AddFilter(null_renderer, new_nr_name);
cap_graph->RenderStream(&PIN_CATEGORY_PREVIEW,
&MEDIATYPE_Video, new_source, sample_grabber_filter, null_renderer);
}
Would something like the above implementation work? If not, then is there some other way to do this? Any help is much appreciated. Thanks!
Yes, you can create and insert into your graph many instances of sample grabber and renderers. Just don't forget to create them separately (call CoCreateInstance for each one), do not try to insert the same instance many times.
精彩评论