[DirectShow] 024 - Dynamic Reconnection

In most DirectShow filters, pins cannot be reconnected while the graph is actively streaming data. The application must stop the graph before reconnecting the pins. However, some filters do support pin reconnections while the graph is running, a process known as dynamic reconnection. This can be done either by the application, or by a filter in the graph.

在大部分 DirectShow filter 中,当 graph 是活动的流数据时, pin 不能被再次连接。应用程序在再次连接 pin 之前必须停止 graph 。不过,一些 filter 支持在 graph 运行时再次连接 pin 。这一处理被看作动态重连。这个处理可以在应用程序实现,也可以在 filter 上实现。

One scenario for dynamic reconnection might be to remove Filter 2 from the graph, while the graph is running, and replace it with another filter. For this scenario to work, the following must be true:

一种方案是当 graph 运行时,从 graph 中移除 Filter2 ,用其他的 filter 代替。为了是这种方案有效,下列必须为真:

·         The input pin on Filter 3 (pin D) must support the IPinConnection interface. This interface enables the pin to be reconnected without stopping the filter.

·         The output pin on Filter 1 (pin A) must be able to block the flow of media data while the reconnection occurs. No data can travel between pin A and pin D during the reconnection. Generally, this means the output pin must support the IPinFlowControl interface. However, if Filter 1 is the filter that initiates the reconnection, it might have some internal mechanism to block its own data flow.

·         Filter3 input pin pin D )必须支持 IPinConnection 接口。这个接口可以在不停止 filter 的情况下重新连接 pin

·         Filter1 output pin pin A )在重连发生时能够阻塞媒体数据流。当重连的时候,没有数据能从 pin A pin D 。这就意味 output pin 必须支持 IPinFlowControl 接口。如果 Filter 1 是一个初始重连 filter ,那么它可以有一些阻塞自己数据流的内部机制。

The dynamic reconnection will involve the following steps:

动态连接按照下列步骤:

1. Block the data stream from pin A.

2. Reconnect pin A to pin D, possibly through a new intermediate filter.

3. Unblock pin A so that data starts to flow again.

1. pin A 阻塞数据流。

2. 连接 pin A pin D ,有可能通过新的中间 filter

3. 唤醒 pin A 让数据开始流动。

Step 1. Block the Data Stream

To block the data stream, call IPinFlowControl::Block on pin A. This method can be called either asynchronously or synchronously. To call the method asynchronously , create a Win32 event object and pass the event handle to the Block method. The method will return immediately. Wait for the event to be signaled, using a function such as WaitForSingleObject . The pin signals the event when it has blocked the data flow. For example:

pin A 上调用 IPinFlowControl::Block 来阻塞数据流。既可以同步也可以异步调用这个函数。异步调用的时候,创建一个 Win32 event 对象,把这个 event 的句柄作为 Block 函数的参数。这个方法会立即返回,调用类似 WaitForSingleObject 的函数等待 event 激活。当 pin 阻塞数据流的时候激活 event 。例子:

// Create an event

HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

if (hEvent != NULL)

{

    // Block the data flow.

    hr = pFlowControl->Block(AM_PIN_FLOW_CONTROL_BLOCK, hEvent);

    if (SUCCEEDED(hr))

    {

        // Wait for the pin to finish.

        DWORD dwRes = WaitForSingleObject(hEvent, dwMilliseconds);

    }

}

To call the method synchronously , simply pass the value NULL instead of the event handle. Now the method will block until the operation completes. This may not happen until the pin is ready to deliver a new sample. If the filter is paused, this can take an arbitrary length of time. Therefore, do not make the synchronous call from your main application thread. Use a worker thread, or else call the method asynchronously.

简单的传入一个 NULL 代替 event 句柄实现同步调用。这个函数阻塞知道操作完成。这些也可能不会发生,直到 pin 准备好传递一个新的 sample 。如果 filter 被暂停,有可能花费很长的时间。因此,不要在应用程序的主线程中同步调用。使用工作线程,或者异步调用。

Step 2. Reconnect the Pins

