#include <mil.h>
#include "..\..\CommonWrapper\C++\FilterGraphWrapper.h"
#include "MilSampleGrabberWrapper.h"
void ProcessSample(MIL_ID bufID, AM_MEDIA_TYPE* pmt, void* pUserData);
typedef struct
{
MIL_ID MilImageDisp;
MIL_INT ProcessedImageCount;
} HookDataStruct;
int MosMain(void)
{
HRESULT hr = S_OK;
MIL_ID MilApp = M_NULL;
MIL_ID MilHostSystem = M_NULL;
MIL_ID MilDisplayImage = M_NULL;
MIL_ID MilDisplay = M_NULL;
FilterGraphWrapper WrapperFilterGraph;
MilSampleGrabberWrapper WrapperMilSampleGrabber;
HookDataStruct UserHookData;
MosPrintf(MIL_TEXT("Matrox Sample Grabber DirectShow filter example:\n"));
MosPrintf(MIL_TEXT("------------------------------------------------\n\n"));
MosPrintf(MIL_TEXT("This example will insert a video capture source to the graph\n"));
MosPrintf(MIL_TEXT("and using \"Matrox Sample Grabber\" filter between the source\n"));
MosPrintf(MIL_TEXT("and a \"Null Renderer\".\n"));
MosPrintf(MIL_TEXT("\"Matrox Sample Grabber\" filter will send a MIL buffer\n"));
MosPrintf(MIL_TEXT("to the registered callback function where it will be copied\n"));
MosPrintf(MIL_TEXT("to a MIL display.\n"));
MosPrintf(MIL_TEXT("\n"));
MappAlloc(M_DEFAULT, &MilApp);
MsysAlloc(M_SYSTEM_HOST, M_DEFAULT, M_DEFAULT, &MilHostSystem);
WrapperFilterGraph.InitGraph(CLSID_NullRenderer);
BOOL bCaptureFilterSelected = FALSE;
if (WrapperFilterGraph.IsInitialized())
{
if (FilterFormCategoryCount(CLSID_VideoInputDeviceCategory) == 0)
{
MosPrintf(MIL_TEXT("\nERROR\n\n"));
MosPrintf(MIL_TEXT("No video capture filters are available.\n"));
MosPrintf(MIL_TEXT("Make sure the desired filter is properly installed.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
WrapperMilSampleGrabber.ReleaseFilter();
WrapperFilterGraph.UninitGraph();
MsysFree(MilHostSystem);
MappFree(MilApp);
return 0;
}
CComPtr<IBaseFilter> selectedCaptureFilter = NULL;
LPCWSTR selectedCaptureFilterName = new WCHAR[1024];
MosPrintf(MIL_TEXT("Select a video capture source from the list:\n"));
bCaptureFilterSelected = SelectFilterFromCategory(CLSID_VideoInputDeviceCategory, &selectedCaptureFilter, selectedCaptureFilterName);
if (bCaptureFilterSelected && !selectedCaptureFilter)
{
MosPrintf(MIL_TEXT("\nERROR\n\n"));
MosPrintf(MIL_TEXT("The selected filter creation failed.\n"));
MosPrintf(MIL_TEXT("Make sure the desired filter is properly installed and configured.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
selectedCaptureFilter = NULL;
WrapperFilterGraph.UninitGraph();
MsysFree(MilHostSystem);
MappFree(MilApp);
return 0;
}
if (bCaptureFilterSelected && selectedCaptureFilter)
{
CComPtr<IPin> selectedCapturePin = NULL;
CComPtr<IPin> sampleGrabberOutputPin = NULL;
WrapperFilterGraph.InsertFilter(selectedCaptureFilter, selectedCaptureFilterName, &selectedCapturePin);
hr = WrapperMilSampleGrabber.AllocateFilter();
if (FAILED(hr))
{
MosPrintf(MIL_TEXT("\nERROR\n\n"));
MosPrintf(MIL_TEXT("\"Matrox Sample Grabber\" filter creation failed.\n"));
MosPrintf(MIL_TEXT("Make sure the filter is properly registered in \n"));
MosPrintf(MIL_TEXT("MILConfig under \"Benchmarks and Utilities/DirectShow\"\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
selectedCaptureFilter = NULL;
selectedCapturePin = NULL;
WrapperMilSampleGrabber.ReleaseFilter();
WrapperFilterGraph.UninitGraph();
MsysFree(MilHostSystem);
MappFree(MilApp);
return 0;
}
WrapperMilSampleGrabber.SetDesiredMediaType(MEDIASUBTYPE_YUY2);
WrapperFilterGraph.InsertFilter(WrapperMilSampleGrabber.GetFilterInterface(), L"Mil Sample Grabber", &sampleGrabberOutputPin);
WrapperFilterGraph.Connect(selectedCapturePin, WrapperMilSampleGrabber.GetFilterInterface());
WrapperFilterGraph.Render(sampleGrabberOutputPin);
MIL_INT sizeX = 0;
MIL_INT sizeY = 0;
WrapperMilSampleGrabber.GetSize(sizeX, sizeY);
if(sizeX == 0 || sizeY == 0 )
{
MosPrintf(MIL_TEXT("\nERROR\n\n"));
MosPrintf(MIL_TEXT("Invalid source size.\n"));
MosPrintf(MIL_TEXT("Could not allocate a display buffer.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
selectedCaptureFilter = NULL;
selectedCapturePin = NULL;
WrapperMilSampleGrabber.ReleaseFilter();
WrapperFilterGraph.UninitGraph();
MsysFree(MilHostSystem);
MappFree(MilApp);
return 0;
}
MbufAllocColor(MilHostSystem, 3, sizeX, sizeY, 8 + M_UNSIGNED, M_IMAGE + M_PROC + M_YUV16 + M_PACKED + M_DISP, &MilDisplayImage);
MdispAlloc(MilHostSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDisplay);
MdispSelect(MilDisplay, MilDisplayImage);
UserHookData.MilImageDisp = MilDisplayImage;
UserHookData.ProcessedImageCount = 0;
WrapperMilSampleGrabber.RegisterCallback((MANAGEDCALLBACKPROC)ProcessSample, &UserHookData);
selectedCapturePin = NULL;
sampleGrabberOutputPin = NULL;
}
if (selectedCaptureFilterName)
{
delete [] selectedCaptureFilterName;
selectedCaptureFilterName = NULL;
}
selectedCaptureFilter = NULL;
}
if (WrapperFilterGraph.IsInitialized() && bCaptureFilterSelected)
{
WrapperFilterGraph.StartGraph();
MosPrintf(MIL_TEXT("Grabbing from selected source and copying buffer to MIL display.\n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to stop graph.\n\n"));
MosGetch();
WrapperFilterGraph.StopGraph();
}
MosPrintf(MIL_TEXT("\nPress <Enter> to end.\n"));
MosGetch();
WrapperMilSampleGrabber.ReleaseFilter();
WrapperFilterGraph.UninitGraph();
MdispFree(MilDisplay);
MbufFree(MilDisplayImage);
MsysFree(MilHostSystem);
MappFree(MilApp);
return 0;
}
void ProcessSample(MIL_ID bufID, AM_MEDIA_TYPE* pmt, void* pUserData)
{
HookDataStruct* pHookData = (HookDataStruct*)pUserData;
MIL_ID dispImage = pHookData->MilImageDisp;
pHookData->ProcessedImageCount++;
MosPrintf(MIL_TEXT("\rFrames processed: %d"), pHookData->ProcessedImageCount);
MbufCopy(bufID, dispImage);
}