senggen
senggen

Reputation: 41

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);
    }
}

Upvotes: 1

Views: 689

Answers (2)

Roman Ryltsov
Roman Ryltsov

Reputation: 69632

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.

Upvotes: 4

Rachel
Rachel

Reputation: 1370

Try this:

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

Upvotes: 0

Related Questions