IMediaControl.Run() is successful still camera preview is black

814 views Asked by At

By using Directshow.NET and c# I have developed an application which shows camera preview. Everything is working fine since 1 year but suddenly client complaint about black camera preview.

After some digging to issue I come to know that anti-virus program is blocking camera usages by my application. After adding my application in exclusion list of that anti-virus program makes application to work as like before.

In program I have properly thrown exception for HRESULT like below:

try
{
    //FilterGraph creation
    //CaptureGraphBuilder2 creation
    //AddSourceFilterForMoniker()
    //SampleGrabber configuration
    //VMR9 configuration

    hr = mediaControl.Run();
    DsError.ThrowExceptionForHR(hr);
}
catch(Exception ex)
{
    //logging part
}

Still there is nothing in log, even I debug that application on that particular machine but program executes successfully. So the question is, To avoid such issues how can I make sure that camera preview is actually started? Is there any way to check that stream is not blocked.

3

There are 3 answers

6
Roman Ryltsov On BEST ANSWER

Black camera preview is not necessarily a symptom of a failure to run the graph. For example, whether this is your problem exactly or not, a filter graph esp. displaying IP camera feed might be put into running state and the video renderer would expect a frame to be received and processed respectively to the state it's display ready. However if an antivirus or a firewall blocks traffic, the video frame never reaches the renderer. The filter graph is running though and there is no failure or error.

You normally want to request some statistics from filters in your filter graph, that prove there is payload processing happening. This might include:

  • a query from source filter that it actually produces data
  • an intermediate component such as decoder capable to provide stats of processed data
  • a Sample Grabber filter in the pipeline with a callback counting samples without modification
  • a request of statistics from video renderer (such as e.g. IQualProp::get_FramesDrawn "...retrieves the number of frames actually drawn since streaming started")
  • an attempt to read back a snapshot of currently displayed video frame from video renderer

Once you see the data is being processed as the time goes, you know there is no unexpected locking happens.

0
xiaox2y2 On

You can subscribe the event MediaElementBase.NewAllocatorFrame, and you can know whether the camera source filter is working.

0
Bafro On

I suggest You to Adds a DirectShow filter graph to the Running Object Table, allowing GraphEdit to "spy" on a remote filter graph. This can help to check graph connections, filter state, pin properties and correct or optimize it in order resolve issues.

After building and/or starting capture graph just call this method in order to se graph and filter state via Graphedit application.

///
/// \fn AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister)
/// 
HRESULT CCapture::AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister)
{
    IMoniker * pMoniker;
    IRunningObjectTable *pROT;
    WCHAR wsz[128];
    HRESULT hr;
    ULONG ret = 0;

    if (FAILED(GetRunningObjectTable(0, &pROT)))
        return E_FAIL;

    wsprintfW(wsz, L"FilterGraph %08x pid %08x", (DWORD_PTR)pUnkGraph,
        GetCurrentProcessId());

    hr = CreateItemMoniker(L"!", wsz, &pMoniker);
    if (SUCCEEDED(hr)) {
        hr = pROT->Register(0, pUnkGraph, pMoniker, pdwRegister);
        ret = pMoniker->Release();
    }   
    pROT->Release();
    return hr;
}

GraphEdit is available in the Microsoft Windows Software Development Kit (SDK) (http://go.microsoft.com/fwlink/p/?linkid=62332).