To reconnect the pins, query the Filter Graph Manager for the IGraphConfig interface and call either IGraphConfig::Reconnect or IGraphConfig::Reconfigure . The Reconnect method is simpler to use; it does the following:

要重新连接 pin ,请求 Filter Graph Manager IGraphConfig 接口并调用 IGraphConfig::Reconnect 或者 IGraphConfig::Reconfigure Reconnect 方法比较简单:

·         Stops the intermediate filters (Filter 2 in the example) and removes them from the graph.

·         Adds new intermediate filters, if needed.

·         Connects all the pins.

·         Pauses or runs any new filters, to match the state of the graph.

·         停止中间 filter (例子中的 Filter 2 )并从 graph 中移除。

·         如果需要,添加新的中间 filter

·         连接所有 pin

·         暂停或运行新的 filter ,匹配 graph 的状态。

The Reconnect method has several optional parameters that can be used to specify the media type for the pin connection and the intermediate filter to use. For example:

Reconnect 函数有一些附加参数用来指定 pin 连接的媒体类型和供中间 filter 使用:

pGraph->AddFilter(pNewFilter, L"New Filter for the Graph");

pConfig->Reconnect(

    pPinA,      // Reconnect this output pin...

    pPinD,      // ... to this input pin.

    pMediaType, // Use this media type.

    pNewFilter, // Connect them through this filter.

    NULL,

    0);     

For details, consult the reference page. If the Reconnect method is not flexible enough, you can use the Reconfigure method, which calls an application-defined callback method to reconnect the pins. To use this method, implement the IGraphConfigCallback interface in your application.

详细信息看参考。如果 Reconnect 函数不够灵活,可以使用 Reconfigure 函数,这个函数调用应用程序定义的回调函数连接 pin 。要使用这个方法,可以在应用程序中执行 IGraphConfigCallback 接口。

Before calling Reconfigure , block the data flow from the output pin, as described previously. Then push any data that is still pending in the section of the graph that you are reconnecting, as follows:

调用 Reconfigure 之前,阻塞 output pin 出来的数据流,就像前面描述的一样。

1. Call IPinConnection::NotifyEndOfStream on the input pin furthest downstream in the reconnection chain (pin D in the example). Pass in a handle to a Win32 event.

2. Call IPin::EndOfStream on the input pin that is immediately downstream from the output pin where you blocked the data flow. (In this example, the data flow was blocked at pin A, so you would call EndOfStream on pin B.)

3. Wait for the event to be signaled. The input pin (pin D) signals the event when it receives the end-of-stream notification. This indicates that no data is traveling between the pins, and that the caller can safely reconnect the pins.

1. 在重新连接的链上最下游的 input pin 上调用 IPinConnection::NotifyEndOfStream ,传递一个 Win32 event 句柄作为参数。

2. 在阻塞数据的 output pin 的直接下游的 input pin 上调用 IPin::EndOfStream

3. 等待 event 被激发,当 input pin(pin D) 接收到流结束通知时激发 event 。这表明在两个 pin 之间没有数据,可以安全的重新连接两个 pin

Note that the IGraphConfig::Reconnect method automatically handles the previous steps. You only need to perform these steps if you are using the Reconfigure method.

注意, IGraphConfig::Reconnect 自动处理之前的步骤。如果使用 Reconfigure 只需要执行这些步骤。

After the data is pushed through the graph, call Reconfigure and pass a pointer to your IGraphConfigCallback callback interface. The Filter Graph Manager will call the IGraphConfigCallback::Reconfigure method that you have provided.

数据被推入 graph 之后,调用 Reconfigure 并把指向你的 IGraphConfigCallback 回调接口的指针作为参数。 Filter Graph Manager 会调用你提供的 IGraphConfigCallback::Reconfigure 函数。

Step 3. Unblock the Data Flow

After you have reconnected the pins, unblock the data flow by calling IPinFlowControl::Block with a value of zero for the first parameter.

重新连接 pin 之后,调用 IPinFlowControl::Block 函数唤醒数据流,第一个参数传递 0

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值