dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Directshow - Previewing Video

  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    320

    Directshow - Previewing Video

    I'm following this MSDN tutorial to preview video from my webcam.

    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    I wrote the code but it's crashing on
    Code:
    pGraph->AddFilter(pCap, L"Capture Filter");
    so I dont have error code to know why.

    What am I doing wrong?

    Code:
    #include <Windows.h>
    #include <dshow.h>
    #include <iostream>
    using namespace std;
    
    #pragma comment(lib,"Strmiids.lib")
    
    HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum);
    HRESULT InitCaptureGraphBuilder(IGraphBuilder *pGraph,
                            ICaptureGraphBuilder2 *pBuild);
                            
    int main()
    {
        HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
        if (SUCCEEDED(hr))
        {
            cout << " CoInitializeEx " << hex << hr << endl;
            IGraphBuilder *pGraph;
            ICaptureGraphBuilder2 *pBuild; // Capture Graph Builder
            IBaseFilter *pCap; // Video capture filter.
    
            /// Enum devices
            IEnumMoniker *pEnum;
            IMoniker *pMoniker = NULL;
            hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
            if (SUCCEEDED(hr))
            {
                /// Find webcam
                if (pEnum->Next(1, &pMoniker, NULL) != S_OK)
                {
                    cout << " pEnum->Next failed " << endl;
                }
            }
            else
            {
                cout << " EnumerateDevices " << hex << hr << endl;
            }
    
            if (SUCCEEDED(hr))
            {
                hr = pMoniker->BindToObject(0, 0,
                                            IID_IBaseFilter,
                                            (void**)&pCap);
                cout << " BindToObject " << hex << hr << endl;
            }
    
            hr=InitCaptureGraphBuilder(pGraph,pBuild);
    
            if (SUCCEEDED(hr))
            {
                hr = pGraph->AddFilter(pCap, L"Capture Filter");
                cout << " AddFilter " << hex << hr << endl;
    
                if (SUCCEEDED(hr))
                {
                    hr = pBuild->RenderStream(&PIN_CATEGORY_PREVIEW,
                                              &MEDIATYPE_Video,
                                              pCap,
                                              NULL, NULL);
                    cout << " RenderStream " << hex << hr << endl;
                }
            }
            else
            {
                cout << " InitCaptureGraphBuilder " << hex << hr << endl;
            }
    
            CoUninitialize();
            // The caller must release both interfaces.
            pGraph->Release();
            pBuild->Release();
        }
        //getchar();
        return 0;
    }
    
    HRESULT InitCaptureGraphBuilder(IGraphBuilder *pGraph,
                            ICaptureGraphBuilder2 *pBuild)
    {
    
        // Create the Capture Graph Builder.
        HRESULT hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL,
                                      CLSCTX_INPROC_SERVER,
                                      IID_ICaptureGraphBuilder2,
                                      (void**)&pBuild );
        if (SUCCEEDED(hr))
        {
            // Create the Filter Graph Manager.
            hr = CoCreateInstance(CLSID_FilterGraph, 0,
                                  CLSCTX_INPROC_SERVER,
                                  IID_IGraphBuilder,
                                  (void**)&pGraph);
            if (SUCCEEDED(hr))
            {
                // Initialize the Capture Graph Builder.
                pBuild->SetFiltergraph(pGraph);
                return S_OK;
            }
            else
            {
                // The caller must release both interfaces.
                pBuild->Release();
            }
        }
        return hr; // Failed
    }
    
    
    HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum)
    {
        // Create the System Device Enumerator.
        ICreateDevEnum *pDevEnum;
        HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,
                                      NULL,
                                      CLSCTX_INPROC_SERVER,
                                      IID_PPV_ARGS(&pDevEnum));
    
        if (SUCCEEDED(hr))
        {
            // Create an enumerator for the category.
            hr = pDevEnum->CreateClassEnumerator(category, ppEnum, 0);
            if (hr == S_FALSE)
            {
                hr = VFW_E_NOT_FOUND;  // The category is empty. Treat as an error.
            }
            pDevEnum->Release();
        }
        return hr;
    }
    Last edited by MasterDucky; January 30th, 2015 at 11:54 AM.

  2. #2
    Join Date
    Feb 2015
    Posts
    8

    Re: Directshow - Previewing Video

    A translation of the DirectShow PlayCap program to show how this would appear in c#.

    This application creates a preview window for the first video capture device that it locates on the user's system (if any). It demonstrates a simple example of using the ICaptureGraphBuilder2 and ICreateDevEnum interfaces to build a capture graph.

    public IBaseFilter FindCaptureDevice()
    {
    // ...
    if (classEnum.Next (moniker.Length, moniker, IntPtr.Zero) == 0)
    {
    // Bind Moniker to a filter object
    Guid iid = typeof(IBaseFilter).GUID;
    moniker[0].BindToObject(null, null, ref iid, out source);

  3. #3
    Join Date
    Dec 2007
    Location
    France
    Posts
    320

    Re: Directshow - Previewing Video

    Thanks Addy!

  4. #4
    Join Date
    Jan 2015
    Posts
    3

    Re: Directshow - Previewing Video

    Recently I've had similar problem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)