重新連線您的輸入,以確保特定輸出類型
[與此頁面相關聯的功能,DirectShow是舊版功能。 它已被 MediaPlayer、IMFMediaEngine以及媒體基金會中的音訊/視訊擷取所取代。 這些功能已針對 Windows 10 和 Windows 11 進行優化。 Microsoft強烈建議新程式代碼盡可能在媒體 基礎中使用 MediaPlayer、IMFMediaEngine 和 音訊/視訊擷取,而不是 DirectShow。 Microsoft建議使用舊版 API 的現有程式代碼,盡可能改寫成使用新的 API。]
篩選會實作 IAMStreamConfig::SetFormat 方法,以在連接篩選的針腳之前設定音訊或視訊格式。 如果您的輸出端點已連接,並且您可以提供新的類型,請重新連接您的端點,但前提是其他過濾器能夠接受新的類型。 如果另一個篩選無法接受媒體類型,則使對 SetFormat 的呼叫失敗,並保持連線不變。
轉換過濾器不會有任何偏好的輸出類型,除非其輸入端子已連接。 在此情況下,SetFormat 和 IAMStreamConfig::GetStreamCaps 方法應該傳回 VFW_E_NOT_CONNECTED,直到輸入接腳連線為止。 否則,這些方法可以照常運作。
在某些情況下,當您在已建立的連線上提供或支援格式時,重新連接接腳會很有用。 例如,假設濾波器可以將 24 位元 RGB 視訊壓縮成 X 格式,並且可以將 8 位元 RGB 視訊壓縮成格式 Y。輸出接腳可以執行以下兩項中的一項:
- 請在 GetStreamCaps中始終提供 X 和 Y,並在 SetFormat中始終接受 X 和 Y。
- 如果輸入類型為 24 位 RGB,則只接受和使用 X 格式。 如果輸入類型為 8 位 RGB,則僅支援且接受 Y 格式。 如果輸入針腳未連接,這兩種方法都會失敗。
不論是哪一種情況,您都需要一些看起來像這樣的重連代碼:
HRESULT MyOutputPin::CheckMediaType(const CMediaType *pmtOut)
{
// Fail if the input pin is not connected.
if (!m_pFilter->m_pInput->IsConnected()) {
return VFW_E_NOT_CONNECTED;
}
// (Not shown: Reject any media types that you know in advance your
// filter cannot use. Check the major type and subtype GUIDs.)
// (Not shown: If SetFormat was previously called, check whether
// pmtOut exactly matches the format that was specified in SetFormat.
// Return S_OK if they match, or VFW_E_INVALIDMEDIATYPE otherwise.)
// Now do the normal check for this media type.
HRESULT hr;
hr = m_pFilter->CheckTransform(
&m_pFilter->m_pInput->CurrentMediaType(), // The input type.
pmtOut // The proposed output type.
);
if (hr == S_OK)
{
// This format is compatible with the current input type.
return S_OK;
}
// This format is not compatible with the current input type.
// Maybe we can reconnect the input pin with a new input type.
// Enumerate the upstream filter's preferred output types, and
// see if one of them will work.
CMediaType *pmtEnum;
BOOL fFound = FALSE;
IEnumMediaTypes *pEnum;
hr = m_pFilter->m_pInput->GetConnected()->EnumMediaTypes(&pEnum);
if (hr != S_OK)
{
return E_FAIL;
}
while (hr = pEnum->Next(1, (AM_MEDIA_TYPE **)&pmtEnum, NULL), hr == S_OK)
{
// Check this input type against the proposed output type.
hr = m_pFilter->CheckTransform(pmtEnum, pmtOut);
if (hr != S_OK)
{
DeleteMediaType(pmtEnum);
continue; // Try the next one.
}
// This input type is a possible candidate. But, we have to make
// sure that the upstream filter can switch to this type.
hr = m_pFilter->m_pInput->GetConnected()->QueryAccept(pmtEnum);
if (hr != S_OK)
{
// The upstream filter will not switch to this type.
DeleteMediaType(pmtEnum);
continue; // Try the next one.
}
fFound = TRUE;
DeleteMediaType(pmtEnum);
break;
}
pEnum->Release();
if (fFound)
{
// This output type is OK, but if we are asked to use it, we will
// need to reconnect our input pin. (See SetFormat, below.)
return S_OK;
}
else
{
return VFW_E_INVALIDMEDIATYPE;
}
}
HRESULT MyOutputPin::SetFormat(AM_MEDIA_TYPE *pmt)
{
CheckPointer(pmt, E_POINTER);
HRESULT hr;
// Hold the filter state lock, to make sure that streaming isn't
// in the middle of starting or stopping:
CAutoLock cObjectLock(&m_pFilter->m_csFilter);
// Cannot set the format unless the filter is stopped.
if (m_pFilter->m_State != State_Stopped)
{
return VFW_E_NOT_STOPPED;
}
// The set of possible output formats depends on the input format,
// so if the input pin is not connected, return a failure code.
if (!m_pFilter->m_pInput->IsConnected())
{
return VFW_E_NOT_CONNECTED;
}
// If the pin is already using this format, there's nothing to do.
if (IsConnected() && CurrentMediaType() == *pmt)
{
return S_OK;
}
// See if this media type is acceptable.
if ((hr = CheckMediaType((CMediaType *)pmt)) != S_OK)
{
return hr;
}
// If we're connected to a downstream filter, we have to make
// sure that the downstream filter accepts this media type.
if (IsConnected())
{
hr = GetConnected()->QueryAccept(pmt);
if (hr != S_OK)
{
return VFW_E_INVALIDMEDIATYPE;
}
}
// Now make a note that from now on, this is the only format allowed,
// and refuse anything but this in the CheckMediaType code above.
// Changing the format means reconnecting if necessary.
if (IsConnected())
{
m_pFilter->m_pGraph->Reconnect(this);
}
return NOERROR;
}
// Override CTransformFilter::SetMediaType to reconnect the input pin.
// This method is called immediately after the media type is set on a pin.
HRESULT MyFilter::SetMediaType(
PIN_DIRECTION direction,
const CMediaType *pmt
)
{
HRESULT hr;
if (direction == PINDIR_OUTPUT)
{
// Before we set the output type, we might need to reconnect
// the input pin with a new type.
if (m_pInput && m_pInput->IsConnected())
{
// Check if the current input type is compatible.
hr = CheckTransform(
&m_pInput->CurrentMediaType(),
&m_pOutput->CurrentMediaType());
if (SUCCEEDED(hr))
{
return S_OK;
}
// Otherwise, we need to reconnect the input pin.
// Note: The CheckMediaType method has already called
// QueryAccept on the upstream filter.
hr = m_pGraph->Reconnect(m_pInput);
return hr;
}
}
return S_OK;
}