1

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);
    }
}
Roman R.
  • 68,205
  • 6
  • 94
  • 158
senggen
  • 41
  • 3

2 Answers2

4

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.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Is there any way to detect the resolution changed and does not interrupt the capturing like amcap. – senggen May 24 '13 at 09:57
  • AMCap is available in source code, so you can look it up. To my best knowledg it rebuilds and restarts the graph and does not update resolution dynamically. – Roman R. May 24 '13 at 13:01
0

Try this:

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

Rachel
  • 1,372
  • 1
  • 8
  • 11
  • I want to know how to check the resolution is changed, while the samplegrabber capturing. – senggen May 24 '13 at 08:57
  • 1
    >If the resolution changed from 1920x1080 to 1680x1050, the value you get is 1920x1080 always. How did you change the resolution? And how do you know it's changed successfully? – Rachel May 24 '13 at 09:05
  • Yes, that's my question, how do I know the resolution is changed. – senggen May 24 '13 at 09:10
  • You said you tested your current code but didn't get the correct resolution result after you change it. I meant to know how you did the testing... – Rachel May 24 '13 at 09:13
  • I know what confused you, the source resolution changed by other not by code. And I want to know when the resolution is changed from samplegrabber. – senggen May 24 '13 at 09:15