How to add/remove some filters to graph at runtime?

三世轮回 提交于 2020-01-06 08:50:33

问题


I want to add Compressor, Avi Muxer, and File Writer to my graph, at runtime. I`ve added an InfTee to my graph like this:

            IBaseFilter sourceTee = (IBaseFilter)new InfTee();
            graphBuilder.AddFilter(sourceTee, "Infinite Tee");
            outPin = DsFindPin.ByDirection(theVideoDevice, PinDirection.Output, 0);
            inPin = DsFindPin.ByDirection(sourceTee, PinDirection.Input, 0);
            hr = graphBuilder.Connect(outPin, inPin); 

But when I try to get output on a button click like the below code, I get an error.

    private void button1_Click(object sender, EventArgs e)
    {

        IPin outPin, inPin;
        int hr;


        // Connect To Compressor
        outPin = DsFindPin.ByDirection(Preview_Class.smartTeeFilter, PinDirection.Output ,1);
        inPin = DsFindPin.ByDirection(Preview_Class.theVideoCompressor, PinDirection.Input, 0);
        hr = Preview_Class.graphBuilder.Connect(outPin, inPin);
        DsError.ThrowExceptionForHR(hr);
        // etc
        }

I must note that I am getting an output pin 0 for my video renderer at preview time, but I want to add recording feature by clicking a button.

Any help would be appreciated.


回答1:


Short answer

When filter graph is running, you cannot add/remove filters, you cannot connect/disconnect pins. The reason behind your inability to do it, is that the actions assume stopped state of the filters, and filter graph state transitions assume that all filters in the graph change state with the graph itself.

Longer answer

MSDN/DirectShow adds flexibility of changing topology while running using Dynamic Reconnection approach. While the algorithm is defined pretty well, stock filters and third party filters rarely implement it. That is, this entire section of DirectShow is more a hint to developers "how you can implement it yourself in your own filter if you long for that".

C# code is the consumer of DirectShow technology and deals with what is de facto available, and it is not the dynamic reconnection. A typical approach would be bridging (see Wimmel's comment above, and search the forum - it is mentioned many times).

See also:

  • Reconnect Directshow Filter Pin
  • Directshow continious capture
  • GMFBridge usage in DirectShow


来源:https://stackoverflow.com/questions/25834627/how-to-add-remove-some-filters-to-graph-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!