使用釘選類別
[與此頁面相關聯的功能,DirectShow是舊版功能。 它已被 MediaPlayer、IMFMediaEngine和媒體基礎結構中的音訊/視訊擷取 取代。 這些功能已針對 Windows 10 和 Windows 11 進行優化。 Microsoft強烈建議新程式代碼盡可能在媒體 基礎中使用 MediaPlayer、IMFMediaEngine 和 音訊/視訊擷取,而不是 DirectShow。 Microsoft建議使用舊版 API 的現有程式代碼,盡可能改寫成使用新的 API。]
若要搜尋具有指定釘選類別的釘選篩選,您可以使用 ICaptureGraphBuilder2::FindPin 方法。 下列範例會搜尋影片預覽釘選:
int i = 0;
hr = pBuild->FindPin(
pFilter, // Pointer to a filter to search.
PINDIR_OUTPUT, // Which pin direction?
&PIN_CATEGORY_PREVIEW, // Which category? (NULL means "any category")
&MEDIATYPE_Video, // What media type? (NULL means "any type")
FALSE, // Must be connected?
i, // Get the i'th matching pin (0 = first match)
&pPin // Receives a pointer to the pin.
);
第一個參數是指向過濾器的 IBaseFilter 介面的指標。 接下來的三個參數會設定方向、針腳類別和媒體類型。 第五個參數中的值 FALSE 表示針腳可以連接或不連接。 (如需這些參數的確切定義,請參閱此方法的檔案。)如果方法找到相符的針腳,它會傳回 pPin 參數中 IPin 介面的指標。
雖然 FindPin 方法很方便,但您也可以視需要撰寫自己的協助程式函式。 若要判斷釘選的類別,請呼叫 IKsPropertySet::Get 方法,如 釘選屬性集主題中所述。
下列程式碼顯示一個輔助函式,用來檢查引腳是否符合指定的類別:
// Returns TRUE if a pin matches the specified pin category.
BOOL PinMatchesCategory(IPin *pPin, REFGUID Category)
{
BOOL bFound = FALSE;
IKsPropertySet *pKs = NULL;
HRESULT hr = pPin->QueryInterface(IID_PPV_ARGS(&pKs));
if (SUCCEEDED(hr))
{
GUID PinCategory;
DWORD cbReturned;
hr = pKs->Get(AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY, NULL, 0,
&PinCategory, sizeof(GUID), &cbReturned);
if (SUCCEEDED(hr) && (cbReturned == sizeof(GUID)))
{
bFound = (PinCategory == Category);
}
pKs->Release();
}
return bFound;
}
下一個範例是依類別搜尋釘選的函式,類似於 findPin方法:
// Finds the first pin that matches a specified pin category and direction.
HRESULT FindPinByCategory(
IBaseFilter *pFilter, // Pointer to the filter to search.
PIN_DIRECTION PinDir, // Direction of the pin.
REFGUID Category, // Pin category.
IPin **ppPin) // Receives a pointer to the pin.
{
*ppPin = 0;
HRESULT hr = S_OK;
BOOL bFound = FALSE;
IEnumPins *pEnum = 0;
IPin *pPin = 0;
hr = pFilter->EnumPins(&pEnum);
if (FAILED(hr))
{
goto done;
}
while (hr = pEnum->Next(1, &pPin, 0), hr == S_OK)
{
PIN_DIRECTION ThisPinDir;
hr = pPin->QueryDirection(&ThisPinDir);
if (FAILED(hr))
{
goto done;
}
if ((ThisPinDir == PinDir) &&
PinMatchesCategory(pPin, Category))
{
*ppPin = pPin;
(*ppPin)->AddRef(); // The caller must release the interface.
bFound = TRUE;
break;
}
SafeRelease(&pPin);
}
done:
SafeRelease(&pPin);
SafeRelease(&pEnum);
if (SUCCEEDED(hr) && !bFound)
{
hr = E_FAIL;
}
return hr;
}
下列程式碼會使用前述函式來搜尋過濾器上的視訊埠針腳:
IPin *pVP;
hr = FindPinByCategory(pFilter, PINDIR_OUTPUT,
PIN_CATEGORY_VIDEOPORT, &pVP);
if (SUCCEEDED(hr))
{
// Use pVP ...
// Release when you are done.
pVP->Release();
}
如需屬性集的詳細資訊,請參閱 Windows Driver Kit (WDK) 檔。
相關主題