#include <mil.h>
#include <dshow.h>
#include "MilDSFCapture.h"
#include "MILCapture.h"
MIL_INT MFTYPE GrabEndFunction(MIL_ID DigitizerId, MIL_INT HookType, void* UserDataPtr);
typedef struct
{
MIL_INT ProcessedImageCount;
} HookDataStruct;
int MosMain(void)
{
HRESULT hr = S_OK;
HookDataStruct grabEndHookData;
IMilCapture* pMilCapture = NULL;
IMilCapturePin* pMilCapturePin = NULL;
IBaseFilter* pVMR9filter = NULL;
IQualProp* pQualRenderer = NULL;
IGraphBuilder* pGraphBuilder = NULL;
IMediaControl* pMediaControl = NULL;
MosPrintf(MIL_TEXT("\nMatrox Capture DirectShow Filter:\n"));
MosPrintf(MIL_TEXT("---------------------------------\n\n"));
hr = CoInitialize(NULL);
if(SUCCEEDED(hr))
{
hr = CoCreateInstance( CLSID_FilterGraph,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGraphBuilder,
(void**)&pGraphBuilder);
if(SUCCEEDED(hr))
hr = pGraphBuilder->QueryInterface(IID_IMediaControl, (void**)&pMediaControl);
else
MosPrintf(MIL_TEXT("\nERROR\n\nFailed to initialize IMediaControl Interface\n"));
}
else
MosPrintf(MIL_TEXT("\nERROR\n\nFailed to initialize DirectShow FilterGraph Builder\n"));
CHECK_FOR_ERROR(hr);
hr = CoCreateInstance( CLSID_VideoMixingRenderer9,
NULL,
CLSCTX_INPROC_SERVER,
IID_IBaseFilter,
(void**)&pVMR9filter);
if(SUCCEEDED(hr))
{
pGraphBuilder->AddFilter(pVMR9filter, L"Video Mixing Renderer 9");
}
hr = CoCreateInstance( CLSID_MilCapture,
NULL,
CLSCTX_INPROC_SERVER,
IID_IMilCapture,
(void**)&pMilCapture);
if(SUCCEEDED(hr))
{
pMilCapture->setSystemInfo(MIL_TEXT("M_SYSTEM_DEFAULT"), M_DEFAULT);
pGraphBuilder->AddFilter(pMilCapture, NULL);
MosPrintf(MIL_TEXT("Matrox Capture Filter Added to the FilterGraph\n"));
IPin* pIPin = NULL;
pMilCapture->FindPin(L"default", &pIPin);
if ( pIPin )
hr = pIPin->QueryInterface(IID_IMilCapturePin, (void**)&pMilCapturePin);
else
{
hr = E_FAIL;
MosPrintf(MIL_TEXT("\nERROR\n\nFailed to find desired Pin\n"));
}
CHECK_FOR_ERROR(hr);
HELPER_RELEASE(pIPin);
pMilCapturePin->set_DigitizerInfo(M_DEFAULT, M_DEFAULT, MIL_TEXT("M_DEFAULT"));
pMilCapturePin->Set_CaptureBufferFormatPriority(CBF_MONO8, 0);
pMilCapturePin->Set_CaptureBufferFormatPriority(CBF_YUV16_YUYV, 1);
pMilCapturePin->Set_CaptureBufferFormatPriority(CBF_BGR32, 2);
pMilCapturePin->Set_PinMediaSubTypePriority(MF_RGB32, 0);
pMilCapturePin->Set_PinMediaSubTypePriority(MF_YUV16, 1);
hr = pGraphBuilder->Render(pMilCapturePin);
IPin* connectedPin = NULL;
PIN_INFO connectedPinInfo;
FILTER_INFO connectedPinFilterInfo;
pMilCapturePin->ConnectedTo(&connectedPin);
connectedPin->QueryPinInfo(&connectedPinInfo);
connectedPinInfo.pFilter->QueryFilterInfo(&connectedPinFilterInfo);
MosPrintf(MIL_TEXT("Pins are connected to render filter called (%s).\n\n"), connectedPinFilterInfo.achName);
MosPrintf(MIL_TEXT("**********************************************************************\n"));
MosPrintf(MIL_TEXT("Renderer performance can vary depending on your system specifications.\n"));
MosPrintf(MIL_TEXT("If you encounter any problem on the display,\n"));
MosPrintf(MIL_TEXT("we suggest using another renderer if available.\n"));
MosPrintf(MIL_TEXT("**********************************************************************\n\n"));
connectedPinInfo.pFilter->QueryInterface(IID_IQualProp, (void**)&pQualRenderer);
HELPER_RELEASE(connectedPinFilterInfo.pGraph);
HELPER_RELEASE(connectedPinInfo.pFilter);
HELPER_RELEASE(connectedPin);
grabEndHookData.ProcessedImageCount = 0;
pMilCapturePin->digHookFunction(M_GRAB_END, GrabEndFunction, &grabEndHookData);
MIL_INT sysDevNum = -1;
MIL_INT digCurChannelNum = NULL;
MIL_INT digChannelNum = NULL;
MIL_INT digSizeX = NULL;
MIL_INT digSizeY = NULL;
MIL_DOUBLE digScaleX = 0.0;
MIL_DOUBLE digScaleY = 0.0;
hr = pMilCapture->sysInquire(M_NUMBER, &sysDevNum);
MIL_INT sysDescriptorNameLen = 0;
hr = pMilCapture->sysInquire(M_SYSTEM_DESCRIPTOR_SIZE, &sysDescriptorNameLen);
MIL_TEXT_PTR sysDescriptorName = new MIL_TEXT_CHAR[sysDescriptorNameLen+2];
hr = pMilCapture->sysInquire(M_SYSTEM_DESCRIPTOR, sysDescriptorName);
MIL_INT digFormatLen = 0;
hr = pMilCapturePin->digInquire(M_FORMAT_SIZE, &digFormatLen);
MIL_TEXT_PTR digFormat = new MIL_TEXT_CHAR[digFormatLen+2];
hr = pMilCapturePin->digInquire(M_FORMAT, digFormat);
hr = pMilCapturePin->digInquire(M_CHANNEL_NUM, &digChannelNum);
hr = pMilCapturePin->digInquire(M_SIZE_X, &digSizeX);
hr = pMilCapturePin->digInquire(M_SIZE_Y, &digSizeY);
hr = pMilCapturePin->digInquire(M_GRAB_SCALE_X, &digScaleX);
hr = pMilCapturePin->digInquire(M_GRAB_SCALE_Y, &digScaleY);
MosPrintf(MIL_TEXT("\nSystem Information:\n"));
MosPrintf(MIL_TEXT("System descriptor: %s (%d)\n"), sysDescriptorName, sysDevNum);
MosPrintf(MIL_TEXT("Digitizer format: %s\n"), digFormat);
MosPrintf(MIL_TEXT("Total Number of Channel : %d\n"), digChannelNum);
MosPrintf(MIL_TEXT("Grab Size : %dx%d\n"), digSizeX, digSizeY);
MosPrintf(MIL_TEXT("Grab Scale X: %f \tY: %f\n"), digScaleX, digScaleY);
MosPrintf(MIL_TEXT("\n"));
delete [] sysDescriptorName;
delete [] digFormat;
}
else
{
MosPrintf(MIL_TEXT("\nERROR\n\nFailed to instantiate MilCaptureFilter\n"));
}
CHECK_FOR_ERROR(hr);
MosPrintf(MIL_TEXT("FilterGraph is running.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosPrintf(MIL_TEXT("| Buffer ID | Grab frame | Avg. | Frame dropped |\n"));
MosPrintf(MIL_TEXT("| used | count | frame rate | by renderer |\n"));
pMediaControl->Run();
bool endloop = false;
bool printedFrameRate = false;
while (!endloop)
{
if( !printedFrameRate && grabEndHookData.ProcessedImageCount % 10 == 0)
{
int framerate = 0;
int totalFrameDropped = 0;
if(pQualRenderer)
{
pQualRenderer->get_AvgFrameRate(&framerate);
pQualRenderer->get_FramesDroppedInRenderer(&totalFrameDropped);
MosPrintf(MIL_TEXT("| %10.2f | %13d |"), framerate/100.0, totalFrameDropped);
}
printedFrameRate = true;
}
if(printedFrameRate && grabEndHookData.ProcessedImageCount % 10 != 0)
{
printedFrameRate = false;
}
if(pVMR9filter)
{
MSG msg;
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
DispatchMessage(&msg);
}
}
if(MosKbhit())
{
endloop = true;
}
}
pMediaControl->Stop();
HELPER_RELEASE(pVMR9filter);
CleanUpDirectShow(&pMilCapture, &pMilCapturePin, &pGraphBuilder, &pMediaControl);
return 0;
}
void CleanUpDirectShow( IMilCapture** pMilCapture, IMilCapturePin** pMilCapturePin, IGraphBuilder** pGraphBuilder, IMediaControl** pMediaControl )
{
HELPER_RELEASE((*pMilCapturePin));
HELPER_RELEASE((*pMilCapture))
HELPER_RELEASE((*pMediaControl));
HELPER_RELEASE((*pGraphBuilder));
CoUninitialize();
}
MIL_INT MFTYPE GrabEndFunction(MIL_ID DigitizerId, MIL_INT HookType, void* UserDataPtr)
{
HookDataStruct* pUserData = (HookDataStruct*)UserDataPtr;
MIL_ID ModifiedBufferId = M_NULL;
if(HookType != M_NULL)
{
MdigGetHookInfo(HookType, M_MODIFIED_BUFFER+M_BUFFER_ID, &ModifiedBufferId);
}
MosPrintf(MIL_TEXT("\r|%10d | %10d "), ModifiedBufferId, pUserData->ProcessedImageCount++);
return 0;
}