Microsoft DirectX 8.1 (C++) |
You must configure the Sample Grabber for the desired media type before you connect it to any other filters. First call CoCreateInstance to create the Sample Grabber, and IFilterGraph::AddFilter to add it to the filter graph. Then query the Sample Grabber for the ISampleGrabber interface and call the ISampleGrabber::SetMediaType method. This method specifies the media type that the Sample Grabber will use to connect. You can specify just the major type; or the major type plus the subtype; or the major type, subtype, and format type. (For more information, see AM_MEDIA_TYPE.)
For example, if you want to grab an uncompressed video frame that is compatible with the current display mode, set the major type to MEDIATYPE_Video and set the subtype based on the current bit depth of the display:
// Create the Sample Grabber.
IBaseFilter *pF = NULL;
ISampleGrabber *pGrabber = NULL;
hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, reinterpret_cast<void**>(&pF));
hr = pF->QueryInterface(IID_ISampleGrabber,
reinterpret_cast<void**>(&pGrabber));
hr = pGraph->AddFilter(pF, L"SampleGrabber");
// Find the current bit depth.
HDC hdc = GetDC(NULL);
int iBitDepth = GetDeviceCaps(hdc, BITSPIXEL);
ReleaseDC(NULL, hdc);
// Set the media type.
AM_MEDIA_TYPE mt;
ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
mt.majortype = MEDIATYPE_Video;
switch (iBitDepth)
{
case 8:
mt.subtype = MEDIASUBTYPE_RGB8;
break;
case 16:
mt.subtype = MEDIASUBTYPE_RGB555;
break;
case 24:
mt.subtype = MEDIASUBTYPE_RGB24;
break;
case 32:
mt.subtype = MEDIASUBTYPE_RGB32;
break;
default:
return E_FAIL;
}
hr = pGrabber->SetMediaType(&mt);