简体   繁体   中英

How to get the realtime resolution in directshow?

How to get the current resolution of SamleGrabber in DirectShow?

I tried the below code, it doesn't work. The value you get is always 1920x1080, while the source resolution changed from 1920x1080 to 1680x1050.

void GetCurrentResolution(ISampleGrabber* pGrabber, int* pWidth, int* pHeight) 
{
    AM_MEDIA_TYPE pmt = {0};
    hr = pGrabber->GetConnectedMediaType(&pmt);
    if (SUCCEEDED(hr)) 
    {
        if(pmt.formattype == FORMAT_VideoInfo) 
        {
            VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)pmt.pbFormat;
            *pWidth = pVih->bmiHeader.biWidth;
            *pHeight = pVih->bmiHeader.biHeight;
        }
        FreeMediaType(pmt);
    }
}

The code snippet you provided is about right. It is not accurate because it assumes things that does not have to happen, but in most cases it is going to work.

Your incorrect assumption is that resolution can change on a running graph. No it does not happen: Sample Grabber's media types on pins don't change once connected. If there is any need to re-agree resolution, you need to start with reconnecting pins, typically starting from upstream pins.

Try this:

When resolution changes: call pGraph->Stop(); then reget and call pGraph->Run()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM