我如何让IAMStreamConfig工作?

时间:2013-07-17 06:37:12

标签: c++ directshow

我正在尝试使用IAMStreamConfig,但无论我把它放在哪里

int iCount = 0, iSize = 0;
hr = pConfig->GetNumberOfCapabilities(&iCount, &iSize);

// Check the size to make sure we pass in the correct structure.
if (iSize == sizeof(VIDEO_STREAM_CONFIG_CAPS))
{
    // Use the video capabilities structure.

    for (int iFormat = 0; iFormat < iCount; iFormat++)
    {
        VIDEO_STREAM_CONFIG_CAPS scc;
        AM_MEDIA_TYPE *pmtConfig;
        hr = pConfig->GetStreamCaps(iFormat, &pmtConfig, (BYTE*)&scc);
        if (SUCCEEDED(hr))
        {
            /* Examine the format, and possibly use it. */
            cout << scc.CropAlignX << endl;
            cout << scc.CropAlignY << endl;
            // Delete the media type when you are done.
            DeleteMediaType(pmtConfig);
        }
}

在我的代码中查找CropAlignX和CropAlignY。它编译,但我得到一个例外,程序崩溃。这就是我的代码看起来的样子,有人可以告诉我在哪里插入代码以使其工作吗?

int main(int argc, char **argv)
{
// Intialise COM
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (hr != S_OK)
   exit_message("Could not initialise COM", 7);

hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder,
            (void**)&pGraph);
if (hr != S_OK)
    exit_message("Could not create filter graph", 8);

hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2,
        (void **)&pBuilder);
if (hr != S_OK)
    exit_message("Could not create capture graph builder", 9);

hr = pBuilder->SetFiltergraph(pGraph);
if (hr != S_OK)
    exit_message("Could not attach capture graph builder to graph", 10);

hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum));
if (hr != S_OK)
    exit_message("Could not create system device enumerator", 11);

// Video input device enumerator
hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0);
if (hr != S_OK)
    exit_message("No video devices found", 12);

VARIANT var;
n = 0;
while(1)
{
    // Access next device
    hr = pEnum->Next(1, &pMoniker, NULL);
    if (hr == S_OK)
    {
        n++; // increment device count
    }
    else
    {
        if (device_number == 0)
        {
            fprintf(stderr,
                "Video capture device %s not found\n",
                device_name);
        }
        else
        {
            fprintf(stderr,
                "Video capture device %d not found\n",
                device_number);
        }
        exit_message("", 14);
    }
    else if (n >= device_number) break;
}

hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
VariantInit(&var);
hr = pPropBag->Read(L"FriendlyName", &var, 0);
VariantClear(&var);

hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pCap);
if (hr != S_OK) exit_message("Could not create capture filter", 16);

hr = pGraph->AddFilter(pCap, L"Capture Filter");
if (hr != S_OK) exit_message("Could not add capture filter to graph", 17);

hr = CoCreateInstance(CLSID_SampleGrabber, NULL,CLSCTX_INPROC_SERVER, IID_IBaseFilter,
        (void**)&pSampleGrabberFilter);
if (hr != S_OK)
    exit_message("Could not create Sample Grabber filter", 18);

hr = pSampleGrabberFilter->QueryInterface(IID_ISampleGrabber, (void**)&pSampleGrabber);
if (hr != S_OK)
    exit_message("Could not get ISampleGrabber interface to sample grabber filter", 19);

hr = pSampleGrabber->SetBufferSamples(TRUE);
if (hr != S_OK)
    exit_message("Could not enable sample buffering in the sample grabber", 20);

AM_MEDIA_TYPE mt;
ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;

hr = pSampleGrabber->SetMediaType((_AMMediaType *)&mt);
if (hr != S_OK)
    exit_message("Could not set media type in sample grabber", 21);
//////////////// IID_IAMStreamConfig ////////////////////////////////////////////////////
    hr = pBuilder->FindInterface(&PIN_CATEGORY_PREVIEW, 0, pCap, IID_IAMStreamConfig, (void**)&pConfig);

hr = pGraph->AddFilter(pSampleGrabberFilter, L"SampleGrab");
if (hr != S_OK)
    exit_message("Could not add Sample Grabber to filter graph", 22);

hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter,
        (void**)&pNullRenderer);
if (hr != S_OK)
    exit_message("Could not create Null Renderer filter", 23);
/*
hr = pBuilder->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,
            pCap, NULL, NULL);
    if (hr != S_OK && hr != VFW_S_NOPREVIEWPIN)
        exit_message("Could not render preview video stream", 26);
*/

hr = pGraph->AddFilter(pNullRenderer, L"NullRender");
if (hr != S_OK)
    exit_message("Could not add Null Renderer to filter graph", 24);

hr = pBuilder->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
        pCap,  pSampleGrabberFilter, pNullRenderer);
if (hr != S_OK)
    exit_message("Could not render capture video stream", 25);

hr = pGraph->QueryInterface(IID_IMediaControl, (void**)&pMediaControl);
if (hr != S_OK) exit_message("Could not get media control interface", 27);

while(1)
{
    hr = pMediaControl->Run();

    // Hopefully, the return value was S_OK or S_FALSE
    if (hr == S_OK) break; // graph is now running
    if (hr == S_FALSE) continue; // graph still preparing to run

    fprintf(stderr, "Error: %u\n", hr);
    exit_message("Could not run filter graph", 28);

}

long buffer_size = 0;
while(true)
{
    if ( (cvWaitKey(10) & 255) == 27 ) break;
    while(1)
    {
        hr = pSampleGrabber->GetCurrentBuffer(&buffer_size, NULL);

        if (hr == S_OK && buffer_size != 0) break;

        if (hr != S_OK && hr != VFW_E_WRONG_STATE)
            exit_message("Could not get buffer size", 29);
    }

    pBuffer = new char[buffer_size];
    if (!pBuffer)
        exit_message("Could not allocate data buffer for image", 30);

    hr = pSampleGrabber->GetCurrentBuffer(&buffer_size, (long*)pBuffer);
    if (hr != S_OK)
        exit_message("Could not get buffer data from sample grabber", 31);

    hr = pSampleGrabber->GetConnectedMediaType((_AMMediaType *)&mt);
    if (hr != S_OK)
        exit_message("Could not get media type", 32);

    VIDEOINFOHEADER *pVih = NULL;
    if ((mt.formattype == FORMAT_VideoInfo) && (mt.cbFormat >= sizeof(VIDEOINFOHEADER)) && (mt.pbFormat != NULL))
    {
        pVih = (VIDEOINFOHEADER*)mt.pbFormat;

        long cbBitmapInfoSize = mt.cbFormat - SIZE_PREHEADER;
        BITMAPFILEHEADER bfh;
        ZeroMemory(&bfh, sizeof(bfh));
        bfh.bfType = 'MB'; // Little-endian for "BM".
        bfh.bfSize = sizeof(bfh) + buffer_size + cbBitmapInfoSize;
        bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + cbBitmapInfoSize;

..Blah blah..

修改: 附加调用堆栈

0 个答案:

没有答案